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
|
/*
* This is derived from the file of the same name dated June 5, 1995,
* copied from the Army High Performance Computing Research Center's
* media-tools.tar.gz package, received from
* http://www.arc.umn.edu/gvl-software/media-tools.tar.gz on 2000.04.13.
*
* This software is copyrighted as noted below. It may be freely copied,
* modified, and redistributed, provided that the copyright notice is
* preserved on all copies.
*
* There is no warranty or other guarantee of fitness for this software,
* it is provided solely "as is". Bug reports or fixes may be sent
* to the author, who may or may not act on them as he desires.
*
* You may not include this software in a program or other software product
* without supplying the source, or without informing the end-user that the
* source is available for no extra charge.
*
* If you modify this software, you should include a notice giving the
* name of the person performing the modification, the date of modification,
* and the reason for such modification.
*/
/*
* pnmtorle - A program which will convert pbmplus (ppm or pgm) images
* to Utah's "rle" image format.
*
* Author: Wes Barris (wes@msc.edu)
* AHPCRC
* Minnesota Supercomputer Center, Inc.
* Date: March 30, 1994
* Copyright (c) Minnesota Supercomputer Center, Inc.
*
* 2000.04.13 adapted for Netpbm by Bryan Henderson. Quieted compiler
* warnings.
*
* 2022.03.06 revision by Akira F Urushibata
* use shhopt instead of scanargs
* proper handling of multiple image files with -h
*
*/
/*-----------------------------------------------------
* System includes.
*/
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "pnm.h"
#include "mallocvar.h"
#include "rle.h"
#include "shhopt.h"
#include "pm_c_util.h"
struct CmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
const char * inFileName;
const char * outfile;
unsigned int verbose;
unsigned int header;
unsigned int alpha;
};
static void
parseCommandLine(int argc, const char ** argv,
struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
Note that the file spec array we return is stored in the storage that
was passed to us as the argv array.
-----------------------------------------------------------------------------*/
optEntry * option_def;
/* Instructions to pm_optParseOptions3 on how to parse our options. */
optStruct3 opt;
unsigned int option_def_index;
unsigned int outfileSpec;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENTRY */
OPTENT3(0, "alpha", OPT_FLAG, NULL, &cmdlineP->alpha, 0);
OPTENT3(0, "header", OPT_FLAG, NULL, &cmdlineP->header, 0);
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
OPTENT3(0, "outfile", OPT_STRING, &cmdlineP->outfile,
&outfileSpec, 0);
opt.opt_table = option_def;
opt.short_allowed = false; /* We have no short (old-fashioned) options */
opt.allowNegNum = false; /* We have no parms that are negative numbers */
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
free(option_def);
if (argc-1 == 0)
cmdlineP->inFileName = "-";
else if (argc-1 != 1) {
pm_error("Program takes zero or one argument (filename). You "
"specified %d", argc-1);
}
else
cmdlineP->inFileName = argv[1];
if (!outfileSpec)
cmdlineP->outfile = "-";
}
static void
readPnmHeader(bool const verbose,
bool const wantAlpha,
FILE * const ifP,
int * const widthP,
int * const heightP,
gray * const maxvalP,
int * const formatP) {
/*-----------------------------------------------------------------------------
Read the pnm image file header.
---------------------------------------------------------------------------- */
int width;
int height;
gray maxval;
int format;
const char * type;
pnm_readpnminit(ifP, &width, &height, &maxval, &format);
switch (format) {
case PBM_FORMAT:
type="plain pbm";
break;
case RPBM_FORMAT:
type="raw pbm";
break;
case PGM_FORMAT:
type="plain pgm";
break;
case RPGM_FORMAT:
type="raw pgm";
break;
case PPM_FORMAT:
type="plain ppm";
break;
case RPPM_FORMAT:
type="raw ppm";
break;
}
if (verbose) {
pm_message("Image type: %s format", type);
pm_message("Full image: %dx%d", width, height);
pm_message("Maxval: %d", maxval);
if (wantAlpha)
pm_message("Computing alpha channel...");
}
*widthP = width;
*heightP = height;
*maxvalP = maxval;
*formatP = format;
}
static void
writeRleHeader(bool const wantAlpha,
int const format,
unsigned int const width,
unsigned int const height,
rle_hdr * const hdrP) {
rle_hdr hdr;
hdr = *hdrP; /* initial value */
hdr.xmin = 0;
hdr.xmax = width-1;
hdr.ymin = 0;
hdr.ymax = height-1;
hdr.background = 0;
switch (format) {
case PBM_FORMAT:
case RPBM_FORMAT:
case PGM_FORMAT:
case RPGM_FORMAT:
hdr.ncolors = 1;
RLE_SET_BIT(hdr, RLE_RED);
break;
case PPM_FORMAT:
case RPPM_FORMAT:
hdr.ncolors = 3;
RLE_SET_BIT(hdr, RLE_RED);
RLE_SET_BIT(hdr, RLE_GREEN);
RLE_SET_BIT(hdr, RLE_BLUE);
break;
}
if (wantAlpha) {
hdr.alpha = 1;
RLE_SET_BIT(hdr, RLE_ALPHA);
}
rle_put_setup(&hdr);
*hdrP = hdr;
}
static void
writeRleData(bool const verbose,
bool const wantAlpha,
FILE * const ifP,
rle_hdr * const hdrP,
unsigned int const width,
unsigned int const height,
gray const maxval,
int const format) {
unsigned int scan;
xel * xelrow;
rle_pixel *** scanlines;
MALLOCARRAY(xelrow, width);
if (xelrow == NULL)
pm_error("Failed to allocate memory for row of %u pixels", width);
MALLOCARRAY(scanlines, height);
if (scanlines == NULL)
pm_error("Failed to allocate memory for %u scanline pointers", height);
for (scan = 0; scan < height; ++scan) {
int rc;
rc = rle_row_alloc(hdrP, &scanlines[scan]);
if (rc < 0)
pm_error("Failed to allocate memory for a scanline");
}
/* Loop through the pnm files image window, read data and flip vertically.
*/
switch (format) {
case PBM_FORMAT:
case RPBM_FORMAT: {
unsigned int scan;
for (scan = 0; scan < height; ++scan) {
rle_pixel ** const scanline = scanlines[height - scan - 1];
unsigned int col;
pnm_readpnmrow(ifP, xelrow, width, maxval, format);
for (col = 0; col < width; ++col) {
scanline[RLE_RED][col] = PNM_GET1(xelrow[col]) ? 255 : 0;
if (wantAlpha)
scanline[RLE_ALPHA][col] = scanline[RLE_RED][col];
}
}
} break;
case PGM_FORMAT:
case RPGM_FORMAT: {
unsigned int scan;
for (scan = 0; scan < height; ++scan) {
rle_pixel ** const scanline = scanlines[height - scan - 1];
unsigned int col;
pnm_readpnmrow(ifP, xelrow, width, maxval, format);
for (col = 0; col < width; ++col) {
scanline[RLE_RED][col] = PNM_GET1(xelrow[col]);
if (wantAlpha)
scanline[RLE_ALPHA][col] =
scanline[RLE_RED][col] ? 255 : 0;
}
}
} break;
case PPM_FORMAT:
case RPPM_FORMAT: {
unsigned int scan;
for (scan = 0; scan < height; scan++) {
rle_pixel ** const scanline = scanlines[height - scan - 1];
unsigned int col;
pnm_readpnmrow(ifP, xelrow, width, maxval, format);
for (col = 0; col < width; ++col) {
scanline[RLE_RED][col] = PPM_GETR(xelrow[col]);
scanline[RLE_GREEN][col] = PPM_GETG(xelrow[col]);
scanline[RLE_BLUE][col] = PPM_GETB(xelrow[col]);
if (wantAlpha)
scanline[RLE_ALPHA][col] =
(scanline[RLE_RED][col] ||
scanline[RLE_GREEN][col] ||
scanline[RLE_BLUE][col] ? 255 : 0);
}
}
} break;
}
/* Write out data in URT order (bottom to top). */
for (scan = 0; scan < height; ++scan)
rle_putrow(scanlines[scan], width, hdrP);
for (scan = 0; scan < height; ++scan)
rle_row_free(hdrP, scanlines[scan]);
free(scanlines);
free(xelrow);
if (verbose)
pm_message("Done -- write eof to RLE data.");
rle_puteof(hdrP);
}
static void
skipData(FILE * const ifP,
unsigned int const width,
unsigned int const height,
gray const maxval,
int const format) {
xel * xelrow;
unsigned int scan;
MALLOCARRAY(xelrow, width);
if (xelrow == NULL)
pm_error("Failed to allocate memory for row of %u pixels", width);
for (scan=0; scan < height; ++scan)
pnm_readpnmrow(ifP, xelrow, width, maxval, format);
free(xelrow);
}
int
main(int argc, char ** argv) {
struct CmdlineInfo cmdline;
FILE * ifP;
rle_hdr hdr;
int format;
int width, height;
gray maxval;
bool verbose;
const char ** argvWork;
unsigned int i;
int eof;
MALLOCARRAY_NOFAIL(argvWork, argc + 1);
for (i = 0; i < argc; ++i) /* Make a copy of argv */
argvWork[i] = argv[i];
pm_proginit(&argc, argvWork);
parseCommandLine(argc, argvWork, &cmdline);
verbose = cmdline.verbose || cmdline.header;
hdr = *rle_hdr_init(NULL);
rle_names(&hdr, "pnmtorle", cmdline.outfile, 0);
/* Open the file. */
assert(cmdline.inFileName != NULL);
ifP = pm_openr(cmdline.inFileName);
hdr.rle_file = rle_open_f(hdr.cmd, cmdline.outfile, "wb");
for (eof = 0; !eof; ) {
readPnmHeader(verbose, cmdline.alpha, ifP,
&width, &height, &maxval, &format);
if (cmdline.header) {
skipData(ifP, width, height, maxval, format);
} else {
rle_addhist(argv, NULL, &hdr);
writeRleHeader(cmdline.alpha, format, width, height, &hdr);
writeRleData(verbose, cmdline.alpha, ifP, &hdr,
width, height, maxval, format);
}
pnm_nextimage(ifP, &eof);
}
pm_close(ifP);
return 0;
}
|