File: readWriteICO.c

package info (click to toggle)
xpaint 2.9.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,552 kB
  • sloc: ansic: 73,017; sh: 492; yacc: 247; lex: 126; sed: 43; makefile: 10
file content (750 lines) | stat: -rw-r--r-- 19,249 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/* ico2xpm.c -- Convert icons to pixmaps
 * Copyright (C) 1998 Philippe Martin
 * Modified by Brion Vibber
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>

#include <X11/Intrinsic.h>
#include <X11/xpm.h>

#include "xpaint.h"
#include "image.h"
#include "libpnmrw.h"

extern void * xmalloc(size_t n);

#if defined(__alpha) || (defined(_MIPS_SZLONG) && _MIPS_SZLONG == 64)
typedef unsigned int DWord;
typedef int Long;
#else
typedef unsigned long DWord;
typedef long Long;
#endif

#define TRUE 1
#define FALSE 0

#ifndef MAX_PATH_LEN
#define MAX_PATH_LEN 1024
#endif

/* The maximum number of icons we'll try to handle in any one file */
#define MAXICONS 32

struct _RGBQuad
{
  unsigned char rgbBlue;
  unsigned char rgbGreen;
  unsigned char rgbRed;
  unsigned char rgbReserved;
};
typedef struct _RGBQuad RGBQuad;

struct _ColorMap
{
  DWord nbr_colors;
  RGBQuad *color_list;
};
typedef struct _ColorMap ColorMap;

struct _DrawData
{
  unsigned char *data;
  DWord width;
  DWord height;
};
typedef struct _DrawData DrawData;

typedef void (*ColorConverter) (XpmColor *color, RGBQuad *wincolor);

/* Header of a single icon image in an icon file */
typedef struct IconDirectoryEntry {
	unsigned char  bWidth;
	unsigned char  bHeight;
	unsigned char  bColorCount;
	unsigned char  bReserved;
	unsigned short wPlanes;
	unsigned short wBitCount;
	unsigned long  dwBytesInRes;
	unsigned long  dwImageOffset;
} ICONDIRENTRY;

/* Header of an icon file */
typedef struct ICONDIR {
	 unsigned short          idReserved;
	 unsigned short          idType;
	 unsigned short          idCount;
/*    ICONDIRENTRY  idEntries[1];*/
	 ICONDIRENTRY  idEntries[MAXICONS];
} ICONHEADER;

/* Bitmap header - this is on the images themselves */
typedef struct tagBITMAPINFOHEADER{
        unsigned long      biSize;

        long       biWidth;
        long       biHeight;
        unsigned short       biPlanes;
        unsigned short       biBitCount;
        unsigned long      biCompression;
        unsigned long      biSizeImage;
        long       biXPelsPerMeter;
        long       biYPelsPerMeter;
        unsigned long      biClrUsed;
        unsigned long      biClrImportant;
} BITMAPINFOHEADER;

/* Magic number for an icon file */
/* This is the the idReserved and idType fields of the header */
static char ico_magic_number[4] = {0, 0, 1, 0};

/*
 * Conversion
 */

/* The color index of a pixel in the icon colormap */
#define PIXEL_INDEX(x,y) \
   (color_level == 4 ? \
     ((x) % 2 == 0 ? \
      (image [bytes_per_line * (height - 1 - (y)) / 2 + (x) / 2] & 0xF0) >> 4 \
	 : image [bytes_per_line * (height - 1 - (y)) / 2 + (x) / 2] & 0x0F) \
      : image [bytes_per_line * (height - 1 - (y)) + (x)])
     
/* The color index of a pixel in the (reduced) pixmap colormap */
#define PIXEL_REDUCED_INDEX(x,y) \
     (reduced_colormap_index [PIXEL_INDEX((x),(y))])
  
/* The transparency of a pixel */
#define MASK_BYTE(x,y) (bytes_per_mask_line * (height - 1 - (y)) + (x) / 8)
#define MASK_BIT(x,y) (7 - (x) % 8)
#define IS_TRANSPARENT(x,y) \
     (mask[MASK_BYTE((x),(y))] & (1 << MASK_BIT((x),(y))))
  
