Debian Codegraph @

converter/other/pnmtoxwd.c

raw
1 /* pnmtoxwd.c - read a portable anymap and produce a color X11 window dump
2 **
3 ** Copyright (C) 1989, 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 #include <string.h>
14
15 #include "pm_c_util.h"
16 #include "pnm.h"
17 #include "shhopt.h"
18 #include "mallocvar.h"
19 #include "nstring.h"
20 #include "x11wd.h"
21
22
23 struct CmdlineInfo {
24 /* All the information the user supplied in the command line,
25 in a form easy for the program to use.
26 */
27 const char * inputFilename; /* Filename of input file */
28 unsigned int pseudodepth;
29 unsigned int directcolor;
30 };
31
32
33
34 static void
35 parseCommandLine(int argc,
36 char ** argv,
37 struct CmdlineInfo * const cmdlineP) {
38 /* --------------------------------------------------------------------------
39 Parse program command line described in Unix standard form by argc
40 and argv. Return the information in the options as *cmdlineP.
41
42 If command line is internally inconsistent (invalid options, etc.),
43 issue error message to stderr and abort program.
44
45 Note that the strings we return are stored in the storage that
46 was passed to us as the argv array. We also trash *argv.
47 --------------------------------------------------------------------------*/
48 optEntry *option_def;
49 /* Instructions to pm_optParseOptions3 on how to parse our options. */
50 optStruct3 opt;
51
52 unsigned int option_def_index;
53 unsigned int depthSpec;
54
55 MALLOCARRAY_NOFAIL(option_def, 100);
56
57 option_def_index = 0; /* incremented by OPTENT3 */
58 OPTENT3(0, "directcolor", OPT_FLAG, NULL, &cmdlineP->directcolor, 0);
59 OPTENT3(0, "pseudodepth", OPT_UINT, &cmdlineP->pseudodepth,
60 &depthSpec, 0);
61
62 opt.opt_table = option_def;
63 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
64 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
65
66 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0 );
67 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
68
69 if (!depthSpec)
70 cmdlineP->pseudodepth = 8;
71 else {
72 if (cmdlineP->pseudodepth < 1)
73 pm_error("-pseudodepth option value must be at least 1. "
74 "You specified %u", cmdlineP->pseudodepth);
75 else if (cmdlineP->pseudodepth > 16)
76 pm_error("-pseudodepth option value must be at most 16. "
77 "You specified %u", cmdlineP->pseudodepth);
78 }
79
80 if (argc-1 == 0)
81 cmdlineP->inputFilename = "-";
82 else if (argc-1 != 1)
83 pm_error("Program takes zero or one argument (filename). You "
84 "specified %d", argc-1);
85 else
86 cmdlineP->inputFilename = argv[1];
87 }
88
89
90
91 static void
92 setupX11Header(X11WDFileHeader * const h11P,
93 const char * const dumpname,
94 unsigned int const cols,
95 unsigned int const rows,
96 int const format,
97 bool const direct,
98 bool const grayscale,
99 unsigned int const colors,
100 unsigned int const pseudodepth) {
101
102 /* Set up the header. */
103
104 h11P->header_size = sizeof(*h11P) + (xwdval) strlen(dumpname) + 1;
105 h11P->file_version = X11WD_FILE_VERSION;
106 h11P->pixmap_format = ZPixmap;
107 h11P->pixmap_width = cols;
108 h11P->pixmap_height = rows;
109 h11P->xoffset = 0;
110 h11P->byte_order = MSBFirst;
111 h11P->bitmap_bit_order = MSBFirst;
112 h11P->window_width = cols;
113 h11P->window_height = rows;
114 h11P->window_x = 0;
115 h11P->window_y = 0;
116 h11P->window_bdrwidth = 0;
117
118 if (direct) {
119 h11P->pixmap_depth = 24;
120 h11P->bitmap_unit = 32;
121 h11P->bitmap_pad = 32;
122 h11P->bits_per_pixel = 32;
123 h11P->visual_class = DirectColor;
124 h11P->colormap_entries = 256;
125 h11P->ncolors = 256;
126 h11P->red_mask = 0xff0000;
127 h11P->green_mask = 0xff00;
128 h11P->blue_mask = 0xff;
129 h11P->bytes_per_line = cols * 4;
130 } else { /* pseudocolor -- i.e. regular paletted raster */
131 if (grayscale) {
132 if (PNM_FORMAT_TYPE(format) == PBM_TYPE) {
133 h11P->pixmap_depth = 1;
134 h11P->bits_per_pixel = 1;
135 h11P->colormap_entries = colors;
136 h11P->bytes_per_line = (cols + 7) / 8;
137 } else {
138 h11P->pixmap_depth = pseudodepth;
139 h11P->bits_per_pixel = pseudodepth;
140 h11P->colormap_entries = colors;
141 h11P->bytes_per_line = cols;
142 }
143 h11P->bitmap_unit = 8;
144 h11P->bitmap_pad = 8;
145 h11P->visual_class = StaticGray;
146 h11P->red_mask = 0;
147 h11P->green_mask = 0;
148 h11P->blue_mask = 0;
149 } else {
150 h11P->pixmap_depth = pseudodepth;
151 h11P->bits_per_pixel = pseudodepth;
152 h11P->visual_class = PseudoColor;
153 h11P->colormap_entries = 1 << pseudodepth;
154 h11P->red_mask = 0;
155 h11P->green_mask = 0;
156 h11P->blue_mask = 0;
157 h11P->bytes_per_line = cols;
158 h11P->bitmap_unit = 8;
159 h11P->bitmap_pad = 8;
160 }
161 h11P->ncolors = colors;
162 }
163 h11P->bits_per_rgb = h11P->pixmap_depth;
164 }
165
166
167
168 static void
169 writeX11Header(X11WDFileHeader const h11,
170 FILE * const ofP) {
171
172 /* Write out the header in big-endian order. */
173
174 pm_writebiglong(ofP, h11.header_size);
175 pm_writebiglong(ofP, h11.file_version);
176 pm_writebiglong(ofP, h11.pixmap_format);
177 pm_writebiglong(ofP, h11.pixmap_depth);
178 pm_writebiglong(ofP, h11.pixmap_width);
179 pm_writebiglong(ofP, h11.pixmap_height);
180 pm_writebiglong(ofP, h11.xoffset);
181 pm_writebiglong(ofP, h11.byte_order);
182 pm_writebiglong(ofP, h11.bitmap_unit);
183 pm_writebiglong(ofP, h11.bitmap_bit_order);
184 pm_writebiglong(ofP, h11.bitmap_pad);
185 pm_writebiglong(ofP, h11.bits_per_pixel);
186 pm_writebiglong(ofP, h11.bytes_per_line);
187 pm_writebiglong(ofP, h11.visual_class);
188 pm_writebiglong(ofP, h11.red_mask);
189 pm_writebiglong(ofP, h11.green_mask);
190 pm_writebiglong(ofP, h11.blue_mask);
191 pm_writebiglong(ofP, h11.bits_per_rgb);
192 pm_writebiglong(ofP, h11.colormap_entries);
193 pm_writebiglong(ofP, h11.ncolors);
194 pm_writebiglong(ofP, h11.window_width);
195 pm_writebiglong(ofP, h11.window_height);
196 pm_writebiglong(ofP, h11.window_x);
197 pm_writebiglong(ofP, h11.window_y);
198 pm_writebiglong(ofP, h11.window_bdrwidth);
199 }
200
201
202
203 static void
204 writePseudoColormap(FILE * const ofP,
205 colorhist_vector const chv,
206 unsigned int const colors,
207 bool const grayscale,
208 bool const backwardMap,
209 xelval const maxval) {
210 /* Write out the colormap, big-endian order. */
211
212 X11XColor color;
213 unsigned int i;
214
215 color.flags = 7;
216 color.pad = 0;
217 for (i = 0; i < colors; ++i) {
218 color.num = i;
219 if (grayscale) {
220 /* Stupid hack because xloadimage and xwud disagree on
221 how to interpret bitmaps.
222 */
223 if (backwardMap)
224 color.red = (long) (colors-1-i) * 65535L / (colors - 1);
225 else
226 color.red = (long) i * 65535L / (colors - 1);
227
228 color.green = color.red;
229 color.blue = color.red;
230 } else {
231 color.red = PPM_GETR(chv[i].color);
232 color.green = PPM_GETG(chv[i].color);
233 color.blue = PPM_GETB(chv[i].color);
234 if (maxval != 65535L) {
235 color.red = (long) color.red * 65535L / maxval;
236 color.green = (long) color.green * 65535L / maxval;
237 color.blue = (long) color.blue * 65535L / maxval;
238 }
239 }
240 pm_writebiglong( ofP, color.num);
241 pm_writebigshort(ofP, color.red);
242 pm_writebigshort(ofP, color.green);
243 pm_writebigshort(ofP, color.blue);
244 putc(color.flags, ofP);
245 putc(color.pad, ofP);
246 }
247 }
248
249
250
251 static void
252 writeDirectColormap(FILE * const ofP) {
253 /*----------------------------------------------------------------------------
254 Write the XWD colormap.
255
256 We use a constant (independent of input) color map which simply has
257 each of the values 0-255, scaled to 16 bits, for each of the three
258 maps
259 -----------------------------------------------------------------------------*/
260 X11XColor color;
261 unsigned int i;
262
263 color.flags = 7;
264 color.pad = 0;
265
266 for (i = 0; i < 256; ++i) {
267 color.red = (short)(i << 8 | i);
268 color.green = (short)(i << 8 | i);
269 color.blue = (short)(i << 8 | i);
270 color.num = i << 16 | i << 8 | i;
271
272 pm_writebiglong( ofP, color.num);
273 pm_writebigshort(ofP, color.red);
274 pm_writebigshort(ofP, color.green);
275 pm_writebigshort(ofP, color.blue);
276 putc(color.flags, ofP);
277 putc(color.pad, ofP);
278 }
279 }
280
281
282
283 static void
284 writeRowDirect(FILE * const ofP,
285 xel * const xelrow,
286 unsigned int const cols,
287 int const format,
288 long const xmaxval,
289 xelval const maxval) {
290
291 switch (PNM_FORMAT_TYPE(format)) {
292 case PPM_TYPE: {
293 unsigned int col;
294 for (col = 0; col < cols; ++col) {
295 unsigned long const ul =
296 ((PPM_GETR(xelrow[col]) * xmaxval / maxval) << 16) |
297 ((PPM_GETG(xelrow[col]) * xmaxval / maxval) << 8) |
298 (PPM_GETB(xelrow[col]) * xmaxval / maxval);
299 pm_writebiglong(ofP, ul);
300 }
301 }
302 break;
303
304 default: {
305 unsigned int col;
306 for (col = 0; col < cols; ++col) {
307 unsigned long const val = PNM_GET1(xelrow[col]);
308 unsigned long const ul =
309 ((val * xmaxval / maxval) << 16) |
310 ((val * xmaxval / maxval) << 8) |
311 (val * xmaxval / maxval);
312 pm_writebiglong(ofP, ul);
313 }
314 break;
315 }
316 }
317 }
318
319
320
321 static void
322 writeRowGrayscale(FILE * const ofP,
323 xel * const xelrow,
324 unsigned int const cols,
325 xelval const maxval,
326 bool const backwardMap,
327 unsigned int const bitsPerPixel) {
328
329 xelval bigger_maxval;
330 int bitshift;
331 unsigned char byte;
332 unsigned int col;
333
334 bigger_maxval = pm_bitstomaxval(bitsPerPixel);
335 bitshift = 8 - bitsPerPixel;
336 byte = 0;
337 for (col = 0; col < cols; ++col) {
338 xelval s;
339 s = PNM_GET1(xelrow[col]);
340 if (backwardMap)
341 s = 1 - s;
342
343 if (maxval != bigger_maxval)
344 s = (long) s * bigger_maxval / maxval;
345 byte |= s << bitshift;
346 bitshift -= bitsPerPixel;
347 if (bitshift < 0) {
348 putc(byte, stdout);
349 bitshift = 8 - bitsPerPixel;
350 byte = 0;
351 }
352 }
353 if (bitshift < 8 - bitsPerPixel)
354 putc(byte, ofP);
355 }
356
357
358
359 static void
360 writeRowPseudoColor(FILE * const ofP,
361 xel * const xelrow,
362 unsigned int const cols,
363 colorhash_table const cht) {
364
365 unsigned int col;
366
367 for (col = 0; col < cols; ++col)
368 putc(ppm_lookupcolor(cht, &xelrow[col]), ofP);
369 }
370
371
372
373 static void
374 writeRaster(FILE * const ofP,
375 xel ** const xels,
376 unsigned int const cols,
377 unsigned int const rows,
378 int const format,
379 xelval const maxval,
380 long const xmaxval,
381 colorhist_vector const chv,
382 unsigned int const colors,
383 bool const direct,
384 bool const grayscale,
385 bool const backwardMap,
386 unsigned int const bitsPerPixel) {
387
388 unsigned int row;
389 colorhash_table cht;
390
391 if (chv)
392 cht = ppm_colorhisttocolorhash(chv, colors);
393
394 for (row = 0; row < rows; ++row) {
395 if (direct)
396 writeRowDirect(ofP, xels[row], cols, format, xmaxval, maxval);
397 else {
398 if (grayscale)
399 writeRowGrayscale(ofP, xels[row], cols, maxval, backwardMap,
400 bitsPerPixel);
401 else
402 writeRowPseudoColor(ofP, xels[row], cols, cht);
403 }
404 }
405 }
406
407
408
409 int
410 main(int argc, char * argv[]) {
411
412 struct CmdlineInfo cmdline;
413 FILE* ifP;
414 xel ** xels;
415 int rows, cols, format, colors;
416 xelval maxval;
417 long xmaxval;
418 colorhist_vector chv;
419 X11WDFileHeader h11;
420 bool direct, grayscale;
421 const char * dumpname;
422 bool backwardMap;
423
424 pnm_init(&argc, argv);
425
426 parseCommandLine(argc, argv, &cmdline);
427
428 ifP = pm_openr(cmdline.inputFilename);
429
430 xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);
431 xmaxval = (1 << cmdline.pseudodepth) - 1;
432 pm_close(ifP);
433
434 if (cmdline.directcolor) {
435 direct = TRUE;
436 grayscale = FALSE;
437 chv = NULL;
438 } else {
439 /* Figure out the colormap. */
440 switch (PNM_FORMAT_TYPE(format)) {
441 case PPM_TYPE:
442 pm_message("computing colormap...");
443 chv = ppm_computecolorhist(xels, cols, rows, xmaxval+1, &colors);
444 if (!chv) {
445 pm_message("Too many colors - "
446 "proceeding to write a 24-bit DirectColor "
447 "dump file. If you want PseudoColor, "
448 "try doing a 'pnmquant %ld'.",
449 xmaxval);
450 direct = TRUE;
451 } else {
452 pm_message("%d colors found", colors);
453 direct = FALSE;
454 }
455 grayscale = FALSE;
456 break;
457
458 case PBM_TYPE:
459 chv = NULL;
460 direct = FALSE;
461 grayscale = TRUE;
462 colors = 2;
463 break;
464 case PGM_TYPE:
465 chv = NULL;
466 direct = FALSE;
467 grayscale = TRUE;
468 colors = xmaxval + 1;
469 break;
470 default:
471 pm_error("INTERNAL ERROR: impossible format type");
472 }
473 }
474
475 if (streq(cmdline.inputFilename, "-"))
476 dumpname = "stdin";
477 else {
478 if (strlen(cmdline.inputFilename) > XWDVAL_MAX - sizeof(h11) - 1)
479 pm_error("Input file name is ridiculously long.");
480 else
481 dumpname = cmdline.inputFilename;
482 }
483
484 setupX11Header(&h11, dumpname, cols, rows, format,
485 direct, grayscale, colors,
486 cmdline.pseudodepth);
487
488 writeX11Header(h11, stdout);
489
490 /* Write out the dump name. */
491 fwrite(dumpname, 1, strlen(dumpname) + 1, stdout);
492
493 backwardMap = PNM_FORMAT_TYPE(format) == PBM_TYPE;
494
495 if (direct)
496 writeDirectColormap(stdout);
497 else
498 writePseudoColormap(stdout, chv, colors,
499 grayscale, backwardMap, maxval);
500
501 writeRaster(stdout, xels, cols, rows, format, maxval, xmaxval,
502 chv, colors, direct, grayscale,
503 backwardMap, h11.bits_per_pixel);
504
505 return 0;
506 }
507
508
509