Debian Codegraph @

converter/ppm/pjtoppm.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* pjtoppm.c - convert an HP PainJetXL image to a PPM
2 **
3 ** Copyright (C) 1990 by Christos Zoulas ([email protected])
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 #include <stdbool.h>
14 #include <assert.h>
15
16 #include "ppm.h"
17 #include "pm_c_util.h"
18 #include "mallocvar.h"
19
20
21
22 static unsigned int
23 uintProduct(unsigned int const multiplicand,
24 unsigned int const multiplier) {
25
26 if (UINT_MAX / multiplier < multiplicand)
27 pm_error("Airthmetic overflow");
28
29 return multiplicand * multiplier;
30 }
31
32
33
34 static int
35 egetc(FILE * const ifP) {
36 int c;
37
38 c = fgetc(ifP);
39
40 if (c == -1)
41 pm_error("unexpected end of file");
42
43 return c;
44 }
45
46
47
48 static void
49 modifyImageMode1(unsigned int const rows,
50 unsigned int const planes,
51 const int * const imlen,
52 unsigned char ** const image,
53 unsigned int * const colsP) {
54
55 unsigned int const newcols = 10240;
56 /* It could not be larger than that! */
57
58 unsigned int cols;
59 unsigned int row;
60
61 for (row = 0, cols = 0; row < rows; ++row) {
62 unsigned int plane;
63 if (image[row * planes]) {
64 for (plane = 0; plane < planes; ++plane) {
65 unsigned int i;
66 unsigned int col;
67 unsigned char * buf;
68
69 MALLOCARRAY(buf, newcols);
70 if (buf == NULL)
71 pm_error("out of memory");
72 for (i = 0, col = 0;
73 col < imlen[row * planes + plane];
74 col += 2) {
75 int cmd, val;
76 for (cmd = image[row * planes + plane][col],
77 val = image[plane + row * planes][col+1];
•78 cmd >= 0 && i < newcols; --cmd, ++i) {
79 overflow_add(i, 1);
80 buf[i] = val;
81 }
82 }
83 cols = MAX(cols, i);
84 free(image[row * planes + plane]);
85
86 /*
87 * This is less than what we have so it realloc should
88 * not return null. Even if it does, tough! We will
89 * lose a line, and probably die on the next line anyway
90 */
91 image[row * planes + plane] = realloc(buf, i);
92 }
93 }
94 }
95 *colsP = cols * 8; assert(cols < UINT_MAX/8);
96 }
97
98
99
100 static void
101 writePpm(FILE * const ofP,
102 unsigned int const cols,
103 unsigned int const rows,
104 unsigned int const planes,
105 unsigned char ** const image,
106 int const mode,
107 const int * const imlen) {
108
109 pixel * pixrow;
110 unsigned int row;
111
112 ppm_writeppminit(stdout, cols, rows, 255, 0);
113 pixrow = ppm_allocrow(cols);
114
115 for (row = 0; row < rows; ++row) {
116 if (image[row * planes + 0] == NULL) {
117 unsigned int col;
118 for (col = 0; col < cols; ++col)
119 PPM_ASSIGN(pixrow[col], 0, 0, 0);
120 } else {
121 unsigned int col;
122 unsigned int cmd;
123 for (cmd = 0, col = 0; col < cols; col += 8, ++cmd) {
124 unsigned int i;
125 for (i = 0; i < 8 && col + i < cols; ++i) {
126 unsigned int plane;
127 unsigned char bf[3];
128
129 assert(planes == 3);
130
131 for (plane = 0; plane < planes; ++plane) {
132 if (mode == 0 && cmd >= imlen[row * planes + plane])
133 bf[plane] = 0;
134 else
135 bf[plane] = (image[row * planes + plane][cmd] &
136 (1 << (7 - i))) ? 255 : 0;
137 }
138 PPM_ASSIGN(pixrow[col + i], bf[0], bf[1], bf[2]);
139 }
140 }
141 ppm_writeppmrow(stdout, pixrow, cols, 255, 0);
142 }
143 }
144 }
145
146
147
148 int
149 main(int argc, const char ** argv) {
150
151 int cmd, val;
152 char buffer[BUFSIZ];
153 unsigned int planes;
154 unsigned int rows;
155 unsigned int rowsX;
156 unsigned int cols;
157 bool colsIsSet;
158 unsigned char ** image; /* malloc'ed */
159 int * imlen; /* malloc'ed */
160 FILE * ifP;
161 int mode;
162 bool modeIsSet;
163 int c;
164 unsigned int plane;
165 unsigned int row;
166
167 pm_proginit(&argc, argv);
168
169 if (argc-1 > 0)
170 ifP = pm_openr(argv[1]);
171 else
172 ifP = stdin;
173
174 if (argc-1 > 2)
175 pm_error("Too many arguments (%u). Only possible argument is "
176 "input file name", argc-1);
177
178 row = 0; /* initial value */
179 plane = 0; /* initial value */
180 modeIsSet = false; /* initial value */
181 colsIsSet = false; /* initial value */
182 rowsX = 0; /* initial value */
183 image = NULL; /* initial value */
184 imlen = NULL; /* initial value */
185 planes = 3; /* initial value */
186
187 while ((c = fgetc(ifP)) != -1) {
188 if (c != '\033')
189 continue;
190 switch (c = egetc(ifP)) {
191 case 'E': /* reset */
192 break;
193 case '*': {
194 unsigned int i;
195 cmd = egetc(ifP);
196 for (i = 0; i < BUFSIZ; i++) {
197 if (!isdigit(c = egetc(ifP)) && c != '+' && c != '-')
198 break;
199 buffer[i] = c;
200 }
201 if (i != 0) {
202 buffer[i] = '\0';
203 if (sscanf(buffer, "%d", &val) != 1)
204 pm_error("bad value `%s' at <ESC>*%c%c", buffer, cmd, c);
205 }
206 else
207 val = -1;
208 switch (cmd) {
209 case 't':
210 switch (c) {
211 case 'J': /* render */
212 break;
213 case 'K': /* back scale */
214 break;
215 case 'I': /* gamma */
216 break;
217 case 'R':
218 break; /* set resolution */
219 default:
220 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
221 break;
222 }
223 break;
224 case 'r':
225 switch (c) {
226 case 'S': /* width */
227 if (val < 0)
228 pm_error("invalid width value");
229 else {
230 cols = val;
231 colsIsSet = true;
232 }
233 break;
234 case 'T': /* height */
235 if (val < 0)
236 pm_error ("invalid height value");
237 else
238 rowsX = val;
239 break;
240 case 'U': /* planes */
241 planes = val;
242 if (planes != 3)
243 pm_error("can handle only 3 plane files");
244 break;
245 case 'A': /* begin raster */
246 break;
247 case 'B':
248 case 'C': /* end raster */
249 break;
250 case 'V':
251 break; /* set deci height */
252 case 'H':
253 break; /* set deci width */
254 default:
255 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
256 break;
257 }
258 break;
259 case 'b':
260 switch (c) {
261 case 'M': /* transmission mode */
262 if (val != 0 && val != 1)
263 pm_error("unimplemented transmission mode %d", val);
264 mode = val;
265 modeIsSet = true;
266 break;
267 case 'V': /* send plane */
268 case 'W': /* send last plane */
269 if (row >= rowsX || image == NULL) {
•270 if (row >= rowsX) {
271 overflow_add(rowsX, 100);
272 rowsX += 100;
•273 }
274 REALLOCARRAY(image, uintProduct(rowsX, planes));
275 REALLOCARRAY(imlen, uintProduct(rowsX, planes));
276 }
277 if (image == NULL || imlen == NULL)
278 pm_error("out of memory");
279 if (plane >= planes)
280 pm_error("too many planes");
281 if (!colsIsSet)
282 pm_error("missing width value");
283
284 cols = MAX(cols, val);
285 imlen[row * planes + plane] = val;
286 MALLOCARRAY(image[row * planes + plane], val);
287 if (image[row * planes + plane] == NULL)
288 pm_error("out of memory");
289 if (fread(image[row * planes + plane], 1, val, ifP) != val)
290 pm_error("short data");
291 if (c == 'V')
292 ++plane;
293 else {
294 plane = 0;
295 if (row > UINT_MAX/planes-100)
296 pm_error("Too many rows (more than %u) "
297 "for computation", row);
298 ++row;
299 }
300 break;
301 default:
302 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
303 break;
304 }
305 break;
306 case 'p': /* Position */
307 if (plane != 0)
308 pm_error("changed position in the middle of "
309 "transferring planes");
310 switch (c) {
311 case 'X':
312 pm_message("can only position in y");
313 break;
314 case 'Y':
315 if (buffer[0] == '+')
316 val = row + val;
317 if (buffer[0] == '-')
318 val = row - val;
319 for (; val > row; ++row)
320 for (plane = 0; plane < 3; ++plane) {
321 imlen[row * planes + plane] = 0;
322 image[row * planes + plane] = NULL;
323 }
324 row = val;
325 break;
326 default:
327 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
328 break;
329 }
330 default:
331 pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
332 break;
333 }
334 } /* case */
335 } /* switch */
336 }
337 pm_close(ifP);
338
339 if (!modeIsSet)
340 pm_error("Input does not contain a 'bM' transmission mode order");
341
342 rows = row;
343
344 if (mode == 1) {
345 modifyImageMode1(rows, planes, imlen, image, &cols);
346
347 }
348
349 writePpm(stdout, cols, rows, planes, image, mode, imlen);
350
351 pm_close(stdout);
352
353 return 0;
354 }
355
356
357