Image *
ReadICO (char *file)
{
   Image *xpaint_image = NULL;

   FILE *ico_stream;
/* Values read from icon file */
   ICONHEADER iconheader;
   BITMAPINFOHEADER bitmapinfoheader;
/*   unsigned char magic_number [sizeof(ico_magic_number)];*/
   unsigned char *magic_number;
   unsigned char color_level;
   unsigned char colormap [256 * 4];
   unsigned char image [256 * 256]; 
   unsigned char mask [256 * 32];

/* Different variables computed from the read ones */
   int whichimage = 0;
   int ncolors;
   int width = 0;
   int height = 0;
   unsigned char bytes_per_line;
   unsigned char bytes_per_pixel;
   unsigned char bytes_per_mask_line;
   unsigned char *dp, *mp = NULL;
   int image_length;
   int mask_length;
   int i, x, y;

/* Variables for the reduced colormap */
   int *reduced_colormap_index = NULL;
   int *color_used = NULL;
   int nb_color_used;

/* Does the icon have transparent pixels ? */
   int have_transparent_pixels;

  /* 
   * Read the icon file 
   */
  
  if ((ico_stream = fopen (file, "r")) == NULL)
      return NULL;

  /* Check the magic number & header */
  /*fread (magic_number, 1, sizeof(ico_magic_number), ico_stream);*/
  fread (&iconheader, 1, 6 /*sizeof(unsigned short)*3*/, ico_stream);
  magic_number = (unsigned char *)&iconheader;

  if (memcmp(magic_number, ico_magic_number, sizeof(ico_magic_number))) {
       fprintf (stderr, "ReadICO: %s: %s\n",
		   file, "Not a recognized icon file !!");
       fclose (ico_stream);
       return NULL;
  }
  
  /* Read in the rest of the icon directory entries */
  fread(&iconheader.idEntries[0],iconheader.idCount,
    /*sizeof(ICONDIRENTRY)*/ 16, ico_stream);
  
  /* Cycle through each icon image 
     and take largest width+height */
  for(whichimage = 0; whichimage < iconheader.idCount; whichimage++) {

    /* For debugging
      fprintf(stdout, "(%d) identry: %d %d %d %d %d %d %d %d\n",
        whichimage,
        (int)iconheader.idEntries[whichimage].bWidth,
        (int)iconheader.idEntries[whichimage].bHeight,
        (int)iconheader.idEntries[whichimage].bColorCount,
        (int)iconheader.idEntries[whichimage].bReserved,
        (int)iconheader.idEntries[whichimage].wPlanes,
        (int)iconheader.idEntries[whichimage].wBitCount,
        (int)iconheader.idEntries[whichimage].dwBytesInRes,
        (int)iconheader.idEntries[whichimage].dwImageOffset);
    */
    
    /* Read the icon size */
    if (iconheader.idEntries[whichimage].bWidth +
        iconheader.idEntries[whichimage].bWidth <=
        width + height) continue;
    width = iconheader.idEntries[whichimage].bWidth;
    height = iconheader.idEntries[whichimage].bHeight;

    /* Let's surf on over to the bitmap image & read the BMIH. */
    fseek (ico_stream, iconheader.idEntries[whichimage].dwImageOffset,SEEK_SET);
    fread (&bitmapinfoheader, 1, sizeof(bitmapinfoheader), ico_stream);

    /* Read the number of colors.
     * TODO: add support for monochrome, 24-bit icons
     */
    /*
        fprintf(stderr,
          "ReadICO: %s:\n   Number of colors in image "
          "(%d planes, %d bpp)\n",
          file,
          (int)bitmapinfoheader.biPlanes,
          (int)bitmapinfoheader.biBitCount);
        fprintf(stderr, 
          "width = %d, height = %d (bpl = %d)\n", width, height); 
    */

    color_level = bitmapinfoheader.biPlanes * bitmapinfoheader.biBitCount ;
    if (color_level&3 || color_level<4 || color_level>32 || 
        (color_level>8 && color_level&7)) {
        fprintf(stderr,
          "ReadICO: %s:\n   Unsupported number of colors in image! "
          "Skipping. (%d planes, %d bpp)\n",
          file,
          (int)bitmapinfoheader.biPlanes,
          (int)bitmapinfoheader.biBitCount);
        return NULL;
    }
    
    /* Determine the number of bytes defining a line of the icon. */
    if (color_level>=16) {
       bytes_per_pixel = color_level/8;
       bytes_per_line =  4 * ((bytes_per_pixel * width * 8 + 31) / 32);
       bytes_per_mask_line = 4 * ((width + 31) / 32);
    } else {
       bytes_per_line =  4 * ((width * 8 + 31) / 32);
       bytes_per_pixel = 1;
       bytes_per_mask_line = 4 * ((width + 31) / 32);
    }
    /*
    if (color_level==TRUE_COLOR) {
       bytes_per_mask_line = 0;
    } else {
       bytes_per_line =  4 * ((width + 3) / 4);
       bytes_per_mask_line = 4 * ((width + 31) / 32);
    }
    */

    /* Read the colormap */
    if (bitmapinfoheader.biClrImportant != 0)
      ncolors = bitmapinfoheader.biClrImportant;
    else if (bitmapinfoheader.biClrUsed != 0)
      ncolors = bitmapinfoheader.biClrUsed;
    else
      ncolors = 1 << (bitmapinfoheader.biPlanes*bitmapinfoheader.biBitCount);

    if (ncolors>256) ncolors = 256;
    if (color_level>=16) ncolors = 0;

    fseek (ico_stream, iconheader.idEntries[whichimage].dwImageOffset
      + bitmapinfoheader.biSize, SEEK_SET);
    fread (colormap, 1, ncolors * 4, ico_stream);

    /* Read the image */
    if (color_level == 4)
        image_length = bytes_per_line * height / 2;
    else
        image_length = bytes_per_line * height;

    fread (image, 1, image_length, ico_stream);

    /* Read the mask */
    mask_length = bytes_per_mask_line * height;
    fread (mask, 1, mask_length, ico_stream);

    /* Read error / prematured EOF  occured ? */

    if (feof (ico_stream))
      fprintf (stderr, "ReadICO: %s: Premature end of file.\n", file);
    if (ferror (ico_stream))
      fprintf (stderr, "ReadICO: %s: Read error.\n", file);
    if (feof (ico_stream) || ferror (ico_stream))
      {
        fclose (ico_stream);
        return NULL;
      }

    /* Reduce the number of colors */
    if (ncolors) {
        color_used = (int *)xmalloc(ncolors*sizeof(int));
        reduced_colormap_index = (int *)xmalloc(ncolors*sizeof(int));
    }

    for (i = 0 ; i < ncolors ; i++)
      color_used [i] = 0;

    if (ncolors)  
    for (y = 0 ; y <height ; y++)
      for (x = 0 ; x < width ; x++)
        color_used [PIXEL_INDEX(x,y)] = 1;

    nb_color_used = 0;
    for (i = 0 ; i < ncolors ; i++)
      if (color_used [i])
        reduced_colormap_index[i] = nb_color_used ++;

    /* Check for at least one transparent pixel */
    have_transparent_pixels = 0;
    for (y=0; y<height && have_transparent_pixels == 0 ; y++)
      for (x=0; x<width; x++)
        if (IS_TRANSPARENT(x,y)) {
	    have_transparent_pixels = 1;
	    break;
	}
  
    /*
     * Write the image
     */

    if (xpaint_image) ImageDelete(xpaint_image);
    xpaint_image = ImageNew(width, height);
    dp = xpaint_image->data;

    if (have_transparent_pixels) {
	ImageMakeMask(xpaint_image);
	mp = xpaint_image->alpha;
    }
    /* Write the image */
    for (y=0; y<height; y++)
      {
        for (x=0; x<width; x++)
	  {
	    if (IS_TRANSPARENT(x,y)) {
	        *dp++ = 0;
	        *dp++ = 0;
	        *dp++ = 0;
		*mp = True;
	    } else {
	      if (color_level>=16) {
                /* Fix it : this should not work for color_level == 16 ... */
                i = bytes_per_line * (height-1-y) + bytes_per_pixel * x + 2;
                *dp++ = image[i--];
                *dp++ = image[i--];
                *dp++ = image[i];
	      } else {
	        i = 4 * PIXEL_INDEX(x,y);
	        *dp++ = colormap[i--];
	        *dp++ = colormap[i--];
	        *dp++ = colormap[i];
	      }
	    }
	    mp++;
	  }
      }
  }
  fclose (ico_stream);
  free(color_used);
  return xpaint_image;
}

