39 #include "irplib_hist.h"
56 return (irplib_hist *) cpl_calloc(1,
sizeof(irplib_hist));
64 irplib_hist_delete(irplib_hist * d)
80 irplib_hist_init(irplib_hist * hist,
86 cpl_ensure_code(hist != NULL, CPL_ERROR_NULL_INPUT);
87 cpl_ensure_code(nbins > 0, CPL_ERROR_ILLEGAL_INPUT);
88 cpl_ensure_code(range > 0, CPL_ERROR_ILLEGAL_INPUT);
89 cpl_ensure_code(hist -> bins == NULL, CPL_ERROR_ILLEGAL_INPUT);
92 hist -> bins = (
unsigned long *) cpl_calloc(nbins,
sizeof(
unsigned long));
93 hist -> nbins = nbins;
94 hist -> start = start;
95 hist -> range = range;
97 return cpl_error_get_code();
106 irplib_hist_get_value(
const irplib_hist * hist,
107 const unsigned long binpos)
109 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
110 cpl_ensure(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT, 0);
111 cpl_ensure(binpos < hist -> nbins, CPL_ERROR_ILLEGAL_INPUT, 0);
113 return hist -> bins[binpos];
121 irplib_hist_get_nbins(
const irplib_hist * hist)
123 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
125 return hist -> nbins;
133 irplib_hist_get_bin_size(
const irplib_hist * hist)
135 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
136 cpl_ensure(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT, 0);
138 return hist -> range / (double)(hist -> nbins - 2);
146 irplib_hist_get_range(
const irplib_hist * hist)
148 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
150 return hist -> range;
159 irplib_hist_get_start(
const irplib_hist * hist)
161 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
162 cpl_ensure(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT, 0);
164 return hist -> start;
174 irplib_hist_fill(irplib_hist * hist,
175 const cpl_image * image)
177 double binwidth = 1.0;
180 const float * data = 0;
181 const cpl_binary* bpm_data = 0;
182 const cpl_mask* bpm = 0;
185 cpl_ensure_code(hist != NULL, CPL_ERROR_NULL_INPUT);
186 cpl_ensure_code(image != NULL, CPL_ERROR_NULL_INPUT);
188 if (hist -> bins == NULL) {
189 const double hstart = cpl_image_get_min(image);
190 const double hrange = cpl_image_get_max(image) - hstart;
191 cpl_error_code error;
199 const unsigned long nbins = (
unsigned long) (hrange / binwidth) + 2;
201 error = irplib_hist_init(hist, nbins, hstart, hrange);
202 cpl_ensure_code(!error, error);
204 cpl_ensure_code(hist -> range > 0, CPL_ERROR_ILLEGAL_INPUT);
207 binwidth = hist -> range / (double)(hist -> nbins - 2);
210 nsamples = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
211 data = cpl_image_get_data_float_const(image);
212 bpm = cpl_image_get_bpm_const(image);
215 bpm_data = cpl_mask_get_data_const(bpm);
218 for (i = 0; i < nsamples; i++)
221 if(bpm_data && bpm_data[i] != CPL_BINARY_0)
225 pos = (int)((data[i] - hist -> start) / binwidth);
229 }
else if ((
unsigned long) pos >= (hist -> nbins - 2))
231 hist -> bins[hist -> nbins - 1]++;
234 hist -> bins[pos + 1]++;
238 return cpl_error_get_code();
249 irplib_hist_get_max(
const irplib_hist * hist,
250 unsigned long * maxpos)
252 unsigned long max = 0;
255 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, 0);
256 cpl_ensure(maxpos != NULL, CPL_ERROR_NULL_INPUT, 0);
257 cpl_ensure(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT, 0);
259 for(ui = 0; ui < hist -> nbins; ui++) {
260 double c_value = irplib_hist_get_value(hist, ui);
275 irplib_hist_cast_table(
const irplib_hist * hist)
278 cpl_error_code error;
280 cpl_ensure(hist != NULL, CPL_ERROR_NULL_INPUT, NULL);
281 cpl_ensure(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
283 table = cpl_table_new(hist -> nbins);
285 error = cpl_table_new_column(table,
"HIST", CPL_TYPE_INT);
286 cpl_ensure(!error, error, NULL);
288 error = cpl_table_copy_data_int(table,
"HIST", (
int *)(hist -> bins));
289 cpl_ensure(!error, error, NULL);
300 irplib_hist_collapse(irplib_hist * hist,
301 unsigned long new_nbins)
303 unsigned long ui, nuj;
304 unsigned long * old_bins;
305 unsigned long old_nbins;
306 double collapse_rate;
307 cpl_error_code error;
310 cpl_ensure_code(hist != NULL, CPL_ERROR_NULL_INPUT);
311 cpl_ensure_code(hist -> bins != NULL, CPL_ERROR_ILLEGAL_INPUT);
312 cpl_ensure_code(new_nbins > 0, CPL_ERROR_ILLEGAL_INPUT);
313 cpl_ensure_code(new_nbins <= hist -> nbins, CPL_ERROR_ILLEGAL_INPUT);
315 old_bins = hist -> bins;
316 old_nbins = hist -> nbins;
319 error = irplib_hist_init(hist, new_nbins, hist -> start, hist -> range);
320 cpl_ensure_code(!error, error);
322 collapse_rate = (double) (old_nbins - 2) / (double) (new_nbins - 2);
325 hist -> bins[0] = old_bins[0];
326 hist -> bins[new_nbins - 1] = old_bins[old_nbins - 1];
331 for (ui = 1; ui < new_nbins - 1; ui++) {
333 const double up = collapse_rate * ui;
335 hist -> bins[ui] += rest;
337 for (uj = nuj; uj < (
unsigned long) up + 1; uj++)
338 hist -> bins[ui] += old_bins[uj];
340 rest = (
unsigned long)(up - (
unsigned long) up) * old_bins[uj];
341 hist -> bins[ui] += rest;
343 rest = old_bins[uj] - rest;
349 return cpl_error_get_code();