| 1 |
/* pnmtofits.c - read a PNM image and produce a FITS file |
| 2 |
** |
| 3 |
** Copyright (C) 1989 by Wilson H. Bent ([email protected]). |
| 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 |
** Modified by Alberto Accomazzi ([email protected]), Dec 1, 1992. |
| 13 |
** |
| 14 |
** Added PPM input capability; the program is renamed pnmtofits. |
| 15 |
** This program produces files with NAXIS = 2 if input file is in PBM |
| 16 |
** or PGM format, and NAXIS = 3, NAXIS3 = 3 if input file is a PPM file. |
| 17 |
** Data is written out as either 8 bits/pixel or 16 bits/pixel integers, |
| 18 |
** depending on the value of maxval in the input file. |
| 19 |
** Flags -max, -min can be used to set DATAMAX, DATAMIN, BSCALE and BZERO |
| 20 |
** in the FITS header, but do not cause the data to be rescaled. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
The official specification of FITS format (which is for more than |
| 25 |
just visual images) is at |
| 26 |
ftp://legacy.gsfc.nasa.gov/fits_info/fits_office/fits_standard.pdf |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <assert.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdio.h> |
| 32 |
|
| 33 |
#include "pm_c_util.h" |
| 34 |
#include "mallocvar.h" |
| 35 |
#include "shhopt.h" |
| 36 |
#include "nstring.h" |
| 37 |
#include "pam.h" |
| 38 |
|
| 39 |
struct cmdlineInfo { |
| 40 |
const char * inputFileName; |
| 41 |
unsigned int maxSpec; |
| 42 |
float max; |
| 43 |
float min; |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static void |
| 49 |
parseCommandLine(int argc, char ** argv, |
| 50 |
struct cmdlineInfo * const cmdlineP) { |
| 51 |
/*-------------------------------------------------------------------------- |
| 52 |
Parse program command line described in Unix standard form by argc |
| 53 |
and argv. Return the information in the options as *cmdlineP. |
| 54 |
|
| 55 |
If command line is internally inconsistent (invalid options, etc.), |
| 56 |
issue error message to stderr and abort program. |
| 57 |
|
| 58 |
Note that the strings we return are stored in the storage that |
| 59 |
was passed to us as the argv array. We also trash *argv. |
| 60 |
--------------------------------------------------------------------------*/ |
| 61 |
optEntry * option_def; |
| 62 |
/* Instructions to pm_optParseOptions3 on how to parse our options. */ |
| 63 |
optStruct3 opt; |
| 64 |
|
| 65 |
unsigned int minSpec; |
| 66 |
unsigned int option_def_index; |
| 67 |
|
| 68 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 69 |
|
| 70 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 71 |
OPTENT3(0, "min", OPT_FLOAT, |
| 72 |
&cmdlineP->min, &minSpec, 0); |
| 73 |
OPTENT3(0, "max", OPT_FLOAT, |
| 74 |
&cmdlineP->max, &cmdlineP->maxSpec, 0); |
| 75 |
|
| 76 |
opt.opt_table = option_def; |
| 77 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 78 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 79 |
|
| 80 |
/* Set some defaults the lazy way (using multiple setting of variables) */ |
| 81 |
|
| 82 |
pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); |
| 83 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 84 |
|
| 85 |
if (!minSpec) |
| 86 |
cmdlineP->min = 0.0; |
| 87 |
|
| 88 |
if (cmdlineP->maxSpec) { |
| 89 |
if (cmdlineP->max <= cmdlineP->min) |
| 90 |
pm_error("-max must be greater than min (%f). You specified %f", |
| 91 |
cmdlineP->min, cmdlineP->max); |
| 92 |
} |
| 93 |
|
| 94 |
if (argc-1 < 1) |
| 95 |
cmdlineP->inputFileName = "-"; |
| 96 |
else { |
| 97 |
cmdlineP->inputFileName = argv[1]; |
| 98 |
|
| 99 |
if (argc-1 > 1) |
| 100 |
pm_error("Too many arguments (%u). The only non-option argument " |
| 101 |
"is the input file name.", argc-1); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void |
| 108 |
writeHeaderCard(const char * const s) { |
| 109 |
/*---------------------------------------------------------------------------- |
| 110 |
Write the string 's', padded with spaces to 80 characters. |
| 111 |
-----------------------------------------------------------------------------*/ |
| 112 |
const char * card; |
| 113 |
|
| 114 |
pm_asprintf(&card, "%-80.80s", s); |
| 115 |
|
| 116 |
fwrite(card, sizeof(card[0]), 80, stdout); |
| 117 |
|
| 118 |
pm_strfree(card); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static void |
| 124 |
padToMultipleOf36Cards(unsigned int const nCardsAlreadyWritten) { |
| 125 |
|
| 126 |
/* pad with blanks cards to multiple of 36 cards */ |
| 127 |
|
| 128 |
unsigned int const npadCard = 36 - (nCardsAlreadyWritten % 36); |
| 129 |
unsigned int i; |
| 130 |
|
| 131 |
for (i = 0; i < npadCard; ++i) |
| 132 |
writeHeaderCard(""); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void |
| 138 |
writeFitsHeader(int const bitpix, |
| 139 |
int const planes, |
| 140 |
int const cols, |
| 141 |
int const rows, |
| 142 |
double const bscale, |
| 143 |
double const fitsBzero, |
| 144 |
double const datamax, |
| 145 |
double const datamin) { |
| 146 |
|
| 147 |
char buffer[80+1]; |
| 148 |
unsigned int cardsWritten; |
| 149 |
|
| 150 |
cardsWritten = 0; /* initial value */ |
| 151 |
|
| 152 |
sprintf(buffer, "%-20.20s%10.10s", "SIMPLE =", "T"); |
| 153 |
writeHeaderCard(buffer); |
| 154 |
++cardsWritten; |
| 155 |
|
| 156 |
sprintf(buffer, "%-20.20s%10d", "BITPIX =", bitpix); |
| 157 |
writeHeaderCard(buffer); |
| 158 |
++cardsWritten; |
| 159 |
|
| 160 |
sprintf(buffer, "%-20.20s%10d", "NAXIS =", (planes == 3) ? 3 : 2); |
| 161 |
writeHeaderCard(buffer); |
| 162 |
++cardsWritten; |
| 163 |
|
| 164 |
sprintf(buffer, "%-20.20s%10d", "NAXIS1 =", cols); |
| 165 |
writeHeaderCard(buffer); |
| 166 |
++cardsWritten; |
| 167 |
|
| 168 |
sprintf(buffer, "%-20.20s%10d", "NAXIS2 =", rows); |
| 169 |
writeHeaderCard(buffer); |
| 170 |
++cardsWritten; |
| 171 |
|
| 172 |
if (planes == 3) { |
| 173 |
sprintf(buffer, "%-20.20s%10d", "NAXIS3 =", 3); |
| 174 |
writeHeaderCard(buffer); |
| 175 |
++cardsWritten; |
| 176 |
} |
| 177 |
|
| 178 |
sprintf(buffer, "%-18.18s%12.5E", "BSCALE =", bscale); |
| 179 |
writeHeaderCard(buffer); |
| 180 |
++cardsWritten; |
| 181 |
|
| 182 |
sprintf(buffer, "%-18.18s%12.5E", "BZERO =", fitsBzero); |
| 183 |
writeHeaderCard(buffer); |
| 184 |
++cardsWritten; |
| 185 |
|
| 186 |
sprintf(buffer, "%-18.18s%12.5E", "DATAMAX =", datamax); |
| 187 |
writeHeaderCard(buffer); |
| 188 |
++cardsWritten; |
| 189 |
|
| 190 |
sprintf(buffer, "%-18.18s%12.5E", "DATAMIN =", datamin); |
| 191 |
writeHeaderCard(buffer); |
| 192 |
++cardsWritten; |
| 193 |
|
| 194 |
writeHeaderCard("HISTORY Created by pnmtofits."); |
| 195 |
++cardsWritten; |
| 196 |
|
| 197 |
writeHeaderCard("END"); |
| 198 |
++cardsWritten; |
| 199 |
|
| 200 |
padToMultipleOf36Cards(cardsWritten); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static void |
| 206 |
writeRaster(struct pam * const pamP, |
| 207 |
tuple ** const tuples, |
| 208 |
unsigned int const bitpix, |
| 209 |
int const offset) { |
| 210 |
|
| 211 |
/* Note: the FITS specification does not give the association between |
| 212 |
file position and image position (i.e. is the first pixel in the |
| 213 |
file the top left, bottom left, etc.). We use the common sense, |
| 214 |
popular order of row major, top to bottom, left to right. This |
| 215 |
has been the case and accepted since 1989, but in 2008, we discovered |
| 216 |
that Gimp and ImageMagick do bottom to top. |
| 217 |
*/ |
| 218 |
|
| 219 |
unsigned int plane; |
| 220 |
|
| 221 |
for (plane = 0; plane < pamP->depth; ++plane) { |
| 222 |
unsigned int row; |
| 223 |
for (row = 0; row < pamP->height; ++row) { |
| 224 |
unsigned int col; |
| 225 |
for (col = 0; col < pamP->width; ++col) { |
| 226 |
if (bitpix == 16) { |
| 227 |
/* 16 bit FITS samples are signed integers */ |
| 228 |
int const fitsSample = |
| 229 |
(int)tuples[row][col][plane] - offset; |
| 230 |
pm_writebigshort(stdout, (short)fitsSample); |
| 231 |
} else |
| 232 |
/* 8 bit FITS samples are unsigned integers */ |
| 233 |
putchar(tuples[row][col][plane] - offset); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
{ |
| 238 |
/* pad raster to 36 cards with nulls */ |
| 239 |
unsigned int const bytesWritten = |
| 240 |
pamP->height * pamP->width * pamP->depth * bitpix/8; |
| 241 |
unsigned int const npad = (36*80) - (bytesWritten % (36*80)); |
| 242 |
unsigned int i; |
| 243 |
|
| 244 |
for (i = 0; i < npad; ++i) |
| 245 |
putchar('\0'); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
int |
| 252 |
main(int argc, char * argv[]) { |
| 253 |
|
| 254 |
struct cmdlineInfo cmdline; |
| 255 |
FILE * ifP; |
| 256 |
tuple ** tuples; |
| 257 |
struct pam pam; |
| 258 |
unsigned int bitpix; |
| 259 |
double datamin, datamax, bscale, fitsBzero; |
| 260 |
int pnmSampleOffsetFromFits; |
| 261 |
/* This is what you add to a FITS sample (raster) value in order |
| 262 |
to get the PNM sample value which it represents. Note that in |
| 263 |
the default case, that PNM sample value is also the FITS "physical" |
| 264 |
value, but user options can change that. |
| 265 |
*/ |
| 266 |
|
| 267 |
pnm_init(&argc, argv); |
| 268 |
|
| 269 |
parseCommandLine(argc, argv, &cmdline); |
| 270 |
|
| 271 |
ifP = pm_openr(cmdline.inputFileName); |
| 272 |
|
| 273 |
tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 274 |
|
| 275 |
datamin = cmdline.min; |
| 276 |
|
| 277 |
if (cmdline.maxSpec) |
| 278 |
datamax = cmdline.max; |
| 279 |
else { |
| 280 |
if (pam.maxval <= cmdline.min) |
| 281 |
pm_error("You must specify -max greater than -min (%f). " |
| 282 |
"max defaults to the maxval, which is %u", |
| 283 |
cmdline.min, (unsigned)pam.maxval); |
| 284 |
datamax = pam.maxval; |
| 285 |
} |
| 286 |
|
| 287 |
assert(datamax > datamin); |
| 288 |
|
| 289 |
bscale = (datamax - datamin) / pam.maxval; |
| 290 |
|
| 291 |
if (pam.maxval > 255) { |
| 292 |
bitpix = 16; |
| 293 |
/* Because 16 bit FITS samples are signed, we have to do a 2**15 |
| 294 |
offset to get any possible unsigned 16 bit PNM sample into a FITS |
| 295 |
sample. |
| 296 |
*/ |
| 297 |
fitsBzero = 1 << 15; |
| 298 |
pnmSampleOffsetFromFits = 1 << 15; |
| 299 |
} else { |
| 300 |
bitpix = 8; |
| 301 |
fitsBzero = datamin; |
| 302 |
/* Both 8 bit FITS samples and PNM samples are unsigned 8 bits, so |
| 303 |
we make them identical. |
| 304 |
*/ |
| 305 |
pnmSampleOffsetFromFits = 0; |
| 306 |
} |
| 307 |
|
| 308 |
fitsBzero = datamin + pnmSampleOffsetFromFits; |
| 309 |
pm_close(ifP); |
| 310 |
|
| 311 |
writeFitsHeader(bitpix, pam.depth, pam.width, pam.height, |
| 312 |
bscale, fitsBzero, datamax, datamin); |
| 313 |
|
| 314 |
writeRaster(&pam, tuples, bitpix, pnmSampleOffsetFromFits); |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|