void
write_byte (FILE *fd, unsigned char c)
{
  fputc(c, fd);
}

void
write_word (FILE *fd, short word)
{
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
   fwrite (&word, sizeof (short), 1, fd);
#else
  int i = 0;

  while (i < sizeof (short)) {
    fputc (word & 0xff, fd);
    word >>= 8;
    i++;
  }
#endif
}

void
write_dword(FILE *fd, DWord dword)
{
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
  fwrite (&dword, sizeof (DWord), 1, fd);
#else
  int i = 0;

  while (i < sizeof (DWord)) {
    fputc (dword & 0xff, fd);
    dword >>= 8;
    i++;
  }
#endif
}

void
write_long(FILE *fd, Long dword)
{
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
  fwrite (&dword, sizeof (Long), 1, fd);
#else
  int i = 0;

  while (i < sizeof (Long)) {
    fputc (dword & 0xff, fd);
    dword >>= 8;
    i++;
  }
#endif
}

static RGBQuad s_color;

RGBQuad
convert_color (XpmColor *xpm_color)
{
  unsigned int r, g, b;
  if (xpm_color->c_color && xpm_color->c_color[0] == '#') {
     sscanf(xpm_color->c_color+1, "%4x%4x%4x", &r, &g, &b);
     s_color.rgbRed = r>>8;
     s_color.rgbGreen = g>>8;
     s_color.rgbBlue = b>>8;
     s_color.rgbReserved = 0;   
  } else {
     s_color.rgbRed = 0;
     s_color.rgbGreen = 0;
     s_color.rgbBlue = 0;
     s_color.rgbReserved = 0;
  }
  return s_color;
}

