28#include "hdrl_types.h"
29#include "hdrl_image.h"
30#include "hdrl_imagelist.h"
31#include "hdrl_utils.h"
33#include "hdrl_bpm_3d.h"
34#include "hdrl_prototyping.h"
81 hdrl_bpm_3d_method method ;
82} hdrl_bpm_3d_parameter;
85static hdrl_parameter_typeobj hdrl_bpm_3d_parameter_type = {
86 HDRL_PARAMETER_BPM_3D,
87 (hdrl_alloc *)&cpl_malloc,
88 (hdrl_free *)&cpl_free,
90 sizeof(hdrl_bpm_3d_parameter),
111 hdrl_bpm_3d_method method)
113 hdrl_bpm_3d_parameter * p = (hdrl_bpm_3d_parameter *)
114 hdrl_parameter_new(&hdrl_bpm_3d_parameter_type);
115 p->kappa_low = kappa_low ;
116 p->kappa_high = kappa_high ;
118 return (hdrl_parameter *)p;
129 const hdrl_parameter * param)
131 const hdrl_bpm_3d_parameter * param_loc = (
const hdrl_bpm_3d_parameter *)param ;
133 cpl_error_ensure(param != NULL, CPL_ERROR_NULL_INPUT,
134 return CPL_ERROR_NULL_INPUT,
"NULL Input Parameters");
136 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
137 "Expected BPM image parameter") ;
139 cpl_error_ensure(param_loc->method == HDRL_BPM_3D_THRESHOLD_ABSOLUTE ||
140 param_loc->method == HDRL_BPM_3D_THRESHOLD_RELATIVE ||
141 param_loc->method == HDRL_BPM_3D_THRESHOLD_ERROR,
142 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
143 "Unsupported method");
144 switch (param_loc->method) {
145 case HDRL_BPM_3D_THRESHOLD_ABSOLUTE:
146 cpl_error_ensure(param_loc->kappa_high >= param_loc->kappa_low,
147 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
148 "kappa_high must be larger than kappa_low");
150 case HDRL_BPM_3D_THRESHOLD_RELATIVE:
151 case HDRL_BPM_3D_THRESHOLD_ERROR:
152 cpl_error_ensure(param_loc->kappa_low >= 0,
153 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
154 "kappa_low must be >=0");
155 cpl_error_ensure(param_loc->kappa_high >= 0,
156 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
157 "kappa_high must be >=0");
160 return CPL_ERROR_NONE ;
172 return hdrl_parameter_check_type(self, &hdrl_bpm_3d_parameter_type);
183 const hdrl_parameter * p)
185 cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1.0);
186 return ((
const hdrl_bpm_3d_parameter *)p)->kappa_low;
197 const hdrl_parameter * p)
199 cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1.0);
200 return ((
const hdrl_bpm_3d_parameter *)p)->kappa_high;
211 const hdrl_parameter * p)
213 cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
214 return ((
const hdrl_bpm_3d_parameter *)p)->method;
232 const char *base_context,
234 const hdrl_parameter *defaults)
236 cpl_ensure(prefix && base_context && defaults,
237 CPL_ERROR_NULL_INPUT, NULL);
240 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
243 cpl_parameterlist *parlist = cpl_parameterlist_new();
252 cpl_ensure( method_def == HDRL_BPM_3D_THRESHOLD_ABSOLUTE
253 || method_def == HDRL_BPM_3D_THRESHOLD_RELATIVE
254 || method_def == HDRL_BPM_3D_THRESHOLD_ERROR,
255 CPL_ERROR_ILLEGAL_INPUT, NULL) ;
257 const char *method_str;
258 if (method_def == HDRL_BPM_3D_THRESHOLD_ABSOLUTE) {
259 method_str =
"absolute" ;
260 }
else if (method_def == HDRL_BPM_3D_THRESHOLD_RELATIVE) {
261 method_str =
"relative" ;
262 }
else if (method_def == HDRL_BPM_3D_THRESHOLD_ERROR) {
263 method_str =
"error" ;
269 hdrl_setup_vparameter(parlist, prefix,
".",
"",
"kappa-low", base_context,
270 "Low RMS scaling factor for image thresholding.", CPL_TYPE_DOUBLE,
274 hdrl_setup_vparameter(parlist, prefix,
".",
"",
"kappa-high", base_context,
275 "High RMS scaling factor for image thresholding.", CPL_TYPE_DOUBLE,
280 par = cpl_parameter_new_enum(name, CPL_TYPE_STRING,
281 "Thresholdig method to use for bpm detection", context, method_str,
282 3,
"absolute",
"relative",
"error");
285 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, name);
286 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
288 cpl_parameterlist_append(parlist, par);
292 if (cpl_error_get_code()) {
293 cpl_parameterlist_delete(parlist);
315 const cpl_parameterlist * parlist,
318 cpl_ensure(prefix && parlist, CPL_ERROR_NULL_INPUT, NULL);
320 const cpl_parameter * par;
321 const char * tmp_str ;
324 hdrl_bpm_3d_method method;
328 par=cpl_parameterlist_find_const(parlist, name);
329 kappa_low = cpl_parameter_get_double(par);
334 par=cpl_parameterlist_find_const(parlist, name);
335 kappa_high = cpl_parameter_get_double(par);
340 par = cpl_parameterlist_find_const(parlist, name) ;
341 tmp_str = cpl_parameter_get_string(par);
342 if (tmp_str == NULL) {
343 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
344 "Parameter %s not found", name);
349 if(!strcmp(tmp_str,
"absolute")) {
350 method = HDRL_BPM_3D_THRESHOLD_ABSOLUTE ;
351 }
else if(!strcmp(tmp_str,
"relative")) {
352 method = HDRL_BPM_3D_THRESHOLD_RELATIVE ;
353 }
else if(!strcmp(tmp_str,
"error")) {
354 method = HDRL_BPM_3D_THRESHOLD_ERROR ;
356 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
357 "Invalid method: %s", tmp_str);
361 if (cpl_error_get_code()) {
362 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
363 "Error while parsing parameterlist with prefix %s", prefix);
410 const hdrl_imagelist * imglist,
411 const hdrl_parameter * params)
416 cpl_imagelist * imglist_out = NULL;
417 hdrl_imagelist * masterlist = NULL;
418 hdrl_parameter * collapse_params = NULL;
421 cpl_error_ensure(imglist && params, CPL_ERROR_NULL_INPUT,
422 return NULL,
"NULL input");
426 const hdrl_bpm_3d_parameter * p_loc = (
const hdrl_bpm_3d_parameter *)params ;
430 imglist_out = cpl_imagelist_new();
436 cpl_image * contrib_map;
445 cpl_image_delete(contrib_map);
454 cpl_mask * mask_out = NULL;
456 if (p_loc->method == HDRL_BPM_3D_THRESHOLD_ABSOLUTE) {
458 mask_out = cpl_mask_threshold_image_create(
461 cpl_mask_not(mask_out);
464 cpl_mask_xor(mask_out, tmp_cplimg_mask);
465 }
else if (p_loc->method == HDRL_BPM_3D_THRESHOLD_RELATIVE) {
470 mad = nextafter(0, 1.0);
472 std_mad = CPL_MATH_STD_MAD * mad;
473 mask_out = cpl_mask_threshold_image_create(
475 -(p_loc->kappa_low * std_mad),
476 (p_loc->kappa_high * std_mad));
477 cpl_mask_not(mask_out);
480 cpl_mask_xor(mask_out, tmp_cplimg_mask);
481 }
else if (p_loc->method == HDRL_BPM_3D_THRESHOLD_ERROR) {
486 mask_out = cpl_mask_new(nx, ny);
488 for (cpl_size x = 1; x <= nx; ++x) {
489 for (cpl_size y = 1; y <= ny; ++y) {
494 if (v.data < -(v.error * p_loc->kappa_low) ||
495 v.data > (v.error * p_loc->kappa_high) || bad_data) {
496 cpl_mask_set(mask_out, x, y, CPL_BINARY_1);
502 cpl_mask_xor(mask_out, tmp_cplimg_mask);
504 cpl_imagelist_set(imglist_out, cpl_image_new_from_mask(mask_out), var);
505 cpl_mask_delete(mask_out);
509 if (cpl_error_get_code() != CPL_ERROR_NONE) {
510 cpl_imagelist_delete(imglist_out);
518 hdrl_imagelist_unwrap(masterlist);
double hdrl_bpm_3d_parameter_get_kappa_high(const hdrl_parameter *p)
Access the kappa_high in the BPM_3D parameter.
double hdrl_bpm_3d_parameter_get_kappa_low(const hdrl_parameter *p)
Access the kappa_low in the BPM_3D parameter.
cpl_parameterlist * hdrl_bpm_3d_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create a parameter list for the BPM_3D computation.
cpl_imagelist * hdrl_bpm_3d_compute(const hdrl_imagelist *imglist, const hdrl_parameter *params)
detect bad pixels on a stack of identical images
hdrl_parameter * hdrl_bpm_3d_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse a parameterlist to create input parameters for the BPM_3D.
hdrl_bpm_3d_method hdrl_bpm_3d_parameter_get_method(const hdrl_parameter *p)
Access the method in the BPM_3D parameter.
cpl_error_code hdrl_bpm_3d_parameter_verify(const hdrl_parameter *param)
Verify basic correctness of the BPM_3D parameters.
hdrl_parameter * hdrl_bpm_3d_parameter_create(double kappa_low, double kappa_high, hdrl_bpm_3d_method method)
Creates BPM Parameters object for the imagelist method.
cpl_boolean hdrl_bpm_3d_parameter_check(const hdrl_parameter *self)
Check that the parameter is a BPM_3D parameter.
hdrl_parameter * hdrl_collapse_median_parameter_create(void)
create a parameter object for median
hdrl_value hdrl_image_get_pixel(const hdrl_image *self, cpl_size xpos, cpl_size ypos, int *pis_rejected)
get pixel values of hdrl_image
hdrl_image * hdrl_image_sub_image_create(const hdrl_image *self, const hdrl_image *other)
Subtract two images.
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
hdrl_image * hdrl_imagelist_unset(hdrl_imagelist *himlist, cpl_size pos)
Remove an image from an imagelist.
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
const hdrl_image * hdrl_imagelist_get_const(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter
char * hdrl_join_string(const char *sep_, int n,...)
join strings together