about summary refs log tree commit diff
path: root/converter/ppm/ppmtorgb3.c
blob: 3d2f1c21923d92790b01db8a93a2109977eb8b67 (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
/* ppmtorgb3.c - separate a PPM image into 3 PGM images.
**
** Copyright (C) 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 <string.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "ppm.h"
#include "pgm.h"



struct Cmdline {
    const char * inputFileName;
};



static const char *
strippedOfExtension(const char * const arg) {

    char * buffer;

    MALLOCARRAY(buffer, strlen(arg) + 1);

    strcpy(buffer, arg);

    {
        char * const dotPos = strrchr(buffer, '.');
        if (dotPos)
            *dotPos = '\0';
    }
    return buffer;
}



static void
openComponentOut(const char * const suffix,
                 const char * const baseName,
                 FILE **      const fpP) {

    const char * fileName;

    pm_asprintf(&fileName, "%s.%s", baseName, suffix);

    *fpP = pm_openw(fileName);

    pm_strfree(fileName);
}



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

    FILE * ifP;
    FILE * redfP;
    FILE * grnfP;
    FILE * blufP;
    pixel * pixelrow;
    gray * grayrow;
    int rows, cols;
    int format;
    unsigned int row;
    pixval maxval;
    struct Cmdline cmdline;
    const char * baseFileName;

    pm_proginit(&argc, argv);

    if (argc-1 > 1)
        pm_error("Too many arguments (%u).  The only possible argument "
                 "is the input file name", argc-1);

    if (argc-1 < 1)
        cmdline.inputFileName = "-";
    else
        cmdline.inputFileName = argv[1];

    ifP = pm_openr(cmdline.inputFileName);

    if (streq(cmdline.inputFileName, "-"))
        baseFileName = strdup("noname");
    else
        baseFileName = strippedOfExtension(cmdline.inputFileName);

    ppm_readppminit(ifP, &cols, &rows, &maxval, &format);

    pixelrow = ppm_allocrow(cols);

    openComponentOut("red", baseFileName, &redfP);
    openComponentOut("grn", baseFileName, &grnfP);
    openComponentOut("blu", baseFileName, &blufP);

    pgm_writepgminit(redfP, cols, rows, maxval, 0);
    pgm_writepgminit(grnfP, cols, rows, maxval, 0);
    pgm_writepgminit(blufP, cols, rows, maxval, 0);

    grayrow = pgm_allocrow(cols);

    for (row = 0; row < rows; ++row) {
        unsigned int col;

        ppm_readppmrow(ifP, pixelrow, cols, maxval, format);

        for (col = 0; col < cols; ++col)
            grayrow[col] = PPM_GETR(pixelrow[col]);

        pgm_writepgmrow(redfP, grayrow, cols, maxval, 0);

        for (col = 0; col < cols; ++col)
            grayrow[col] = PPM_GETG(pixelrow[col]);

        pgm_writepgmrow(grnfP, grayrow, cols, maxval, 0);

        for (col = 0; col < cols; ++col)
            grayrow[col] = PPM_GETB(pixelrow[col]);

        pgm_writepgmrow(blufP, grayrow, cols, maxval, 0);
    }

    pgm_freerow(grayrow);
    ppm_freerow(pixelrow);
    pm_strfree(baseFileName);
    pm_close(ifP);
    pm_close(blufP);
    pm_close(grnfP);
    pm_close(redfP);

    return 0;
}