static int
white_present (ColorMap *color_map)
{
  RGBQuad *color_quad;
  int ccount, found;

  ccount = 0;
  found = 0;

  while (!found && ccount < color_map->nbr_colors)
    {
      color_quad = color_map->color_list + ccount;
      if ((color_quad->rgbRed == 255)
	  && (color_quad->rgbGreen == 255)
	  && (color_quad->rgbBlue == 255))
	found = !found;
      else
	ccount++;
    }

  return found;
}

void
add_white (ColorMap *color_map, unsigned int clr_count)
{
  RGBQuad *white;

  white = color_map->color_list + clr_count;
  white->rgbRed = 255;
  white->rgbGreen = 255;
  white->rgbBlue = 255;
  white->rgbReserved = 0;
}

static ColorMap *
convert_color_map (XpmImage *icon, DWord ncolors)
{
  unsigned int clr_count;
  ColorMap *color_map;

  if (ncolors <= 256)
    {
      color_map = xmalloc (sizeof (ColorMap));
      color_map->nbr_colors = ncolors;
      color_map->color_list = xmalloc (sizeof (RGBQuad) * ncolors);
      memset (color_map->color_list, 0, sizeof (RGBQuad) * ncolors);

      for (clr_count = 0; clr_count < icon->ncolors; clr_count++)
	*(color_map->color_list + clr_count) =
	  convert_color (icon->colorTable + clr_count);

      if (!white_present (color_map))
	{
	  if (clr_count < ncolors)
	    {
	      add_white (color_map, clr_count);
	      clr_count++;
	    }
	  else
	    fprintf (stderr,
		     "Could not add a white color to the colormap, icon may"
		     " not be displayed properly.\n");
	}
    }
  else
    {
      color_map = NULL;
    }

  return color_map;
}

static void
put_24bit_color (DrawData *draw_data,
		 short x, short y,
		 RGBQuad *color)
{
  unsigned char *ptr;

  ptr = draw_data->data
    + ((draw_data->height - y - 1)
       * draw_data->width
       + x) * 3;

  *ptr = color->rgbBlue;
  *(ptr + 1) = color->rgbGreen;
  *(ptr + 2) = color->rgbRed;
}

void
put_8bit_color (DrawData *draw_data,
		short x, short y,
		unsigned char val)
{
  unsigned char *ptr;

  ptr = draw_data->data
    + (draw_data->height - y - 1)
    * draw_data->width
    + x;

  *ptr = val;
}

static unsigned char *
make_xor_mask (XpmImage *icon, DWord ncolors)
{
  DWord mask_size, x, y;
  DWord *uint_ptr;
  short bypp;
  RGBQuad color; /* for bpp == 24 */
  DrawData *draw_data;
  unsigned char *mask;

  bypp = (ncolors <= 256)? 1 : 3;

  mask_size = (DWord) icon->width * (DWord) icon->height * bypp;

  draw_data = xmalloc (sizeof (DrawData));
  draw_data->data = xmalloc (mask_size);
  draw_data->width = (DWord) icon->width;
  draw_data->height = (DWord) icon->height;

  uint_ptr = (DWord *) icon->data;

  for (y = 0; y < icon->height; y++)
    for (x = 0; x < icon->width; x++)
      {
	if (bypp == 1)
	  put_8bit_color (draw_data, x, y, *uint_ptr);
	else
	  {
	    color = convert_color (icon->colorTable + *uint_ptr);
	    put_24bit_color (draw_data, x, y, &color);
	  }

	uint_ptr++;
      }

  mask = draw_data->data;
  free (draw_data);

  return mask;
}

