about summary refs log tree commit diff
path: root/lib/libpnm3.c
blob: ba408bd1dccbb6bc4b6438502f4f24ef53311938 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* libpnm3.c - pnm utility library part 3
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdbool.h>
#include <assert.h>

#include "pnm.h"
#include "ppm.h"
#include "pgm.h"
#include "pbm.h"



static xel
mean4(int const format,
      xel const a,
      xel const b,
      xel const c,
      xel const d) {
/*----------------------------------------------------------------------------
   Return cartesian mean of the 4 colors.
-----------------------------------------------------------------------------*/
    xel retval;

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PPM_ASSIGN(
            retval,
            (PPM_GETR(a) + PPM_GETR(b) + PPM_GETR(c) + PPM_GETR(d)) / 4,
            (PPM_GETG(a) + PPM_GETG(b) + PPM_GETG(c) + PPM_GETG(d)) / 4,
            (PPM_GETB(a) + PPM_GETB(b) + PPM_GETB(c) + PPM_GETB(d)) / 4);
        break;

    case PGM_TYPE:
    case PBM_TYPE:
        PNM_ASSIGN1(
            retval,
            (PNM_GET1(a) + PNM_GET1(b) + PNM_GET1(c) + PNM_GET1(d))/4);
        break;

    default:
        pm_error("Invalid format passed to pnm_backgroundxel()");
    }
    return retval;
}



xel
pnm_backgroundxel(xel**  const xels,
                  int    const cols,
                  int    const rows,
                  xelval const maxval,
                  int    const format) {

    xel bgxel, ul, ur, ll, lr;

    /* Guess a good background value. */
    ul = xels[0][0];
    ur = xels[0][cols-1];
    ll = xels[rows-1][0];
    lr = xels[rows-1][cols-1];

    /* We first recognize three corners equal.  If not, we look for any
       two.  If not, we just average all four.
    */
    if (PNM_EQUAL(ul, ur) && PNM_EQUAL(ur, ll))
        bgxel = ul;
    else if (PNM_EQUAL(ul, ur) && PNM_EQUAL(ur, lr))
        bgxel = ul;
    else if (PNM_EQUAL(ul, ll) && PNM_EQUAL(ll, lr))
        bgxel = ul;
    else if (PNM_EQUAL(ur, ll) && PNM_EQUAL(ll, lr))
        bgxel = ur;
    else if (PNM_EQUAL(ul, ur))
        bgxel = ul;
    else if (PNM_EQUAL(ul, ll))
        bgxel = ul;
    else if (PNM_EQUAL(ul, lr))
        bgxel = ul;
    else if (PNM_EQUAL(ur, ll))
        bgxel = ur;
    else if (PNM_EQUAL(ur, lr))
        bgxel = ur;
    else if (PNM_EQUAL(ll, lr))
        bgxel = ll;
    else
        bgxel = mean4(format, ul, ur, ll, lr);

    return bgxel;
}



xel
pnm_backgroundxelrow(xel *  const xelrow,
                     int    const cols,
                     xelval const maxval,
                     int    const format) {
/*----------------------------------------------------------------------------
   Guess a good background color for an image that contains row 'xelrow'
   (probably top or bottom edge).

   'cols', 'maxval', and 'format' describe 'xelrow'.
-----------------------------------------------------------------------------*/
    xel bgxel, l, r;

    l = xelrow[0];
    r = xelrow[cols-1];

    if (PNM_EQUAL(l, r))
        /* Both corners are same color, so that's the background color,
           without any extra computation.
        */
        bgxel = l;
    else {
        /* Corners are different, so use cartesian mean of them */
        switch (PNM_FORMAT_TYPE(format)) {
        case PPM_TYPE:
            PPM_ASSIGN(bgxel,
                       (PPM_GETR(l) + PPM_GETR(r)) / 2,
                       (PPM_GETG(l) + PPM_GETG(r)) / 2,
                       (PPM_GETB(l) + PPM_GETB(r)) / 2
                );
            break;

        case PGM_TYPE:
            PNM_ASSIGN1(bgxel, (PNM_GET1(l) + PNM_GET1(r)) / 2);
            break;

        case PBM_TYPE: {
            unsigned int col, blackCnt;

            /* One black, one white.  Gotta count. */
            for (col = 0, blackCnt = 0; col < cols; ++col) {
                if (PNM_GET1(xelrow[col] ) == 0)
                    ++blackCnt;
            }
            if (blackCnt >= cols / 2)
                PNM_ASSIGN1(bgxel, 0);
            else
                PNM_ASSIGN1(bgxel, maxval);
            break;
        }

        default:
            pm_error("Invalid format passed to pnm_backgroundxelrow()");
        }
    }

    return bgxel;
}



