Debian Codegraph @

converter/other/pnmtorle.c

raw
1 /*
2 * This is derived from the file of the same name dated June 5, 1995,
3 * copied from the Army High Performance Computing Research Center's
4 * media-tools.tar.gz package, received from
5 * http://www.arc.umn.edu/gvl-software/media-tools.tar.gz on 2000.04.13.
6 *
7 * This software is copyrighted as noted below. It may be freely copied,
8 * modified, and redistributed, provided that the copyright notice is
9 * preserved on all copies.
10 *
11 * There is no warranty or other guarantee of fitness for this software,
12 * it is provided solely "as is". Bug reports or fixes may be sent
13 * to the author, who may or may not act on them as he desires.
14 *
15 * You may not include this software in a program or other software product
16 * without supplying the source, or without informing the end-user that the
17 * source is available for no extra charge.
18 *
19 * If you modify this software, you should include a notice giving the
20 * name of the person performing the modification, the date of modification,
21 * and the reason for such modification.
22 */
23 /*
24 * pnmtorle - A program which will convert pbmplus (ppm or pgm) images
25 * to Utah's "rle" image format.
26 *
27 * Author: Wes Barris ([email protected])
28 * AHPCRC
29 * Minnesota Supercomputer Center, Inc.
30 * Date: March 30, 1994
31 * Copyright (c) Minnesota Supercomputer Center, Inc.
32 *
33 * 2000.04.13 adapted for Netpbm by Bryan Henderson. Quieted compiler
34 * warnings.
35 *
36 * 2022.03.06 revision by Akira F Urushibata
37 * use shhopt instead of scanargs
38 * proper handling of multiple image files with -h
39 *
40 */
41 /*-----------------------------------------------------
42 * System includes.
43 */
44 #include <string.h>
45 #include <stdio.h>
46 #include <assert.h>
47 #include "pnm.h"
48 #include "mallocvar.h"
49 #include "rle.h"
50 #include "shhopt.h"
51 #include "pm_c_util.h"
52
53
54 struct CmdlineInfo {
55 /* All the information the user supplied in the command line,
56 in a form easy for the program to use.
57 */
58 const char * inFileName;
59 const char * outfile;
60 unsigned int verbose;
61 unsigned int header;
62 unsigned int alpha;
63 };
64
65
66
67 static void
68 parseCommandLine(int argc, const char ** argv,
69 struct CmdlineInfo * const cmdlineP) {
70 /*----------------------------------------------------------------------------
71 Note that the file spec array we return is stored in the storage that
72 was passed to us as the argv array.
73 -----------------------------------------------------------------------------*/
74 optEntry * option_def; /* Used by OPTENT3 */
75 optStruct3 opt;
76
77 unsigned int option_def_index;
78 unsigned int outfileSpec;
79
80 MALLOCARRAY_NOFAIL(option_def, 100);
81
82 option_def_index = 0; /* incremented by OPTENTRY */
83 OPTENT3(0, "alpha", OPT_FLAG, NULL, &cmdlineP->alpha, 0);
84 OPTENT3(0, "header", OPT_FLAG, NULL, &cmdlineP->header, 0);
85 OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
86 OPTENT3(0, "outfile", OPT_STRING, &cmdlineP->outfile,
87 &outfileSpec, 0);
88
89 opt.opt_table = option_def;
90 opt.short_allowed = false; /* We have no short (old-fashioned) options */
91 opt.allowNegNum = false; /* We have no parms that are negative numbers */
92
93 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
94 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
95
96 free(option_def);
97
98 if (argc-1 == 0)
99 cmdlineP->inFileName = "-";
100 else if (argc-1 != 1) {
101 pm_error("Program takes zero or one argument (filename). You "
102 "specified %d", argc-1);
103 }
104 else
105 cmdlineP->inFileName = argv[1];
106
107 if (!outfileSpec)
108 cmdlineP->outfile = "-";
109 }
110
111
112
113 static void
114 readPnmHeader(bool const verbose,
115 bool const wantAlpha,
116 FILE * const ifP,
117 int * const widthP,
118 int * const heightP,
119 gray * const maxvalP,
120 int * const formatP) {
121 /*-----------------------------------------------------------------------------
122 Read the pnm image file header.
123 ---------------------------------------------------------------------------- */
124 int width;
125 int height;
126 gray maxval;
127 int format;
128 const char * type;
129
130 pnm_readpnminit(ifP, &width, &height, &maxval, &format);
131
132 switch (format) {
133 case PBM_FORMAT:
134 type="plain pbm";
135 break;
136 case RPBM_FORMAT:
137 type="raw pbm";
138 break;
139 case PGM_FORMAT:
140 type="plain pgm";
141 break;
142 case RPGM_FORMAT:
143 type="raw pgm";
144 break;
145 case PPM_FORMAT:
146 type="plain ppm";
147 break;
148 case RPPM_FORMAT:
149 type="raw ppm";
150 break;
151 }
152 if (verbose) {
153 pm_message("Image type: %s format", type);
154 pm_message("Full image: %dx%d", width, height);
155 pm_message("Maxval: %d", maxval);
156
157 if (wantAlpha)
158 pm_message("Computing alpha channel...");
159 }
160 *widthP = width;
161 *heightP = height;
162 *maxvalP = maxval;
163 *formatP = format;
164 }
165
166
167
168 static void
169 writeRleHeader(bool const wantAlpha,
170 int const format,
171 unsigned int const width,
172 unsigned int const height,
173 rle_hdr * const hdrP) {
174
175 rle_hdr hdr;
176
177 hdr = *hdrP; /* initial value */
178
179 hdr.xmin = 0;
180 hdr.xmax = width-1;
181 hdr.ymin = 0;
182 hdr.ymax = height-1;
183 hdr.background = 0;
184
185 switch (format) {
186 case PBM_FORMAT:
187 case RPBM_FORMAT:
188 case PGM_FORMAT:
189 case RPGM_FORMAT:
190 hdr.ncolors = 1;
191 RLE_SET_BIT(hdr, RLE_RED);
192 break;
193 case PPM_FORMAT:
194 case RPPM_FORMAT:
195 hdr.ncolors = 3;
196 RLE_SET_BIT(hdr, RLE_RED);
197 RLE_SET_BIT(hdr, RLE_GREEN);
198 RLE_SET_BIT(hdr, RLE_BLUE);
199 break;
200 }
201 if (wantAlpha) {
202 hdr.alpha = 1;
203 RLE_SET_BIT(hdr, RLE_ALPHA);
204 }
205 rle_put_setup(&hdr);
206
207 *hdrP = hdr;
208 }
209
210
211
212 static void
213 writeRleData(bool const verbose,
214 bool const wantAlpha,
215 FILE * const ifP,
216 rle_hdr * const hdrP,
217 unsigned int const width,
218 unsigned int const height,
219 gray const maxval,
220 int const format) {
221
222 unsigned int scan;
223 xel * xelrow;
224 rle_pixel *** scanlines;
225
226 MALLOCARRAY(xelrow, width);
227 if (xelrow == NULL)
228 pm_error("Failed to allocate memory for row of %u pixels", width);
229
230 MALLOCARRAY(scanlines, height);
231 if (scanlines == NULL)
232 pm_error("Failed to allocate memory for %u scanline pointers", height);
233
234 for (scan = 0; scan < height; ++scan) {
235 int rc;
236 rc = rle_row_alloc(hdrP, &scanlines[scan]);
237 if (rc < 0)
238 pm_error("Failed to allocate memory for a scanline");
239 }
240 /* Loop through the pnm files image window, read data and flip vertically.
241 */
242 switch (format) {
243 case PBM_FORMAT:
244 case RPBM_FORMAT: {
245 unsigned int scan;
246 for (scan = 0; scan < height; ++scan) {
247 rle_pixel ** const scanline = scanlines[height - scan - 1];
248 unsigned int col;
249 pnm_readpnmrow(ifP, xelrow, width, maxval, format);
250 for (col = 0; col < width; ++col) {
251 scanline[RLE_RED][col] = PNM_GET1(xelrow[col]) ? 255 : 0;
252 if (wantAlpha)
253 scanline[RLE_ALPHA][col] = scanline[RLE_RED][col];
254 }
255 }
256 } break;
257 case PGM_FORMAT:
258 case RPGM_FORMAT: {
259 unsigned int scan;
260 for (scan = 0; scan < height; ++scan) {
261 rle_pixel ** const scanline = scanlines[height - scan - 1];
262 unsigned int col;
263 pnm_readpnmrow(ifP, xelrow, width, maxval, format);
264 for (col = 0; col < width; ++col) {
265 scanline[RLE_RED][col] = PNM_GET1(xelrow[col]);
266 if (wantAlpha)
267 scanline[RLE_ALPHA][col] =
268 scanline[RLE_RED][col] ? 255 : 0;
269 }
270 }
271 } break;
272 case PPM_FORMAT:
273 case RPPM_FORMAT: {
274 unsigned int scan;
275 for (scan = 0; scan < height; scan++) {
276 rle_pixel ** const scanline = scanlines[height - scan - 1];
277
278 unsigned int col;
279
280 pnm_readpnmrow(ifP, xelrow, width, maxval, format);
281
282 for (col = 0; col < width; ++col) {
283 scanline[RLE_RED][col] = PPM_GETR(xelrow[col]);
284 scanline[RLE_GREEN][col] = PPM_GETG(xelrow[col]);
285 scanline[RLE_BLUE][col] = PPM_GETB(xelrow[col]);
286 if (wantAlpha)
287 scanline[RLE_ALPHA][col] =
288 (scanline[RLE_RED][col] ||
289 scanline[RLE_GREEN][col] ||
290 scanline[RLE_BLUE][col] ? 255 : 0);
291 }
292 }
293 } break;
294 }
295 /* Write out data in URT order (bottom to top). */
296 for (scan = 0; scan < height; ++scan)
297 rle_putrow(scanlines[scan], width, hdrP);
298
299 for (scan = 0; scan < height; ++scan)
300 rle_row_free(hdrP, scanlines[scan]);
301 free(scanlines);
302 free(xelrow);
303
304 if (verbose)
305 pm_message("Done -- write eof to RLE data.");
306
307 rle_puteof(hdrP);
308 }
309
310
311
312 static void
313 skipData(FILE * const ifP,
314 unsigned int const width,
315 unsigned int const height,
316 gray const maxval,
317 int const format) {
318
319 xel * xelrow;
320 unsigned int scan;
321
322 MALLOCARRAY(xelrow, width);
323 if (xelrow == NULL)
324 pm_error("Failed to allocate memory for row of %u pixels", width);
325
326 for (scan=0; scan < height; ++scan)
327 pnm_readpnmrow(ifP, xelrow, width, maxval, format);
328
329 free(xelrow);
330 }
331
332
333
334 int
335 main(int argc, const char ** argv) {
336
337 struct CmdlineInfo cmdline;
338
339 FILE * ifP;
340 rle_hdr hdr;
341 int format;
342 int width, height;
343 gray maxval;
344 bool verbose;
345 const char ** argvWork;
346 unsigned int i;
347 int eof;
348
349 MALLOCARRAY_NOFAIL(argvWork, argc + 1);
350
351 for (i = 0; i < argc; ++i) /* Make a copy of argv */
352 argvWork[i] = argv[i];
353
354 pm_proginit(&argc, argvWork);
355
356 parseCommandLine(argc, argvWork, &cmdline);
357
358 verbose = cmdline.verbose || cmdline.header;
359
360 hdr = *rle_hdr_init(NULL);
361
362 rle_names(&hdr, "pnmtorle", cmdline.outfile, 0);
363
364 /* Open the file. */
365 assert(cmdline.inFileName != NULL);
366 ifP = pm_openr(cmdline.inFileName);
367
368 hdr.rle_file = rle_open_f(hdr.cmd, cmdline.outfile, "wb");
369
370 for (eof = 0; !eof; ) {
371 readPnmHeader(verbose, cmdline.alpha, ifP,
372 &width, &height, &maxval, &format);
373
374 if (cmdline.header) {
375 skipData(ifP, width, height, maxval, format);
376 } else {
377 rle_addhist(argv, NULL, &hdr);
378 writeRleHeader(cmdline.alpha, format, width, height, &hdr);
379 writeRleData(verbose, cmdline.alpha, ifP, &hdr,
380 width, height, maxval, format);
381 }
382 pnm_nextimage(ifP, &eof);
383 }
384
385 pm_close(ifP);
386
387 return 0;
388 }
389
390
391