static unsigned char *
make_and_mask (XpmImage *icon, ColorMap *color_map, DWord ncolors)
{
  DWord width, line_size, mask_size, x;
  DWord *uint_ptr;
  RGBQuad color; /* for bpp >= 16 */
  unsigned char *mask, *line;

  width = icon->width;
  line_size = ((width + 31) / 32) * 4;
  mask_size = icon->height * line_size;

  mask = xmalloc(mask_size * sizeof(unsigned char));
  bzero(mask, mask_size * sizeof(unsigned char));
  uint_ptr = (DWord *) icon->data;

  for (line = mask + mask_size - line_size; line >= mask; line -= line_size)
    for (x = 0; x < width; x++)
      {
	if (ncolors <= 256)
	  color = *(color_map->color_list + *uint_ptr);
	else
	  color = convert_color (icon->colorTable + *uint_ptr);

	if (color.rgbReserved)
	  line[x / 8] |= 128 >> (x % 8);

	uint_ptr++;
      }

  return mask;
}

int
WriteICO(char *file, Image * image)
{
    FILE *fd;
    unsigned char *xor_mask, *and_mask;
    int count, bypp;
    DWord dwBytesInRes, ncolors, xor_size, and_size, total_size;
    XpmAttributes attr;
    XpmImage icon;
    ColorMap *color_map;
    RGBQuad *rgb_quad;

    if (image->width > 256 || image->height > 256)
        return TRUE;
    if ((fd = fopen(file, "w")) == NULL)
	return TRUE;

    attr.valuemask = XpmColormap;
    attr.colormap = (Colormap) image->sourceColormap;

    if (XpmCreateXpmImageFromPixmap(Global.display, 
	image->sourcePixmap, image->sourceMask, &icon, &attr) != XpmSuccess)
        return TRUE;

    if (icon.ncolors <= 256) 
        ncolors = 256;
    else
        ncolors = 65536;
    bypp = (ncolors <= 256)? 1 : 3;

    color_map = convert_color_map (&icon, ncolors);

    xor_mask = make_xor_mask(&icon, ncolors);
    and_mask = make_and_mask(&icon, color_map, ncolors);
    xor_size = image->width * image->height * bypp;
    and_size = ((image->width+31)/32) * 4 * image->height;
    total_size = xor_size + and_size;

    /* write header */
    write_word(fd, 0);
    write_word(fd, 1);
    write_word(fd, 1);

    /* write directory entry */
    write_byte (fd, image->width);
    write_byte (fd, image->height);
    write_byte (fd, 0); /* bColorCount */
    write_byte (fd, 0); /* bReserved */
    write_word (fd, 0); /* wPlanes */
    write_word (fd, 0); /* wBitCount */
    if (ncolors == 256)
        dwBytesInRes = 40 + ncolors * 4 + total_size;
    else
        dwBytesInRes = 40 + total_size;
    write_dword (fd, dwBytesInRes);
    write_dword (fd, 22); /* dwImageOffset */

    /* write bitmap info header */
    write_dword (fd, 40);
    write_long (fd, image->width);
    write_long (fd, image->height * 2);
    write_word (fd, 1);
    write_word (fd, 8 * bypp);
    write_dword (fd, 0);
    write_dword (fd, total_size);
    write_long (fd, 0);
    write_long (fd, 0);
    write_dword (fd, (ncolors==256)? 256 : 0);
    write_dword (fd, 0);

    /* write colormap */
    rgb_quad = color_map->color_list;
    for (count = 0; count < color_map->nbr_colors; count++) {
        write_byte (fd, rgb_quad->rgbBlue);
        write_byte (fd, rgb_quad->rgbGreen);
        write_byte (fd, rgb_quad->rgbRed);
        write_byte (fd, 0);
        rgb_quad++;
    }

    fwrite (xor_mask, 1, xor_size, fd);
    fwrite (and_mask, 1, and_size, fd);
    
    XpmFreeXpmImage(&icon);
    free(color_map->color_list);
    free(color_map);
    free(and_mask);
    free(xor_mask);
    fclose(fd);
    return FALSE;
}

int 
TestICO(char *file)
{
   FILE *ico_stream;
/* Values read from icon file */
   ICONHEADER iconheader;
   unsigned char *magic_number;

   if ((ico_stream = fopen (file, "r")) == NULL)
      return FALSE;

   fread (&iconheader, 1, 6/*sizeof(unsigned short)*3*/, ico_stream);
   magic_number = (unsigned char *)&iconheader;

   if (memcmp(magic_number, ico_magic_number, sizeof(ico_magic_number))) {
       fclose (ico_stream);
       return FALSE;
   }
   return TRUE;
}