| 1 |
/* fitstopnm.c - read a FITS file and produce a PNM. |
| 2 |
** |
| 3 |
** Copyright (C) 1989 by Jef Poskanzer. |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
** |
| 12 |
** Hacked up version by Daniel Briggs ([email protected]) 20-Oct-92 |
| 13 |
** |
| 14 |
** Include floating point formats, more or less. Will only work on |
| 15 |
** machines that understand IEEE-754. Added -scanmax -printmax |
| 16 |
** -min -max and -noraw. Ignore axes past 3, instead of error (many packages |
| 17 |
** use pseudo axes). Use a finite scale when max=min. NB: Min and max |
| 18 |
** are the real world FITS values (scaled), so watch out when bzer & bscale |
| 19 |
** are not 0 & 1. Datamin & datamax interpreted correctly in scaled case, |
| 20 |
** and initialization changed to less likely values. If datamin & max are |
| 21 |
** not present in the header, the a first pass is made to determine them |
| 22 |
** from the array values. |
| 23 |
** |
| 24 |
** Modified by Alberto Accomazzi ([email protected]), Dec 1, 1992. |
| 25 |
** |
| 26 |
** Added understanding of 3-plane FITS files, the program is renamed |
| 27 |
** fitstopnm. Fixed some non-ansi declarations (DBL_MAX and FLT_MAX |
| 28 |
** replace MAXDOUBLE and MAXFLOAT), fixed some scaling parameters to |
| 29 |
** map the full FITS data resolution to the maximum PNM resolution, |
| 30 |
** disabled min max scanning when reading from stdin. |
| 31 |
*/ |
| 32 |
|
| 33 |
/* |
| 34 |
The official specification of FITS format (which is for more than |
| 35 |
just visual images) is at |
| 36 |
ftp://legacy.gsfc.nasa.gov/fits_info/fits_office/fits_standard.pdf |
| 37 |
|
| 38 |
An example FITS file is at |
| 39 |
|
| 40 |
http://fits.gsfc.nasa.gov/nrao_data/tests/incunabula/mndrll-8.fits |
| 41 |
|
| 42 |
*/ |
| 43 |
|
| 44 |
#include <string.h> |
| 45 |
#include <float.h> |
| 46 |
#include <assert.h> |
| 47 |
#include <stdbool.h> |
| 48 |
|
| 49 |
#include "pm_config.h" |
| 50 |
#include "pm_c_util.h" |
| 51 |
#include "mallocvar.h" |
| 52 |
#include "floatcode.h" |
| 53 |
#include "shhopt.h" |
| 54 |
#include "pnm.h" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct CmdlineInfo { |
| 59 |
const char * inputFileName; |
| 60 |
unsigned int image; /* zero if unspecified */ |
| 61 |
float max; |
| 62 |
unsigned int maxSpec; |
| 63 |
float min; |
| 64 |
unsigned int minSpec; |
| 65 |
unsigned int scanmax; |
| 66 |
unsigned int printmax; |
| 67 |
unsigned int noraw; |
| 68 |
/* This is for backward compatibility only. Use the common option |
| 69 |
-plain now. (pnm_init() processes -plain). |
| 70 |
*/ |
| 71 |
unsigned int verbose; |
| 72 |
unsigned int omaxval; |
| 73 |
unsigned int omaxvalSpec; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static void |
| 79 |
parseCommandLine(int argc, const char ** argv, |
| 80 |
struct CmdlineInfo * const cmdlineP) { |
| 81 |
/* -------------------------------------------------------------------------- |
| 82 |
Parse program command line described in Unix standard form by argc |
| 83 |
and argv. Return the information in the options as *cmdlineP. |
| 84 |
|
| 85 |
If command line is internally inconsistent (invalid options, etc.), |
| 86 |
issue error message to stderr and abort program. |
| 87 |
|
| 88 |
Note that the strings we return are stored in the storage that |
| 89 |
was passed to us as the argv array. We also trash *argv. |
| 90 |
--------------------------------------------------------------------------*/ |
| 91 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 92 |
optStruct3 opt; |
| 93 |
|
| 94 |
unsigned int imageSpec; |
| 95 |
unsigned int option_def_index; |
| 96 |
|
| 97 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 98 |
|
| 99 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 100 |
OPTENT3(0, "image", OPT_UINT, |
| 101 |
&cmdlineP->image, &imageSpec, 0); |
| 102 |
OPTENT3(0, "min", OPT_FLOAT, |
| 103 |
&cmdlineP->min, &cmdlineP->minSpec, 0); |
| 104 |
OPTENT3(0, "max", OPT_FLOAT, |
| 105 |
&cmdlineP->max, &cmdlineP->maxSpec, 0); |
| 106 |
OPTENT3(0, "scanmax", OPT_FLAG, |
| 107 |
NULL, &cmdlineP->scanmax, 0); |
| 108 |
OPTENT3(0, "printmax", OPT_FLAG, |
| 109 |
NULL, &cmdlineP->printmax, 0); |
| 110 |
OPTENT3(0, "noraw", OPT_FLAG, |
| 111 |
NULL, &cmdlineP->noraw, 0); |
| 112 |
OPTENT3(0, "verbose", OPT_FLAG, |
| 113 |
NULL, &cmdlineP->verbose, 0); |
| 114 |
OPTENT3(0, "omaxval", OPT_UINT, |
| 115 |
&cmdlineP->omaxval, &cmdlineP->omaxvalSpec, 0); |
| 116 |
|
| 117 |
opt.opt_table = option_def; |
| 118 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 119 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 120 |
|
| 121 |
/* Set some defaults the lazy way (using multiple setting of variables) */ |
| 122 |
|
| 123 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 124 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 125 |
|
| 126 |
if (imageSpec) { |
| 127 |
if (cmdlineP->image == 0) |
| 128 |
pm_error("You may not specify zero for the image number. " |
| 129 |
"Images are numbered starting at 1."); |
| 130 |
} else |
| 131 |
cmdlineP->image = 0; |
| 132 |
|
| 133 |
if (cmdlineP->maxSpec && cmdlineP->minSpec) { |
| 134 |
if (cmdlineP->max <= cmdlineP->min) |
| 135 |
pm_error("-max must be greater than -min. You specified " |
| 136 |
"-max=%f, -min=%f", cmdlineP->max, cmdlineP->min); |
| 137 |
} |
| 138 |
|
| 139 |
if (argc-1 < 1) |
| 140 |
cmdlineP->inputFileName = "-"; |
| 141 |
else { |
| 142 |
cmdlineP->inputFileName = argv[1]; |
| 143 |
|
| 144 |
if (argc-1 > 1) |
| 145 |
pm_error("Too many arguments (%u). The only non-option argument " |
| 146 |
"is the input file name.", argc-1); |
| 147 |
} |
| 148 |
free(option_def); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
typedef enum { |
| 154 |
VF_CHAR, VF_SHORT, VF_LONG, VF_FLOAT, VF_DOUBLE |
| 155 |
} ValFmt; |
| 156 |
|
| 157 |
struct FITS_Header { |
| 158 |
bool simple; /* basic format */ |
| 159 |
ValFmt valFmt; /* format of values -- bits per pixel, integer/float */ |
| 160 |
unsigned int naxis; /* number of axes */ |
| 161 |
unsigned int naxis1; /* number of points on axis 1 */ |
| 162 |
unsigned int naxis2; /* number of points on axis 2 */ |
| 163 |
unsigned int naxis3; /* number of points on axis 3 */ |
| 164 |
double datamin; /* min # (Physical value!) */ |
| 165 |
double datamax; /* max # " " */ |
| 166 |
double bzer; /* Physical value = Array value*bscale + bzero */ |
| 167 |
double bscale; |
| 168 |
}; |
| 169 |
|
| 170 |
struct FitsRasterInfo { |
| 171 |
ValFmt valFmt; |
| 172 |
double bzer; |
| 173 |
double bscale; |
| 174 |
}; |
| 175 |
|
| 176 |
/* This code deals properly with integers, no matter what the byte order |
| 177 |
or integer size of the host machine. We handle sign extension manually to |
| 178 |
prevent problems with signed/unsigned characters. We read floating point |
| 179 |
values properly only when the host architecture conforms to IEEE-754. If |
| 180 |
you need to tweak this code for other machines, you might want to get a |
| 181 |
copy of the FITS documentation from nssdca.gsfc.nasa.gov |
| 182 |
*/ |
| 183 |
|
| 184 |
static void |
| 185 |
readFitsChar(FILE * const ifP, |
| 186 |
double * const vP) { |
| 187 |
|
| 188 |
/* 8 bit FITS integers are unsigned */ |
| 189 |
|
| 190 |
int const ich = getc(ifP); |
| 191 |
|
| 192 |
if (ich == EOF) |
| 193 |
pm_error("EOF / read error"); |
| 194 |
else |
| 195 |
*vP = ich; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static void |
| 201 |
readFitsShort(FILE * const ifP, |
| 202 |
double * const vP) { |
| 203 |
|
| 204 |
int ich; |
| 205 |
int ival; |
| 206 |
unsigned char c[8]; |
| 207 |
|
| 208 |
ich = getc(ifP); |
| 209 |
|
| 210 |
if (ich == EOF) |
| 211 |
pm_error("EOF / read error"); |
| 212 |
|
| 213 |
c[0] = ich; |
| 214 |
|
| 215 |
ich = getc(ifP); |
| 216 |
|
| 217 |
if (ich == EOF) |
| 218 |
pm_error("EOF / read error"); |
| 219 |
|
| 220 |
c[1] = ich; |
| 221 |
|
| 222 |
if (c[0] & 0x80) |
| 223 |
ival = ~0xFFFF | c[0] << 8 | c[1]; |
| 224 |
else |
| 225 |
ival = c[0] << 8 | c[1]; |
| 226 |
|
| 227 |
*vP = ival; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
static void |
| 233 |
readFitsLong(FILE * const ifP, |
| 234 |
double * const vP) { |
| 235 |
|
| 236 |
unsigned int i; |
| 237 |
long int lval; |
| 238 |
unsigned char c[4]; |
| 239 |
|
| 240 |
for (i = 0; i < 4; ++i) { |
| 241 |
int const ich = getc(ifP); |
| 242 |
if (ich == EOF) |
| 243 |
pm_error("EOF / read error"); |
| 244 |
c[i] = ich; |
| 245 |
} |
| 246 |
|
| 247 |
if (c[0] & 0x80) |
| 248 |
lval = ~0xFFFFFFFF | c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3]; |
| 249 |
else |
| 250 |
lval = c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3] << 0; |
| 251 |
|
| 252 |
*vP = lval; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
static void |
| 258 |
readFitsFloat(FILE * const ifP, |
| 259 |
double * const vP) { |
| 260 |
|
| 261 |
unsigned int i; |
| 262 |
pm_bigendFloat bigend; |
| 263 |
|
| 264 |
for (i = 0; i < 4; ++i) { |
| 265 |
int const ich = getc(ifP); |
| 266 |
if (ich == EOF) |
| 267 |
pm_error("EOF / read error"); |
| 268 |
bigend.bytes[i] = ich; |
| 269 |
} |
| 270 |
|
| 271 |
*vP = pm_floatFromBigendFloat(bigend); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static void |
| 277 |
readFitsDouble(FILE * const ifP, |
| 278 |
double * const vP) { |
| 279 |
|
| 280 |
unsigned int i; |
| 281 |
pm_bigendDouble bigend; |
| 282 |
|
| 283 |
for (i = 0; i < 8; ++i) { |
| 284 |
int const ich = getc(ifP); |
| 285 |
if (ich == EOF) |
| 286 |
pm_error("EOF / read error"); |
| 287 |
bigend.bytes[i] = ich; |
| 288 |
} |
| 289 |
|
| 290 |
*vP = pm_doubleFromBigendDouble(bigend); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static ValFmt |
| 296 |
valFmtFromBitpix(int const bitpix) { |
| 297 |
/*---------------------------------------------------------------------------- |
| 298 |
Return the format of a "value" in the FITS file, given the value |
| 299 |
of the BITPIX header in the FITS file. |
| 300 |
|
| 301 |
BITPIX has a stupid format wherein it is fundamentally the number |
| 302 |
of bits per value, but its sign indicates whether it is integer |
| 303 |
or floating point. |
| 304 |
-----------------------------------------------------------------------------*/ |
| 305 |
switch (bitpix) { |
| 306 |
case +8: return VF_CHAR; |
| 307 |
case +16: return VF_SHORT; |
| 308 |
case +32: return VF_LONG; |
| 309 |
case -32: return VF_FLOAT; |
| 310 |
case -64: return VF_DOUBLE; |
| 311 |
default: |
| 312 |
/* Every possibility is covered above. */ |
| 313 |
assert(false); |
| 314 |
return 0; /* quiet compiler warning */ |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
static void |
| 321 |
readVal(FILE * const ifP, |
| 322 |
ValFmt const fmt, |
| 323 |
double * const vP) { |
| 324 |
|
| 325 |
switch (fmt) { |
| 326 |
case VF_CHAR: |
| 327 |
readFitsChar(ifP, vP); |
| 328 |
break; |
| 329 |
|
| 330 |
case VF_SHORT: |
| 331 |
readFitsShort(ifP, vP); |
| 332 |
break; |
| 333 |
|
| 334 |
case VF_LONG: |
| 335 |
readFitsLong(ifP, vP); |
| 336 |
break; |
| 337 |
|
| 338 |
case VF_FLOAT: |
| 339 |
readFitsFloat(ifP, vP); |
| 340 |
break; |
| 341 |
|
| 342 |
case VF_DOUBLE: |
| 343 |
readFitsDouble(ifP, vP); |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static void |
| 351 |
readCard(FILE * const ifP, |
| 352 |
char * const buf) { |
| 353 |
|
| 354 |
size_t bytesRead; |
| 355 |
|
| 356 |
bytesRead = fread(buf, 1, 80, ifP); |
| 357 |
if (bytesRead < 80) |
| 358 |
pm_error("error reading header"); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
static void |
| 364 |
processNaxisN(unsigned int const n, |
| 365 |
int const value, |
| 366 |
bool const gotNaxis, |
| 367 |
unsigned int const naxis, |
| 368 |
bool * const gotNaxisNP, |
| 369 |
unsigned int * const naxisNP) { |
| 370 |
|
| 371 |
if (*gotNaxisNP) |
| 372 |
pm_error("Invalid FITS header: two NAXIS%u keywords", n); |
| 373 |
else { |
| 374 |
*gotNaxisNP = true; |
| 375 |
|
| 376 |
if (!gotNaxis) |
| 377 |
pm_error("Invalid FITS header: NAXIS must precede NAXIS%u", n); |
| 378 |
else if (naxis < n) |
| 379 |
pm_error("Invalid FITS header: NAXIS%u for image with " |
| 380 |
"only %u axes", n, naxis); |
| 381 |
else if (value < 0) |
| 382 |
pm_error("Invalid NAXIS%u value %d in FITS header: " |
| 383 |
"Must not be negative", n, value); |
| 384 |
else |
| 385 |
*naxisNP = value; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
static void |
| 392 |
readFitsHeader(FILE * const ifP, |
| 393 |
struct FITS_Header * const hP) { |
| 394 |
|
| 395 |
|
| 396 |
bool gotEmpty, gotSimple, gotNaxis, gotN1, gotN2, gotN3, gotBitpix, gotEnd; |
| 397 |
|
| 398 |
gotEmpty = false; /* initial value */ |
| 399 |
gotSimple = false; /* initial value */ |
| 400 |
gotNaxis = false; /* initial value */ |
| 401 |
gotN1 = false; /* initial value */ |
| 402 |
gotN2 = false; /* initial value */ |
| 403 |
gotN3 = false; /* initial value */ |
| 404 |
gotBitpix = false; /* initial value */ |
| 405 |
gotEnd = false; /* initial value */ |
| 406 |
|
| 407 |
/* Set defaults */ |
| 408 |
hP->bzer = 0.0; |
| 409 |
hP->bscale = 1.0; |
| 410 |
hP->datamin = - DBL_MAX; |
| 411 |
hP->datamax = DBL_MAX; |
| 412 |
|
| 413 |
while (!gotEnd) { |
| 414 |
unsigned int i; |
| 415 |
|
| 416 |
for (i = 0; i < 36; ++i) { |
| 417 |
char buf[81]; |
| 418 |
char c; |
| 419 |
int n; |
| 420 |
|
| 421 |
readCard(ifP, buf); /* Reads into first 80 elements of buf[] */ |
| 422 |
|
| 423 |
buf[80] = '\0'; /* Make ASCIIZ string */ |
| 424 |
|
| 425 |
if (sscanf(buf, " %c", &c) < 1) { |
| 426 |
gotEmpty = true; |
| 427 |
} else if (sscanf(buf, "SIMPLE = %c", &c) == 1) { |
| 428 |
if (gotSimple) |
| 429 |
pm_error("FITS header has two SIMPLE keywords"); |
| 430 |
gotSimple = true; |
| 431 |
if (c == 'T' || c == 't') |
| 432 |
hP->simple = true; |
| 433 |
else if (c == 'F' || c == 'f') |
| 434 |
hP->simple = false; |
| 435 |
else |
| 436 |
pm_error("Invalid SIMPLE value '%c'. Only 'T' and 'F' " |
| 437 |
"are recognized", c); |
| 438 |
} else if (sscanf(buf, "BITPIX = %d", &n) == 1) { |
| 439 |
if (gotBitpix) |
| 440 |
pm_error("FITS header has two NAXIS keywords"); |
| 441 |
gotBitpix = true; |
| 442 |
hP->valFmt = valFmtFromBitpix(n); |
| 443 |
} else if (sscanf(buf, "NAXIS = %d", &n) == 1) { |
| 444 |
gotNaxis = true; |
| 445 |
if (n < 0) |
| 446 |
pm_error("Invalid value %d for NAXIS in FITS header. " |
| 447 |
"Value must not be negative", n); |
| 448 |
else |
| 449 |
hP->naxis = n; |
| 450 |
} else if (sscanf(buf, "NAXIS1 = %d", &n) == 1) { |
| 451 |
processNaxisN(1, n, gotNaxis, hP->naxis, &gotN1, &hP->naxis1); |
| 452 |
} else if (sscanf(buf, "NAXIS2 = %d", &n) == 1) { |
| 453 |
processNaxisN(2, n, gotNaxis, hP->naxis, &gotN2, &hP->naxis2); |
| 454 |
} else if (sscanf(buf, "NAXIS3 = %d", &n) == 1) { |
| 455 |
processNaxisN(3, n, gotNaxis, hP->naxis, &gotN3, &hP->naxis3); |
| 456 |
} else if (sscanf(buf, "DATAMIN = %lf", &(hP->datamin)) == 1) { |
| 457 |
} else if (sscanf(buf, "DATAMAX = %lf", &(hP->datamax)) == 1) { |
| 458 |
} else if (sscanf(buf, "BZERO = %lf", &(hP->bzer)) == 1) { |
| 459 |
} else if (sscanf(buf, "BSCALE = %lf", &(hP->bscale)) == 1) { |
| 460 |
} else if (strncmp(buf, "END ", 4 ) == 0) { |
| 461 |
gotEnd = true; |
| 462 |
if (gotEmpty == true) |
| 463 |
pm_message("Blank card(s) were encountered before " |
| 464 |
"END in header"); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
if (!gotSimple) |
| 469 |
pm_error("FITS header missing the SIMPLE keyword"); |
| 470 |
if (!gotBitpix) |
| 471 |
pm_error("FITS header missing the BITPIX keyword"); |
| 472 |
if (!gotNaxis) |
| 473 |
pm_error("FITS header missing the NAXIS keyword"); |
| 474 |
|
| 475 |
if (hP->naxis > 3) |
| 476 |
pm_error("FITS file has %u axes; this program can handle " |
| 477 |
"no more than 3", hP->naxis); |
| 478 |
|
| 479 |
if (hP->naxis > 0 && !gotN1) |
| 480 |
pm_error("FITS header missing NAXIS1 keyword"); |
| 481 |
if (hP->naxis > 1 && !gotN2) |
| 482 |
pm_error("FITS header missing NAXIS1 keyword"); |
| 483 |
if (hP->naxis > 2 && !gotN3) |
| 484 |
pm_error("FITS header missing NAXIS3 keyword"); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
static void |
| 490 |
interpretPlanes(struct FITS_Header const fitsHeader, |
| 491 |
unsigned int const imageRequest, |
| 492 |
bool const verbose, |
| 493 |
unsigned int * const imageCountP, |
| 494 |
bool * const multiplaneP, |
| 495 |
unsigned int * const desiredImageP) { |
| 496 |
|
| 497 |
if (fitsHeader.naxis == 2) { |
| 498 |
*imageCountP = 1; |
| 499 |
*multiplaneP = FALSE; |
| 500 |
*desiredImageP = 1; |
| 501 |
} else { |
| 502 |
if (imageRequest) { |
| 503 |
if (imageRequest > fitsHeader.naxis3) |
| 504 |
pm_error("Only %u plane%s in this file. " |
| 505 |
"You requested image %u", |
| 506 |
fitsHeader.naxis3, fitsHeader.naxis3 > 1 ? "s" : "", |
| 507 |
imageRequest); |
| 508 |
else { |
| 509 |
*imageCountP = fitsHeader.naxis3; |
| 510 |
*multiplaneP = FALSE; |
| 511 |
*desiredImageP = imageRequest; |
| 512 |
} |
| 513 |
} else { |
| 514 |
if (fitsHeader.naxis3 == 3) { |
| 515 |
*imageCountP = 1; |
| 516 |
*multiplaneP = TRUE; |
| 517 |
*desiredImageP = 1; |
| 518 |
} else if (fitsHeader.naxis3 > 1) |
| 519 |
pm_error("This FITS file contains multiple (%u) images. " |
| 520 |
"You must specify which one you want with a " |
| 521 |
"-image option.", fitsHeader.naxis3); |
| 522 |
else { |
| 523 |
*imageCountP = fitsHeader.naxis3; |
| 524 |
*multiplaneP = FALSE; |
| 525 |
*desiredImageP = 1; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
if (verbose) { |
| 530 |
|
| 531 |
pm_message("FITS stream is %smultiplane", *multiplaneP ? "" : "not "); |
| 532 |
pm_message("We will take image %u (1 is first) of %u " |
| 533 |
"in the FITS stream", |
| 534 |
*desiredImageP, *imageCountP); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
static void |
| 541 |
scanImageForMinMax(FILE * const ifP, |
| 542 |
unsigned int const images, |
| 543 |
int const cols, |
| 544 |
int const rows, |
| 545 |
ValFmt const valFmt, |
| 546 |
double const bscale, |
| 547 |
double const bzer, |
| 548 |
unsigned int const imagenum, |
| 549 |
bool const multiplane, |
| 550 |
double * const dataminP, |
| 551 |
double * const datamaxP) { |
| 552 |
|
| 553 |
/* Note that a value in the file might be Not-A-Number. We ignore |
| 554 |
such entries in computing the minimum and maximum for the image. |
| 555 |
*/ |
| 556 |
double dmax, dmin; |
| 557 |
unsigned int image; |
| 558 |
pm_filepos rasterPos; |
| 559 |
double fmaxval; |
| 560 |
|
| 561 |
pm_tell2(ifP, &rasterPos, sizeof(rasterPos)); |
| 562 |
|
| 563 |
pm_message("Scanning file for scaling parameters"); |
| 564 |
|
| 565 |
switch (valFmt) { |
| 566 |
case VF_CHAR: fmaxval = 255.0; break; |
| 567 |
case VF_SHORT: fmaxval = 65535.0; break; |
| 568 |
case VF_LONG: fmaxval = 4294967295.0; break; |
| 569 |
case VF_FLOAT: fmaxval = FLT_MAX; break; |
| 570 |
case VF_DOUBLE: fmaxval = DBL_MAX; break; |
| 571 |
} |
| 572 |
|
| 573 |
dmax = -fmaxval; |
| 574 |
dmin = fmaxval; |
| 575 |
for (image = 1; image <= images; ++image) { |
| 576 |
unsigned int row; |
| 577 |
for (row = 0; row < rows; ++row) { |
| 578 |
unsigned int col; |
| 579 |
for (col = 0; col < cols; ++col) { |
| 580 |
double val; |
| 581 |
readVal(ifP, valFmt, &val); |
| 582 |
if (image == imagenum || multiplane ) { |
| 583 |
/* Note: if 'val' is NaN, result is 2nd operand */ |
| 584 |
dmax = MAX(val, dmax); |
| 585 |
dmin = MIN(val, dmin); |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
if (bscale < 0.0) { |
| 590 |
double const origDmax = dmax; |
| 591 |
dmax = dmin; |
| 592 |
dmin = origDmax; |
| 593 |
} |
| 594 |
} |
| 595 |
*dataminP = dmin * bscale + bzer; |
| 596 |
*datamaxP = dmax * bscale + bzer; |
| 597 |
|
| 598 |
pm_message("Scan results: min=%f max=%f", *dataminP, *datamaxP); |
| 599 |
|
| 600 |
pm_seek2(ifP, &rasterPos, sizeof(rasterPos)); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static void |
| 606 |
computeMinMax(FILE * const ifP, |
| 607 |
unsigned int const images, |
| 608 |
int const cols, |
| 609 |
int const rows, |
| 610 |
struct FITS_Header const h, |
| 611 |
unsigned int const imagenum, |
| 612 |
bool const multiplane, |
| 613 |
bool const forcemin, |
| 614 |
bool const forcemax, |
| 615 |
double const frmin, |
| 616 |
double const frmax, |
| 617 |
double * const dataminP, |
| 618 |
double * const datamaxP) { |
| 619 |
|
| 620 |
double datamin, datamax; |
| 621 |
|
| 622 |
datamin = -DBL_MAX; /* initial assumption */ |
| 623 |
datamax = DBL_MAX; /* initial assumption */ |
| 624 |
|
| 625 |
if (forcemin) |
| 626 |
datamin = frmin; |
| 627 |
if (forcemax) |
| 628 |
datamax = frmax; |
| 629 |
|
| 630 |
if (datamin == -DBL_MAX) |
| 631 |
datamin = h.datamin; |
| 632 |
if (datamax == DBL_MAX) |
| 633 |
datamax = h.datamax; |
| 634 |
|
| 635 |
if (datamin == -DBL_MAX || datamax == DBL_MAX) { |
| 636 |
double scannedDatamin, scannedDatamax; |
| 637 |
scanImageForMinMax(ifP, images, cols, rows, |
| 638 |
h.valFmt, h.bscale, h.bzer, |
| 639 |
imagenum, multiplane, |
| 640 |
&scannedDatamin, &scannedDatamax); |
| 641 |
|
| 642 |
if (datamin == -DBL_MAX) |
| 643 |
datamin = scannedDatamin; |
| 644 |
if (datamax == DBL_MAX) |
| 645 |
datamax = scannedDatamax; |
| 646 |
} |
| 647 |
*dataminP = datamin; |
| 648 |
*datamaxP = datamax; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
static xelval |
| 654 |
determineMaxval(struct CmdlineInfo const cmdline, |
| 655 |
ValFmt const valFmt, |
| 656 |
double const datamax, |
| 657 |
double const datamin) { |
| 658 |
|
| 659 |
xelval retval; |
| 660 |
|
| 661 |
if (cmdline.omaxvalSpec) |
| 662 |
retval = cmdline.omaxval; |
| 663 |
else { |
| 664 |
if (valFmt == VF_FLOAT || valFmt == VF_DOUBLE) { |
| 665 |
/* samples are floating point, which means the resolution |
| 666 |
could be anything. So we just pick a convenient maxval |
| 667 |
of 255. Before Netpbm 10.20 (January 2004), we did |
| 668 |
maxval = max - min for floating point as well as |
| 669 |
integer samples. |
| 670 |
*/ |
| 671 |
retval = 255; |
| 672 |
if (cmdline.verbose) |
| 673 |
pm_message("FITS image has floating point samples. " |
| 674 |
"Using maxval = %u.", (unsigned int)retval); |
| 675 |
} else { |
| 676 |
retval = MAX(1, MIN(PNM_OVERALLMAXVAL, datamax - datamin)); |
| 677 |
if (cmdline.verbose) |
| 678 |
pm_message("FITS image has samples in the range %d-%d. " |
| 679 |
"Using maxval %u.", |
| 680 |
(int)(datamin+0.5), (int)(datamax+0.5), |
| 681 |
(unsigned int)retval); |
| 682 |
} |
| 683 |
} |
| 684 |
return retval; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
static void |
| 690 |
convertPgmRaster(FILE * const ifP, |
| 691 |
unsigned int const cols, |
| 692 |
unsigned int const rows, |
| 693 |
xelval const maxval, |
| 694 |
unsigned int const desiredImage, |
| 695 |
unsigned int const imageCount, |
| 696 |
struct FitsRasterInfo const rasterInfo, |
| 697 |
double const scale, |
| 698 |
double const datamin, |
| 699 |
xel ** const xels) { |
| 700 |
|
| 701 |
/* Note: the FITS specification does not give the association between |
| 702 |
file position and image position (i.e. is the first pixel in the |
| 703 |
file the top left, bottom left, etc.). We use the common sense, |
| 704 |
popular order of row major, top to bottom, left to right. This |
| 705 |
has been the case and accepted since 1989, but in 2008, we discovered |
| 706 |
that Gimp and ImageMagick do bottom to top. |
| 707 |
*/ |
| 708 |
unsigned int image; |
| 709 |
|
| 710 |
pm_message("writing PGM file"); |
| 711 |
|
| 712 |
for (image = 1; image <= desiredImage; ++image) { |
| 713 |
unsigned int row; |
| 714 |
if (image != desiredImage) |
| 715 |
pm_message("skipping image plane %u of %u", image, imageCount); |
| 716 |
else if (imageCount > 1) |
| 717 |
pm_message("reading image plane %u of %u", image, imageCount); |
| 718 |
for (row = 0; row < rows; ++row) { |
| 719 |
unsigned int col; |
| 720 |
for (col = 0; col < cols; ++col) { |
| 721 |
double val; |
| 722 |
readVal(ifP, rasterInfo.valFmt, &val); |
| 723 |
{ |
| 724 |
double const t = scale * |
| 725 |
(val * rasterInfo.bscale + rasterInfo.bzer - datamin); |
| 726 |
xelval const tx = MAX(0, MIN(t, maxval)); |
| 727 |
if (image == desiredImage) |
| 728 |
PNM_ASSIGN1(xels[row][col], tx); |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
static void |
| 738 |
convertPpmRaster(FILE * const ifP, |
| 739 |
unsigned int const cols, |
| 740 |
unsigned int const rows, |
| 741 |
xelval const maxval, |
| 742 |
struct FitsRasterInfo const rasterInfo, |
| 743 |
double const scale, |
| 744 |
double const datamin, |
| 745 |
xel ** const xels) { |
| 746 |
/*---------------------------------------------------------------------------- |
| 747 |
Read the FITS raster from file *ifP into xels[][]. Image dimensions |
| 748 |
are 'cols' by 'rows'. The FITS raster is 3 planes composing one |
| 749 |
image: a red plane followed by a green plane followed by a blue plane. |
| 750 |
-----------------------------------------------------------------------------*/ |
| 751 |
unsigned int plane; |
| 752 |
|
| 753 |
pm_message("Writing PPM file " |
| 754 |
"(Probably not what you want - consider an -image option)"); |
| 755 |
|
| 756 |
for (plane = 0; plane < 3; ++plane) { |
| 757 |
unsigned int row; |
| 758 |
pm_message("reading image plane %u (%s)", |
| 759 |
plane, plane == 0 ? "red" : plane == 1 ? "green" : "blue"); |
| 760 |
for (row = 0; row < rows; ++row) { |
| 761 |
unsigned int col; |
| 762 |
for (col = 0; col < cols; ++col) { |
| 763 |
double val; |
| 764 |
readVal(ifP, rasterInfo.valFmt, &val); |
| 765 |
{ |
| 766 |
double const t = scale * |
| 767 |
(val * rasterInfo.bscale + rasterInfo.bzer - datamin); |
| 768 |
xelval const sample = MAX(0, MIN(t, maxval)); |
| 769 |
|
| 770 |
switch (plane) { |
| 771 |
case 0: PPM_PUTR(xels[row][col], sample); break; |
| 772 |
case 1: PPM_PUTG(xels[row][col], sample); break; |
| 773 |
case 2: PPM_PUTB(xels[row][col], sample); break; |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
static void |
| 784 |
convertRaster(FILE * const ifP, |
| 785 |
unsigned int const cols, |
| 786 |
unsigned int const rows, |
| 787 |
xelval const maxval, |
| 788 |
bool const forceplain, |
| 789 |
bool const multiplane, |
| 790 |
unsigned int const desiredImage, |
| 791 |
unsigned int const imageCount, |
| 792 |
struct FitsRasterInfo const rasterInfo, |
| 793 |
double const scale, |
| 794 |
double const datamin) { |
| 795 |
|
| 796 |
xel ** xels; |
| 797 |
int format; |
| 798 |
|
| 799 |
xels = pnm_allocarray(cols, rows); |
| 800 |
|
| 801 |
if (multiplane) { |
| 802 |
format = PPM_FORMAT; |
| 803 |
convertPpmRaster(ifP, cols, rows, maxval, rasterInfo, scale, datamin, |
| 804 |
xels); |
| 805 |
} else { |
| 806 |
format = PGM_FORMAT; |
| 807 |
convertPgmRaster(ifP, cols, rows, maxval, |
| 808 |
desiredImage, imageCount, rasterInfo, scale, datamin, |
| 809 |
xels); |
| 810 |
} |
| 811 |
pnm_writepnm(stdout, xels, cols, rows, maxval, format, forceplain); |
| 812 |
pnm_freearray(xels, rows); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
int |
| 818 |
main(int argc, const char * argv[]) { |
| 819 |
|
| 820 |
struct CmdlineInfo cmdline; |
| 821 |
FILE * ifP; |
| 822 |
unsigned int cols, rows; |
| 823 |
xelval maxval; |
| 824 |
double scale; |
| 825 |
double datamin, datamax; |
| 826 |
struct FITS_Header fitsHeader; |
| 827 |
struct FitsRasterInfo rasterInfo; |
| 828 |
|
| 829 |
unsigned int imageCount; |
| 830 |
unsigned int desiredImage; |
| 831 |
/* Plane number (starting at one) of plane that contains the image |
| 832 |
we want. |
| 833 |
*/ |
| 834 |
bool multiplane; |
| 835 |
/* This is a one-image multiplane stream; 'desiredImage' |
| 836 |
is undefined |
| 837 |
*/ |
| 838 |
|
| 839 |
pm_proginit(&argc, argv); |
| 840 |
|
| 841 |
parseCommandLine(argc, argv, &cmdline); |
| 842 |
|
| 843 |
ifP = pm_openr(cmdline.inputFileName); |
| 844 |
|
| 845 |
readFitsHeader(ifP, &fitsHeader); |
| 846 |
|
| 847 |
if (!fitsHeader.simple) |
| 848 |
pm_error("FITS file is not in simple format, can't read"); |
| 849 |
|
| 850 |
if (fitsHeader.naxis != 2 && fitsHeader.naxis != 3) |
| 851 |
pm_message("Warning: FITS file has %u axes", fitsHeader.naxis); |
| 852 |
|
| 853 |
cols = fitsHeader.naxis1; |
| 854 |
rows = fitsHeader.naxis2; |
| 855 |
|
| 856 |
rasterInfo.bscale = fitsHeader.bscale; |
| 857 |
rasterInfo.bzer = fitsHeader.bzer; |
| 858 |
rasterInfo.valFmt = fitsHeader.valFmt; |
| 859 |
|
| 860 |
interpretPlanes(fitsHeader, cmdline.image, cmdline.verbose, |
| 861 |
&imageCount, &multiplane, &desiredImage); |
| 862 |
|
| 863 |
computeMinMax(ifP, imageCount, cols, rows, fitsHeader, |
| 864 |
desiredImage, multiplane, |
| 865 |
cmdline.minSpec, cmdline.maxSpec, |
| 866 |
cmdline.min, cmdline.max, |
| 867 |
&datamin, &datamax); |
| 868 |
|
| 869 |
maxval = determineMaxval(cmdline, rasterInfo.valFmt, datamax, datamin); |
| 870 |
|
| 871 |
if (datamax - datamin == 0) |
| 872 |
scale = 1.0; |
| 873 |
else |
| 874 |
scale = maxval / (datamax - datamin); |
| 875 |
|
| 876 |
if (cmdline.printmax) |
| 877 |
printf("%f %f\n", datamin, datamax); |
| 878 |
else |
| 879 |
convertRaster(ifP, cols, rows, maxval, cmdline.noraw, |
| 880 |
multiplane, desiredImage, imageCount, |
| 881 |
rasterInfo, scale, datamin); |
| 882 |
|
| 883 |
pm_close(ifP); |
| 884 |
pm_close(stdout); |
| 885 |
|
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|