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
|
/* $Id: irplib_utils.h,v 1.60 2013-08-21 14:55:14 cgarcia Exp $
*
* This file is part of the irplib package
* Copyright (C) 2002,2003 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 02111-1307 USA
*/
/*
* $Author: cgarcia $
* $Date: 2013-08-21 14:55:14 $
* $Revision: 1.60 $
* $Name: not supported by cvs2svn $
* $Log: not supported by cvs2svn $
* Revision 1.59 2013/03/15 09:06:06 jtaylor
* add irplib_aligned_{[mc]alloc,free}
*
* allow portable allocation of aligned memory for vectorization
*
* Revision 1.58 2013/03/15 09:05:28 jtaylor
* move isnan and isinf to header so it is inlineable and use gcc builtin for better performance
*
* Revision 1.57 2013/02/27 16:02:02 jtaylor
* add diagnostic pragma macros
*
* Revision 1.56 2012/08/06 06:14:18 llundin
* irplib_errorstate_warning(): Replaced by cpl_errorstate_dump_one_warning() from CPL 6.X
*
* Revision 1.55 2011/06/01 06:47:56 llundin
* skip_if_lt(): Fix previous edits switch of A and B in error message
*
* Revision 1.54 2011/05/26 08:08:56 llundin
* skip_if_lt(): Support printf-style error message, name-space protect temporary variables
*
* Revision 1.53 2011/05/09 07:51:18 llundin
* irplib_dfs_save_image_(): Modified from cpl_dfs_save_image(). irplib_dfs_save_image(): Use irplib_dfs_save_image_()
*
* Revision 1.52 2010/03/23 07:57:59 kmirny
* DFS08552, Documentation for irplib_frameset_sort
*
* Revision 1.51 2009/12/16 14:59:30 cgarcia
* Avoid name clash with index function
*
* Revision 1.50 2009/08/17 15:10:16 kmirny
*
* DFS07454 DFS07437
*
*/
#ifndef IRPLIB_UTILS_H
#define IRPLIB_UTILS_H
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include <cpl.h>
#include <math.h>
#include <stdarg.h>
/*-----------------------------------------------------------------------------
Defines
-----------------------------------------------------------------------------*/
/* Concatenate two macro arguments */
#define IRPLIB_CONCAT(a,b) a ## _ ## b
#define IRPLIB_CONCAT2X(a,b) IRPLIB_CONCAT(a,b)
/*----------------------------------------------------------------------------*/
/*
@brief Swap two double values
@param A The 1st double to swap
@param B The 2nd double to swap
@note A and B are evaluated twice, so side-effects should be avoided
*/
/*----------------------------------------------------------------------------*/
#define IRPLIB_SWAP_DOUBLE(A, B) do { \
const double irplib_swap_double=(A); \
(A)=(B); \
(B)=irplib_swap_double; \
} while (0)
#define IRPLIB_XSTRINGIFY CPL_XSTRINGIFY
#define IRPLIB_STRINGIFY CPL_STRINGIFY
#if !defined __GNUC__ && !defined __inline__
#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define __inline__ inline
#else
#define __inline__
#endif
#endif
#define IRPLIB_DIAG_PRAGMA_PUSH_IGN CPL_DIAG_PRAGMA_PUSH_IGN
#define IRPLIB_DIAG_PRAGMA_PUSH_ERR CPL_DIAG_PRAGMA_PUSH_ERR
#define IRPLIB_DIAG_PRAGMA_POP CPL_DIAG_PRAGMA_POP
/* FIXME: Remove when no longer used by any irplib-based pipelines */
/* Useful for debugging */
#define irplib_trace() do if (cpl_error_get_code()) { \
cpl_msg_debug(cpl_func, __FILE__ " at line " \
CPL_STRINGIFY(__LINE__) ": ERROR '%s' at %s", \
cpl_error_get_message(), cpl_error_get_where()); \
} else { \
cpl_msg_debug(cpl_func, __FILE__ " at line " \
CPL_STRINGIFY(__LINE__) ": OK"); \
} while (0)
#define irplib_error_recover(ESTATE, ...) \
do if (!cpl_errorstate_is_equal(ESTATE)) { \
cpl_msg_warning(cpl_func, __VA_ARGS__); \
cpl_msg_indent_more(); \
cpl_errorstate_dump(ESTATE, CPL_FALSE, \
cpl_errorstate_dump_one_warning); \
cpl_msg_indent_less(); \
cpl_errorstate_set(ESTATE); \
} while (0)
/*----------------------------------------------------------------------------*/
/*
@brief Declare a function suitable for use with irplib_dfs_table_convert()
@param table_set_row The name of the function to declare
@see irplib_dfs_table_convert(), irplib_table_read_from_frameset()
*/
/*----------------------------------------------------------------------------*/
#define IRPLIB_UTIL_SET_ROW(table_set_row) \
cpl_boolean table_set_row(cpl_table *, \
const char *, \
int, \
const cpl_frame *, \
const cpl_parameterlist *)
/*----------------------------------------------------------------------------*/
/*
@brief Declare a function suitable for use with irplib_dfs_table_convert()
@param table_check The name of the function to declare
@see irplib_dfs_table_convert()
*/
/*----------------------------------------------------------------------------*/
#define IRPLIB_UTIL_CHECK(table_check) \
cpl_error_code table_check(cpl_table *, \
const cpl_frameset *, \
const cpl_parameterlist *)
/*----------------------------------------------------------------------------*/
/*
@brief Conditional skip to the (unqiue) return point of the function
@param CONDITION The condition to check
@see cpl_error_ensure()
skip_if() takes one argument, which is a logical expression.
If the logical expression is false skip_if() takes no action and
program execution continues.
If the logical expression is true this indicates an error. In this case
skip_if() will set the location of the error to the point where it
was invoked in the recipe code (unless the error location is already in the
recipe code). If no error code had been set, then skip_if() will set one.
Finally, skip_if() causes program execution to skip to the macro 'end_skip'.
The macro end_skip is located towards the end of the function, after
which all resource deallocation and the function return is located.
The use of skip_if() assumes the following coding practice:
1) Pointers used for dynamically allocated memory that they "own" shall always
point to either NULL or to allocated memory (including CPL-objects).
2) Such pointers may not be reused to point to memory whose deallocation
requires calls to different functions.
3) Pointers of type FILE should be set NULL when not pointing to an open
stream and their closing calls (fclose(), freopen(), etc.) following the
'end_skip' should be guarded against such NULL pointers.
Error checking with skip_if() is encouraged due to the following advantages:
1) It ensures that a CPL-error code is set.
2) It ensures that the location of the error in the _recipe_ code is noted.
3) The error checking may be confined to a single concise line.
4) It is not necessary to replicate memory deallocation for every error
condition.
5) If more extensive error reporting/handling is required it is not precluded
by the use of skip_if().
6) It allows for a single point of function return.
7) It allows for optional, uniformly formatted debugging/tracing information
at each macro invocation.
The implementation of skip_if() uses a goto/label construction.
According to Kerningham & Ritchie, The C Programming Language, 2nd edition,
Section 3.8:
"This organization is handy if the error-handling code is non-trivial,
and if errors can occur in several places."
The use of goto for any other purpose should be avoided.
*/
/*----------------------------------------------------------------------------*/
#define skip_if(CONDITION) \
do { \
cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
goto cleanup, "Propagating a pre-existing error"); \
cpl_error_ensure(!(CONDITION), cpl_error_get_code(), \
goto cleanup, "Propagating error");\
} while (0)
/*----------------------------------------------------------------------------*/
/*
@brief Skip if A != B
@param A The 1st double to compare
@param B The 2nd double to compare
@param MSG A printf-style error message, 1st arg should be a string literal
@see skip_if()
@note A and B are evaluated exactly once
If no CPL error is set, sets CPL_ERROR_DATA_NOT_FOUND on failure
*/
/*----------------------------------------------------------------------------*/
#define skip_if_ne(A, B, ...) \
do { \
/* Name-space protected one-time only evaluation */ \
const double irplib_utils_a = (double)(A); \
const double irplib_utils_b = (double)(B); \
\
cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
goto cleanup, "Propagating a pre-existing error"); \
if (irplib_utils_a != irplib_utils_b) { \
char * irplib_utils_msg = cpl_sprintf(__VA_ARGS__); \
(void)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
"Need %g (not %g) %s", \
irplib_utils_b, irplib_utils_a, \
irplib_utils_msg); \
cpl_free(irplib_utils_msg); \
goto cleanup; \
} \
} while (0)
/*----------------------------------------------------------------------------*/
/*
@brief Skip if A < B
@param A The 1st double to compare
@param B The 2nd double to compare
@param MSG A printf-style error message, 1st arg should be a string literal
@see skip_if()
@note A and B are evaluated exactly once
If no CPL error is set, sets CPL_ERROR_DATA_NOT_FOUND on failure
*/
/*----------------------------------------------------------------------------*/
#define skip_if_lt(A, B, ...) \
do { \
/* Name-space protected one-time only evaluation */ \
const double irplib_utils_a = (double)(A); \
const double irplib_utils_b = (double)(B); \
\
cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
goto cleanup, "Propagating a pre-existing error"); \
if (irplib_utils_a < irplib_utils_b) { \
char * irplib_utils_msg = cpl_sprintf(__VA_ARGS__); \
(void)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
"Need at least %g (not %g) %s", \
irplib_utils_b, irplib_utils_a, \
irplib_utils_msg); \
cpl_free(irplib_utils_msg); \
goto cleanup; \
} \
} while (0)
/*----------------------------------------------------------------------------*/
/*
@brief Conditional skip on coding bug
@param CONDITION The condition to check
@see skip_if()
@note unlike assert() this check cannot be disabled
*/
/*----------------------------------------------------------------------------*/
#define bug_if(CONDITION) \
do { \
cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
goto cleanup, "Propagating an unexpected error, " \
"please report to " PACKAGE_BUGREPORT); \
cpl_error_ensure(!(CONDITION), CPL_ERROR_UNSPECIFIED, \
goto cleanup, "Internal error, please report to " \
PACKAGE_BUGREPORT); \
} while (0)
/*----------------------------------------------------------------------------*/
/*
@brief Conditional skip with error creation
@param CONDITION The condition to check
@param ERROR The error code to set
@param MSG A printf-style error message. As a matter of
user-friendliness the message should mention any
value that caused the @em CONDITION to fail.
@see skip_if()
@note unlike assert() this check cannot be disabled
*/
/*----------------------------------------------------------------------------*/
#define error_if(CONDITION, ERROR, ...) \
cpl_error_ensure(cpl_error_get_code() == CPL_ERROR_NONE && \
!(CONDITION), ERROR, goto cleanup, __VA_ARGS__)
/*----------------------------------------------------------------------------*/
/*
@brief Propagate a preexisting error, if any
@param MSG A printf-style error message.
@see skip_if()
*/
/*----------------------------------------------------------------------------*/
#define any_if(...) \
cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
goto cleanup, __VA_ARGS__)
/*----------------------------------------------------------------------------*/
/*
@brief Define the single point of resource deallocation and return
@see skip_if()
@note end_skip should be used exactly once in functions that use skip_if() etc
*/
/*----------------------------------------------------------------------------*/
#define end_skip \
do { \
cleanup: \
if (cpl_error_get_code()) \
cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line " \
CPL_STRINGIFY(__LINE__) " with error '%s' at %s", \
cpl_error_get_message(), cpl_error_get_where()); \
else \
cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line " \
CPL_STRINGIFY(__LINE__)); \
} while (0)
/*----------------------------------------------------------------------------*/
/**
@brief Conditional skip to the (unqiue) return point of the function
@param CONDITION An error is set if this test condition
evaluates to false
@param ec The CPL error code
@param msg A printf-style error
message. As a matter of user-friendliness
the message should mention any value
that caused the @em CONDITION to fail.
@see skip_if()
*/
/*----------------------------------------------------------------------------*/
#define irplib_ensure(CONDITION, ec, ...) \
cpl_error_ensure(CONDITION, ec, goto cleanup, __VA_ARGS__)
/*----------------------------------------------------------------------------*/
/**
@brief Catch an error
@param COMMAND Command to execute and check. This command is
expected to set the cpl_error_code in case of
failure
@param msg A printf-style error message.
@see skip_if()
This macro is used to catch an error from a function that sets the
@c cpl_error_code in case of error.
Example:
@code
irplib_check( cpl_object_do_something(object), ("Could not do something"));
@endcode
If the @c cpl_error_code is set before or after calling the function,
execution jumps to the @em cleanup label.
The macro can also be used to check a sequence of commands:
@code
irplib_check(
x = cpl_table_get_int(table, "x", 0, NULL);
y = cpl_table_get_int(table, "y", 0, NULL);
z = cpl_table_get_int(table, "z", 0, NULL), / * Comma here! * /
("Error reading wavelength catalogue"));
@endcode
*/
/*----------------------------------------------------------------------------*/
#define irplib_check(COMMAND, ...) \
do { \
cpl_errorstate irplib_check_prestate = cpl_errorstate_get(); \
skip_if(0); \
COMMAND; \
irplib_trace(); \
irplib_ensure(cpl_errorstate_is_equal(irplib_check_prestate), \
cpl_error_get_code(), __VA_ARGS__); \
irplib_trace(); \
} while (0)
/*-----------------------------------------------------------------------------
Function prototypes
-----------------------------------------------------------------------------*/
cpl_error_code irplib_dfs_save_image(cpl_frameset *,
const cpl_parameterlist *,
const cpl_frameset *,
const cpl_image *,
cpl_type_bpp ,
const char *,
const char *,
const cpl_propertylist *,
const char *,
const char *,
const char *);
cpl_error_code irplib_dfs_save_propertylist(cpl_frameset *,
const cpl_parameterlist *,
const cpl_frameset *,
const char *,
const char *,
const cpl_propertylist *,
const char *,
const char *,
const char *);
cpl_error_code irplib_dfs_save_imagelist(cpl_frameset *,
const cpl_parameterlist *,
const cpl_frameset *,
const cpl_imagelist *,
cpl_type_bpp ,
const char *,
const char *,
const cpl_propertylist *,
const char *,
const char *,
const char *);
cpl_error_code irplib_dfs_save_table(cpl_frameset *,
const cpl_parameterlist *,
const cpl_frameset *,
const cpl_table *,
const cpl_propertylist *,
const char *,
const char *,
const cpl_propertylist *,
const char *,
const char *,
const char *);
cpl_error_code irplib_dfs_save_image_(cpl_frameset *,
cpl_propertylist *,
const cpl_parameterlist *,
const cpl_frameset *,
const cpl_frame *,
const cpl_image *,
cpl_type ,
const char *,
const cpl_propertylist *,
const char *,
const char *,
const char *);
void irplib_reset(void);
int irplib_compare_tags(cpl_frame *, cpl_frame *);
const char * irplib_frameset_find_file(const cpl_frameset *, const char *);
const cpl_frame * irplib_frameset_get_first_from_group(const cpl_frameset *,
cpl_frame_group);
cpl_error_code irplib_apertures_find_max_flux(const cpl_apertures *, int *,
int);
#if defined HAVE_ISNAN && HAVE_ISNAN != 0
#if !defined isnan && defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 0
/* HP-UX and Solaris may have isnan() available at link-time
without the prototype */
int isnan(double);
#endif
#endif
cpl_error_code
irplib_dfs_table_convert(cpl_table *, cpl_frameset *, const cpl_frameset *,
int, char, const char *, const char *,
const cpl_parameterlist *, const char *,
const cpl_propertylist *, const cpl_propertylist *,
const char *, const char *, const char *,
cpl_boolean (*)(cpl_table *, const char *, int,
const cpl_frame *,
const cpl_parameterlist *),
cpl_error_code (*)(cpl_table *,
const cpl_frameset *,
const cpl_parameterlist *));
cpl_error_code irplib_table_read_from_frameset(cpl_table *,
const cpl_frameset *,
int,
char,
const cpl_parameterlist *,
cpl_boolean (*)
(cpl_table *, const char *,
int, const cpl_frame *,
const cpl_parameterlist *));
cpl_error_code irplib_image_split(const cpl_image *,
cpl_image *, cpl_image *, cpl_image *,
double, cpl_boolean,
double, cpl_boolean,
double, double,
cpl_boolean, cpl_boolean, cpl_boolean);
void irplib_errorstate_dump_warning(unsigned, unsigned, unsigned);
void irplib_errorstate_dump_info(unsigned, unsigned, unsigned);
void irplib_errorstate_dump_debug(unsigned, unsigned, unsigned);
/* wrapper for replace deprecated function cpl_polynomial_fit_1d_create*/
cpl_polynomial * irplib_polynomial_fit_1d_create(
const cpl_vector * x_pos,
const cpl_vector * values,
int degree,
double * mse
);
cpl_polynomial * irplib_polynomial_fit_1d_create_chiq(
const cpl_vector * x_pos,
const cpl_vector * values,
int degree,
double * rechiq
);
/*----------------------------------------------------------------------------*/
/**
@brief Sort a frameset based on the exposure time
@param self Frameset to sort (input)
@param iindex Index array with sort results (output), each element is
a frame number
@param exptime Array with exposure time for each frame (output)
*/
cpl_error_code irplib_frameset_sort(
const cpl_frameset * self,
int* iindex,
double* exptime);
/* FIXME: add alloc_size(2) */
void * irplib_aligned_malloc(size_t alignment, size_t size) CPL_ATTR_ALLOC;
void * irplib_aligned_calloc(size_t alignment,
size_t nelem, size_t nbytes) CPL_ATTR_ALLOC;
void irplib_aligned_free (void * aligned_ptr);
const cpl_frame *
irplib_frameset_get_first_const(cpl_frameset_iterator **iterator,
const cpl_frameset *frameset);
const cpl_frame *
irplib_frameset_get_next_const(cpl_frameset_iterator *iterator);
void irplib_vector_get_kth(cpl_vector *, cpl_size);
/*-----------------------------------------------------------------------------
Function inlines
-----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/**
@brief portable isinf
*/
/*----------------------------------------------------------------------------*/
static __inline__ int irplib_isinf(double value)
{
/* documented only on 4.4, but available in at least 4.2 */
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
return __builtin_isinf(value);
#elif defined HAVE_ISINF && HAVE_ISINF
return isinf(value);
#else
return value != 0 && value == 2 * value;
#endif
}
/*----------------------------------------------------------------------------*/
/**
@brief portable isnan
*/
/*----------------------------------------------------------------------------*/
static __inline__ int irplib_isnan(double value)
{
/* documented only on 4.4, but available in at least 4.2 */
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
return __builtin_isnan(value);
#elif defined HAVE_ISNAN && HAVE_ISNAN
return isnan(value);
#else
return value != value;
#endif
}
#endif
|