about summary refs log tree commit diff
path: root/converter/other/ppmtopgm.c
blob: 40df422e4a2784ea1d1d0f8235fd670b40b98633 (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
/* ppmtopgm.c - convert a portable pixmap to a portable graymap
**
** Copyright (C) 1989 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 "pm_c_util.h"
#include "ppm.h"
#include "pgm.h"
#include "lum.h"

static void
convertRaster(FILE *       const ifP,
              unsigned int const cols,
              unsigned int const rows,
              pixval       const maxval,
              int          const format,
              pixel *      const inputRow,
              gray *       const outputRow,
              FILE *       const ofP) {

    unsigned int row;

    for (row = 0; row < rows; ++row) {
        ppm_readppmrow( ifP, inputRow, cols, maxval, format );
        if (maxval <= 255) {
            /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b */
            unsigned int col;
            for (col = 0; col < cols; ++col)
                outputRow[col] = (gray) ppm_fastlumin(inputRow[col]);
        } else {
            /* Can't use fast approximation, so fall back on floats. */
            unsigned int col;
            for (col = 0; col < cols; ++col)
                outputRow[col] = ppm_luminosity(inputRow[col]);
        }
        pgm_writepgmrow(ofP, outputRow, cols, maxval, 0);
    }
}



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

    FILE* ifP;
    const char * inputFilespec;
    int eof;

    ppm_init( &argc, argv );

    if (argc-1 > 1)
        pm_error("The only argument is the (optional) input filename");

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

    ifP = pm_openr(inputFilespec);

    eof = FALSE;  /* initial assumption */

    while (!eof) {
        ppm_nextimage(ifP, &eof);
        if (!eof) {
            int rows, cols, format;
            pixval maxval;
            pixel* inputRow;
            gray* outputRow;

            ppm_readppminit(ifP, &cols, &rows, &maxval, &format);
            pgm_writepgminit(stdout, cols, rows, maxval, 0);

            inputRow = ppm_allocrow(cols);
            outputRow = pgm_allocrow(cols);

            convertRaster(ifP, cols, rows, maxval, format,
                          inputRow, outputRow, stdout);

            ppm_freerow(inputRow);
            pgm_freerow(outputRow);
        }
    }
    pm_close(ifP);
    pm_close(stdout);

    return 0;
}