about summary refs log tree commit diff
path: root/converter/other/pamtopfm.c
blob: 7991e0dd63895e199ff18db734bfe1624fb572f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*****************************************************************************
                                  pamtopfm
******************************************************************************
  This program converts a PAM image to PFM (Portable Float Map).

  By Bryan Henderson, San Jose, CA April 2004.

  Contributed to the public domain by its author.

*****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>

#include "pm_c_util.h"
#include "pam.h"
#include "pm_gamma.h"
#include "shhopt.h"
#include "mallocvar.h"
#include "nstring.h"

enum endian {ENDIAN_BIG, ENDIAN_LITTLE};

struct cmdlineInfo {
    const char * inputFilespec;
    unsigned int verbose;
    enum endian endian;
    float scale;
};



static enum endian machineEndianness;



static void
parseCommandLine(int argc,
                 char ** argv,
                 struct cmdlineInfo  * const cmdlineP) {
/* --------------------------------------------------------------------------
   Parse program command line described in Unix standard form by argc
   and argv.  Return the information in the options as *cmdlineP.

   If command line is internally inconsistent (invalid options, etc.),
   issue error message to stderr and abort program.

   Note that the strings we return are stored in the storage that
   was passed to us as the argv array.  We also trash *argv.
--------------------------------------------------------------------------*/
    optEntry *option_def = malloc( 100*sizeof( optEntry ) );
    /* Instructions to pm_optParseOptions3 on how to parse our options. */
    optStruct3 opt;

    unsigned int option_def_index;
    char * endianOpt;
    unsigned int endianSpec, scaleSpec;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "endian",   OPT_STRING, &endianOpt, &endianSpec,        0);
    OPTENT3(0, "scale",    OPT_FLOAT,  &cmdlineP->scale, &scaleSpec,   0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;   /* We have no parms that are negative numbers */

    pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
    /* Uses and sets argc, argv, and some of *cmdline_p and others. */

    if (endianSpec) {
        if (streq(endianOpt, "big"))
            cmdlineP->endian = ENDIAN_BIG;
        else if (streq(endianOpt, "little"))
            cmdlineP->endian = ENDIAN_LITTLE;
        else
            pm_error("Invalid value '%s' for -endian.  "
                     "Must be 'big' or 'little'.", endianOpt);
    } else
        cmdlineP->endian = machineEndianness;

    if (!scaleSpec) {
        cmdlineP->scale = 1.0;
    }
    if (cmdlineP->scale == 0.0)
        pm_error("Scale factor cannot be zero");

    /* Get the program parameters */

    if (argc-1 >= 1)
        cmdlineP->inputFilespec = argv[1];
    else
        cmdlineP->inputFilespec = "-";

    if (argc-1 > 1)
        pm_error("Program takes at most one argument:  the file name.  "
                 "You specified %d", argc-1);
}



static enum endian
thisMachineEndianness(void) {
/*----------------------------------------------------------------------------
   Endianness is a component of the format in which a machine represents
   a number in memory or a register.  It is the only component of the format
   that varies among typical machines.

   Big endianness is the natural format.  In this format, if an integer is
   4 bytes, to be stored at memory address 100-103, the most significant
   byte goes at 100, the next most significant at 101, and the least
   significant byte at 103.  This is natural because it matches the way
   humans read and write numbers.  I.e. 258 is stored as 0x00000102.

   Little endian is extremely common because it is used by IA32.  In the
   example above, the least significant byte goes first, so 258 would be
   stored as 0x02010000.

   You can extend this concept to floating point numbers, even though the
   bytes of a floating point number differ by more than significance.
-----------------------------------------------------------------------------*/
    short const testNumber = 0x0001;

    unsigned char * const storedNumber = (unsigned char *)&testNumber;
    enum endian endianness;

    if (storedNumber[0] == 0x01)
        endianness = ENDIAN_LITTLE;
    else
        endianness = ENDIAN_BIG;

    return endianness;
}



typedef struct {
    unsigned char bytes[4];
} pfmSample;



static void
floatToPfmSample(float       const input,
                 pfmSample *       outputP,
                 enum endian const pfmEndianness) {
/*----------------------------------------------------------------------------
   Type converter
-----------------------------------------------------------------------------*/
    if (machineEndianness == pfmEndianness) {
        MEMSCPY(&outputP->bytes, &input);
    } else {
        unsigned char reversed[sizeof(pfmSample)];
        unsigned int i, j;

        MEMSCPY(&reversed, &input);

        for (i = 0, j = sizeof(pfmSample)-1;
             i < sizeof(pfmSample);
             ++i, --j)

            outputP->bytes[i] = reversed[j];
    }
}