xel
pnm_whitexel(xelval const maxval,
             int    const format) {

    xel retval;

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PPM_ASSIGN(retval, maxval, maxval, maxval);
        break;

    case PGM_TYPE:
    case PBM_TYPE:
        PNM_ASSIGN1(retval, maxval);
        break;

    default:
        pm_error("Invalid format %d passed to pnm_whitexel()", format);
    }

    return retval;
}



xel
pnm_blackxel(xelval const maxval,
             int    const format) {

    xel retval;

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PPM_ASSIGN(retval, 0, 0, 0);
        break;

    case PGM_TYPE:
    case PBM_TYPE:
        PNM_ASSIGN1(retval, 0);
        break;

    default:
        pm_error("Invalid format %d passed to pnm_blackxel()", format);
    }

    return retval;
}



void
pnm_invertxel(xel*   const xP,
              xelval const maxval,
              int    const format) {

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PPM_ASSIGN(*xP,
                   maxval - PPM_GETR(*xP),
                   maxval - PPM_GETG(*xP),
                   maxval - PPM_GETB(*xP));
        break;

    case PGM_TYPE:
        PNM_ASSIGN1(*xP, maxval - PNM_GET1(*xP));
        break;

    case PBM_TYPE:
        PNM_ASSIGN1(*xP, (PNM_GET1(*xP) == 0) ? maxval : 0);
        break;

    default:
        pm_error("Invalid format passed to pnm_invertxel()");
    }
}



void
pnm_promoteformat(xel ** const xels,
                  int    const cols,
                  int    const rows,
                  xelval const maxval,
                  int    const format,
                  xelval const newmaxval,
                  int    const newformat) {

    unsigned int row;

    for (row = 0; row < rows; ++row)
        pnm_promoteformatrow(
            xels[row], cols, maxval, format, newmaxval, newformat);
}



void
pnm_promoteformatrow(xel *  const xelrow,
                     int    const cols,
                     xelval const maxval,
                     int    const format,
                     xelval const newmaxval,
                     int    const newformat) {

    if ((PNM_FORMAT_TYPE(format) == PPM_TYPE &&
         (PNM_FORMAT_TYPE(newformat) == PGM_TYPE ||
          PNM_FORMAT_TYPE(newformat) == PBM_TYPE)) ||
        (PNM_FORMAT_TYPE(format) == PGM_TYPE &&
         PNM_FORMAT_TYPE(newformat) == PBM_TYPE)) {

        pm_error( "pnm_promoteformatrow: can't promote downwards!" );
    } else if (PNM_FORMAT_TYPE(format) == PNM_FORMAT_TYPE(newformat)) {
        /* We're promoting to the same type - but not necessarily maxval */
        if (PNM_FORMAT_TYPE(format) == PBM_TYPE) {
            /* PBM doesn't have maxval, so this is idempotent */
        } else if (newmaxval < maxval)
            pm_error("pnm_promoteformatrow: can't decrease maxval - "
                     "try using pamdepth");
        else if (newmaxval == maxval) {
            /* Same type, same maxval => idempotent function */
        } else {
            /* Increase maxval. */
            switch (PNM_FORMAT_TYPE(format)) {
            case PGM_TYPE: {
                unsigned int col;
                for (col = 0; col < cols; ++col)
                    PNM_ASSIGN1(xelrow[col],
                                PNM_GET1(xelrow[col]) * newmaxval / maxval);
            } break;

            case PPM_TYPE: {
                unsigned int col;
                for (col = 0; col < cols; ++col)
                    PPM_DEPTH(xelrow[col], xelrow[col], maxval, newmaxval);
            } break;

            default:
                pm_error("Invalid old format passed to "
                         "pnm_promoteformatrow()");
            }
        }
    } else {
        /* Promote to a higher type. */
        switch (PNM_FORMAT_TYPE(format)) {
        case PBM_TYPE:
            switch (PNM_FORMAT_TYPE(newformat)) {
            case PGM_TYPE: {
                unsigned int col;
                for (col = 0; col < cols; ++col) {
                    if (PNM_GET1(xelrow[col]) == 0)
                        PNM_ASSIGN1(xelrow[col], 0);
                    else
                        PNM_ASSIGN1(xelrow[col], newmaxval);
                }
            } break;

            case PPM_TYPE: {
                unsigned int col;
                for (col = 0; col < cols; ++col) {
                    if (PNM_GET1(xelrow[col]) == 0)
                        PPM_ASSIGN(xelrow[col], 0, 0, 0);
                    else
                        PPM_ASSIGN(xelrow[col],
                                   newmaxval, newmaxval, newmaxval );
                }
            } break;

            default:
                pm_error("Invalid new format passed to "
                         "pnm_promoteformatrow()");
            }
            break;

        case PGM_TYPE:
            switch (PNM_FORMAT_TYPE(newformat)) {
            case PPM_TYPE:
                if (newmaxval < maxval)
                    pm_error("pnm_promoteformatrow: can't decrease maxval - "
                             "try using pamdepth");
                else if (newmaxval == maxval) {
                    unsigned int col;
                    for (col = 0; col < cols; ++col) {
                        PPM_ASSIGN(xelrow[col],
                                   PNM_GET1(xelrow[col]),
                                   PNM_GET1(xelrow[col]),
                                   PNM_GET1(xelrow[col]));
                    }
                } else {
                    /* Increase maxval. */
                    unsigned int col;
                    for (col = 0; col < cols; ++col) {
                        PPM_ASSIGN(xelrow[col],
                                   PNM_GET1(xelrow[col]) * newmaxval / maxval,
                                   PNM_GET1(xelrow[col]) * newmaxval / maxval,
                                   PNM_GET1(xelrow[col]) * newmaxval / maxval);
                    }
                }
                break;

            default:
                pm_error("Invalid new format passed to "
                         "pnm_promoteformatrow()");
            }
            break;

        default:
            pm_error("Invalid old format passed to pnm_promoteformatrow()");
        }
    }
}



