Changeset 16185 for trunk/psLib/src/fits/psFitsImage.c
- Timestamp:
- Jan 22, 2008, 5:08:03 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/fits/psFitsImage.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/fits/psFitsImage.c
r16095 r16185 7 7 * @author Robert DeSonia, MHPCC 8 8 * 9 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $10 * @date $Date: 2008-01- 16 20:10:35$9 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2008-01-23 03:08:03 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 14 14 15 15 #ifdef HAVE_CONFIG_H 16 # include "config.h"16 #include "config.h" 17 17 #endif 18 18 … … 36 36 #include "psFitsFloatFile.h" 37 37 #include "psFitsHeader.h" 38 #include "psFitsScale.h" 38 39 39 40 #include "psMemory.h" … … 241 242 } 242 243 243 # if (0)244 // XXX this needs to be optional (eg, invalid for a mask)245 246 // Apply the BSCALE and BZERO for an image with a "fuzz", so that we get the image as it should be written to247 // disk.248 // The idea is that the "fuzz" (adding a random number between 0 and 1) preserves the expectation value of249 // the image (e.g., a value of 0.1 will get translated to zero 90% of the time, and unity 10% of the time),250 // though at the cost of adding an additional variance of 1/12 (a standard deviation of ~0.29).251 static psImage *scaleImageForDisk(psImage *image, // Image to which to apply BSCALE and BZERO252 int bitpix, // Output BITPIX253 double bscale, // Scaling254 double bzero, // Zero point255 psRandom *rng // Random number generator (for the "fuzz"), or NULL256 )257 {258 assert(image);259 260 if (!PS_IS_PSELEMTYPE_REAL(image->type.type) || bitpix == 0) {261 return psMemIncrRefCounter(image);262 }263 264 psElemType outType; // Type for output image265 // Choosing to use signed types because those don't require BSCALE,BZERO to represent them in the FITS266 // file267 switch (bitpix) {268 case 8:269 outType = PS_TYPE_S8;270 break;271 case 16:272 outType = PS_TYPE_S16;273 break;274 case 32:275 outType = PS_TYPE_S32;276 break;277 case 64:278 outType = PS_TYPE_S64;279 break;280 default:281 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Target bitpix (%d) is not one of 8,16,32,64", bitpix);282 return NULL;283 }284 285 if (bscale == 1.0 && bzero == 0.0) {286 return psImageCopy(NULL, image, outType);287 }288 289 int numCols = image->numCols, numRows = image->numRows; // Size of image290 psImage *out = psImageAlloc(numCols, numRows, outType); // Output image291 292 if (!psMemIncrRefCounter(rng)) {293 // Don't blab about which seed we're going to get --- it's not necessary for this purpose294 psU64 seed = p_psRandomGetSystemSeed(false);295 rng = psRandomAlloc(PS_RANDOM_TAUS, seed);296 }297 298 299 #define SCALE_WRITE_OUT_CASE(IN, INTYPE, OUT, OUTTYPE) \300 case PS_TYPE_##OUTTYPE: { \301 ps##INTYPE scale = 1.0 / bscale; \302 ps##INTYPE zero = bzero; \303 for (int y = 0; y < numRows; y++) { \304 for (int x = 0; x < numCols; x++) { \305 /* Add random factor [0,1): adds a variance of 1/12, but preserves the expectation value */ \306 ps##INTYPE random = psRandomUniform(rng); \307 (OUT)->data.OUTTYPE[y][x] = ((IN)->data.INTYPE[y][x] - zero) * scale + random; \308 } \309 } \310 break; \311 }312 313 #define SCALE_WRITE_IN_CASE(IN, INTYPE, OUT) \314 case PS_TYPE_##INTYPE: { \315 switch (outType) { \316 SCALE_WRITE_OUT_CASE(IN, INTYPE, OUT, S8); \317 SCALE_WRITE_OUT_CASE(IN, INTYPE, OUT, S16); \318 SCALE_WRITE_OUT_CASE(IN, INTYPE, OUT, S32); \319 SCALE_WRITE_OUT_CASE(IN, INTYPE, OUT, S64); \320 default: \321 psAbort("Should be unreachable."); \322 } \323 break; \324 }325 326 switch (image->type.type) {327 SCALE_WRITE_IN_CASE(image, F32, out);328 SCALE_WRITE_IN_CASE(image, F64, out);329 default:330 psAbort("Should be unreachable.");331 }332 333 psFree(rng);334 335 return out;336 }337 # endif338 339 # if (0)340 // XXX supporting code needs to make this an optional operation341 // Determine BSCALE and BZERO for an image, and generate a new image with it applied342 // TRUE = BZERO + BSCALE * FITS343 static psImage *scaleImageDetermine(double *bscale, // Scaling, to return344 double *bzero, // Zero point, to return345 psImage *image, // Image to scale346 int bitpix, // Desired bits per pixel347 psRandom *rng // Random number generator for scaleImageForDisk348 )349 {350 PS_ASSERT_PTR_NON_NULL(bscale, NULL);351 PS_ASSERT_PTR_NON_NULL(bzero, NULL);352 PS_ASSERT_IMAGE_NON_NULL(image, NULL);353 PS_ASSERT_IMAGE_TYPE_F32_OR_F64(image, NULL);354 355 *bscale = 0.0;356 *bzero = 0.0;357 358 switch (bitpix) {359 case 0:360 // No scaling applied361 return psMemIncrRefCounter(image);362 case 8:363 case 16:364 case 32:365 case 64:366 // Nothing to do; just allowing these values to pass through367 break;368 default:369 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Target bitpix (%d) is not one of 8,16,32,64", bitpix);370 return NULL;371 }372 373 int numCols = image->numCols, numRows = image->numRows;374 double range = pow(2.0, bitpix); // Range of values for target BITPIX375 376 #define SCALE_DETERMINE_CASE(IN, INTYPE) \377 case PS_TYPE_##INTYPE: { \378 ps##INTYPE min = INFINITY, max = -INFINITY; /* Minimum and maximum values */ \379 for (int y = 0; y < numRows; y++) { \380 for (int x = 0; x < numCols; x++) { \381 ps##INTYPE value = (IN)->data.INTYPE[y][x]; /* Value of interest */ \382 if (isfinite(value)) { \383 if (value < min) { \384 min = value; \385 } \386 if (value > max) { \387 max = value; \388 } \389 } \390 } \391 } \392 if (!isfinite(min) || !isfinite(max)) { \393 psWarning("No valid values in image to derive BSCALE,BZERO --- using original image."); \394 *bscale = 1.0; \395 *bzero = 0.0; \396 return psMemIncrRefCounter(image); \397 } \398 if (min == max) { \399 *bscale = 1.0; \400 *bzero = min; \401 } else { \402 *bscale = (max - min) / (range - 1.0); \403 *bzero = min + 0.5 * range * (*bscale); \404 } \405 break; \406 }407 408 switch (image->type.type) {409 SCALE_DETERMINE_CASE(image, F32);410 SCALE_DETERMINE_CASE(image, F64);411 default:412 psAbort("Should be unreachable.");413 }414 psTrace("psLib.fits", 3, "BSCALE = %.10lf, BZERO = %.10lf\n", *bscale, *bzero);415 416 return scaleImageForDisk(image, bitpix, *bscale, *bzero, rng);417 }418 # endif419 420 #if 0421 // This function to apply BSCALE and BZERO to an image read immediately from disk should not be necessary at422 // the present time, since cfitsio should apply the scaling itself in the process of reading. However, we may423 // later desire it.424 static psImage *scaleImageFromDisk(psFits *fits, psImage *image)425 {426 PS_ASSERT_IMAGE_NON_NULL(image, NULL);427 428 if (bscale == 0.0) {429 // BSCALE = 0 means don't apply anything430 return psMemIncrRefCounter(image);431 }432 433 psElemType inType = image->type.type; // Type for input image434 psElemType outType; // Type for output image435 switch (inType) {436 case PS_TYPE_S8:437 case PS_TYPE_S16:438 case PS_TYPE_S32:439 case PS_TYPE_U8:440 case PS_TYPE_U16:441 outType = PS_TYPE_F32;442 break;443 case PS_TYPE_S64:444 case PS_TYPE_U32:445 case PS_TYPE_U64:446 outType = PS_TYPE_F64;447 break;448 // Including floating-point types just in case someone wants to apply a BSCALE and BZERO to them.449 case PS_TYPE_F32:450 outType = PS_TYPE_F32;451 break;452 case PS_TYPE_F64:453 outType = PS_TYPE_F64;454 break;455 default:456 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unsupported image type: %x", inType);457 return NULL;458 }459 460 int numCols = image->numCols, numRows = image->numRows;461 psImage *out = psImageAlloc(numCols, numRows, outType); // Output scaled image462 463 464 #define SCALE_READ_OUT_CASE(INTYPE, OUTTYPE) \465 case PS_TYPE_##OUTTYPE: { \466 for (int y = 0; y < numRows; y++) { \467 for (int x = 0; x < numCols; x++) { \468 out->data.OUTTYPE[y][x] = image->data.INTYPE[y][x] * bscale + bzero; \469 } \470 } \471 break; \472 }473 474 #define SCALE_READ_IN_CASE(INTYPE) \475 case PS_TYPE_##INTYPE: { \476 switch (outType) { \477 SCALE_READ_OUT_CASE(INTYPE, F32); \478 SCALE_READ_OUT_CASE(INTYPE, F64); \479 default: \480 psAbort("Should never get here: type %x should be F32 or F64", outType); \481 } \482 break; \483 }484 485 switch (inType) {486 SCALE_READ_IN_CASE(S8);487 SCALE_READ_IN_CASE(S16);488 SCALE_READ_IN_CASE(S32);489 SCALE_READ_IN_CASE(S64);490 SCALE_READ_IN_CASE(U8);491 SCALE_READ_IN_CASE(U16);492 SCALE_READ_IN_CASE(U32);493 SCALE_READ_IN_CASE(U64);494 SCALE_READ_IN_CASE(F32);495 SCALE_READ_IN_CASE(F64);496 default:497 psAbort("Should never get here: type %x should be integer", inType);498 }499 500 return out;501 }502 #endif503 244 504 245 // Convert an image to the desired BITPIX, i.e., the desired disk representation … … 522 263 *floatType = PS_FITS_FLOAT_NONE; 523 264 265 psFitsOptions *options = fits->options; 266 if (!options) { 267 return psMemIncrRefCounter((psImage*)image); // Casting away const 268 } 269 524 270 // Custom floating-point 525 if (PS_IS_PSELEMTYPE_REAL(image->type.type) && fits->conventions.psBitpix &&526 fits->floatType != PS_FITS_FLOAT_NONE) {527 *floatType = fits->floatType;528 return psFitsFloatImageToDisk(image, fits->floatType);271 if (PS_IS_PSELEMTYPE_REAL(image->type.type) && options->conventions.psBitpix && 272 options->floatType != PS_FITS_FLOAT_NONE) { 273 *floatType = options->floatType; 274 return psFitsFloatImageToDisk(image, options->floatType); 529 275 } 530 276 531 277 // Automatically select what we're given 532 if ( fits->bitpix == 0) {278 if (options->bitpix == 0) { 533 279 return psMemIncrRefCounter((psImage*)image); // Casting away const 534 280 } 535 281 536 282 // Quantise floating-point images 537 // XXX this needs to be more controlled: certainly not valid for output masks! 538 # if (0) 539 if (PS_IS_PSELEMTYPE_REAL(image->type.type) && fits->bitpix > 0) { 283 if (PS_IS_PSELEMTYPE_REAL(image->type.type) && options->bitpix > 0) { 540 284 if (newScaleZero) { 541 return scaleImageDetermine(bscale, bzero, (psImage*)image, fits->bitpix, rng); 542 } 543 // Get the current BSCALE and BZERO 544 int status = 0; // Status of cfitsio 545 if (fits_read_key_dbl(fits->fd, "BSCALE", bscale, NULL, &status) && status != KEY_NO_EXIST) { 546 psFitsError(status, true, "Unable to read header."); 547 return NULL; 548 } 549 status = 0; 550 if (fits_read_key_dbl(fits->fd, "BZERO", bzero, NULL, &status) && status != KEY_NO_EXIST) { 551 psFitsError(status, true, "Unable to read header."); 552 return NULL; 553 } 554 status = 0; 555 if (*bscale == 0.0) { 556 psError(PS_ERR_IO, true, 557 "Supposed to use old values of BSCALE and BZERO, but they don't exist."); 558 return NULL; 559 } 560 return scaleImageForDisk((psImage*)image, fits->bitpix, *bscale, *bzero, rng); 561 } 562 # endif 285 // Choose an appropriate BSCALE and BZERO 286 if (!psFitsScaleDetermine(bscale, bzero, image, fits)) { 287 psError(PS_ERR_UNKNOWN, false, "Unable to determine BSCALE and BZERO for image."); 288 return NULL; 289 } 290 } else { 291 // Don't want to muck with the current BSCALE and BZERO. Get the current values and use those. 292 int status = 0; // Status of cfitsio 293 if (fits_read_key_dbl(fits->fd, "BSCALE", bscale, NULL, &status) && status != KEY_NO_EXIST) { 294 psFitsError(status, true, "Unable to read header."); 295 return NULL; 296 } 297 status = 0; 298 if (fits_read_key_dbl(fits->fd, "BZERO", bzero, NULL, &status) && status != KEY_NO_EXIST) { 299 psFitsError(status, true, "Unable to read header."); 300 return NULL; 301 } 302 status = 0; 303 if (*bscale == 0.0) { 304 psError(PS_ERR_IO, true, 305 "Supposed to use old values of BSCALE and BZERO, but they don't exist."); 306 return NULL; 307 } 308 } 309 310 return psFitsScaleForDisk(image, fits, *bscale, *bzero, rng); 311 } 563 312 564 313 // Choose the appropriate output type, given the input type and desired bits per pixel … … 576 325 psElemType inType = image->type.type; // Type for input image 577 326 psElemType outType; // Type for output image 578 switch ( fits->bitpix) {327 switch (options->bitpix) { 579 328 CONVERT_TYPE_INT_CASE(outType, inType, 8); 580 329 CONVERT_TYPE_INT_CASE(outType, inType, 16); … … 585 334 default: 586 335 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Target bitpix (%d) is not one of 8,16,32,64", 587 fits->bitpix);336 options->bitpix); 588 337 return NULL; 589 338 } … … 592 341 return psMemIncrRefCounter((psImage*)image); 593 342 } 343 344 if (PSELEMTYPE_SIZEOF(inType) > PSELEMTYPE_SIZEOF(outType)) { 345 psWarning("Truncating image pixels to write to disk."); 346 } 347 594 348 return psImageCopy(NULL, image, outType); 595 349 } … … 765 519 bzero = cfitsioBzero; 766 520 } 767 assert(bitPix == fits->bitpix || fits->bitpix == 0); 521 psFitsOptions *options = fits->options; // FITS I/O options 522 assert(!options || bitPix == options->bitpix || options->bitpix == 0); 768 523 769 524 int naxis = 3; // Number of axes … … 807 562 // an unsigned integer type). In all other cases, we have already converted the image to use the 808 563 // appropriate scale and zero (because we want to apply a randomiser to the quantisation). 809 fits_set_bscale(fits->fd, 1.0, bzero, &status);564 fits_set_bscale(fits->fd, 1.0, cfitsioBzero, &status); 810 565 811 566 if (bscale != 0.0) { … … 895 650 bzero = cfitsioBzero; 896 651 } 897 assert(bitPix == fits->bitpix || fits->bitpix == 0); 652 psFitsOptions *options = fits->options; // FITS I/O options 653 assert(!options || bitPix == options->bitpix || options->bitpix == 0); 898 654 899 655 //check to see if the HDU has the same datatype
Note:
See TracChangeset
for help on using the changeset viewer.
