about summary refs log tree commit diff
path: root/lib/libpamread.c
blob: 1ebf4b2ff9231ee959e464f233f947bbe1bfe7a1 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*=============================================================================
                                  libpamread.c
===============================================================================
   These are the library functions, which belong in the libnetpbm library,
   that deal with reading the PAM (Portable Arbitrary Format) image format
   raster (not the header).

   This file was originally written by Bryan Henderson and is contributed
   to the public domain by him and subsequent authors.
=============================================================================*/

/* See pmfileio.c for the complicated explanation of this 32/64 bit file
   offset stuff.
*/
#define _FILE_OFFSET_BITS 64
#define _LARGE_FILES

#include <string.h>
#include <limits.h>
#include <assert.h>

#include "netpbm/pm_config.h"
#include "netpbm/nstring.h"

#include "fileio.h"
#include "pam.h"


static void
readPbmRow(const struct pam * const pamP,
           tuple *            const tuplerow) {

    if (pamP->depth != 1)
        pm_error("Invalid pam structure passed to pnm_readpamrow().  "
                 "It says PBM format, but 'depth' member is not 1.");
    else {
        jmp_buf jmpbuf;
        jmp_buf * origJmpbufP;
        unsigned char * bitrow;

        bitrow = (unsigned char *) pbm_allocrow(pbm_packed_bytes(pamP->width));

        if (setjmp(jmpbuf) != 0) {
            pbm_freerow(bitrow);
            pm_setjmpbuf(origJmpbufP);
            pm_longjmp();
        } else {
            pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

            pbm_readpbmrow_packed(pamP->file, bitrow, pamP->width,
                                  pamP->format);

            if (tuplerow) {
                unsigned int col;
                for (col = 0; col < pamP->width; ++col) {
                    tuplerow[col][0] =
                        ( ((bitrow[col/8] >> (7-col%8)) & 1 ) == PBM_BLACK)
                        ? PAM_PBM_BLACK : PAM_PBM_WHITE
                        ;
                }
            }
            pm_setjmpbuf(origJmpbufP);
        }
        pbm_freerow(bitrow);
    }
}



static void
readPlainNonPbmRow(const struct pam * const pamP,
                   tuple *            const tuplerow) {

    int col;

    for (col = 0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            if (tuplerow) {
                tuplerow[col][plane] = pm_getuint(pamP->file);

                if (tuplerow[col][plane] > pamP->maxval)
                    pm_error("Plane %u sample value %lu exceeds the "
                             "image maxval of %lu",
                             plane, tuplerow[col][plane], pamP->maxval);
            } else
                pm_getuint(pamP->file);  /* read data and discard */
    }
}



/* Though it is possible to simplify the bytesToSampleN() and
   parsexNBpsRow() functions into a single routine that handles all
   sample widths, we value efficiency higher here.  Earlier versions
   of Netpbm (before 10.25) did that, with a loop, and performance
   suffered visibly.
*/

static __inline__ sample
bytes2ToSample(unsigned char buff[2]) {

    return (buff[0] << 8) + buff[1];
}



static __inline__ sample
bytes3ToSample(unsigned char buff[3]) {
    return (buff[0] << 16) + (buff[1] << 8) + buff[2];
}



static __inline__ sample
bytes4ToSample(unsigned char buff[4]) {

    return (buff[0] << 24) + (buff[1] << 16) + (buff[2] << 8) + buff[3];
}



static void
parse1BpsRow(const struct pam *    const pamP,
             tuple *               const tuplerow,
             const unsigned char * const inbuf) {

    int col;
    unsigned int bufferCursor;

    bufferCursor = 0;  /* initial value */

    for (col = 0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            tuplerow[col][plane]= inbuf[bufferCursor++];
    }
}



static void
parse2BpsRow(const struct pam *    const pamP,
             tuple *               const tuplerow,
             const unsigned char * const inbuf) {

    unsigned char (* const ib)[2] = (unsigned char (*)[2]) inbuf;

    int col;
    unsigned int bufferCursor;

    bufferCursor = 0;  /* initial value */

    for (col=0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            tuplerow[col][plane] = bytes2ToSample(ib[bufferCursor++]);
    }
}



static void
parse3BpsRow(const struct pam *    const pamP,
             tuple *               const tuplerow,
             const unsigned char * const inbuf) {

    unsigned char (* const ib)[3] = (unsigned char (*)[3]) inbuf;

    int col;
    unsigned int bufferCursor;

    bufferCursor = 0;  /* initial value */

    for (col=0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            tuplerow[col][plane] = bytes3ToSample(ib[bufferCursor++]);
    }
}



static void
parse4BpsRow(const struct pam *    const pamP,
             tuple *               const tuplerow,
             const unsigned char * const inbuf) {

    unsigned char (* const ib)[4] = (unsigned char (*)[4]) inbuf;

    int col;
    unsigned int bufferCursor;

    bufferCursor = 0;  /* initial value */

    for (col=0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane)
            tuplerow[col][plane] = bytes4ToSample(ib[bufferCursor++]);
    }
}



