| 1 |
/***************************************************************************** |
| 2 |
pfmtopam |
| 3 |
****************************************************************************** |
| 4 |
This program converts a PFM (Portable Float Map) image to PAM. |
| 5 |
|
| 6 |
By Bryan Henderson, San Jose, CA April 2004. |
| 7 |
|
| 8 |
Contributed to the public domain by its author. |
| 9 |
|
| 10 |
*****************************************************************************/ |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <math.h> |
| 15 |
#include <string.h> |
| 16 |
#include <assert.h> |
| 17 |
|
| 18 |
#include "pm_c_util.h" |
| 19 |
#include "pam.h" |
| 20 |
#include "pm_gamma.h" |
| 21 |
#include "shhopt.h" |
| 22 |
#include "mallocvar.h" |
| 23 |
|
| 24 |
|
| 25 |
struct cmdlineInfo { |
| 26 |
const char * inputFilespec; |
| 27 |
unsigned int verbose; |
| 28 |
sample maxval; |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static void |
| 34 |
parseCommandLine(int argc, |
| 35 |
char ** argv, |
| 36 |
struct cmdlineInfo * const cmdlineP) { |
| 37 |
/* -------------------------------------------------------------------------- |
| 38 |
Parse program command line described in Unix standard form by argc |
| 39 |
and argv. Return the information in the options as *cmdlineP. |
| 40 |
|
| 41 |
If command line is internally inconsistent (invalid options, etc.), |
| 42 |
issue error message to stderr and abort program. |
| 43 |
|
| 44 |
Note that the strings we return are stored in the storage that |
| 45 |
was passed to us as the argv array. We also trash *argv. |
| 46 |
--------------------------------------------------------------------------*/ |
| 47 |
optEntry *option_def = malloc( 100*sizeof( optEntry ) ); |
| 48 |
/* Instructions to pm_optParseOptions3 on how to parse our options. */ |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
unsigned int maxvalSpec; |
| 53 |
|
| 54 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 55 |
OPTENT3(0, "maxval", OPT_UINT, &cmdlineP->maxval, &maxvalSpec, 0); |
| 56 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 57 |
|
| 58 |
opt.opt_table = option_def; |
| 59 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 60 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 61 |
|
| 62 |
pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); |
| 63 |
/* Uses and sets argc, argv, and some of *cmdline_p and others. */ |
| 64 |
|
| 65 |
if (!maxvalSpec) |
| 66 |
cmdlineP->maxval = PNM_MAXMAXVAL; |
| 67 |
|
| 68 |
if (cmdlineP->maxval > PNM_OVERALLMAXVAL) |
| 69 |
pm_error("Maximum allowed -maxval is %u. You specified %u", |
| 70 |
PNM_OVERALLMAXVAL, (unsigned)cmdlineP->maxval); |
| 71 |
else if (cmdlineP->maxval == 0) |
| 72 |
pm_error("-maxval cannot be 0"); |
| 73 |
|
| 74 |
/* Get the program parameters */ |
| 75 |
|
| 76 |
if (argc-1 >= 1) |
| 77 |
cmdlineP->inputFilespec = argv[1]; |
| 78 |
else |
| 79 |
cmdlineP->inputFilespec = "-"; |
| 80 |
|
| 81 |
if (argc-1 > 1) |
| 82 |
pm_error("Program takes at most one argument: the file name. " |
| 83 |
"You specified %d", argc-1); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
enum endian {ENDIAN_BIG, ENDIAN_LITTLE}; |
| 89 |
|
| 90 |
|
| 91 |
static enum endian machineEndianness; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static enum endian |
| 96 |
thisMachineEndianness(void) { |
| 97 |
/*---------------------------------------------------------------------------- |
| 98 |
Endianness is a component of the format in which a machine represents |
| 99 |
a number in memory or a register. It is the only component of the format |
| 100 |
that varies among typical machines. |
| 101 |
|
| 102 |
Big endianness is the natural format. In this format, if an integer is |
| 103 |
4 bytes, to be stored at memory address 100-103, the most significant |
| 104 |
byte goes at 100, the next most significant at 101, and the least |
| 105 |
significant byte at 103. This is natural because it matches the way |
| 106 |
humans read and write numbers. I.e. 258 is stored as 0x00000102. |
| 107 |
|
| 108 |
Little endian is extremely common because it is used by IA32. In the |
| 109 |
example above, the least significant byte goes first, so 258 would be |
| 110 |
stored as 0x02010000. |
| 111 |
|
| 112 |
You can extend this concept to floating point numbers, even though the |
| 113 |
bytes of a floating point number differ by more than significance. |
| 114 |
-----------------------------------------------------------------------------*/ |
| 115 |
short const testNumber = 0x0001; |
| 116 |
|
| 117 |
unsigned char * const storedNumber = (unsigned char *)&testNumber; |
| 118 |
enum endian endianness; |
| 119 |
|
| 120 |
if (storedNumber[0] == 0x01) |
| 121 |
endianness = ENDIAN_LITTLE; |
| 122 |
else |
| 123 |
endianness = ENDIAN_BIG; |
| 124 |
|
| 125 |
return endianness; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
typedef union { |
| 131 |
unsigned char bytes[4]; /* as read from the file */ |
| 132 |
float value; |
| 133 |
/* This is valid only if the pfmSample has the same endianness |
| 134 |
as the machine we're running on. |
| 135 |
*/ |
| 136 |
} pfmSample; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
static float |
| 141 |
floatFromPfmSample(pfmSample const sample, |
| 142 |
enum endian const pfmEndianness) { |
| 143 |
/*---------------------------------------------------------------------------- |
| 144 |
Type converter |
| 145 |
-----------------------------------------------------------------------------*/ |
| 146 |
if (machineEndianness == pfmEndianness) { |
| 147 |
return sample.value; |
| 148 |
} else { |
| 149 |
pfmSample rightEndianSample; |
| 150 |
unsigned int i, j; |
| 151 |
|
| 152 |
for (i = 0, j = sizeof(sample.bytes)-1; |
| 153 |
i < sizeof(sample.bytes); |
| 154 |
++i, --j) |
| 155 |
|
| 156 |
rightEndianSample.bytes[i] = sample.bytes[j]; |
| 157 |
|
| 158 |
return rightEndianSample.value; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
struct pfmHeader { |
| 165 |
unsigned int width; |
| 166 |
unsigned int height; |
| 167 |
bool color; |
| 168 |
float scaleFactor; |
| 169 |
enum endian endian; |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
static void |
| 174 |
readPfmHeader(FILE * const ifP, |
| 175 |
struct pfmHeader * const pfmHeaderP) { |
| 176 |
|
| 177 |
int firstChar; |
| 178 |
int secondChar; |
| 179 |
float scaleFactorEndian; |
| 180 |
|
| 181 |
firstChar = fgetc(ifP); |
| 182 |
if (firstChar == EOF) |
| 183 |
pm_error("Error reading first character of PFM file"); |
| 184 |
secondChar = fgetc(ifP); |
| 185 |
if (secondChar == EOF) |
| 186 |
pm_error("Error reading second character of PFM file"); |
| 187 |
|
| 188 |
if (firstChar != 'P' || (secondChar != 'F' && secondChar != 'f')) |
| 189 |
pm_error("First two characters of input file are '%c%c', but " |
| 190 |
"for a valid PFM file, they must be 'PF' or 'Pf'.", |
| 191 |
firstChar, secondChar); |
| 192 |
|
| 193 |
{ |
| 194 |
int whitespace; |
| 195 |
|
| 196 |
whitespace = fgetc(ifP); |
| 197 |
if (whitespace == EOF) |
| 198 |
pm_error("Error reading third character of PFM file"); |
| 199 |
|
| 200 |
if (!isspace(whitespace)) |
| 201 |
pm_error("The 3rd character of the input file is not whitespace."); |
| 202 |
} |
| 203 |
{ |
| 204 |
int rc; |
| 205 |
char whitespace; |
| 206 |
|
| 207 |
rc = fscanf(ifP, "%u %u%c", |
| 208 |
&pfmHeaderP->width, &pfmHeaderP->height, &whitespace); |
| 209 |
|
| 210 |
if (rc == EOF) |
| 211 |
pm_error("Error reading the width and height from input file."); |
| 212 |
else if (rc != 3) |
| 213 |
pm_error("Invalid input file format where width and height " |
| 214 |
"are supposed to be (should be two positive decimal " |
| 215 |
"integers separated by a space and followed by " |
| 216 |
"white space)"); |
| 217 |
|
| 218 |
if (!isspace(whitespace)) |
| 219 |
pm_error("Invalid input file format -- '%c' instead of " |
| 220 |
"white space after height", whitespace); |
| 221 |
|
| 222 |
if (pfmHeaderP->width == 0) |
| 223 |
pm_error("Invalid input file: image width is zero"); |
| 224 |
if (pfmHeaderP->height == 0) |
| 225 |
pm_error("Invalid input file: image height is zero"); |
| 226 |
} |
| 227 |
{ |
| 228 |
int rc; |
| 229 |
char whitespace; |
| 230 |
|
| 231 |
rc = fscanf(ifP, "%f%c", &scaleFactorEndian, &whitespace); |
| 232 |
|
| 233 |
if (rc == EOF) |
| 234 |
pm_error("Error reading the scale factor from input file."); |
| 235 |
else if (rc != 2) |
| 236 |
pm_error("Invalid input file format where scale factor " |
| 237 |
"is supposed to be (should be a floating point decimal " |
| 238 |
"number followed by white space"); |
| 239 |
|
| 240 |
if (!isspace(whitespace)) |
| 241 |
pm_error("Invalid input file format -- '%c' instead of " |
| 242 |
"white space after scale factor", whitespace); |
| 243 |
} |
| 244 |
|
| 245 |
pfmHeaderP->color = (secondChar == 'F'); |
| 246 |
/* 'PF' = RGB, 'Pf' = monochrome */ |
| 247 |
|
| 248 |
if (scaleFactorEndian > 0.0) { |
| 249 |
pfmHeaderP->endian = ENDIAN_BIG; |
| 250 |
pfmHeaderP->scaleFactor = scaleFactorEndian; |
| 251 |
} else if (scaleFactorEndian < 0.0) { |
| 252 |
pfmHeaderP->endian = ENDIAN_LITTLE; |
| 253 |
pfmHeaderP->scaleFactor = - scaleFactorEndian; |
| 254 |
} else |
| 255 |
pm_error("Scale factor/endianness in PFM header is 0"); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
static void |
| 261 |
dumpPfmHeader(struct pfmHeader const pfmHeader) { |
| 262 |
|
| 263 |
pm_message("width: %u, height: %u", pfmHeader.width, pfmHeader.height); |
| 264 |
pm_message("color: %s", pfmHeader.color ? "YES" : "NO"); |
| 265 |
pm_message("endian: %s", |
| 266 |
pfmHeader.endian == ENDIAN_BIG ? "BIG" : "LITTLE"); |
| 267 |
pm_message("scale factor: %f", pfmHeader.scaleFactor); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
static void |
| 273 |
initPam(struct pam * const pamP, |
| 274 |
int const width, |
| 275 |
int const height, |
| 276 |
bool const color, |
| 277 |
sample const maxval) { |
| 278 |
|
| 279 |
pamP->size = sizeof(*pamP); |
| 280 |
pamP->len = PAM_STRUCT_SIZE(tuple_type); |
| 281 |
pamP->file = stdout; |
| 282 |
pamP->format = PAM_FORMAT; |
| 283 |
pamP->plainformat = FALSE; |
| 284 |
pamP->width = width; |
| 285 |
pamP->height = height; |
| 286 |
pamP->maxval = maxval; |
| 287 |
if (color) { |
| 288 |
pamP->depth = 3; |
| 289 |
strcpy(pamP->tuple_type, "RGB"); |
| 290 |
} else { |
| 291 |
pamP->depth = 1; |
| 292 |
strcpy(pamP->tuple_type, "GRAYSCALE"); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static void |
| 299 |
makePamRow(struct pam * const pamP, |
| 300 |
FILE * const ifP, |
| 301 |
unsigned int const pfmRow, |
| 302 |
unsigned int const pfmSamplesPerRow, |
| 303 |
tuplen ** const tuplenArray, |
| 304 |
enum endian const endian, |
| 305 |
float const scaleFactor, |
| 306 |
pfmSample * const pfmRowBuffer) { |
| 307 |
/*---------------------------------------------------------------------------- |
| 308 |
Make a PAM (tuple) row of the form described by *pamP, from the next |
| 309 |
row in the PFM file identified by 'ifP'. Place it in the proper location |
| 310 |
in the tuple array 'tuplenArray'. |
| 311 |
|
| 312 |
'endian' is the endianness of the samples in the PFM file. |
| 313 |
|
| 314 |
'pfmRowNum' is the sequence number (starting at 0, which is the |
| 315 |
bottommost row of the image) of the row we are converting in the |
| 316 |
PFM file. |
| 317 |
|
| 318 |
Use 'pfmRowBuffer' as a work space; Caller must ensure this array |
| 319 |
has at least 'pfmSamplesPerRow' elements of space in it. |
| 320 |
-----------------------------------------------------------------------------*/ |
| 321 |
int const row = pamP->height - pfmRow - 1; |
| 322 |
tuplen * const tuplenRow = tuplenArray[row]; |
| 323 |
|
| 324 |
int col; |
| 325 |
int pfmCursor; |
| 326 |
int rc; |
| 327 |
|
| 328 |
rc = fread(pfmRowBuffer, sizeof(pfmSample), pfmSamplesPerRow, ifP); |
| 329 |
if (rc != pfmSamplesPerRow) |
| 330 |
pm_error("End of file in the middle of row %d", pfmRow); |
| 331 |
|
| 332 |
pfmCursor = 0; |
| 333 |
|
| 334 |
for (col = 0; col < pamP->width; ++col) { |
| 335 |
/* The order of planes (R, G, B) is the same in PFM as in PAM. */ |
| 336 |
unsigned int plane; |
| 337 |
for (plane = 0; plane < pamP->depth; ++plane) { |
| 338 |
float const val = |
| 339 |
floatFromPfmSample(pfmRowBuffer[pfmCursor++], endian); |
| 340 |
tuplenRow[col][plane] = val / scaleFactor; |
| 341 |
} |
| 342 |
} |
| 343 |
assert(pfmCursor == pfmSamplesPerRow); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
int |
| 349 |
main(int argc, char **argv ) { |
| 350 |
|
| 351 |
struct cmdlineInfo cmdline; |
| 352 |
FILE* ifP; |
| 353 |
struct pam pam; |
| 354 |
struct pfmHeader pfmHeader; |
| 355 |
pfmSample * pfmRowBuffer; |
| 356 |
unsigned int pfmSamplesPerRow; |
| 357 |
unsigned pfmRow; |
| 358 |
tuplen ** tuplenArray; |
| 359 |
|
| 360 |
machineEndianness = thisMachineEndianness(); |
| 361 |
|
| 362 |
pnm_init(&argc, argv); |
| 363 |
|
| 364 |
parseCommandLine(argc, argv, &cmdline); |
| 365 |
|
| 366 |
ifP = pm_openr(cmdline.inputFilespec); |
| 367 |
|
| 368 |
readPfmHeader(ifP, &pfmHeader); |
| 369 |
|
| 370 |
if (cmdline.verbose) |
| 371 |
dumpPfmHeader(pfmHeader); |
| 372 |
|
| 373 |
initPam(&pam, |
| 374 |
pfmHeader.width, pfmHeader.height, pfmHeader.color, |
| 375 |
cmdline.maxval); |
| 376 |
|
| 377 |
tuplenArray = pnm_allocpamarrayn(&pam); |
| 378 |
|
| 379 |
pfmSamplesPerRow = pam.width * pam.depth; |
| 380 |
|
| 381 |
MALLOCARRAY_NOFAIL(pfmRowBuffer, pfmSamplesPerRow); |
| 382 |
|
| 383 |
/* PFMs are upside down like BMPs */ |
| 384 |
for (pfmRow = 0; pfmRow < pam.height; ++pfmRow) |
| 385 |
makePamRow(&pam, ifP, pfmRow, pfmSamplesPerRow, |
| 386 |
tuplenArray, pfmHeader.endian, pfmHeader.scaleFactor, |
| 387 |
pfmRowBuffer); |
| 388 |
|
| 389 |
pnm_writepamn(&pam, tuplenArray); |
| 390 |
|
| 391 |
pnm_freepamarrayn(tuplenArray, &pam); |
| 392 |
free(pfmRowBuffer); |
| 393 |
|
| 394 |
pm_close(ifP); |
| 395 |
pm_close(pam.file); |
| 396 |
|
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|