struct pfmHeader {
    unsigned int width;
    unsigned int height;
    bool color;
    float scaleFactor;
    enum endian endian;
};


static void
writePfmHeader(FILE *           const ofP,
               struct pfmHeader const pfmHeader) {

    const char * const magic = pfmHeader.color ? "PF" : "Pf";
    float const scaleFactorEndian =
        pfmHeader.endian == ENDIAN_BIG ?
            pfmHeader.scaleFactor :
            - pfmHeader.scaleFactor;

    fprintf(ofP, "%s\n",    magic);
    fprintf(ofP, "%u %u\n", pfmHeader.width, pfmHeader.height);
    fprintf(ofP, "%f\n",    scaleFactorEndian);
}



static void
writePfmRow(struct pam * const pamP,
            FILE *       const ofP,
            unsigned int const pfmRow,
            unsigned int const pfmSamplesPerRow,
            tuplen **    const tuplenArray,
            enum endian  const endian,
            float        const scaleFactor,
            pfmSample *  const pfmRowBuffer) {

    int const row = pamP->height - pfmRow - 1;
    tuplen * const tuplenRow = tuplenArray[row];

    int col;
    int pfmCursor;
    int rc;

    pfmCursor = 0;  /* initial value */

    for (col = 0; col < pamP->width; ++col) {
        /* The order of planes (R, G, B) is the same in PFM as in PAM. */
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane) {
            pfmSample val;
            floatToPfmSample(tuplenRow[col][plane] * scaleFactor,
                             &val, endian);
            pfmRowBuffer[pfmCursor++] = val;
        }
    }
    assert(pfmCursor == pfmSamplesPerRow);

    rc = fwrite(pfmRowBuffer, sizeof(pfmSample), pfmSamplesPerRow, ofP);
    if (rc != pfmSamplesPerRow)
        pm_error("Unable to write to output file in the middle of row %d",
                 pfmRow);


}



static struct pfmHeader
makePfmHeader(const struct pam * const pamP,
              float              const scaleFactor,
              enum endian        const endian) {

    struct pfmHeader pfmHeader;

    pfmHeader.width  = pamP->width;
    pfmHeader.height = pamP->height;

    if (strncmp(pamP->tuple_type, "RGB", 3) == 0)
        pfmHeader.color = TRUE;
    else if (strncmp(pamP->tuple_type, "GRAYSCALE", 9) == 0)
        pfmHeader.color = FALSE;
    else if (strncmp(pamP->tuple_type, "BLACKANDWHITE", 13) == 0)
        pfmHeader.color = FALSE;
    else
        pm_error("Invalid PAM input.  Tuple type is '%s'.  "
                 "We understand only RGB* and GRAYSCALE*", pamP->tuple_type);

    pfmHeader.scaleFactor = scaleFactor;
    pfmHeader.endian = endian;

    return pfmHeader;
}



int
main(int argc, char **argv ) {

    struct cmdlineInfo cmdline;
    FILE* ifP;
    struct pam pam;
    pfmSample * pfmRowBuffer;
    unsigned int pfmSamplesPerRow;
    unsigned int pfmRow;
    tuplen ** tuplenArray;

    pnm_init(&argc, argv);

    machineEndianness = thisMachineEndianness();

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    tuplenArray = pnm_readpamn(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));

    writePfmHeader(stdout,
                   makePfmHeader(&pam, cmdline.scale, cmdline.endian));

    pfmSamplesPerRow = pam.width * pam.depth;

    MALLOCARRAY_NOFAIL(pfmRowBuffer, pfmSamplesPerRow);

    /* PFMs are upside down like BMPs */
    for (pfmRow = 0; pfmRow < pam.height; ++pfmRow)
        writePfmRow(&pam, stdout, pfmRow, pfmSamplesPerRow,
                    tuplenArray, cmdline.endian, cmdline.scale,
                    pfmRowBuffer);

    pnm_freepamarrayn(tuplenArray, &pam);
    free(pfmRowBuffer);

    pm_close(stdout);
    pm_close(pam.file);

    return 0;
}