Debian Codegraph @

converter/ppm/ppmtowinicon.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* ppmtowinicon.c - read PPM images and write a MS Windows .ico
2 **
3 ** Copyright (C) 2000 by Lee Benfield - [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 #include <math.h>
16 #include <string.h>
17
18 #include "pm_c_util.h"
19 #include "winico.h"
20 #include "ppm.h"
21 #include "mallocvar.h"
22 #include "shhopt.h"
23 #include "nstring.h"
24
25 #define MAJVERSION 0
26 #define MINVERSION 3
27
28 #define MAXCOLORS 256
29
30 struct CmdlineInfo {
31 /* All the information the user supplied in the command line,
32 in a form easy for the program to use.
33 */
34 unsigned int iconCount;
35 const char ** inputFileNm; /* '-' if stdin; malloc'ed array */
36 const char ** andpgmFileNm; /* NULL if unspecified; malloc'ed array */
37 const char * output; /* '-' if unspecified */
38 unsigned int truetransparent;
39 unsigned int verbose;
40 };
41
42
43 static bool verbose;
44
45 static int fileOffset = 0; /* not actually used, but useful for debug. */
46
47 static void
48 parseCommandLine(int argc,
49 const char ** argv,
50 struct CmdlineInfo * cmdlineP) {
51 /*----------------------------------------------------------------------------
52 Parse program command line described in Unix standard form by argc
53 and argv. Return the information in the options as *cmdlineP.
54
55 If command line is internally inconsistent (invalid options, etc.),
56 issue error message to stderr and abort program.
57
58 Note that the strings we return are stored in the storage that
59 was passed to us as the argv array. We also trash *argv.
60 -----------------------------------------------------------------------------*/
61 optEntry * option_def;
62 optStruct3 opt;
63
64 unsigned int option_def_index;
65
66 unsigned int outputSpec, andpgms;
67
68 MALLOCARRAY_NOFAIL(option_def, 100);
69
70 option_def_index = 0; /* incremented by OPTENT3 */
71 OPTENT3(0, "output", OPT_STRING, &cmdlineP->output,
72 &outputSpec, 0);
73 OPTENT3(0, "andpgms", OPT_FLAG, NULL,
74 &andpgms, 0);
75 OPTENT3(0, "truetransparent", OPT_FLAG, NULL,
76 &cmdlineP->truetransparent, 0);
77 OPTENT3(0, "verbose", OPT_STRING, NULL,
78 &cmdlineP->verbose, 0);
79
80 opt.opt_table = option_def;
81 opt.short_allowed = false; /* We have no short (old-fashioned) options */
82 opt.allowNegNum = false; /* We have no parms that are negative numbers */
83
84 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
85 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
86
87 if (!outputSpec)
88 cmdlineP->output = "-";
89
90 if (!andpgms) {
91 if (argc-1 == 0) {
92 cmdlineP->iconCount = 1;
93 MALLOCARRAY_NOFAIL(cmdlineP->inputFileNm, cmdlineP->iconCount);
94 cmdlineP->inputFileNm[0] = "-";
95 } else {
96 unsigned int iconIndex;
97
98 cmdlineP->iconCount = argc-1;
99 MALLOCARRAY_NOFAIL(cmdlineP->inputFileNm, cmdlineP->iconCount);
100 for (iconIndex = 0; iconIndex < cmdlineP->iconCount; ++iconIndex)
101 cmdlineP->inputFileNm[iconIndex] = argv[iconIndex+1];
102 }
103 {
104 unsigned int iconIndex;
105 MALLOCARRAY_NOFAIL(cmdlineP->andpgmFileNm, cmdlineP->iconCount);
106 for (iconIndex = 0; iconIndex < cmdlineP->iconCount; ++iconIndex)
107 cmdlineP->andpgmFileNm[iconIndex] = NULL;
108 }
109 } else {
110 if (argc-1 < 2)
111 pm_error("with -andpgms, you must specify at least two "
112 "arguments: image file name and and mask file name. "
113 "You specified %d", argc-1);
114 else if ((argc-1)/2*2 != (argc-1))
115 pm_error("with -andpgms, you must specify an even number of "
116 "arguments. You specified %d", argc-1);
117 else {
118 unsigned int iconIndex;
119 cmdlineP->iconCount = (argc-1)/2;
120 MALLOCARRAY_NOFAIL(cmdlineP->inputFileNm, cmdlineP->iconCount);
121 MALLOCARRAY_NOFAIL(cmdlineP->andpgmFileNm, cmdlineP->iconCount);
122 for (iconIndex = 0; iconIndex < cmdlineP->iconCount; ++iconIndex) {
123 cmdlineP->inputFileNm[iconIndex] = argv[1 + iconIndex*2];
124 cmdlineP->andpgmFileNm[iconIndex] = argv[2 + iconIndex*2];
125 }
126 }
127 }
128
129 }
130
131
132
133 static void
134 freeCmdline(struct CmdlineInfo const cmdline) {
135
136 free(cmdline.inputFileNm);
137 free(cmdline.andpgmFileNm);
138 }
139
140
141
142 static void
143 writeU1(FILE * const ofP,
144 u1 const v) {
145
146 ++fileOffset;
147 pm_writechar(ofP, v);
148 }
149
150
151
152 static void
153 writeU2(FILE * const ofP,
154 u2 const v) {
155
156 fileOffset +=2;
157 pm_writelittleshort(ofP, v);
158 }
159
160
161
162 static void
163 writeU4(FILE * const ofP,
164 u4 const v) {
165
166 fileOffset += 4;
167 pm_writelittlelong(ofP, v);
168 }
169
170
171
172 static MS_Ico *
173 newIconFile(void) {
174
175 MS_Ico * MSIconDataP;
176
177 MALLOCVAR_NOFAIL(MSIconDataP);
178
179 MSIconDataP->reserved = 0;
180 MSIconDataP->type = 1;
181 MSIconDataP->count = 0;
182 MSIconDataP->entries = NULL;
183
184 return MSIconDataP;
185 }
186
187
188
189 static ICON_bmp *
190 newAndBitmap(gray ** const ba,
191 unsigned int const cols,
192 unsigned int const rows,
193 gray const maxval) {
194 /*----------------------------------------------------------------------------
195 create andBitmap from PGM
196 -----------------------------------------------------------------------------*/
197 unsigned int const xByteCt = ROUNDUP(cols, 32)/8;
198 /* How wide the u1 string for each row should be -- Each byte is 8
199 pixels, but must be a multiple of 4 bytes.
200 */
201 ICON_bmp * icBitmapP;
202 unsigned int row;
203 u1 ** rowData;
204
205 MALLOCVAR_NOFAIL(icBitmapP);
206
207 MALLOCARRAY_NOFAIL(rowData, rows);
208 icBitmapP->xBytes = xByteCt;
209 icBitmapP->data = rowData;
210 icBitmapP->size = xByteCt * rows;
211 for (row = 0; row < rows; ++row) {
212 u1 * thisRow; /* malloc'ed */
213 unsigned int byteOn;
214 unsigned int bitOn;
215
216 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
217
218 byteOn = 0; /* initial value */
219 bitOn = 128; /* initial value */
220
221 memset (thisRow, 0, xByteCt);
222 rowData[rows - row - 1] = thisRow;
223
224 if (ba) {
225 unsigned int col;
226
227 for (col = 0; col < cols; ++col) {
228 /* Black (bit clear) is transparent in PGM alpha maps,
229 in ICO bit *set* is transparent.
230 */
231 if (ba[row][col] <= maxval/2) thisRow[byteOn] |= bitOn;
232
233 if (bitOn == 1) {
234 ++byteOn;
235 bitOn = 128;
236 } else {
237 bitOn >>= 1;
238 }
239 }
240 } else {
241 /* No array -- we're just faking this */
242 }
243 }
244 return icBitmapP;
245 }
246
247
248
249 /* Depending on if the image is stored as 1bpp, 4bpp or 8bpp, the
250 encoding mechanism is different.
251
252 I didn't re-use the code from ppmtobmp since I need to keep the
253 bitmaps in memory until I've loaded all ppms.
254
255 8bpp => 1 byte/palette index.
256 4bpp => High Nibble, Low Nibble
257 1bpp => 1 palette value per bit, high bit 1st.
258 */
259
260
261
262 static void
263 fillInRaster1(u1 ** const rowData,
264 unsigned int const xByteCt,
265 pixel ** const pa,
266 unsigned int const cols,
267 unsigned int const rows,
268 colorhash_table const cht) {
269
270 unsigned int row;
271
272 for (row = 0; row <rows; ++row) {
273 u1 * thisRow; /* malloc'ed */
274 unsigned int byteSeq;
275 unsigned int bitOnMask;
276
277 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
278 memset(thisRow, 0, xByteCt); /* initial value */
279 rowData[rows - row - 1] = thisRow;
280 byteSeq = 0; /* initial value */
281 bitOnMask = 0x80; /* initial value */
282
283 if (pa) {
284 unsigned int col;
285
286 for (col = 0; col < cols; ++col) {
287 /* So we've got a colorhash_table with two colors in it. Which
288 is black?!
289
290 Unless the hashing function changes, 0's black.
291 */
292 int const value = ppm_lookupcolor(cht, &pa[row][col]);
293
294 assert(byteSeq < xByteCt);
295
296 if (!value) {
297 /* leave black. */
298 } else {
299 thisRow[byteSeq] |= bitOnMask;
300 }
301 if (bitOnMask == 0x1) {
302 ++byteSeq;
303 bitOnMask = 0x80;
304 } else {
305 bitOnMask >>= 1;
306 }
307 }
308 } else {
309 /* No pixel array -- we're just faking this */
310 }
311 }
312 }
313
314
315
316 static void
317 fillInRaster4(u1 ** const rowData,
318 unsigned int const xByteCt,
319 pixel ** const pa,
320 unsigned int const cols,
321 unsigned int const rows,
322 colorhash_table const cht) {
323
324 unsigned int row;
325
326 for (row = 0; row < rows; ++row) {
327 u1 * thisRow;
328 unsigned int byteSeq;
329 unsigned int nibbleSig; /* high nibble = 1, low nibble = 0; */
330
331 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
332
333 memset(thisRow, 0, xByteCt);
334 rowData[rows - row - 1] = thisRow;
335 byteSeq = 0; /* initial value */
336 nibbleSig = 1; /* initial value */
337
338 if (pa) {
339 unsigned int col;
340
341 for (col = 0; col < cols; ++col) {
342 int value;
343
344 assert(byteSeq < xByteCt);
345
346 value = ppm_lookupcolor(cht, &pa[row][col]); /* init value */
347 /* Shift it, if we're putting it in the high nibble. */
348 if (nibbleSig == 1)
349 value <<= 4;
350 thisRow[byteSeq] |= value;
351 if (nibbleSig == 1)
352 nibbleSig = 0;
353 else {
354 nibbleSig = 1;
355 ++byteSeq;
356 }
357 }
358 } else {
359 /* No pixel array -- we're just faking this */
360 }
361 }
362 }
363
364
365
366 static void
367 fillInRaster8(u1 ** const rowData,
368 unsigned int const xByteCt,
369 pixel ** const pa,
370 unsigned int const cols,
371 unsigned int const rows,
372 colorhash_table const cht) {
373
374 unsigned int row;
375
376 for (row = 0; row < rows; ++row) {
377 u1 * thisRow; /* malloc'ed */
378
379 assert(cols <= xByteCt);
380
381 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
382 memset(thisRow, 0, xByteCt);
383 rowData[rows - row - 1] = thisRow;
384 if (pa) {
385 unsigned int col;
386
387 for (col = 0; col < cols; ++col)
388 thisRow[col] = ppm_lookupcolor(cht, &pa[row][col]);
389 } else {
390 /* No pixel array -- we're just faking this */
391 }
392 }
393 }
394
395
396
397 static ICON_bmp *
398 newBitmap(unsigned int const bpp,
399 pixel ** const pa,
400 unsigned int const cols,
401 unsigned int const rows,
402 colorhash_table const cht) {
403
404 unsigned int const xByteCt = ROUNDUP(cols * bpp, 32)/8;
405 /* How wide the u1 string for each row should be. Pixels are packed
406 into bytes, padded to a multiple of 4 bytes.
407 */
408 ICON_bmp * icBitmapP; /* malloc'ed */
409 u1 ** rowData; /* malloc'ed */
410
411 assert(cols < 256); assert(rows < 256); /* required for no overflow */
412
413 MALLOCVAR_NOFAIL(icBitmapP);
414
415 MALLOCARRAY_NOFAIL(rowData, rows);
416
417 icBitmapP->xBytes = xByteCt;
418 icBitmapP->data = rowData;
419 icBitmapP->size = xByteCt * rows;
420
421 unsigned int const assumedBpp = (pa == NULL) ? 1 : bpp;
422
423 switch (assumedBpp) {
424 case 1:
425 fillInRaster1(rowData, xByteCt, pa, cols, rows, cht);
426 break;
427 case 4:
428 fillInRaster4(rowData, xByteCt, pa, cols, rows, cht);
429 break;
430 case 8:
431 default:
432 fillInRaster8(rowData, xByteCt, pa, cols, rows, cht);
433 break;
434 }
435 return icBitmapP;
436 }
437
438
439
440 static IC_InfoHeader *
441 newInfoHeader(IC_Entry const entry) {
442
443 IC_InfoHeader * ihP;
444
445 MALLOCVAR_NOFAIL(ihP);
446
447 ihP->size = 40;
448 ihP->width = entry.width;
449 ihP->height = entry.height * 2;
450 ihP->planes = 1;
451 ihP->bitcount = entry.bitcount;
452 ihP->compression = 0;
453 ihP->imagesize = entry.width * entry.height * 8 / entry.bitcount;
454 ihP->x_pixels_per_m = 0;
455 ihP->y_pixels_per_m = 0;
456 ihP->colors_used = 1 << entry.bitcount;
457 ihP->colors_important = 0;
458
459 return ihP;
460 }
461
462
463
464 static IC_Palette *
465 newCleanPalette(void) {
466
467 IC_Palette * paletteP; /* malloc'ed */
468
469 unsigned int i;
470
471 MALLOCVAR_NOFAIL(paletteP);
472
473 MALLOCARRAY_NOFAIL(paletteP->colors, MAXCOLORS);
474
475 for (i=0; i <MAXCOLORS; ++i) {
476 paletteP->colors[i] = NULL;
477 }
478 return paletteP;
479 }
480
481
482
483 static void
484 addColorToPalette(IC_Palette * const paletteP,
485 unsigned int const i,
486 unsigned int const r,
487 unsigned int const g,
488 unsigned int const b) {
489
490 MALLOCVAR_NOFAIL(paletteP->colors[i]);
491
492 paletteP->colors[i]->red = r;
493 paletteP->colors[i]->green = g;
494 paletteP->colors[i]->blue = b;
495 paletteP->colors[i]->reserved = 0;
496 }
497
498
499
500 static void
501 makePalette(pixel ** const xorPPMarray,
502 unsigned int const xorCols,
503 unsigned int const xorRows,
504 pixval const xorMaxval,
505 IC_Palette ** const palettePP,
506 colorhash_table * const xorChtP,
507 unsigned int * const colorsP,
508 const char ** const errorP) {
509 /*----------------------------------------------------------------------------
510 Figure out the colormap and turn it into the appropriate GIF colormap -
511 this code's pretty much straight from 'ppmtobpm'.
512 -----------------------------------------------------------------------------*/
513 IC_Palette * const paletteP = newCleanPalette();
514
515 colorhist_vector xorChv;
516 unsigned int i;
517 int colorCt;
518
519 if (verbose)
520 pm_message("computing colormap...");
521
522 xorChv = ppm_computecolorhist(xorPPMarray, xorCols, xorRows, MAXCOLORS,
523 &colorCt);
524 if (!xorChv)
525 pm_asprintf(errorP,
526 "image has too many colors - try doing a 'pnmquant %u'",
527 MAXCOLORS);
528 else {
529 *errorP = NULL;
530
531 if (verbose)
532 pm_message("%u colors found", colorCt);
533
534 if (verbose && (xorMaxval > 255))
535 pm_message("maxval is not 255 - automatically rescaling colors");
536 for (i = 0; i < colorCt; ++i) {
537 if (xorMaxval == 255) {
538 addColorToPalette(paletteP, i,
539 PPM_GETR(xorChv[i].color),
540 PPM_GETG(xorChv[i].color),
541 PPM_GETB(xorChv[i].color));
542 } else {
543 addColorToPalette(paletteP, i,
544 PPM_GETR(xorChv[i].color) * 255 / xorMaxval,
545 PPM_GETG(xorChv[i].color) * 255 / xorMaxval,
546 PPM_GETB(xorChv[i].color) * 255 / xorMaxval);
547 }
548 }
549
550 /* And make a hash table for fast lookup. */
551 *xorChtP = ppm_colorhisttocolorhash(xorChv, colorCt);
552
553 ppm_freecolorhist(xorChv);
554
555 *palettePP = paletteP;
556 *colorsP = colorCt;
557 }
558 }
559
560
561
562 static void
563 getOrFakeAndMap(const char * const andPgmFname,
564 unsigned int const xorCols,
565 unsigned int const xorRows,
566 gray *** const andPGMarrayP,
567 pixval * const andMaxvalP,
568 colorhash_table * const andChtP,
569 const char ** const errorP) {
570
571 int andRows, andCols;
572
573 if (!andPgmFname) {
574 /* He's not supplying a bitmap for 'and'. Fake the bitmap. */
575 *andPGMarrayP = NULL;
576 *andMaxvalP = 1;
577 *andChtP = NULL;
578 *errorP = NULL;
579 } else {
580 FILE * andFileP;
581 andFileP = pm_openr(andPgmFname);
582 *andPGMarrayP = pgm_readpgm(andFileP, &andCols, &andRows, andMaxvalP);
583 pm_close(andFileP);
584
585 if ((andCols != xorCols) || (andRows != xorRows)) {
586 pm_asprintf(errorP,
587 "And mask and image have different dimensions "
588 "(%u x %u vs %u x %u). Aborting.",
589 andCols, xorCols, andRows, xorRows);
590 } else
591 *errorP = NULL;
592 }
593 }
594
595
596
597 static void
598 blackenTransparentAreas(pixel ** const xorPPMarray,
599 unsigned int const cols,
600 unsigned int const rows,
601 gray ** const andPGMarray,
602 pixval const andMaxval) {
603
604 unsigned int row;
605
606 if (verbose)
607 pm_message("Setting transparent pixels to black");
608
609 for (row = 0; row < rows; ++row) {
610 unsigned int col;
611
612 for (col = 0; col < cols; ++col) {
613 if (andPGMarray[row][col] < andMaxval)
614 /* It's not opaque here; make it black */
615 PPM_ASSIGN(xorPPMarray[row][col], 0, 0, 0);
616 }
617 }
618 }
619
620
621
622 static void
623 addEntryToIcon(MS_Ico * const MSIconDataP,
624 const char * const xorPpmFname,
625 const char * const andPgmFname,
626 bool const trueTransparent) {
627
628 IC_Entry * entryP;
629 FILE * xorfile;
630 pixel ** xorPPMarray;
631 gray ** andPGMarray;
632 ICON_bmp * xorBitmapP;
633 ICON_bmp * andBitmapP;
634 int rows, cols;
635 unsigned int bpp;
636 unsigned int colorCt;
637 unsigned int entryCols;
638 IC_Palette * paletteP;
639 colorhash_table xorCht;
640 colorhash_table andCht;
641 const char * error;
642
643 pixval xorMaxval;
644 gray andMaxval;
645
646 MALLOCVAR_NOFAIL(entryP);
647
648 /* Read the xor PPM. */
649 xorfile = pm_openr(xorPpmFname);
650 xorPPMarray = ppm_readppm(xorfile, &cols, &rows, &xorMaxval);
651 pm_close(xorfile);
652
653 /* Since the entry uses 1 byte to hold the width and height of the icon,
654 the image can't be more than 256 x 256.
655 */
656 if (rows > 255 || cols > 255) {
657 pm_error("Max size for a icon is 255 x 255 (1 byte fields). "
658 "%s is %u x %u", xorPpmFname, cols, rows);
659 }
660
661 if (verbose)
662 pm_message("read PPM: %uw x %uh, maxval = %u", cols, rows, xorMaxval);
663
664 makePalette(xorPPMarray, cols, rows, xorMaxval,
665 &paletteP, &xorCht, &colorCt, &error);
666
667 if (error)
668 pm_error("Unable to make palette for '%s'. %s", xorPpmFname, error);
669
670 /* All the icons I found seemed to pad the palette to the max entries for
671 that bitdepth.
672
673 The spec indicates this isn't necessary, but I'll follow this behaviour
674 just in case.
675 */
676 if (colorCt < 3) {
677 bpp = 1;
678 entryCols = 2;
679 } else if (colorCt < 17) {
680 bpp = 4;
681 entryCols = 16;
682 } else {
683 bpp = 8;
684 entryCols = 256;
685 }
686
687 getOrFakeAndMap(andPgmFname, cols, rows,
688 &andPGMarray, &andMaxval, &andCht, &error);
689 if (error)
690 pm_error("Error in and map for '%s'. %s", xorPpmFname, error);
691
692 if (andPGMarray && trueTransparent)
693 blackenTransparentAreas(xorPPMarray, cols, rows,
694 andPGMarray, andMaxval);
695
696 xorBitmapP = newBitmap(bpp, xorPPMarray, cols, rows, xorCht);
697 andBitmapP = newAndBitmap(andPGMarray, cols, rows, andMaxval);
698
699 /* Fill in the entry data fields. */
700 entryP->width = cols;
701 entryP->height = rows;
702 entryP->color_count = entryCols;
703 entryP->reserved = 0;
704 entryP->planes = 1;
705 entryP->bitcount = bpp;
706 /* all the icons I looked at ignored this value */
707 entryP->ih = newInfoHeader(*entryP);
708 entryP->colors = paletteP->colors;
709 entryP->size_in_bytes =
710 xorBitmapP->size + andBitmapP->size + 40 + (4 * entryCols);
711 if (verbose)
712 pm_message("entry->size_in_bytes = %u + %u + %u = %u",
713 xorBitmapP->size, andBitmapP->size,
714 40, entryP->size_in_bytes );
715
716 /* We don't know the offset ATM, set to 0 for now. Have to calculate this
717 at the end.
718 */
719 entryP->file_offset = 0;
720 entryP->xorBitmapOut = xorBitmapP->data;
721 entryP->andBitmapOut = andBitmapP->data;
722 entryP->xBytesXor = xorBitmapP->xBytes;
723 entryP->xBytesAnd = andBitmapP->xBytes;
724
725 /* Add the entry to the entries array. */
726 ++MSIconDataP->count;
727
728 /* Perhaps I should allocate ahead, and take fewer trips to the well. */
729 REALLOCARRAY(MSIconDataP->entries, MSIconDataP->count);
730 MSIconDataP->entries[MSIconDataP->count - 1] = entryP;
731 }
732
733
734
735 static void
736 writeIC_Entry(FILE * const ofP,
737 IC_Entry * const entryP) {
738
739 writeU1(ofP, entryP->width);
740 writeU1(ofP, entryP->height);
741 writeU1(ofP, entryP->color_count); /* chops 256->0 on its own.. */
742 writeU1(ofP, entryP->reserved);
743 writeU2(ofP, entryP->planes);
744 writeU2(ofP, entryP->bitcount);
745 writeU4(ofP, entryP->size_in_bytes);
746 writeU4(ofP, entryP->file_offset);
747 }
748
749
750
751 static void
752 writeIC_InfoHeader(FILE * const ofP,
753 IC_InfoHeader * const ihP) {
754
755 writeU4(ofP, ihP->size);
756 writeU4(ofP, ihP->width);
757 writeU4(ofP, ihP->height);
758 writeU2(ofP, ihP->planes);
759 writeU2(ofP, ihP->bitcount);
760 writeU4(ofP, ihP->compression);
761 writeU4(ofP, ihP->imagesize);
762 writeU4(ofP, ihP->x_pixels_per_m);
763 writeU4(ofP, ihP->y_pixels_per_m);
764 writeU4(ofP, ihP->colors_used);
765 writeU4(ofP, ihP->colors_important);
766 }
767
768
769
770 static void
771 writeIC_Color(FILE * const ofP,
772 IC_Color * const colorP) {
773
774 /* Since the ppm might not have as many colors in it as we'd like,
775 (2, 16, 256), stick 0 in the gaps.
776
777 This means that we lose palette information, but that can't be helped.
778 */
779 if (!colorP) {
780 writeU1(ofP, 0);
781 writeU1(ofP, 0);
782 writeU1(ofP, 0);
783 writeU1(ofP, 0);
784 } else {
785 writeU1(ofP, colorP->blue);
786 writeU1(ofP, colorP->green);
787 writeU1(ofP, colorP->red);
788 writeU1(ofP, colorP->reserved);
789 }
790 }
791
792
793
794 static void
795 writeBitmap(FILE * const ofP,
796 u1 ** const bitmap,
797 unsigned int const xByteCt,
798 unsigned int const height) {
799
800 unsigned int row;
801
802 for (row = 0; row < height; ++row) {
803 fwrite(bitmap[row], 1, xByteCt, ofP);
804 fileOffset += xByteCt;
805 }
806 }
807
808
809
810 static void
811 writeMS_Ico(MS_Ico * const MSIconDataP,
812 const char * const outFname) {
813
814 FILE * const ofP = pm_openw(outFname);
815
816 unsigned int i;
817
818 writeU2(ofP, MSIconDataP->reserved);
819 writeU2(ofP, MSIconDataP->type);
820 writeU2(ofP, MSIconDataP->count);
821
822 for (i = 0; i < MSIconDataP->count; ++i)
823 writeIC_Entry(ofP, MSIconDataP->entries[i]);
824
825 for (i = 0; i < MSIconDataP->count; ++i) {
826 IC_Entry * const entryP = MSIconDataP->entries[i];
827
828 unsigned int j;
829
830 writeIC_InfoHeader(ofP, MSIconDataP->entries[i]->ih);
831
832 for (j = 0; j < (MSIconDataP->entries[i]->color_count); ++j) {
833 writeIC_Color(ofP, MSIconDataP->entries[i]->colors[j]);
834 }
835 if (verbose)
836 pm_message("writing xor bitmap");
837
838 writeBitmap(ofP,
839 entryP->xorBitmapOut, entryP->xBytesXor, entryP->height);
840 if (verbose)
841 pm_message("writing and bitmap");
842
843 writeBitmap(ofP,
844 entryP->andBitmapOut, entryP->xBytesAnd, entryP->height);
845 }
846 fclose(ofP);
847 }
848
849
850
851 int
852 main(int argc, const char ** argv) {
853
854 MS_Ico * const MSIconDataP = newIconFile();
855
856 struct CmdlineInfo cmdline;
857 unsigned int iconIndex;
858 unsigned int offset;
859
860 pm_proginit(&argc, argv);
861
862 parseCommandLine(argc, argv, &cmdline);
863
864 verbose = cmdline.verbose;
865
866 for (iconIndex = 0; iconIndex < cmdline.iconCount; ++iconIndex) {
867 addEntryToIcon(MSIconDataP, cmdline.inputFileNm[iconIndex],
868 cmdline.andpgmFileNm[iconIndex],
869 cmdline.truetransparent);
870 }
871 /*
872 * Now we have to go through and calculate the offsets.
873 * The first infoheader starts at 6 + count*16 bytes.
874 */
875 offset = (MSIconDataP->count * 16) + 6;
876 for (iconIndex = 0; iconIndex < MSIconDataP->count; ++iconIndex) {
877 IC_Entry * const entryP = MSIconDataP->entries[iconIndex];
878
879 entryP->file_offset = offset;
880 /*
881 * Increase the offset by the size of this offset & data.
882 * this includes the size of the color data.
883 */
884 offset += entryP->size_in_bytes;
885 }
886 /*
887 * And now, we have to actually SAVE the .ico!
888 */
889 writeMS_Ico(MSIconDataP, cmdline.output);
890
891 freeCmdline(cmdline);
892
893 return 0;
894 }
895
896
897