Debian Codegraph @

converter/other/pgmtoppm.c

raw
1 /* pgmtoppm.c - colorize a portable graymap into a portable pixmap
2 **
3 ** Copyright (C) 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
14 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
15 #define _BSD_SOURCE /* Make sure strdup() is in <string.h> */
16 #include <string.h>
17
18 #include "pm_c_util.h"
19 #include "mallocvar.h"
20 #include "nstring.h"
21 #include "shhopt.h"
22 #include "ppm.h"
23
24 struct CmdlineInfo {
25 /* All the information the user supplied in the command line,
26 in a form easy for the program to use.
27 */
28 const char * inputFilename; /* '-' if stdin */
29 const char * map;
30 const char * colorBlack; /* malloc'ed */
31 /* The color to which the user says to map black */
32 const char * colorWhite; /* malloc'ed */
33 /* The color to which the user says to map white */
34 };
35
36
37
38 static void
39 parseCommandLine(int argc, const char ** argv,
40 struct CmdlineInfo * const cmdlineP) {
41 /*----------------------------------------------------------------------------
42 parse program command line described in Unix standard form by argc
43 and argv. Return the information in the options as *cmdlineP.
44
45 If command line is internally inconsistent (invalid options, etc.),
46 issue error message to stderr and abort program.
47
48 Note that the strings we return are stored in the storage that
49 was passed to us as the argv array. We also trash *argv.
50 -----------------------------------------------------------------------------*/
51 optEntry * option_def; /* Used by OPTENT3 */
52 optStruct3 opt;
53
54 unsigned int option_def_index;
55
56 unsigned int blackSpec, whiteSpec, mapSpec;
57
58 const char * blackOpt;
59 const char * whiteOpt;
60
61 MALLOCARRAY_NOFAIL(option_def, 100);
62
63 option_def_index = 0; /* incremented by OPTENT3 */
64 OPTENT3(0, "black", OPT_STRING, &blackOpt,
65 &blackSpec, 0);
66 OPTENT3(0, "white", OPT_STRING, &whiteOpt,
67 &whiteSpec, 0);
68 OPTENT3(0, "map", OPT_STRING, &cmdlineP->map,
69 &mapSpec, 0);
70
71 opt.opt_table = option_def;
72 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
73 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
74
75 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
76
77 if (!mapSpec)
78 cmdlineP->map = NULL;
79
80 if (mapSpec) {
81 if (blackSpec || whiteSpec)
82 pm_error("You may not specify -black or -white "
83 "together with -map");
84
85 cmdlineP->colorBlack = NULL;
86 cmdlineP->colorWhite = NULL;
87
88 /* No color argument; only argument is file name */
89 if (argc-1 < 1)
90 cmdlineP->inputFilename = "-";
91 else {
92 cmdlineP->inputFilename = argv[1];
93 if (argc-1 > 1)
94 pm_error("With -map option, there is at most one argument: "
95 "the file name. You specified %u", argc-1);
96 }
97 } else {
98 /* For default colors, we use "rgbi:..." instead of the simpler
99 "black" and "white" so that we don't have an unnecessary dependency
100 on a color dictionary being available.
101 */
102 if (blackSpec || whiteSpec) {
103 cmdlineP->colorBlack =
104 pm_strdup(blackSpec ? blackOpt : "rgbi:0/0/0");
105 cmdlineP->colorWhite =
106 pm_strdup(whiteSpec ? whiteOpt : "rgbi:1/1/1");
107
108 /* The only possibly argument is input file name */
109 if (argc-1 < 1)
110 cmdlineP->inputFilename = "-";
111 else {
112 cmdlineP->inputFilename = argv[1];
113 if (argc-1 > 1)
114 pm_error("Whten you specify -black or -white, "
115 "there can be at most one non-option arguments: "
116 "the file name. "
117 "You specified %u", argc-1);
118 }
119 } else {
120 /* Arguments are color or color range and optional file name */
121
122 if (argc-1 < 1) {
123 cmdlineP->colorBlack = pm_strdup("rgbi:0/0/0");
124 cmdlineP->colorWhite = pm_strdup("rgbi:1/1/1");
125 } else {
126 char * buffer = strdup(argv[1]);
127 if (!buffer)
128 pm_error("Out of memory allocating tiny buffer");
129 char * hyphenPos = strchr(buffer, '-');
130 if (hyphenPos) {
131 *hyphenPos = '\0';
132 cmdlineP->colorBlack = pm_strdup(buffer);
133 cmdlineP->colorWhite = pm_strdup(hyphenPos+1);
134 } else {
135 cmdlineP->colorBlack = pm_strdup("rgbi:0/0/0");
136 cmdlineP->colorWhite = pm_strdup(buffer);
137 }
138 free(buffer);
139 }
140
141 if (argc-1 < 2)
142 cmdlineP->inputFilename = "-";
143 else {
144 cmdlineP->inputFilename = argv[2];
145
146 if (argc-1 > 2)
147 pm_error("Program takes at most 2 arguments: "
148 "color name/range and input file name. "
149 "You specified %u", argc-1);
150 }
151 }
152 }
153 }
154
155
156
157 static void
158 freeCommandLine(struct CmdlineInfo const cmdline) {
159
160 if (cmdline.colorBlack)
161 pm_strfree(cmdline.colorBlack);
162 if (cmdline.colorWhite)
163 pm_strfree(cmdline.colorWhite);
164 }
165
166
167
168 static void
169 convertWithMap(FILE * const ifP,
170 unsigned int const cols,
171 unsigned int const rows,
172 gray const maxval,
173 int const format,
174 const char * const mapFileName,
175 FILE * const ofP,
176 gray * const grayrow,
177 pixel * const pixelrow) {
178
179 unsigned int row;
180 FILE * mapFileP;
181 int mapcols, maprows;
182 pixval mapmaxval;
183 pixel ** mappixels;
184 unsigned int mapmaxcolor;
185
186 mapFileP = pm_openr(mapFileName);
187 mappixels = ppm_readppm(mapFileP, &mapcols, &maprows, &mapmaxval);
188 pm_close(mapFileP);
189 mapmaxcolor = maprows * mapcols - 1;
190
191 ppm_writeppminit(ofP, cols, rows, mapmaxval, 0);
192
193 for (row = 0; row < rows; ++row) {
194 unsigned int col;
195
196 pgm_readpgmrow(ifP, grayrow, cols, maxval, format);
197
198 for (col = 0; col < cols; ++col) {
199 unsigned int c;
200 if (maxval == mapmaxcolor)
201 c = grayrow[col];
202 else
203 c = grayrow[col] * mapmaxcolor / maxval;
204 pixelrow[col] = mappixels[c / mapcols][c % mapcols];
205 }
206 ppm_writeppmrow(ofP, pixelrow, cols, mapmaxval, 0);
207 }
208 ppm_freearray(mappixels, maprows);
209 }
210
211
212
213 static void
214 convertLinear(FILE * const ifP,
215 unsigned int const cols,
216 unsigned int const rows,
217 gray const maxval,
218 int const format,
219 const char * const colorNameBlack,
220 const char * const colorNameWhite,
221 FILE * const ofP,
222 gray * const grayrow,
223 pixel * const pixelrow) {
224
225 pixel const colorBlack = ppm_parsecolor(colorNameBlack, maxval);
226 pixel const colorWhite = ppm_parsecolor(colorNameWhite, maxval);
227
228 pixval const red0 = PPM_GETR(colorBlack);
229 pixval const grn0 = PPM_GETG(colorBlack);
230 pixval const blu0 = PPM_GETB(colorBlack);
231 pixval const red1 = PPM_GETR(colorWhite);
232 pixval const grn1 = PPM_GETG(colorWhite);
233 pixval const blu1 = PPM_GETB(colorWhite);
234
235 unsigned int row;
236
237 ppm_writeppminit(ofP, cols, rows, maxval, 0);
238
239 for (row = 0; row < rows; ++row) {
240 unsigned int col;
241
242 pgm_readpgmrow(ifP, grayrow, cols, maxval, format);
243
244 for (col = 0; col < cols; ++col) {
245 gray const input = grayrow[col];
246 PPM_ASSIGN(
247 pixelrow[col],
248 (red0 * (maxval - input) + red1 * input) / maxval,
249 (grn0 * (maxval - input) + grn1 * input) / maxval,
250 (blu0 * (maxval - input) + blu1 * input) / maxval);
251 }
252 ppm_writeppmrow(ofP, pixelrow, cols, maxval, 0);
253 }
254 }
255
256
257
258 int
259 main(int argc,
260 const char * argv[]) {
261
262 FILE * ifP;
263 struct CmdlineInfo cmdline;
264 gray * grayrow;
265 pixel * pixelrow;
266 int rows, cols, format;
267 gray maxval;
268
269 pm_proginit(&argc, argv);
270
271 parseCommandLine(argc, argv, &cmdline);
272
273 ifP = pm_openr(cmdline.inputFilename);
274
275 pgm_readpgminit(ifP, &cols, &rows, &maxval, &format);
276 grayrow = pgm_allocrow(cols);
277 pixelrow = ppm_allocrow(cols);
278
279 if (cmdline.map)
280 convertWithMap(ifP, cols, rows, maxval, format, cmdline.map,
281 stdout, grayrow, pixelrow);
282 else
283 convertLinear(ifP, cols, rows, maxval, format,
284 cmdline.colorBlack, cmdline.colorWhite, stdout,
285 grayrow, pixelrow);
286
287 ppm_freerow(pixelrow);
288 pgm_freerow(grayrow);
289 pm_close(ifP);
290
291 freeCommandLine(cmdline);
292
293 /* If the program failed, it previously aborted with nonzero completion
294 code, via various function calls.
295 */
296 return 0;
297 }
298
299
300