00001 /* * 00002 * This file is part of the ESO IRPLIB package * 00003 * Copyright (C) 2004,2005 European Southern Observatory * 00004 * * 00005 * This library is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the Free Software * 00017 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA * 00018 * */ 00019 00020 #ifdef HAVE_CONFIG_H 00021 #include <config.h> 00022 #endif 00023 00024 /*---------------------------------------------------------------------------- 00025 Includes 00026 ----------------------------------------------------------------------------*/ 00027 00028 00029 #include <irplib_hist.h> 00030 00031 #include <math.h> 00032 /*---------------------------------------------------------------------------*/ 00033 /* 00034 * @defgroup irplib_hist_test Testing of the IRPLIB utilities 00035 */ 00036 /*---------------------------------------------------------------------------*/ 00037 00038 00039 /*---------------------------------------------------------------------------- 00040 Defines 00041 ----------------------------------------------------------------------------*/ 00042 00043 #define NBINS 100 00044 00045 /*---------------------------------------------------------------------------- 00046 Private Function prototypes 00047 ----------------------------------------------------------------------------*/ 00048 00049 static void irplib_hist_tests(void); 00050 00051 /*---------------------------------------------------------------------------*/ 00052 /* 00053 * @brief Unit tests of fit module 00054 */ 00055 /*---------------------------------------------------------------------------*/ 00056 00057 int main(void) 00058 { 00059 /* Initialize CPL + IRPLIB */ 00060 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING); 00061 00062 irplib_hist_tests(); 00063 00064 return cpl_test_end(0); 00065 } 00066 00067 static void irplib_hist_tests(void) 00068 { 00069 irplib_hist * hist; 00070 cpl_image * image; 00071 cpl_error_code error; 00072 int i, j; 00073 float * data; 00074 00075 unsigned long max_where; 00076 00077 /* 1. trial: Create a right histogram */ 00078 hist = irplib_hist_new(); 00079 cpl_test_nonnull(hist); 00080 cpl_test_error(CPL_ERROR_NONE); 00081 irplib_hist_delete(hist); 00082 00083 /* 3. trial: Histogram for a uniform image */ 00084 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT); 00085 cpl_image_add_scalar(image, 202); 00086 00087 hist = irplib_hist_new(); 00088 00089 error = irplib_hist_init(hist, NBINS, 0, 500); 00090 cpl_test_zero(error); 00091 error = irplib_hist_fill(hist, image); 00092 cpl_test_zero(error); 00093 00094 for(i = 0; i < 40; i++) { 00095 cpl_test_zero(irplib_hist_get_value(hist, i)); 00096 } 00097 00098 /* The following call retrieves the value of the 42-st bin */ 00099 /* When i = 41, 42-th is retrieved. 500 - 0 / 100 = 5; 202/5=40,xx 00100 it should be in the 41-th bin but it is in the next one because 00101 there is one before left empty for possible values out of range 00102 0 (hinit) < 202 (image constant) 00103 */ 00104 00105 cpl_test_eq(irplib_hist_get_value(hist, 40), 10000); 00106 for(i = 42; i < NBINS; i++) { 00107 cpl_test_zero(irplib_hist_get_value(hist, i)); 00108 } 00109 00110 irplib_hist_delete(hist); 00111 cpl_image_delete(image); 00112 00113 /* 4. trial: Histogram for a normal image: no checking of the output */ 00114 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT); 00115 cpl_image_fill_noise_uniform(image, 0, 200); 00116 00117 hist = irplib_hist_new(); 00118 error = irplib_hist_fill(hist,image); 00119 cpl_test_zero(error); 00120 00121 irplib_hist_delete(hist); 00122 cpl_image_delete(image); 00123 00124 /* 5. trial: Histogram */ 00125 image = cpl_image_new(100, 100, CPL_TYPE_FLOAT); 00126 data = cpl_image_get_data_float(image); 00127 for (i = 0; i < 100; i++) { 00128 for (j = 0; j < 100; j++) { 00129 *(data + 100*i + j) = i +j; 00130 } 00131 } 00132 00133 hist = irplib_hist_new(); 00134 error = irplib_hist_fill(hist, image); 00135 00136 irplib_hist_get_max(hist, &max_where); 00137 00138 /* The following call retrieves the value of the 41-st bin */ 00139 /* cpl_test_eq(irplib_hist_get_value(hist, 40), 10000); 00140 for(i = 42; i < NBINS; i++) { 00141 cpl_test_zero(irplib_hist_get_value(hist, i)); 00142 }*/ 00143 00144 /* 6. trial: all by default ( we use the same image) */ 00145 00146 cpl_test_eq(max_where, irplib_hist_get_nbins(hist)/2); 00147 00148 irplib_hist_delete(hist); 00149 cpl_image_delete(image); 00150 }