28#include "hdrl_imagelist.h"
29#include "hdrl_imagelist_view.h"
30#include "hdrl_imagelist_defs.h"
31#include "hdrl_image.h"
32#include "hdrl_collapse.h"
61 HDRL_IMLIST_BASIC_IMLIST = 0,
62 HDRL_IMLIST_BASIC_IMAGE,
63 HDRL_IMLIST_BASIC_SCALAR
66typedef cpl_error_code (*hdrl_f_image)(hdrl_image *,
const hdrl_image *);
67typedef cpl_error_code (*hdrl_f_scalar)(hdrl_image *, hdrl_value);
73static cpl_error_code hdrl_imagelist_basic_operation(
76 hdrl_imagelist *himlist1,
77 const hdrl_imagelist *himlist2,
78 const hdrl_image *himg,
81static cpl_error_code hdrl_imagelist_collapse_interface(
82 const hdrl_imagelist *himlist,
83 hdrl_collapse_imagelist_to_image_t *collapse_method,
109 hdrl_imagelist * himlist1,
110 const hdrl_imagelist * himlist2)
112 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMLIST,
127 hdrl_imagelist * himlist1,
128 const hdrl_imagelist * himlist2)
130 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMLIST,
145 hdrl_imagelist * himlist1,
146 const hdrl_imagelist * himlist2)
148 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMLIST,
163 hdrl_imagelist * himlist1,
164 const hdrl_imagelist * himlist2)
166 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMLIST,
186 hdrl_imagelist * himlist,
187 const hdrl_image * himg)
189 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMAGE,
209 hdrl_imagelist * himlist,
210 const hdrl_image * himg)
212 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMAGE,
232 hdrl_imagelist * himlist,
233 const hdrl_image * himg)
235 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMAGE,
255 hdrl_imagelist * himlist,
256 const hdrl_image * himg)
258 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_IMAGE,
277 hdrl_imagelist * himlist,
280 const cpl_error_code code =
281 hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_SCALAR,
283 himlist, NULL, NULL, &value);
284 return code ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
301 hdrl_imagelist * himlist,
304 const cpl_error_code code =
305 hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_SCALAR,
307 himlist, NULL, NULL, &value);
308 return code ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
325 hdrl_imagelist * himlist,
328 const cpl_error_code code =
329 hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_SCALAR,
331 himlist, NULL, NULL, &value);
332 return code ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
349 hdrl_imagelist * himlist,
352 return hdrl_imagelist_basic_operation(HDRL_IMLIST_BASIC_SCALAR,
371 hdrl_imagelist * himlist,
374 cpl_ensure_code(himlist != NULL, CPL_ERROR_NULL_INPUT);
377 for (cpl_size i = 0 ; i<nima ; i++) {
379 exponent), cpl_error_get_code());
381 return CPL_ERROR_NONE;
408 const hdrl_imagelist * himlist,
409 const hdrl_parameter * param,
411 cpl_image ** contrib)
413 cpl_ensure_code(himlist, CPL_ERROR_NULL_INPUT);
414 cpl_ensure_code(param, CPL_ERROR_NULL_INPUT);
415 cpl_ensure_code(out, CPL_ERROR_NULL_INPUT);
416 cpl_ensure_code(contrib, CPL_ERROR_NULL_INPUT);
432 out, contrib, NULL, NULL);
438 out, contrib, NULL, NULL);
450 return cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE,
451 "Invalid parameter input for "
452 "hdrl_imagelist_collapse");
455 return cpl_error_get_code();
471 const hdrl_imagelist * himlist,
473 cpl_image ** contrib)
475 hdrl_collapse_imagelist_to_image_t * method =
476 hdrl_collapse_imagelist_to_image_mean();
477 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, NULL) ;
478 hdrl_collapse_imagelist_to_image_delete(method) ;
479 return cpl_error_get_code();
495 const hdrl_imagelist * himlist,
497 cpl_image ** contrib)
499 hdrl_collapse_imagelist_to_image_t * method =
500 hdrl_collapse_imagelist_to_image_weighted_mean();
501 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, NULL) ;
502 hdrl_collapse_imagelist_to_image_delete(method) ;
503 return cpl_error_get_code();
519 const hdrl_imagelist * himlist,
521 cpl_image ** contrib)
523 hdrl_collapse_imagelist_to_image_t * method =
524 hdrl_collapse_imagelist_to_image_median();
525 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, NULL) ;
526 hdrl_collapse_imagelist_to_image_delete(method) ;
527 return cpl_error_get_code();
549 const hdrl_imagelist * himlist,
554 cpl_image ** contrib,
555 cpl_image ** reject_low,
556 cpl_image ** reject_high)
558 hdrl_sigclip_image_output * sigclipout;
559 hdrl_collapse_imagelist_to_image_t * method =
560 hdrl_collapse_imagelist_to_image_sigclip(kappa_low, kappa_high, niter);
561 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, (
void**)&sigclipout);
563 if (cpl_error_get_code() != CPL_ERROR_NONE) {
565 hdrl_collapse_imagelist_to_image_delete(method);
575 return cpl_error_get_code();
579 *reject_low = sigclipout->reject_low;
581 cpl_image_delete(sigclipout->reject_low);
585 *reject_high = sigclipout->reject_high;
587 cpl_image_delete(sigclipout->reject_high);
590 hdrl_collapse_imagelist_to_image_unwrap_eout(method, sigclipout);
591 hdrl_collapse_imagelist_to_image_delete(method);
593 return cpl_error_get_code();
614 const hdrl_imagelist * himlist,
618 cpl_image ** contrib,
619 cpl_image ** reject_low,
620 cpl_image ** reject_high)
622 hdrl_minmax_image_output * minmaxout;
623 hdrl_collapse_imagelist_to_image_t * method =
624 hdrl_collapse_imagelist_to_image_minmax(nlow, nhigh);
625 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, (
void**)&minmaxout);
627 if (cpl_error_get_code() != CPL_ERROR_NONE) {
629 hdrl_collapse_imagelist_to_image_delete(method);
639 return cpl_error_get_code();
643 *reject_low = minmaxout->reject_low;
645 cpl_image_delete(minmaxout->reject_low);
649 *reject_high = minmaxout->reject_high;
651 cpl_image_delete(minmaxout->reject_high);
654 hdrl_collapse_imagelist_to_image_unwrap_eout(method, minmaxout);
655 hdrl_collapse_imagelist_to_image_delete(method);
657 return cpl_error_get_code();
684 hdrl_mode_type mode_method,
685 cpl_size error_niter,
687 cpl_image ** contrib)
689 hdrl_collapse_imagelist_to_image_t * method =
690 hdrl_collapse_imagelist_to_image_mode(histo_min, histo_max, bin_size,
691 mode_method, error_niter );
692 hdrl_imagelist_collapse_interface(himlist, method, out, contrib, NULL) ;
693 hdrl_collapse_imagelist_to_image_delete(method) ;
694 return cpl_error_get_code();
720static cpl_error_code hdrl_imagelist_collapse_interface(
721 const hdrl_imagelist * himlist,
722 hdrl_collapse_imagelist_to_image_t * collapse_method,
724 cpl_image ** contrib,
727 cpl_error_code fail = CPL_ERROR_NONE;
730 cpl_ensure_code(himlist != NULL, CPL_ERROR_NULL_INPUT);
731 cpl_ensure_code(out != NULL, CPL_ERROR_NULL_INPUT);
732 cpl_ensure_code(contrib != NULL, CPL_ERROR_NULL_INPUT);
737 if (cpl_error_get_code() != CPL_ERROR_NONE) {
738 return cpl_error_get_code();
742 *contrib = cpl_image_new( nx, ny, CPL_TYPE_INT);
745 cpl_image_get_bpm(*contrib);
750 *eout = hdrl_collapse_imagelist_to_image_create_eout(
757 cpl_size blocksize = 16ul * (1ul<<20ul) / (nz * nx *
sizeof(
double));
758 hdrl_iter *it = hdrl_imagelist_get_iter_row_slices(himlist, blocksize, 0, HDRL_ITER_CONST);
760 cpl_size nit = hdrl_iter_length(it);
761 hdrl_imagelist *vl[nit];
767 for (hdrl_imagelist * v = hdrl_iter_next(it);
769 v = hdrl_iter_next(it)) {
776 hdrl_iter_delete(it);
778 if (cpl_error_get_code() != CPL_ERROR_NONE) {
779 for (cpl_size i = 0; i < nit; i++) {
782 return cpl_error_get_code();
785 HDRL_OMP(omp parallel
for)
786 for (cpl_size i = 0; i < nit; i++) {
789 cpl_image *out_errors;
790 cpl_image *out_contrib;
793 cpl_imagelist *errors;
797 hdrl_imagelist *v = vl[i];
801 hdrl_imagelist_to_cplwrap(v, &data, &errors);
804 hdrl_collapse_imagelist_to_image_call(collapse_method, data, errors,
805 &out_data, &out_errors,
806 &out_contrib, &out_eout);
808 if (cpl_error_get_code() != CPL_ERROR_NONE) {
810 fail = cpl_error_get_code();
811 cpl_imagelist_unwrap(data) ;
812 cpl_imagelist_unwrap(errors) ;
817 cpl_msg_debug(cpl_func,
"Collapsed block %lld to %lld", y,
818 y + cpl_image_get_size_y(out_data) - 1);
822 assert(cpl_image_get_bpm_const(*contrib));
824 cpl_image_copy(*contrib, out_contrib, 1, y);
828 hdrl_collapse_imagelist_to_image_move_eout(collapse_method,
833 cpl_image_delete(out_data);
834 cpl_image_delete(out_errors);
835 cpl_image_delete(out_contrib);
836 cpl_imagelist_unwrap(data) ;
837 cpl_imagelist_unwrap(errors) ;
842 if (fail != CPL_ERROR_NONE) {
845 hdrl_collapse_imagelist_to_image_delete_eout(collapse_method, *eout);
849 cpl_image_delete(*contrib);
851 return cpl_error_set_message(cpl_func, fail,
"hdrl_imagelist_collapse failed");
854 return cpl_error_get_code();
872static cpl_error_code hdrl_imagelist_basic_operation(
875 hdrl_imagelist *himlist1,
876 const hdrl_imagelist *himlist2,
877 const hdrl_image *himg,
881 cpl_ensure_code(func, CPL_ERROR_NULL_INPUT);
882 cpl_ensure_code(himlist1, CPL_ERROR_NULL_INPUT);
885 case HDRL_IMLIST_BASIC_IMLIST:
886 cpl_ensure_code(himlist2, CPL_ERROR_NULL_INPUT);
887 cpl_ensure_code(himlist1->ni == himlist2->ni,
888 CPL_ERROR_INCOMPATIBLE_INPUT);
890 case HDRL_IMLIST_BASIC_IMAGE:
891 cpl_ensure_code(himg, CPL_ERROR_NULL_INPUT);
893 case HDRL_IMLIST_BASIC_SCALAR:
894 cpl_ensure_code(value, CPL_ERROR_NULL_INPUT);
899 for (cpl_size i = 0; i < himlist1->ni; i++) {
900 cpl_error_code err = CPL_ERROR_NONE;
902 case HDRL_IMLIST_BASIC_IMLIST:
904 err = ((hdrl_f_image)func)(himlist1->images[i], himlist2->images[i]);
907 case HDRL_IMLIST_BASIC_IMAGE:
909 err = ((hdrl_f_image)func)(himlist1->images[i], himg);
912 case HDRL_IMLIST_BASIC_SCALAR:
913 err = ((hdrl_f_scalar)func)(himlist1->images[i], *value);
916 cpl_ensure_code(err == CPL_ERROR_NONE, err);
919 return CPL_ERROR_NONE;
double hdrl_collapse_mode_parameter_get_bin_size(const hdrl_parameter *p)
get size of the histogram bins
double hdrl_collapse_mode_parameter_get_histo_min(const hdrl_parameter *p)
get min value
cpl_boolean hdrl_collapse_parameter_is_weighted_mean(const hdrl_parameter *self)
check if parameter is a weighted mean parameter
double hdrl_collapse_sigclip_parameter_get_kappa_low(const hdrl_parameter *p)
get low kappa
cpl_boolean hdrl_collapse_parameter_is_mean(const hdrl_parameter *self)
check if parameter is a mean parameter
cpl_boolean hdrl_collapse_parameter_is_median(const hdrl_parameter *self)
check if parameter is a median parameter
double hdrl_collapse_mode_parameter_get_histo_max(const hdrl_parameter *p)
get high value
int hdrl_collapse_sigclip_parameter_get_niter(const hdrl_parameter *p)
get maximum number of clipping iterations
cpl_size hdrl_collapse_mode_parameter_get_error_niter(const hdrl_parameter *p)
get the error type of the mode
cpl_boolean hdrl_collapse_parameter_is_minmax(const hdrl_parameter *self)
check if parameter is a minmax mean parameter
cpl_boolean hdrl_collapse_parameter_is_mode(const hdrl_parameter *self)
check if parameter is a mode parameter
cpl_boolean hdrl_collapse_parameter_is_sigclip(const hdrl_parameter *self)
check if parameter is a sigclip mean parameter
double hdrl_collapse_sigclip_parameter_get_kappa_high(const hdrl_parameter *p)
get high kappa
double hdrl_collapse_minmax_parameter_get_nlow(const hdrl_parameter *p)
get low value
hdrl_mode_type hdrl_collapse_mode_parameter_get_method(const hdrl_parameter *p)
get the mode determination method
double hdrl_collapse_minmax_parameter_get_nhigh(const hdrl_parameter *p)
get high value
cpl_error_code hdrl_image_sub_image(hdrl_image *self, const hdrl_image *other)
Subtract two images, store the result in the first image.
cpl_error_code hdrl_image_div_scalar(hdrl_image *self, hdrl_value value)
Elementwise division of an image with a scalar.
cpl_error_code hdrl_image_pow_scalar(hdrl_image *self, const hdrl_value exponent)
computes the power of an image by a scalar
cpl_error_code hdrl_image_add_image(hdrl_image *self, const hdrl_image *other)
Add two images, store the result in the first image.
cpl_error_code hdrl_image_div_image(hdrl_image *self, const hdrl_image *other)
Divide two images, store the result in the first image.
cpl_error_code hdrl_image_mul_scalar(hdrl_image *self, hdrl_value value)
Elementwise multiplication of an image with a scalar.
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
cpl_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
cpl_error_code hdrl_image_mul_image(hdrl_image *self, const hdrl_image *other)
Multiply two images, store the result in the first image.
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
cpl_error_code hdrl_image_insert(hdrl_image *self, const cpl_image *image, const cpl_image *error, cpl_size xpos, cpl_size ypos)
Copy cpl images into an hdrl image.
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
cpl_error_code hdrl_image_sub_scalar(hdrl_image *self, hdrl_value value)
Elementwise subtraction of a scalar from an image.
cpl_error_code hdrl_imagelist_collapse_sigclip(const hdrl_imagelist *himlist, double kappa_low, double kappa_high, int niter, hdrl_image **out, cpl_image **contrib, cpl_image **reject_low, cpl_image **reject_high)
Sigma-clipped collapsing of image list.
cpl_error_code hdrl_imagelist_div_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Divide two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_add_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise addition of a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_collapse_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Mean collapsing of image list.
cpl_size hdrl_imagelist_get_size_y(const hdrl_imagelist *himlist)
Get number of rows of images in the imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
cpl_error_code hdrl_imagelist_add_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Add an image to an image list.
cpl_error_code hdrl_imagelist_pow_scalar(hdrl_imagelist *himlist, hdrl_value exponent)
Compute the elementwise power of each image in the himlist.
hdrl_imagelist_basic_type
Define the kind of operation to apply.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
cpl_error_code hdrl_imagelist_div_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise division by a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_sub_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Subtract an image from an image list.
cpl_error_code hdrl_imagelist_sub_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise subtraction of a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_collapse(const hdrl_imagelist *himlist, const hdrl_parameter *param, hdrl_image **out, cpl_image **contrib)
collapsing of image list
cpl_error_code hdrl_imagelist_mul_scalar(hdrl_imagelist *himlist, hdrl_value value)
Elementwise multiplication of a scalar to each image in the himlist.
cpl_error_code hdrl_imagelist_collapse_median(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Median collapsing of image list.
cpl_error_code hdrl_imagelist_mul_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Multiply two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_add_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Add two image lists, the first one is replaced by the result.
cpl_error_code hdrl_imagelist_collapse_mode(const hdrl_imagelist *himlist, double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter, hdrl_image **out, cpl_image **contrib)
Mode collapsing of image list.
cpl_error_code hdrl_imagelist_sub_imagelist(hdrl_imagelist *himlist1, const hdrl_imagelist *himlist2)
Subtract two image lists, the first one is replaced by the result.
cpl_size hdrl_imagelist_get_size_x(const hdrl_imagelist *himlist)
Get number of colums of images in the imagelist.
cpl_error_code hdrl_imagelist_collapse_minmax(const hdrl_imagelist *himlist, double nlow, double nhigh, hdrl_image **out, cpl_image **contrib, cpl_image **reject_low, cpl_image **reject_high)
Minmax-clipped collapsing of image list.
cpl_error_code hdrl_imagelist_collapse_weighted_mean(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Weighted Mean collapsing of image list.
cpl_error_code hdrl_imagelist_mul_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Multiply an image by an image list.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_error_code hdrl_imagelist_div_image(hdrl_imagelist *himlist, const hdrl_image *himg)
Divide an image from an image list.