Debian Codegraph @

converter/ppm/ppmtowinicon.c

raw symbols ◀ ▶ SCIP
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 overflow2(xByteCt,rows);
211 icBitmapP->size = xByteCt * rows;
212 for (row = 0; row < rows; ++row) {
213 u1 * thisRow; /* malloc'ed */
214 unsigned int byteOn;
215 unsigned int bitOn;
216
217 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
218
219 byteOn = 0; /* initial value */
220 bitOn = 128; /* initial value */
221
222 memset (thisRow, 0, xByteCt);
223 rowData[rows - row - 1] = thisRow;
224
225 if (ba) {
226 unsigned int col;
227
228 for (col = 0; col < cols; ++col) {
229 /* Black (bit clear) is transparent in PGM alpha maps,
230 in ICO bit *set* is transparent.
231 */
232 if (ba[row][col] <= maxval/2) thisRow[byteOn] |= bitOn;
233
234 if (bitOn == 1) {
235 ++byteOn;
236 bitOn = 128;
237 } else {
238 bitOn >>= 1;
239 }
240 }
241 } else {
242 /* No array -- we're just faking this */
243 }
244 }
245 return icBitmapP;
246 }
247
248
249
250 /* Depending on if the image is stored as 1bpp, 4bpp or 8bpp, the
251 encoding mechanism is different.
252
253 I didn't re-use the code from ppmtobmp since I need to keep the
254 bitmaps in memory until I've loaded all ppms.
255
256 8bpp => 1 byte/palette index.
257 4bpp => High Nibble, Low Nibble
258 1bpp => 1 palette value per bit, high bit 1st.
259 */
260
261
262
263 static void
264 fillInRaster1(u1 ** const rowData,
265 unsigned int const xByteCt,
266 pixel ** const pa,
267 unsigned int const cols,
268 unsigned int const rows,
269 colorhash_table const cht) {
270
271 unsigned int row;
272
273 for (row = 0; row <rows; ++row) {
274 u1 * thisRow; /* malloc'ed */
275 unsigned int byteSeq;
276 unsigned int bitOnMask;
277
278 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
279 memset(thisRow, 0, xByteCt); /* initial value */
280 rowData[rows - row - 1] = thisRow;
281 byteSeq = 0; /* initial value */
282 bitOnMask = 0x80; /* initial value */
283
284 if (pa) {
285 unsigned int col;
286
287 for (col = 0; col < cols; ++col) {
288 /* So we've got a colorhash_table with two colors in it. Which
289 is black?!
290
291 Unless the hashing function changes, 0's black.
292 */
293 int const value = ppm_lookupcolor(cht, &pa[row][col]);
294
295 assert(byteSeq < xByteCt);
296
297 if (!value) {
298 /* leave black. */
299 } else {
300 thisRow[byteSeq] |= bitOnMask;
301 }
302 if (bitOnMask == 0x1) {
303 ++byteSeq;
304 bitOnMask = 0x80;
305 } else {
306 bitOnMask >>= 1;
307 }
308 }
309 } else {
310 /* No pixel array -- we're just faking this */
311 }
312 }
313 }
314
315
316
317 static void
318 fillInRaster4(u1 ** const rowData,
319 unsigned int const xByteCt,
320 pixel ** const pa,
321 unsigned int const cols,
322 unsigned int const rows,
323 colorhash_table const cht) {
324
325 unsigned int row;
326
327 for (row = 0; row < rows; ++row) {
328 u1 * thisRow;
329 unsigned int byteSeq;
330 unsigned int nibbleSig; /* high nibble = 1, low nibble = 0; */
331
332 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
333
334 memset(thisRow, 0, xByteCt);
335 rowData[rows - row - 1] = thisRow;
336 byteSeq = 0; /* initial value */
337 nibbleSig = 1; /* initial value */
338
339 if (pa) {
340 unsigned int col;
341
342 for (col = 0; col < cols; ++col) {
343 int value;
344
345 assert(byteSeq < xByteCt);
346
347 value = ppm_lookupcolor(cht, &pa[row][col]); /* init value */
348 /* Shift it, if we're putting it in the high nibble. */
349 if (nibbleSig == 1)
350 value <<= 4;
351 thisRow[byteSeq] |= value;
352 if (nibbleSig == 1)
353 nibbleSig = 0;
354 else {
355 nibbleSig = 1;
356 ++byteSeq;
357 }
358 }
359 } else {
360 /* No pixel array -- we're just faking this */
361 }
362 }
363 }
364
365
366
367 static void
368 fillInRaster8(u1 ** const rowData,
369 unsigned int const xByteCt,
370 pixel ** const pa,
371 unsigned int const cols,
372 unsigned int const rows,
373 colorhash_table const cht) {
374
375 unsigned int row;
376
377 for (row = 0; row < rows; ++row) {
378 u1 * thisRow; /* malloc'ed */
379
380 assert(cols <= xByteCt);
381
382 MALLOCARRAY_NOFAIL(thisRow, xByteCt);
383 memset(thisRow, 0, xByteCt);
384 rowData[rows - row - 1] = thisRow;
385 if (pa) {
386 unsigned int col;
387
388 for (col = 0; col < cols; ++col)
389 thisRow[col] = ppm_lookupcolor(cht, &pa[row][col]);
390 } else {
391 /* No pixel array -- we're just faking this */
392 }
393 }
394 }
395
396
397
398 static ICON_bmp *
399 newBitmap(unsigned int const bpp,
400 pixel ** const pa,
401 unsigned int const cols,
402 unsigned int const rows,
403 colorhash_table const cht) {
404
405 unsigned int const xByteCt = ROUNDUP(cols * bpp, 32)/8;
406 /* How wide the u1 string for each row should be. Pixels are packed
407 into bytes, padded to a multiple of 4 bytes.
408 */
409 ICON_bmp * icBitmapP; /* malloc'ed */
410 u1 ** rowData; /* malloc'ed */
411
412 assert(cols < 256); assert(rows < 256); /* required for no overflow */
413
414 MALLOCVAR_NOFAIL(icBitmapP);
415
416 MALLOCARRAY_NOFAIL(rowData, rows);
417
418 icBitmapP->xBytes = xByteCt;
419 icBitmapP->data = rowData;
•420 overflow2(xByteCt,rows);
421 icBitmapP->size = xByteCt * rows;
422
423 unsigned int const assumedBpp = (pa == NULL) ? 1 : bpp;
424
425 switch (assumedBpp) {
426 case 1:
427 fillInRaster1(rowData, xByteCt, pa, cols, rows, cht);
428 break;
429 case 4:
430 fillInRaster4(rowData, xByteCt, pa, cols, rows, cht);
431 break;
432 case 8:
433 default:
434 fillInRaster8(rowData, xByteCt, pa, cols, rows, cht);
435 break;
436 }
437 return icBitmapP;
438 }
439
440
441
442 static IC_InfoHeader *
443 newInfoHeader(IC_Entry const entry) {
444
445 IC_InfoHeader * ihP;
446
447 MALLOCVAR_NOFAIL(ihP);
448
449 ihP->size = 40;
450 ihP->width = entry.width;
451 ihP->height = entry.height * 2;
452 ihP->planes = 1;
453 ihP->bitcount = entry.bitcount;
454 ihP->compression = 0;
455 ihP->imagesize = entry.width * entry.height * 8 / entry.bitcount;
456 ihP->x_pixels_per_m = 0;
457 ihP->y_pixels_per_m = 0;
458 ihP->colors_used = 1 << entry.bitcount;
459 ihP->colors_important = 0;
460
461 return ihP;
462 }
463
464
465
466 static IC_Palette *
467 newCleanPalette(void) {
468
469 IC_Palette * paletteP; /* malloc'ed */
470
471 unsigned int i;
472
473 MALLOCVAR_NOFAIL(paletteP);
474
475 MALLOCARRAY_NOFAIL(paletteP->colors, MAXCOLORS);
476
477 for (i=0; i <MAXCOLORS; ++i) {
478 paletteP->colors[i] = NULL;
479 }
480 return paletteP;
481 }
482
483
484
485 static void
486 addColorToPalette(IC_Palette * const paletteP,
487 unsigned int const i,
488 unsigned int const r,
489 unsigned int const g,
490 unsigned int const b) {
491
492 MALLOCVAR_NOFAIL(paletteP->colors[i]);
493
494 paletteP->colors[i]->red = r;
495 paletteP->colors[i]->green = g;
496 paletteP->colors[i]->blue = b;
497 paletteP->colors[i]->reserved = 0;
498 }
499
500
501
502 static void
503 makePalette(pixel ** const xorPPMarray,
504 unsigned int const xorCols,
505 unsigned int const xorRows,
506 pixval const xorMaxval,
507 IC_Palette ** const palettePP,
508 colorhash_table * const xorChtP,
509 unsigned int * const colorsP,
510 const char ** const errorP) {
511 /*----------------------------------------------------------------------------
512 Figure out the colormap and turn it into the appropriate GIF colormap -
513 this code's pretty much straight from 'ppmtobpm'.
514 -----------------------------------------------------------------------------*/
515 IC_Palette * const paletteP = newCleanPalette();
516
517 colorhist_vector xorChv;
518 unsigned int i;
519 int colorCt;
520
521 if (verbose)
522 pm_message("computing colormap...");
523
524 xorChv = ppm_computecolorhist(xorPPMarray, xorCols, xorRows, MAXCOLORS,
525 &colorCt);
526 if (!xorChv)
527 pm_asprintf(errorP,
528 "image has too many colors - try doing a 'pnmquant %u'",
529 MAXCOLORS);
530 else {
531 *errorP = NULL;
532
533 if (verbose)
534 pm_message("%u colors found", colorCt);
535
536 if (verbose && (xorMaxval > 255))
537 pm_message("maxval is not 255 - automatically rescaling colors");
538 for (i = 0; i < colorCt; ++i) {
539 if (xorMaxval == 255) {
540 addColorToPalette(paletteP, i,
541 PPM_GETR(xorChv[i].color),
542 PPM_GETG(xorChv[i].color),
543 PPM_GETB(xorChv[i].color));
544 } else {
545 addColorToPalette(paletteP, i,
546 PPM_GETR(xorChv[i].color) * 255 / xorMaxval,
547 PPM_GETG(xorChv[i].color) * 255 / xorMaxval,
548 PPM_GETB(xorChv[i].color) * 255 / xorMaxval);
549 }
550 }
551
552 /* And make a hash table for fast lookup. */
553 *xorChtP = ppm_colorhisttocolorhash(xorChv, colorCt);
554
555 ppm_freecolorhist(xorChv);
556
557 *palettePP = paletteP;
558 *colorsP = colorCt;
559 }
560 }
561
562
563
564 static void
565 getOrFakeAndMap(const char * const andPgmFname,
566 unsigned int const xorCols,
567 unsigned int const xorRows,
568 gray *** const andPGMarrayP,
569 pixval * const andMaxvalP,
570 colorhash_table * const andChtP,
571 const char ** const errorP) {
572
573 int andRows, andCols;
574
575 if (!andPgmFname) {
576 /* He's not supplying a bitmap for 'and'. Fake the bitmap. */
577 *andPGMarrayP = NULL;
578 *andMaxvalP = 1;
579 *andChtP = NULL;
580 *errorP = NULL;
581 } else {
582 FILE * andFileP;
583 andFileP = pm_openr(andPgmFname);
584 *andPGMarrayP = pgm_readpgm(andFileP, &andCols, &andRows, andMaxvalP);
585 pm_close(andFileP);
586
587 if ((andCols != xorCols) || (andRows != xorRows)) {
588 pm_asprintf(errorP,
589 "And mask and image have different dimensions "
590 "(%u x %u vs %u x %u). Aborting.",
591 andCols, xorCols, andRows, xorRows);
592 } else
593 *errorP = NULL;
594 }
595 }
596
597
598
599 static void
600 blackenTransparentAreas(pixel ** const xorPPMarray,
601 unsigned int const cols,
602 unsigned int const rows,
603 gray ** const andPGMarray,
604 pixval const andMaxval) {
605
606 unsigned int row;
607
608 if (verbose)
609 pm_message("Setting transparent pixels to black");
610
611 for (row = 0; row < rows; ++row) {
612 unsigned int col;
613
614 for (col = 0; col < cols; ++col) {
615 if (andPGMarray[row][col] < andMaxval)
616 /* It's not opaque here; make it black */
617 PPM_ASSIGN(xorPPMarray[row][col], 0, 0, 0);
618 }
619 }
620 }
621
622
623
624 static void
625 addEntryToIcon(MS_Ico * const MSIconDataP,
626 const char * const xorPpmFname,
627 const char * const andPgmFname,
628 bool const trueTransparent) {
629
630 IC_Entry * entryP;
631 FILE * xorfile;
632 pixel ** xorPPMarray;
633 gray ** andPGMarray;
634 ICON_bmp * xorBitmapP;
635 ICON_bmp * andBitmapP;
636 int rows, cols;
637 unsigned int bpp;
638 unsigned int colorCt;
639 unsigned int entryCols;
640 IC_Palette * paletteP;
641 colorhash_table xorCht;
642 colorhash_table andCht;
643 const char * error;
644
645 pixval xorMaxval;
646 gray andMaxval;
647
648 MALLOCVAR_NOFAIL(entryP);
649
650 /* Read the xor PPM. */
651 xorfile = pm_openr(xorPpmFname);
652 xorPPMarray = ppm_readppm(xorfile, &cols, &rows, &xorMaxval);
653 pm_close(xorfile);
654
655 /* Since the entry uses 1 byte to hold the width and height of the icon,
656 the image can't be more than 256 x 256.
657 */
658 if (rows > 255 || cols > 255) {
659 pm_error("Max size for a icon is 255 x 255 (1 byte fields). "
660 "%s is %u x %u", xorPpmFname, cols, rows);
661 }
662
663 if (verbose)
664 pm_message("read PPM: %uw x %uh, maxval = %u", cols, rows, xorMaxval);
665
666 makePalette(xorPPMarray, cols, rows, xorMaxval,
667 &paletteP, &xorCht, &colorCt, &error);
668
669 if (error)
670 pm_error("Unable to make palette for '%s'. %s", xorPpmFname, error);
671
672 /* All the icons I found seemed to pad the palette to the max entries for
673 that bitdepth.
674
675 The spec indicates this isn't necessary, but I'll follow this behaviour
676 just in case.
677 */
678 if (colorCt < 3) {
679 bpp = 1;
680 entryCols = 2;
681 } else if (colorCt < 17) {
682 bpp = 4;
683 entryCols = 16;
684 } else {
685 bpp = 8;
686 entryCols = 256;
687 }
688
689 getOrFakeAndMap(andPgmFname, cols, rows,
690 &andPGMarray, &andMaxval, &andCht, &error);
691 if (error)
692 pm_error("Error in and map for '%s'. %s", xorPpmFname, error);
693
694 if (andPGMarray && trueTransparent)
695 blackenTransparentAreas(xorPPMarray, cols, rows,
696 andPGMarray, andMaxval);
697
698 xorBitmapP = newBitmap(bpp, xorPPMarray, cols, rows, xorCht);
699 andBitmapP = newAndBitmap(andPGMarray, cols, rows, andMaxval);
700
701 /* Fill in the entry data fields. */
702 entryP->width = cols;
703 entryP->height = rows;
704 entryP->color_count = entryCols;
705 entryP->reserved = 0;
706 entryP->planes = 1;
707 entryP->bitcount = bpp;
708 /* all the icons I looked at ignored this value */
709 entryP->ih = newInfoHeader(*entryP);
710 entryP->colors = paletteP->colors;
•711 overflow2(4, entryCols);
712 overflow_add(xorBitmapP->size, andBitmapP->size);
713 overflow_add(xorBitmapP->size + andBitmapP->size, 40);
714 overflow_add(xorBitmapP->size + andBitmapP->size + 40, 4 * entryCols);
715 entryP->size_in_bytes =
716 xorBitmapP->size + andBitmapP->size + 40 + (4 * entryCols);
717 if (verbose)
718 pm_message("entry->size_in_bytes = %u + %u + %u = %u",
719 xorBitmapP->size, andBitmapP->size,
720 40, entryP->size_in_bytes );
721
722 /* We don't know the offset ATM, set to 0 for now. Have to calculate this
723 at the end.
724 */
725 entryP->file_offset = 0;
726 entryP->xorBitmapOut = xorBitmapP->data;
727 entryP->andBitmapOut = andBitmapP->data;
728 entryP->xBytesXor = xorBitmapP->xBytes;
729 entryP->xBytesAnd = andBitmapP->xBytes;
730
731 /* Add the entry to the entries array. */
732 ++MSIconDataP->count;
733
734 /* Perhaps I should allocate ahead, and take fewer trips to the well. */
735 REALLOCARRAY(MSIconDataP->entries, MSIconDataP->count);
736 MSIconDataP->entries[MSIconDataP->count - 1] = entryP;
737 }
738
739
740
741 static void
742 writeIC_Entry(FILE * const ofP,
743 IC_Entry * const entryP) {
744
745 writeU1(ofP, entryP->width);
746 writeU1(ofP, entryP->height);
747 writeU1(ofP, entryP->color_count); /* chops 256->0 on its own.. */
748 writeU1(ofP, entryP->reserved);
749 writeU2(ofP, entryP->planes);
750 writeU2(ofP, entryP->bitcount);
751 writeU4(ofP, entryP->size_in_bytes);
752 writeU4(ofP, entryP->file_offset);
753 }
754
755
756
757 static void
758 writeIC_InfoHeader(FILE * const ofP,
759 IC_InfoHeader * const ihP) {
760
761 writeU4(ofP, ihP->size);
762 writeU4(ofP, ihP->width);
763 writeU4(ofP, ihP->height);
764 writeU2(ofP, ihP->planes);
765 writeU2(ofP, ihP->bitcount);
766 writeU4(ofP, ihP->compression);
767 writeU4(ofP, ihP->imagesize);
768 writeU4(ofP, ihP->x_pixels_per_m);
769 writeU4(ofP, ihP->y_pixels_per_m);
770 writeU4(ofP, ihP->colors_used);
771 writeU4(ofP, ihP->colors_important);
772 }
773
774
775
776 static void
777 writeIC_Color(FILE * const ofP,
778 IC_Color * const colorP) {
779
780 /* Since the ppm might not have as many colors in it as we'd like,
781 (2, 16, 256), stick 0 in the gaps.
782
783 This means that we lose palette information, but that can't be helped.
784 */
785 if (!colorP) {
786 writeU1(ofP, 0);
787 writeU1(ofP, 0);
788 writeU1(ofP, 0);
789 writeU1(ofP, 0);
790 } else {
791 writeU1(ofP, colorP->blue);
792 writeU1(ofP, colorP->green);
793 writeU1(ofP, colorP->red);
794 writeU1(ofP, colorP->reserved);
795 }
796 }
797
798
799
800 static void
801 writeBitmap(FILE * const ofP,
802 u1 ** const bitmap,
803 unsigned int const xByteCt,
804 unsigned int const height) {
805
806 unsigned int row;
807
808 for (row = 0; row < height; ++row) {
809 fwrite(bitmap[row], 1, xByteCt, ofP);
810 fileOffset += xByteCt;
811 }
812 }
813
814
815
816 static void
817 writeMS_Ico(MS_Ico * const MSIconDataP,
818 const char * const outFname) {
819
820 FILE * const ofP = pm_openw(outFname);
821
822 unsigned int i;
823
824 writeU2(ofP, MSIconDataP->reserved);
825 writeU2(ofP, MSIconDataP->type);
826 writeU2(ofP, MSIconDataP->count);
827
828 for (i = 0; i < MSIconDataP->count; ++i)
829 writeIC_Entry(ofP, MSIconDataP->entries[i]);
830
831 for (i = 0; i < MSIconDataP->count; ++i) {
832 IC_Entry * const entryP = MSIconDataP->entries[i];
833
834 unsigned int j;
835
836 writeIC_InfoHeader(ofP, MSIconDataP->entries[i]->ih);
837
838 for (j = 0; j < (MSIconDataP->entries[i]->color_count); ++j) {
839 writeIC_Color(ofP, MSIconDataP->entries[i]->colors[j]);
840 }
841 if (verbose)
842 pm_message("writing xor bitmap");
843
844 writeBitmap(ofP,
845 entryP->xorBitmapOut, entryP->xBytesXor, entryP->height);
846 if (verbose)
847 pm_message("writing and bitmap");
848
849 writeBitmap(ofP,
850 entryP->andBitmapOut, entryP->xBytesAnd, entryP->height);
851 }
852 fclose(ofP);
853 }
854
855
856
857 int
858 main(int argc, const char ** argv) {
859
860 MS_Ico * const MSIconDataP = newIconFile();
861
862 struct CmdlineInfo cmdline;
863 unsigned int iconIndex;
864 unsigned int offset;
865
866 pm_proginit(&argc, argv);
867
868 parseCommandLine(argc, argv, &cmdline);
869
870 verbose = cmdline.verbose;
871
872 for (iconIndex = 0; iconIndex < cmdline.iconCount; ++iconIndex) {
873 addEntryToIcon(MSIconDataP, cmdline.inputFileNm[iconIndex],
874 cmdline.andpgmFileNm[iconIndex],
875 cmdline.truetransparent);
876 }
877 /*
878 * Now we have to go through and calculate the offsets.
879 * The first infoheader starts at 6 + count*16 bytes.
880 */
881 offset = (MSIconDataP->count * 16) + 6;
882 for (iconIndex = 0; iconIndex < MSIconDataP->count; ++iconIndex) {
883 IC_Entry * const entryP = MSIconDataP->entries[iconIndex];
884
885 entryP->file_offset = offset;
886 /*
887 * Increase the offset by the size of this offset & data.
888 * this includes the size of the color data.
889 */
890 offset += entryP->size_in_bytes;
891 }
892 /*
893 * And now, we have to actually SAVE the .ico!
894 */
895 writeMS_Ico(MSIconDataP, cmdline.output);
896
897 freeCmdline(cmdline);
898
899 return 0;
900 }
901
902
903