1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
/*
* This file is part of the HDRL
* Copyright (C) 2017 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "hdrl_spectrumlist.h"
#include "hdrl_spectrum_resample.h"
#include "hdrl_imagelist.h"
#include <math.h>
/**
*
* @addtogroup hdrl_spectrum1D
*
*/
/*----------------------------------------------------------------------------*/
/**@{*/
/*-----------------------------------------------------------------------------
Private Functions
-----------------------------------------------------------------------------*/
static inline
cpl_error_code check_getter(const hdrl_spectrum1Dlist * s, const cpl_size idx);
static inline
void push_back(hdrl_spectrum1Dlist *self, hdrl_spectrum1D * s);
static inline hdrl_spectrum1D **
safe_realloc(hdrl_spectrum1D ** current, const cpl_size old_capacity,
const cpl_size new_capacity);
static inline hdrl_imagelist *
create_list(hdrl_spectrum1D ** resampled_spectra,
const hdrl_spectrum1Dlist * ori_spectra,
const cpl_boolean mark_bpm_in_interpolation);
static inline void
delete_spectrum1D_array(hdrl_spectrum1D ** resampled_spectra,
const cpl_size num_spectra);
static inline hdrl_image *
get_padded_flux(const hdrl_spectrum1D * resampled_spectrum,
const hdrl_spectrum1D * ori_spectrum,
const cpl_boolean mark_bpm_in_interpolation);
static inline cpl_boolean
are_spectra_valid(const hdrl_spectrum1Dlist * list);
static inline cpl_error_code
get_first_error_code(const cpl_error_code * cds, const cpl_size length);
static inline cpl_boolean
check_scales_are_same(const hdrl_spectrum1Dlist * list);
static inline double
get_wmin_valid(const hdrl_spectrum1D * s);
static inline double
get_wmax_valid(const hdrl_spectrum1D * s);
static inline void
remove_if_neighbors_are_rejected(hdrl_image * flx,
const hdrl_spectrum1D * ori_spectrum, const cpl_array * wlens);
static inline hdrl_spectrum1D *
get_interp_bpm(const hdrl_spectrum1D * s, const cpl_array * wlens);
static inline cpl_boolean
contains(const hdrl_spectrum1Dlist * list, const hdrl_spectrum1D * s);
/* ---------------------------------------------------------------------------*/
/**
* @brief hdrl_spectrum1Dlist default constructor
* @return a newly allocated hdrl_spectrum1Dlist
*
* The constructor allocates memory of a hdrl_spectrum1Dlist structure. The struct
* has zero capacity when allocated.
*/
/* ---------------------------------------------------------------------------*/
hdrl_spectrum1Dlist * hdrl_spectrum1Dlist_new(void){
hdrl_spectrum1Dlist * to_ret = cpl_calloc(1, sizeof(*to_ret));
to_ret->length = 0;
to_ret->capacity= 0;
to_ret->spectra = NULL;
return to_ret;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief hdrl_spectrum1Dlist copy-constructor
* @param l input list
* @return a copy of the input list
*
* The constructor allocates memory of a new hdrl_spectrum1Dlist structure. Each
* element of the new list is a copy of the corresponding element in the source
* list.
*/
/* ---------------------------------------------------------------------------*/
hdrl_spectrum1Dlist * hdrl_spectrum1Dlist_duplicate(const hdrl_spectrum1Dlist * l){
if(l == NULL) return NULL;
hdrl_spectrum1Dlist * to_ret = hdrl_spectrum1Dlist_new();
for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(l); ++i){
const hdrl_spectrum1D * s_old = hdrl_spectrum1Dlist_get_const(l, i);
hdrl_spectrum1D * s = hdrl_spectrum1D_duplicate(s_old);
hdrl_spectrum1Dlist_set(to_ret, s, i);
}
return to_ret;
}
/**
* @brief hdrl_spectrum1Dlist wrapper
* @param self array of pointers to hdrl_spectrum1D
* @param sz number of elements in self
* @return self wrapped in a hdrl_spectrum1Dlist. The returned structure takes
* ownership of the self pointer.
*
* The constructor allocates memory of a new hdrl_spectrum1Dlist structure. The
* structure takes ownership of self, and therefore it is responsible for its
* de-allocation. Self must not contain duplicate or NULL pointers.
*/
/* ---------------------------------------------------------------------------*/
hdrl_spectrum1Dlist *
hdrl_spectrum1Dlist_wrap(hdrl_spectrum1D ** self, const cpl_size sz){
hdrl_spectrum1Dlist * to_ret = hdrl_spectrum1Dlist_new();
to_ret->spectra = self;
to_ret->capacity = to_ret->length = sz;
return to_ret;
}
/**
* @brief hdrl_spectrum1Dlist getter of the i-th element
* @param self hdrl_spectrum1Dlist
* @param idx index of the element we want. 0 <= index < size
* @return a pointer to the i-th element. NULL in case of error.
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if self is NULL
* - CPL_ERROR_ACCESS_OUT_OF_RANGE: index is less than 0 or index >= size
*/
/* ---------------------------------------------------------------------------*/
hdrl_spectrum1D *
hdrl_spectrum1Dlist_get(hdrl_spectrum1Dlist * self, const cpl_size idx){
cpl_error_code fail = check_getter(self, idx);
cpl_ensure(fail == CPL_ERROR_NONE, fail, NULL);
return self->spectra[idx];
}
/**
* @brief hdrl_spectrum1Dlist getter of the i-th element
* @param self hdrl_spectrum1Dlist
* @param idx index of the element we want. 0 <= index < size
* @return a const pointer to the i-th element. NULL in case of error.
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if self is NULL
* - CPL_ERROR_ACCESS_OUT_OF_RANGE: index is less than 0 or index >= size
*/
/* ---------------------------------------------------------------------------*/
const hdrl_spectrum1D *
hdrl_spectrum1Dlist_get_const(const hdrl_spectrum1Dlist *self , const cpl_size idx){
cpl_error_code fail = check_getter(self, idx);
cpl_ensure(fail == CPL_ERROR_NONE, fail, NULL);
return self->spectra[idx];
}
/**
* @brief hdrl_spectrum1Dlist setter of the i-th element
* @param self hdrl_spectrum1Dlist
* @param s hdrl_spectrum1D we want to insert
* @param idx position the spectrum is inserted in. 0 <= index <= size
* @return a cpl_error_code.
*
* The function insert s inside self. if index == size, the capacity of the list
* is increased to accommodate the new element. If a spectrum was already contained
* in the i-th position, it is de-allocated. The list becomes responsible for the
* memory management of s.
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if self is NULL
* - CPL_ERROR_ACCESS_OUT_OF_RANGE: index < 0 or index > size
* - CPL_ERROR_ILLEGAL_INPUT: if s is already contained inside self
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code
hdrl_spectrum1Dlist_set(hdrl_spectrum1Dlist * self,
hdrl_spectrum1D * s, const cpl_size idx){
cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(self->length >= 0, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(idx <= self->length, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(!contains(self, s), CPL_ERROR_ILLEGAL_INPUT);
if(idx == self->length){
push_back(self, s);
return CPL_ERROR_NONE;
}
hdrl_spectrum1D * old = self->spectra[idx];
hdrl_spectrum1D_delete(&old);
self->spectra[idx] = s;
return CPL_ERROR_NONE;
}
/**
* @brief hdrl_spectrum1Dlist remove of the i-th element
* @param self hdrl_spectrum1Dlist
* @param idx position of element we want to remove from the list.
* 0 <= index < size
* @return the removed element. NULL in case of error of if the idx-th element was NULL
*
* The function extract the idx-th element from the list. It shifts all the elements
* from idx + 1 to size - 1 down of one position. If the new size is less than half
* the capacity, the capacity is reduced.
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if self is NULL
* - CPL_ERROR_ACCESS_OUT_OF_RANGE: index < 0 or index >= size
*/
/* ---------------------------------------------------------------------------*/
hdrl_spectrum1D *
hdrl_spectrum1Dlist_unset(hdrl_spectrum1Dlist * self, const cpl_size idx){
cpl_error_code fail = check_getter(self, idx);
cpl_ensure(fail == CPL_ERROR_NONE, fail, NULL);
hdrl_spectrum1D * to_ret = self->spectra[idx];
const cpl_size sz = hdrl_spectrum1Dlist_get_size(self);
for(cpl_size i = idx; i < sz - 1; ++i){
self->spectra[i] = self->spectra[i + 1];
}
self->length--;
const cpl_size new_capacity = self->capacity / 2;
if(hdrl_spectrum1Dlist_get_size(self) <= new_capacity){
self->spectra = safe_realloc(self->spectra, self->capacity, new_capacity);
self->capacity = new_capacity;
}
return to_ret;
}
/**
* @brief hdrl_spectrum1Dlist destructor
* @param l hdrl_spectrum1Dlist
*
* The function deallocates the list and all the spectra it was containing. If l
* is NULL no operation is performed.
*/
/* ---------------------------------------------------------------------------*/
void hdrl_spectrum1Dlist_delete(hdrl_spectrum1Dlist * l){
if(l == NULL) return;
for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(l); ++i){
hdrl_spectrum1D_delete(&l->spectra[i]);
}
cpl_free(l->spectra);
cpl_free(l);
}
/**
* @brief hdrl_spectrum1Dlist getter for size
* @param l hdrl_spectrum1Dlist
*
* @return the size of the list. The size can be less or equal than the capacity
* of the list. The size refers to the number of valid elements in the list.
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if l is NULL
*/
/* ---------------------------------------------------------------------------*/
cpl_size
hdrl_spectrum1Dlist_get_size(const hdrl_spectrum1Dlist * l){
cpl_ensure(l != NULL, CPL_ERROR_NULL_INPUT, 0);
return l->length;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief collapsing a hdrl_spectrum1Dlist. The spectra in list are first
* resampled on the wavelengths wlengths. The resampling method is regulated
* by the resample_par. For more information on samplig_par see
* hdrl_spectrum1D_resample_on_array.
* The resampled fluxes are collapsed as in hdrl_imagelist_collapse and the
* collapsing is regulated by the parameter stacking_par.
* For more information on stacking_par please see the documentation of
* hdrl_imagelist_collapse.
*
* @param list hdrl_spectrum1D list
* @param stacking_par parameter regulating the stacking
* @param wlengths wavelengths the resulting spectrum is
* defined on
* @param resample_par parameter regulating the resampling
* @param mark_bpm_in_interpolation if true interpolated pixels whose
* neighbors (in the original spectrum)
* are rejected, are not considered
* during collapsing
* @param result the resulting spectrum
* @param contrib output contribution mask
* @param resampled_and_aligned_fluxes resampled and aligned fluxes to be
* collapsed
* @return cpl_error_code
*
* Possible cpl-error-code set in this function:
* - CPL_ERROR_NULL_INPUT: if any among input pointers in NULL of if any of the
* spectra inside the list are NULL.
* - CPL_ERROR_ILLEGAL_INPUT: if all the elements inside list are not defined on
* the same scale.
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code
hdrl_spectrum1Dlist_collapse (const hdrl_spectrum1Dlist *list,
const hdrl_parameter * stacking_par,
const cpl_array * wlengths, const hdrl_parameter * resample_par,
const cpl_boolean mark_bpm_in_interpolation,
hdrl_spectrum1D ** result, cpl_image ** contrib,
hdrl_imagelist ** resampled_and_aligned_fluxes){
cpl_ensure_code(are_spectra_valid(list), CPL_ERROR_NULL_INPUT);
cpl_ensure_code(wlengths != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(check_scales_are_same(list), CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(result != NULL, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(resampled_and_aligned_fluxes != NULL, CPL_ERROR_NULL_INPUT);
*result = NULL;
*contrib = NULL;
const cpl_size num_spectra = hdrl_spectrum1Dlist_get_size(list);
hdrl_spectrum1D ** resampled_spectra =
cpl_calloc(num_spectra, sizeof(hdrl_spectrum1D*));
cpl_ensure_code(num_spectra > 0, CPL_ERROR_ILLEGAL_INPUT);
cpl_error_code * errors = cpl_calloc(num_spectra, sizeof(cpl_error_code));
HDRL_OMP(omp parallel for)
for(cpl_size i = 0; i < num_spectra; ++i){
const hdrl_spectrum1D * this_s = hdrl_spectrum1Dlist_get_const(list, i);
resampled_spectra[i] =
hdrl_spectrum1D_resample_on_array(this_s, wlengths, resample_par);
errors[i] = cpl_error_get_code();
}
cpl_error_code err = get_first_error_code(errors, num_spectra);
cpl_free(errors);
if(!err){
hdrl_imagelist * stack_list =
create_list(resampled_spectra, list, mark_bpm_in_interpolation);
hdrl_image * stacked_img = NULL;
err = hdrl_imagelist_collapse(stack_list, stacking_par,
&stacked_img, contrib);
*resampled_and_aligned_fluxes = stack_list;
if(!err){
const hdrl_spectrum1D_wave_scale scale =
hdrl_spectrum1D_get_scale(hdrl_spectrum1Dlist_get_const(list, 0));
*result = hdrl_spectrum1D_create(hdrl_image_get_image(stacked_img),
hdrl_image_get_error(stacked_img),
wlengths,
scale);
}
hdrl_image_delete(stacked_img);
}
delete_spectrum1D_array(resampled_spectra, num_spectra);
return err;
}
/*-----------------------------------------------------------------------------
Private Functions Implementation
-----------------------------------------------------------------------------*/
/*Gets the minimum wavelengths ignoring bad pixels*/
static inline double
get_wmin_valid(const hdrl_spectrum1D * s){
const cpl_size sz = hdrl_spectrum1D_get_size(s);
double wmin = INFINITY;
for(cpl_size i = 0; i < sz; ++i){
int rej = 0;
double w = hdrl_spectrum1D_get_wavelength_value(s, i, &rej);
if(rej) continue;
if(w < wmin) wmin = w;
}
return wmin;
}
/*Gets the maximum wavelengths ignoring bad pixels*/
static inline double
get_wmax_valid(const hdrl_spectrum1D * s){
const cpl_size sz = hdrl_spectrum1D_get_size(s);
double wmax = -INFINITY;
for(cpl_size i = 0; i < sz; ++i){
int rej = 0;
double w = hdrl_spectrum1D_get_wavelength_value(s, i, &rej);
if(rej) continue;
if(w > wmax) wmax = w;
}
return wmax;
}
static inline cpl_image *
get_img_from_bpm(hdrl_spectrum1D_wavelength * w){
if(w->bpm)
return cpl_image_new_from_mask(w->bpm);
const cpl_size sz = cpl_array_get_size(w->wavelength);
return cpl_image_new(sz, 1, CPL_TYPE_INT);
}
static inline hdrl_spectrum1D *
get_interp_bpm(const hdrl_spectrum1D * s, const cpl_array * wlens){
hdrl_spectrum1D_wavelength wlen_ori = hdrl_spectrum1D_get_wavelength(s);
cpl_image * flx = get_img_from_bpm(&wlen_ori);
hdrl_spectrum1D * bpm = hdrl_spectrum1D_create_error_free(flx,
wlen_ori.wavelength, wlen_ori.scale);
cpl_image_delete(flx);
hdrl_parameter * par =
hdrl_spectrum1D_resample_interpolate_parameter_create(
hdrl_spectrum1D_interp_linear);
hdrl_spectrum1D * interp_bpm = hdrl_spectrum1D_resample_on_array(bpm, wlens,
par);
hdrl_spectrum1D_delete(&bpm);
hdrl_parameter_delete(par);
return interp_bpm;
}
static inline void
remove_if_neighbors_are_rejected(hdrl_image * flx,
const hdrl_spectrum1D * ori_spectrum, const cpl_array * wlens){
/*We interpolate the bpm to see if the elements in wlens have a bad pixel
* close by. If that is the case we reject them.*/
hdrl_spectrum1D * interp_bpm = get_interp_bpm(ori_spectrum, wlens);
for(cpl_size i = 0; i < hdrl_spectrum1D_get_size(interp_bpm); ++i){
const double v = hdrl_spectrum1D_get_flux_value(interp_bpm, i, NULL).data;
if(v > HDRL_EPS_DATA){
hdrl_image_reject(flx, i + 1, 1);
}
}
hdrl_spectrum1D_delete(&interp_bpm);
}
static inline hdrl_image *
get_padded_flux(const hdrl_spectrum1D * resampled_spectrum,
const hdrl_spectrum1D * ori_spectrum,
const cpl_boolean mark_bpm_in_interpolation){
if(resampled_spectrum == NULL) return NULL;
const double wmin = get_wmin_valid(ori_spectrum);
const double wmax = get_wmax_valid(ori_spectrum);
if(isinf(wmin) || isinf(wmax)) return NULL;
hdrl_image * flx =
hdrl_image_duplicate(hdrl_spectrum1D_get_flux(resampled_spectrum));
const cpl_array * wlens =
hdrl_spectrum1D_get_wavelength(resampled_spectrum).wavelength;
for(cpl_size i = 0; i < hdrl_spectrum1D_get_size(resampled_spectrum); ++i){
const double wlen = cpl_array_get(wlens, i, NULL);
if(wlen < wmin || wlen > wmax) {
hdrl_image_reject(flx, i + 1, 1);
}
}
if(mark_bpm_in_interpolation)
remove_if_neighbors_are_rejected(flx, ori_spectrum, wlens);
return flx;
}
static inline cpl_boolean
check_scales_are_same(const hdrl_spectrum1Dlist * list){
if(list == NULL) return CPL_TRUE;
const cpl_size sz = hdrl_spectrum1Dlist_get_size(list);
if(sz <= 1) return CPL_TRUE;
const hdrl_spectrum1D_wave_scale scale =
hdrl_spectrum1D_get_scale(hdrl_spectrum1Dlist_get_const(list, 0));
for(cpl_size i = 1; i < sz; ++i){
const hdrl_spectrum1D_wave_scale scale_this =
hdrl_spectrum1D_get_scale(hdrl_spectrum1Dlist_get_const(list, 0));
if(scale_this != scale) return CPL_FALSE;
}
return CPL_TRUE;
}
/*Given a array of hdrl_spectrum1D this function creates an hdrl_imagelist containing
* the fluxes of the spectra. The current implementation of resample extrapolates
* points outside the interval where values are available. We reject those points.*/
static inline hdrl_imagelist *
create_list(hdrl_spectrum1D ** resampled_spectra,
const hdrl_spectrum1Dlist * ori_spectra,
const cpl_boolean mark_bpm_in_interpolation){
const cpl_size num_spectra = hdrl_spectrum1Dlist_get_size(ori_spectra);
hdrl_image ** images = cpl_calloc(num_spectra, sizeof(hdrl_image*));
cpl_error_code * errs = cpl_calloc(num_spectra, sizeof(cpl_error_code));
HDRL_OMP(omp parallel for)
for(cpl_size i = 0; i < num_spectra; i++){
hdrl_image * img = get_padded_flux(resampled_spectra[i],
hdrl_spectrum1Dlist_get_const(ori_spectra, i),
mark_bpm_in_interpolation);
images[i] = img;
errs[i] = cpl_error_get_code();
}
cpl_error_code fail = get_first_error_code(errs, num_spectra);
cpl_free(errs);
hdrl_imagelist * list = NULL;
if(!fail){
list = hdrl_imagelist_new();
/* hdrl_imagelist_set has a race condition when growing the internal buffer
* it cannot be parallelized */
for(cpl_size i = 0; i < num_spectra; i++){
if(images[i] != NULL)
hdrl_imagelist_set(list, images[i], i);
}
}
cpl_free(images);
return list;
}
/*Function used in the getters: check that idx is in the correct ranges.*/
static inline
cpl_error_code check_getter(const hdrl_spectrum1Dlist *s, const cpl_size idx){
if(s == NULL) return CPL_ERROR_NULL_INPUT;
if(idx < 0) return CPL_ERROR_ACCESS_OUT_OF_RANGE;
if(idx >= s->length) return CPL_ERROR_ACCESS_OUT_OF_RANGE;
return CPL_ERROR_NONE;
}
/*wrapper around realloc, works are realloc, except if old_capacity == 0 (in this
* case it works like calloc) and if new_capacity == 0 (in this case it works like free)*/
static inline hdrl_spectrum1D **
safe_realloc(hdrl_spectrum1D ** current, const cpl_size old_capacity,
const cpl_size new_capacity){
/* Going from empty list to list with elements */
if(old_capacity == 0 && new_capacity > 0){
return cpl_calloc(new_capacity, sizeof(hdrl_spectrum1D * ));
}
/* Clean capacity */
if(new_capacity == 0){
cpl_free(current);
return NULL;
}
hdrl_spectrum1D ** to_ret =
cpl_realloc(current, sizeof(hdrl_spectrum1D * ) * new_capacity);
for(cpl_size i = old_capacity + 1; i < new_capacity; ++i){
to_ret[i] = NULL;
}
return to_ret;
}
/*increase the capacity of the list if length >= capacity*/
static inline
void resize_if_needed(hdrl_spectrum1Dlist *self){
if(self->length < self->capacity) return;
const cpl_size new_capacity = self->capacity == 0 ? 1 : 2 * self->capacity;
self->spectra = safe_realloc(self->spectra, self->capacity,
new_capacity);
self->capacity = new_capacity;
}
/*add an element in the back of a list, it can increase the capacity if needed*/
static inline
void push_back(hdrl_spectrum1Dlist *self, hdrl_spectrum1D * s){
resize_if_needed(self);
self->spectra[self->length] = s;
self->length++;
}
/*frees an array of spectrum1D and frees every spectrum1D*/
static inline void
delete_spectrum1D_array(hdrl_spectrum1D ** resampled_spectra,
const cpl_size num_spectra){
hdrl_spectrum1Dlist * l =
hdrl_spectrum1Dlist_wrap(resampled_spectra, num_spectra);
hdrl_spectrum1Dlist_delete(l);
}
/*check that all the spectra in the list are != NULL*/
static inline cpl_boolean
are_spectra_valid(const hdrl_spectrum1Dlist * list){
if(list == NULL) return CPL_FALSE;
const cpl_size length = hdrl_spectrum1Dlist_get_size(list);
for(cpl_size i = 0; i < length; ++i){
const hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get_const(list, i);
if(s == NULL) return CPL_FALSE;
}
return CPL_TRUE;
}
/*get the first element in the array different from CPL_ERROR_NONE*/
static inline cpl_error_code
get_first_error_code(const cpl_error_code * cds, const cpl_size length){
for(cpl_size i = 0; i < length; ++i){
if(cds[i]) return cds[i];
}
return CPL_ERROR_NONE;
}
static inline cpl_boolean
contains(const hdrl_spectrum1Dlist * list, const hdrl_spectrum1D * s){
const cpl_size sz = hdrl_spectrum1Dlist_get_size(list);
for(cpl_size i = 0; i < sz; ++i){
const hdrl_spectrum1D * s_in = hdrl_spectrum1Dlist_get_const(list, i);
if(s_in == s) return CPL_TRUE;
}
return CPL_FALSE;
}
/**@}*/
|