static void
validatePamRow(const struct pam * const pamP,
               tuple *            const tuplerow,
               const char **      const errorP) {
/*----------------------------------------------------------------------------
  Check for sample values above maxval in input.

  Note: a program that wants to deal with invalid sample values itself can
  simply make sure it sets pamP->maxval sufficiently high, so this validation
  never fails.
-----------------------------------------------------------------------------*/
    /* To save time, skip the test for if the maxval is a saturated value
       (255, 65535) or format is PBM.

       This is an expensive test, but is skipped in most cases: in practice
       maxvals other than 255 or 65535 are uncommon.  Thus we do this in a
       separate pass through the row rather than while reading in the row.
    */

    if (pamP->maxval == (((sample) 0x1) << pamP->bytes_per_sample*8) - 1 ||
        PAM_FORMAT_TYPE(pamP->format) == PBM_FORMAT) {
        /* There's no way a sample can be invalid, so we don't need to
           look at the samples individually.
        */
        *errorP = NULL;
    } else {
        unsigned int col;
        for (col = 0; col < pamP->width; ++col) {
            unsigned int plane;
            for (plane = 0; plane < pamP->depth; ++plane) {
                if (tuplerow[col][plane] > pamP->maxval) {
                    pm_asprintf(errorP,
                                "Plane %u sample value %lu exceeds the "
                                "image maxval of %lu",
                                plane, tuplerow[col][plane], pamP->maxval);
                    return;
                }
            }
        }
        *errorP = NULL;
    }
}



static void
readRawNonPbmRow(const struct pam * const pamP,
                 tuple *            const tuplerow) {

    unsigned int const rowImageSize =
        pamP->width * pamP->bytes_per_sample * pamP->depth;

    unsigned char * inbuf;
    size_t bytesRead;
    const char * error;

    inbuf = pnm_allocrowimage(pamP);

    bytesRead = fread(inbuf, 1, rowImageSize, pamP->file);

    if (bytesRead != rowImageSize) {
        if (feof(pamP->file))
            pm_asprintf(&error, "End of file encountered "
                        "when trying to read a row from input file.");
        else
            pm_asprintf(&error, "Error reading a row from input file.  "
                        "fread() fails with errno=%d (%s)",
                        errno, strerror(errno));
    } else {
        error = NULL;  /* initial assumption */
        if (tuplerow) {
            switch (pamP->bytes_per_sample) {
            case 1: parse1BpsRow(pamP, tuplerow, inbuf); break;
            case 2: parse2BpsRow(pamP, tuplerow, inbuf); break;
            case 3: parse3BpsRow(pamP, tuplerow, inbuf); break;
            case 4: parse4BpsRow(pamP, tuplerow, inbuf); break;
            default:
                pm_asprintf(&error, "invalid bytes per sample passed to "
                            "pnm_formatpamrow(): %u", pamP->bytes_per_sample);
            }
            if (error == NULL)
                validatePamRow(pamP, tuplerow, &error);
        }
    }
    pnm_freerowimage(inbuf);

    if (error) {
        pm_errormsg("%s", error);
        pm_strfree(error);
        pm_longjmp();
    }
}



void
pnm_readpamrow(const struct pam * const pamP,
               tuple *            const tuplerow) {
/*----------------------------------------------------------------------------
   Read a row from the Netpbm image file into tuplerow[], at the
   current file position.  If 'tuplerow' is NULL, advance the file
   pointer to the next row, but don't return the contents of the
   current one.

   We assume the file is positioned to the beginning of a row of the
   image's raster.
-----------------------------------------------------------------------------*/
    /* For speed, we don't check any of the inputs for consistency
       here (unless it's necessary to avoid crashing).  Any consistency
       checking should have been done by a prior call to
       pnm_readpaminit().
    */

    /* Need a special case for raw PBM because it has multiple tuples (8)
       packed into one byte.
    */

    switch (pamP->format) {
    case PAM_FORMAT:
    case RPPM_FORMAT:
    case RPGM_FORMAT:
        readRawNonPbmRow(pamP, tuplerow);
        break;
    case PPM_FORMAT:
    case PGM_FORMAT:
        readPlainNonPbmRow(pamP, tuplerow);
        break;
    case RPBM_FORMAT:
    case PBM_FORMAT:
        readPbmRow(pamP, tuplerow);
        break;
    default:
        pm_error("Invalid 'format' member in PAM structure: %u", pamP->format);
    }
}



tuple **
pnm_readpam(FILE *       const fileP,
            struct pam * const pamP,
            int          const size) {

    jmp_buf jmpbuf;
    jmp_buf * origJmpbufP;
    tuple ** tuplearray;

    pnm_readpaminit(fileP, pamP, size);

    tuplearray = pnm_allocpamarray(pamP);

    if (setjmp(jmpbuf) != 0) {
        pnm_freepamarray(tuplearray, pamP);
        pm_setjmpbuf(origJmpbufP);
        pm_longjmp();
    } else {
        unsigned int row;

        pm_setjmpbufsave(&jmpbuf, &origJmpbufP);

        for (row = 0; row < pamP->height; ++row)
            pnm_readpamrow(pamP, tuplearray[row]);

        pm_setjmpbuf(origJmpbufP);
    }
    return tuplearray;
}