Changeset 16122 for branches/pap_branch_080117/psLib/src/fits/psFitsImage.c
- Timestamp:
- Jan 17, 2008, 3:04:21 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/pap_branch_080117/psLib/src/fits/psFitsImage.c
r16095 r16122 7 7 * @author Robert DeSonia, MHPCC 8 8 * 9 * @version $Revision: 1.23 $ $Name: not supported by cvs2svn $10 * @date $Date: 2008-01-1 6 20:10:35$9 * @version $Revision: 1.23.2.1 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2008-01-18 01:04:21 $ 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 … … 535 276 536 277 // Quantise floating-point images 537 // XXX this needs to be more controlled: certainly not valid for output masks!538 # if (0)539 278 if (PS_IS_PSELEMTYPE_REAL(image->type.type) && fits->bitpix > 0) { 540 279 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 280 // Choose an appropriate BSCALE and BZERO 281 if (!psFitsScaleDetermine(bscale, bzero, image, fits)) { 282 psError(PS_ERR_UNKNOWN, false, "Unable to determine BSCALE and BZERO for image."); 283 return NULL; 284 } 285 } else { 286 // Don't want to muck with the current BSCALE and BZERO. Get the current values and use those. 287 int status = 0; // Status of cfitsio 288 if (fits_read_key_dbl(fits->fd, "BSCALE", bscale, NULL, &status) && status != KEY_NO_EXIST) { 289 psFitsError(status, true, "Unable to read header."); 290 return NULL; 291 } 292 status = 0; 293 if (fits_read_key_dbl(fits->fd, "BZERO", bzero, NULL, &status) && status != KEY_NO_EXIST) { 294 psFitsError(status, true, "Unable to read header."); 295 return NULL; 296 } 297 status = 0; 298 if (*bscale == 0.0) { 299 psError(PS_ERR_IO, true, 300 "Supposed to use old values of BSCALE and BZERO, but they don't exist."); 301 return NULL; 302 } 303 } 304 305 return psFitsScaleForDisk(image, fits->bitpix, *bscale, *bzero, fits->fuzz, rng); 306 } 563 307 564 308 // Choose the appropriate output type, given the input type and desired bits per pixel … … 592 336 return psMemIncrRefCounter((psImage*)image); 593 337 } 338 594 339 return psImageCopy(NULL, image, outType); 595 340 }
Note:
See TracChangeset
for help on using the changeset viewer.
