| 1 |
/***************************************************************************** |
| 2 |
pamtopfm |
| 3 |
****************************************************************************** |
| 4 |
This program converts a PAM image to PFM (Portable Float Map). |
| 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 |
#include "nstring.h" |
| 24 |
|
| 25 |
enum endian {ENDIAN_BIG, ENDIAN_LITTLE}; |
| 26 |
|
| 27 |
struct cmdlineInfo { |
| 28 |
const char * inputFilespec; |
| 29 |
unsigned int verbose; |
| 30 |
enum endian endian; |
| 31 |
float scale; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static enum endian machineEndianness; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static void |
| 41 |
parseCommandLine(int argc, |
| 42 |
char ** argv, |
| 43 |
struct cmdlineInfo * const cmdlineP) { |
| 44 |
/* -------------------------------------------------------------------------- |
| 45 |
Parse program command line described in Unix standard form by argc |
| 46 |
and argv. Return the information in the options as *cmdlineP. |
| 47 |
|
| 48 |
If command line is internally inconsistent (invalid options, etc.), |
| 49 |
issue error message to stderr and abort program. |
| 50 |
|
| 51 |
Note that the strings we return are stored in the storage that |
| 52 |
was passed to us as the argv array. We also trash *argv. |
| 53 |
--------------------------------------------------------------------------*/ |
| 54 |
optEntry *option_def = malloc( 100*sizeof( optEntry ) ); |
| 55 |
/* Instructions to pm_optParseOptions3 on how to parse our options. */ |
| 56 |
optStruct3 opt; |
| 57 |
|
| 58 |
unsigned int option_def_index; |
| 59 |
char * endianOpt; |
| 60 |
unsigned int endianSpec, scaleSpec; |
| 61 |
|
| 62 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 63 |
OPTENT3(0, "endian", OPT_STRING, &endianOpt, &endianSpec, 0); |
| 64 |
OPTENT3(0, "scale", OPT_FLOAT, &cmdlineP->scale, &scaleSpec, 0); |
| 65 |
|
| 66 |
opt.opt_table = option_def; |
| 67 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 68 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 69 |
|
| 70 |
pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 ); |
| 71 |
/* Uses and sets argc, argv, and some of *cmdline_p and others. */ |
| 72 |
|
| 73 |
if (endianSpec) { |
| 74 |
if (streq(endianOpt, "big")) |
| 75 |
cmdlineP->endian = ENDIAN_BIG; |
| 76 |
else if (streq(endianOpt, "little")) |
| 77 |
cmdlineP->endian = ENDIAN_LITTLE; |
| 78 |
else |
| 79 |
pm_error("Invalid value '%s' for -endian. " |
| 80 |
"Must be 'big' or 'little'.", endianOpt); |
| 81 |
} else |
| 82 |
cmdlineP->endian = machineEndianness; |
| 83 |
|
| 84 |
if (!scaleSpec) { |
| 85 |
cmdlineP->scale = 1.0; |
| 86 |
} |
| 87 |
if (cmdlineP->scale == 0.0) |
| 88 |
pm_error("Scale factor cannot be zero"); |
| 89 |
|
| 90 |
/* Get the program parameters */ |
| 91 |
|
| 92 |
if (argc-1 >= 1) |
| 93 |
cmdlineP->inputFilespec = argv[1]; |
| 94 |
else |
| 95 |
cmdlineP->inputFilespec = "-"; |
| 96 |
|
| 97 |
if (argc-1 > 1) |
| 98 |
pm_error("Program takes at most one argument: the file name. " |
| 99 |
"You specified %d", argc-1); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static enum endian |
| 105 |
thisMachineEndianness(void) { |
| 106 |
/*---------------------------------------------------------------------------- |
| 107 |
Endianness is a component of the format in which a machine represents |
| 108 |
a number in memory or a register. It is the only component of the format |
| 109 |
that varies among typical machines. |
| 110 |
|
| 111 |
Big endianness is the natural format. In this format, if an integer is |
| 112 |
4 bytes, to be stored at memory address 100-103, the most significant |
| 113 |
byte goes at 100, the next most significant at 101, and the least |
| 114 |
significant byte at 103. This is natural because it matches the way |
| 115 |
humans read and write numbers. I.e. 258 is stored as 0x00000102. |
| 116 |
|
| 117 |
Little endian is extremely common because it is used by IA32. In the |
| 118 |
example above, the least significant byte goes first, so 258 would be |
| 119 |
stored as 0x02010000. |
| 120 |
|
| 121 |
You can extend this concept to floating point numbers, even though the |
| 122 |
bytes of a floating point number differ by more than significance. |
| 123 |
-----------------------------------------------------------------------------*/ |
| 124 |
short const testNumber = 0x0001; |
| 125 |
|
| 126 |
unsigned char * const storedNumber = (unsigned char *)&testNumber; |
| 127 |
enum endian endianness; |
| 128 |
|
| 129 |
if (storedNumber[0] == 0x01) |
| 130 |
endianness = ENDIAN_LITTLE; |
| 131 |
else |
| 132 |
endianness = ENDIAN_BIG; |
| 133 |
|
| 134 |
return endianness; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
typedef struct { |
| 140 |
unsigned char bytes[4]; |
| 141 |
} pfmSample; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
static void |
| 146 |
floatToPfmSample(float const input, |
| 147 |
pfmSample * outputP, |
| 148 |
enum endian const pfmEndianness) { |
| 149 |
/*---------------------------------------------------------------------------- |
| 150 |
Type converter |
| 151 |
-----------------------------------------------------------------------------*/ |
| 152 |
if (machineEndianness == pfmEndianness) { |
| 153 |
MEMSCPY(&outputP->bytes, &input); |
| 154 |
} else { |
| 155 |
unsigned char reversed[sizeof(pfmSample)]; |
| 156 |
unsigned int i, j; |
| 157 |
|
| 158 |
MEMSCPY(&reversed, &input); |
| 159 |
|
| 160 |
for (i = 0, j = sizeof(pfmSample)-1; |
| 161 |
i < sizeof(pfmSample); |
| 162 |
++i, --j) |
| 163 |
|
| 164 |
outputP->bytes[i] = reversed[j]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
struct pfmHeader { |
| 171 |
unsigned int width; |
| 172 |
unsigned int height; |
| 173 |
bool color; |
| 174 |
float scaleFactor; |
| 175 |
enum endian endian; |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
static void |
| 180 |
writePfmHeader(FILE * const ofP, |
| 181 |
struct pfmHeader const pfmHeader) { |
| 182 |
|
| 183 |
const char * const magic = pfmHeader.color ? "PF" : "Pf"; |
| 184 |
float const scaleFactorEndian = |
| 185 |
pfmHeader.endian == ENDIAN_BIG ? |
| 186 |
pfmHeader.scaleFactor : |
| 187 |
- pfmHeader.scaleFactor; |
| 188 |
|
| 189 |
fprintf(ofP, "%s\n", magic); |
| 190 |
fprintf(ofP, "%u %u\n", pfmHeader.width, pfmHeader.height); |
| 191 |
fprintf(ofP, "%f\n", scaleFactorEndian); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static void |
| 197 |
writePfmRow(struct pam * const pamP, |
| 198 |
FILE * const ofP, |
| 199 |
unsigned int const pfmRow, |
| 200 |
unsigned int const pfmSamplesPerRow, |
| 201 |
tuplen ** const tuplenArray, |
| 202 |
enum endian const endian, |
| 203 |
float const scaleFactor, |
| 204 |
pfmSample * const pfmRowBuffer) { |
| 205 |
|
| 206 |
int const row = pamP->height - pfmRow - 1; |
| 207 |
tuplen * const tuplenRow = tuplenArray[row]; |
| 208 |
|
| 209 |
int col; |
| 210 |
int pfmCursor; |
| 211 |
int rc; |
| 212 |
|
| 213 |
pfmCursor = 0; /* initial value */ |
| 214 |
|
| 215 |
for (col = 0; col < pamP->width; ++col) { |
| 216 |
/* The order of planes (R, G, B) is the same in PFM as in PAM. */ |
| 217 |
unsigned int plane; |
| 218 |
for (plane = 0; plane < pamP->depth; ++plane) { |
| 219 |
pfmSample val; |
| 220 |
floatToPfmSample(tuplenRow[col][plane] * scaleFactor, |
| 221 |
&val, endian); |
| 222 |
pfmRowBuffer[pfmCursor++] = val; |
| 223 |
} |
| 224 |
} |
| 225 |
assert(pfmCursor == pfmSamplesPerRow); |
| 226 |
|
| 227 |
rc = fwrite(pfmRowBuffer, sizeof(pfmSample), pfmSamplesPerRow, ofP); |
| 228 |
if (rc != pfmSamplesPerRow) |
| 229 |
pm_error("Unable to write to output file in the middle of row %d", |
| 230 |
pfmRow); |
| 231 |
|
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static struct pfmHeader |
| 238 |
makePfmHeader(const struct pam * const pamP, |
| 239 |
float const scaleFactor, |
| 240 |
enum endian const endian) { |
| 241 |
|
| 242 |
struct pfmHeader pfmHeader; |
| 243 |
|
| 244 |
pfmHeader.width = pamP->width; |
| 245 |
pfmHeader.height = pamP->height; |
| 246 |
|
| 247 |
if (strncmp(pamP->tuple_type, "RGB", 3) == 0) |
| 248 |
pfmHeader.color = TRUE; |
| 249 |
else if (strncmp(pamP->tuple_type, "GRAYSCALE", 9) == 0) |
| 250 |
pfmHeader.color = FALSE; |
| 251 |
else if (strncmp(pamP->tuple_type, "BLACKANDWHITE", 13) == 0) |
| 252 |
pfmHeader.color = FALSE; |
| 253 |
else |
| 254 |
pm_error("Invalid PAM input. Tuple type is '%s'. " |
| 255 |
"We understand only RGB* and GRAYSCALE*", pamP->tuple_type); |
| 256 |
|
| 257 |
pfmHeader.scaleFactor = scaleFactor; |
| 258 |
pfmHeader.endian = endian; |
| 259 |
|
| 260 |
return pfmHeader; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
int |
| 266 |
main(int argc, char **argv ) { |
| 267 |
|
| 268 |
struct cmdlineInfo cmdline; |
| 269 |
FILE* ifP; |
| 270 |
struct pam pam; |
| 271 |
pfmSample * pfmRowBuffer; |
| 272 |
unsigned int pfmSamplesPerRow; |
| 273 |
unsigned int pfmRow; |
| 274 |
tuplen ** tuplenArray; |
| 275 |
|
| 276 |
pnm_init(&argc, argv); |
| 277 |
|
| 278 |
machineEndianness = thisMachineEndianness(); |
| 279 |
|
| 280 |
parseCommandLine(argc, argv, &cmdline); |
| 281 |
|
| 282 |
ifP = pm_openr(cmdline.inputFilespec); |
| 283 |
|
| 284 |
tuplenArray = pnm_readpamn(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 285 |
|
| 286 |
writePfmHeader(stdout, |
| 287 |
makePfmHeader(&pam, cmdline.scale, cmdline.endian)); |
| 288 |
|
| 289 |
pfmSamplesPerRow = pam.width * pam.depth; |
| 290 |
|
| 291 |
MALLOCARRAY_NOFAIL(pfmRowBuffer, pfmSamplesPerRow); |
| 292 |
|
| 293 |
/* PFMs are upside down like BMPs */ |
| 294 |
for (pfmRow = 0; pfmRow < pam.height; ++pfmRow) |
| 295 |
writePfmRow(&pam, stdout, pfmRow, pfmSamplesPerRow, |
| 296 |
tuplenArray, cmdline.endian, cmdline.scale, |
| 297 |
pfmRowBuffer); |
| 298 |
|
| 299 |
pnm_freepamarrayn(tuplenArray, &pam); |
| 300 |
free(pfmRowBuffer); |
| 301 |
|
| 302 |
pm_close(stdout); |
| 303 |
pm_close(pam.file); |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|