pixel
pnm_xeltopixel(xel const inputXel,
               int const format) {

    pixel outputPixel;

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PPM_ASSIGN(outputPixel,
                   PNM_GETR(inputXel),
                   PNM_GETG(inputXel),
                   PNM_GETB(inputXel));
        break;
    case PGM_TYPE:
    case PBM_TYPE:
        PPM_ASSIGN(outputPixel,
                   PNM_GET1(inputXel),
                   PNM_GET1(inputXel),
                   PNM_GET1(inputXel));
        break;
    default:
        pm_error("Invalid format code %d passed to pnm_xeltopixel()",
                 format);
    }

    return outputPixel;
}



xel
pnm_pixeltoxel(pixel const inputPixel) {

    return inputPixel;
}



xel
pnm_graytoxel(gray const inputGray) {

    xel outputXel;

    PNM_ASSIGN1(outputXel, inputGray);

    return outputXel;
}


xel
pnm_bittoxel(bit    const inputBit,
             xelval const maxval) {

    switch (inputBit) {
    case PBM_BLACK: return pnm_blackxel(maxval, PBM_TYPE); break;
    case PBM_WHITE: return pnm_whitexel(maxval, PBM_TYPE); break;
    default:
        assert(false);
    }
}



xel
pnm_parsecolorxel(const char * const colorName,
                  xelval       const maxval,
                  int          const format) {

    pixel const bgColor = ppm_parsecolor(colorName, maxval);

    xel retval;

    switch(PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        PNM_ASSIGN(retval,
                   PPM_GETR(bgColor), PPM_GETG(bgColor), PPM_GETB(bgColor));
        break;
    case PGM_TYPE:
        if (PPM_ISGRAY(bgColor))
            PNM_ASSIGN1(retval, PPM_GETB(bgColor));
        else
            pm_error("Non-gray color '%s' specified for a "
                     "grayscale (PGM) image",
                     colorName);
                   break;
    case PBM_TYPE:
        if (PPM_EQUAL(bgColor, ppm_whitepixel(maxval)))
            PNM_ASSIGN1(retval, maxval);
        else if (PPM_EQUAL(bgColor, ppm_blackpixel()))
            PNM_ASSIGN1(retval, 0);
        else
            pm_error ("Color '%s', which is neither black nor white, "
                      "specified for a black and white (PBM) image",
                      colorName);
        break;
    default:
        pm_error("Invalid format code %d passed to pnm_parsecolorxel()",
                 format);
    }

    return retval;
}