00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032 #include <complex.h>
00033
00034
00035
00036
00037
00038 #include <math.h>
00039 #include <string.h>
00040 #include <assert.h>
00041 #include <float.h>
00042
00043 #include <cpl.h>
00044
00045 #include "irplib_ksigma_clip.h"
00046
00047 #include "irplib_hist.h"
00048 #include "irplib_utils.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 static cpl_error_code
00067 irplib_ksigma_clip_double(const double * pi,
00068 int llx,
00069 int lly,
00070 int urx,
00071 int ury,
00072 int nx,
00073 double var_sum,
00074 int npixs,
00075 double kappa,
00076 int nclip,
00077 double tolerance,
00078 double * mean,
00079 double * stdev);
00080
00081 static cpl_error_code
00082 irplib_ksigma_clip_float(const float * pi,
00083 int llx,
00084 int lly,
00085 int urx,
00086 int ury,
00087 int nx,
00088 double var_sum,
00089 int npixs,
00090 double kappa,
00091 int nclip,
00092 double tolerance,
00093 double * mean,
00094 double * stdev);
00095
00096 static cpl_error_code
00097 irplib_ksigma_clip_int(const int * pi,
00098 int llx,
00099 int lly,
00100 int urx,
00101 int ury,
00102 int nx,
00103 double var_sum,
00104 int npixs,
00105 double kappa,
00106 int nclip,
00107 double tolerance,
00108 double * mean,
00109 double * stdev);
00110
00111
00112
00164
00165 cpl_error_code
00166 irplib_ksigma_clip(const cpl_image * img,
00167 int llx,
00168 int lly,
00169 int urx,
00170 int ury,
00171 double kappa,
00172 int nclip,
00173 double tolerance,
00174 double * kmean,
00175 double * kstdev)
00176 {
00177 cpl_errorstate inistate = cpl_errorstate_get();
00178
00179 int nx, ny;
00180
00181 cpl_stats * stats;
00182 double mean, stdev, var_sum;
00183 int npixs;
00184
00185 cpl_ensure_code(img != NULL, CPL_ERROR_NULL_INPUT);
00186
00187 nx = cpl_image_get_size_x(img);
00188 ny = cpl_image_get_size_y(img);
00189
00190 cpl_ensure_code(llx > 0 && urx > llx && urx <= nx &&
00191 lly > 0 && ury > lly && ury <= ny,
00192 CPL_ERROR_ILLEGAL_INPUT);
00193
00194 cpl_ensure_code(tolerance >= 0.0, CPL_ERROR_ILLEGAL_INPUT);
00195 cpl_ensure_code(kappa > 1.0, CPL_ERROR_ILLEGAL_INPUT);
00196 cpl_ensure_code(nclip > 0, CPL_ERROR_ILLEGAL_INPUT);
00197
00198 stats = cpl_stats_new_from_image_window(img,
00199 CPL_STATS_MEAN | CPL_STATS_STDEV,
00200 llx, lly, urx, ury);
00201
00202 npixs = cpl_stats_get_npix(stats);
00203 mean = cpl_stats_get_mean(stats);
00204 stdev = cpl_stats_get_stdev(stats);
00205 var_sum = stdev * stdev * (npixs - 1);
00206
00207 cpl_stats_delete(stats);
00208
00209
00210 cpl_ensure_code(cpl_errorstate_is_equal(inistate), cpl_error_get_code());
00211
00212 switch (cpl_image_get_type(img)) {
00213 case CPL_TYPE_DOUBLE:
00214 skip_if(irplib_ksigma_clip_double(cpl_image_get_data_double_const(img),
00215 llx, lly, urx, ury, nx, var_sum,
00216 npixs, kappa, nclip, tolerance,
00217 &mean, &stdev));
00218 break;
00219 case CPL_TYPE_FLOAT:
00220 skip_if(irplib_ksigma_clip_float(cpl_image_get_data_float_const(img),
00221 llx, lly, urx, ury, nx, var_sum,
00222 npixs, kappa, nclip, tolerance,
00223 &mean, &stdev));
00224 break;
00225 case CPL_TYPE_INT:
00226 skip_if(irplib_ksigma_clip_int(cpl_image_get_data_int_const(img),
00227 llx, lly, urx, ury, nx, var_sum,
00228 npixs, kappa, nclip, tolerance,
00229 &mean, &stdev));
00230 break;
00231 default:
00232
00233 assert( 0 );
00234 }
00235
00236 *kmean = mean;
00237 if (kstdev != NULL) *kstdev = stdev;
00238
00239 end_skip;
00240
00241 return cpl_error_get_code();
00242 }
00243
00244 #define CONCAT(a,b) a ## _ ## b
00245 #define CONCAT2X(a,b) CONCAT(a,b)
00246
00247 #define CPL_TYPE double
00248 #include "irplib_ksigma_clip_body.h"
00249 #undef CPL_TYPE
00250
00251 #define CPL_TYPE float
00252 #include "irplib_ksigma_clip_body.h"
00253 #undef CPL_TYPE
00254
00255 #define CPL_TYPE int
00256 #include "irplib_ksigma_clip_body.h"
00257 #undef CPL_TYPE
00258