Debian Codegraph @

converter/ppm/pjtoppm.c

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