summaryrefslogtreecommitdiffstats
path: root/pngtoico.c
blob: cbbd1c940c026446650b0bb547b8288c7b516886 (plain)
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
/* $Id$ */
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2001 H. Peter Anvin - All Rights Reserved
 *
 *   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, Inc., 53 Temple Place Ste 330,
 *   Bostom MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * pngtoico.c
 *
 * Convert a set of palettized .png files to a Windoze .ico file
 *
 * The .png files may have transparency which will be reflected in
 * the .ico file; however, partial transparency (alpha values other
 * than 0 and 255) are not supported by the .ico format.
 *
 * Usage: pngtoico file1.png file2.png file3.png > file.ico
 */

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <png.h>

#define min(x,y) (((x) < (y)) ? (x) : (y))
#define max(x,y) (((x) > (y)) ? (x) : (y))

/* argv[0] */
const char *program;

/* Functions to write unaligned littleendian data */
static inline void
write_le16(uint8_t *ptr, uint16_t v)
{
#if defined(__i386__) || defined(__x86_64__)
  *(uint16_t *)ptr = v;
#else
  ptr[0] = (v & 0xff);
  ptr[1] = (v >> 8) & 0xff;
#endif
}

static inline void
write_le32(uint8_t *ptr, uint32_t v)
{
#if defined(__i386__) || defined(__x86_64__)
  *(uint32_t *)ptr = v;
#else
  ptr[0] = (v & 0xff);
  ptr[1] = (v >> 8) & 0xff;
  ptr[2] = (v >> 16) & 0xff;
  ptr[3] = (v >> 24) & 0xff;
#endif
}

struct ico_common {
  uint8_t resv[2];
  uint8_t type[2];		/* == 1 */
  uint8_t count[2];		/* Number of icons */
};

struct ico_hdr {
  uint8_t width;
  uint8_t height;
  uint8_t colorcount;
  uint8_t resv;			/* Always 0 */
  uint8_t planes[2];		/* Always 1 */
  uint8_t bitcount[2];		/* Bits per pixel (1, 4, 8) */
  uint8_t sizeinbytes[4];
  uint8_t fileoffset[4];
};

struct ico_info {
  uint8_t headersize[4];
  uint8_t width[4];
  uint8_t height[4];		/* Actually 2xheight */
  uint8_t planes[2];		/* Always 1 */
  uint8_t bitcount[2];		/* Bits per pixel (1, 4, 8) */
  uint8_t unused[6*4];		/* Always 0 */
};

struct ico_color {
  uint8_t b, g, r;
  uint8_t resv;
};

struct ico_fragment {
  struct ico_fragment *next;
  uint32_t offset;

  struct ico_hdr  hdr;
  struct ico_info info;
  struct ico_color *pal;
  uint32_t pal_size;
  uint8_t *data;
  uint32_t data_size;
  uint8_t *alpha;
  uint32_t alpha_size;
};

void *
xmalloc(size_t s)
{
  void *p = malloc(s);
  if ( !p ) {
    perror(program);
    exit(1);
  }

  return p;
}

void *
zalloc(size_t s)
{
  void *p = xmalloc(s);
  memset(p, 0, s);
  return p;
}

/*
 * Read a single PNG file and create corresponding ICO header and
 * body data.
 */
static struct ico_fragment *
read_png(const char *file)
{
  struct ico_fragment *frag;
  struct ico_hdr *hdr;
  struct ico_info *info;
  struct ico_color *pal = NULL;
  uint8_t *data = NULL;
  uint8_t *alpha = NULL;
  FILE *fp = NULL;
  png_structp png_ptr = NULL;
  png_infop info_ptr = NULL, end_info = NULL;
  png_uint_32 width, height;
  int dwidth, awidth, bit_depth, color_type;
  png_colorp palette;
  int num_palette, max_colors;
  png_bytep trans;
  int num_trans;
  uint8_t **row_pointers;
  uint8_t *dbyte, *abyte;
  int dshift, ashift, pixelshift;
  unsigned int x, y;
  int i;
  int datasize, alphasize;

  frag = zalloc(sizeof(*frag));
  
  hdr  = &frag->hdr;
  info = &frag->info;

  write_le32(info->headersize, sizeof(*info));
  write_le16(hdr->planes, 1);
  write_le16(info->planes, 1);

  fp = fopen(file, "rb");
  if ( !fp ) {
    goto perrexit;
  }

  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
				   NULL, NULL, NULL);
  if ( !png_ptr ) {
    errno = ENOMEM;
    goto perrexit;
  }

  info_ptr = png_create_info_struct(png_ptr);
  if ( !info_ptr ) {
    errno = ENOMEM;
    goto perrexit;
  }

  end_info = png_create_info_struct(png_ptr);
  if ( !end_info ) {
    errno = ENOMEM;
    goto perrexit;
  }

  if ( setjmp(png_ptr->jmpbuf) ) {
    fprintf(stderr, "%s: PNG format error\n", file);
    goto errexit;
  }

  png_init_io(png_ptr, fp);
  png_read_info(png_ptr, info_ptr);
  

  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
	       NULL, NULL, NULL);

  if ( height > 255 || width > 255 ) {
    fprintf(stderr, "%s: Image too large\n", file);
    goto errexit;
  }

  if ( color_type != PNG_COLOR_TYPE_PALETTE ) {
    fprintf(stderr, "%s: Not a palettized image\n", file);
    goto errexit;
  }

  if ( bit_depth > 8 ) {
    fprintf(stderr, "%s: Too many colors used\n", file);
    goto errexit;
  }

  if ( !png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) ) {
    fprintf(stderr, "%s: Palette data missing\n", file);
    goto errexit;
  }
  if ( !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, 0) ) {
    num_trans = 0;		/* Everything is fully opaque */
  }
  
  /* Expand data to one pixel/byte */
  if ( bit_depth < 8 )
    png_set_packing(png_ptr);

  /* Activate transforms */
  png_read_update_info(png_ptr, info_ptr);

  row_pointers = xmalloc(sizeof(uint8_t *) * height);
  for ( y = 0 ; y < height ; y++ ) {
    row_pointers[y] = xmalloc(width);
  }

  png_read_image(png_ptr, row_pointers);

  /* Now we have the image in memory.  Allocate memory for the
     icon output data. */

  hdr->width = width;   write_le32(info->width, width);
  hdr->height = height; write_le32(info->height, 2*height);

  if ( num_palette <= 2 ) {
    /* 1 bit/pixel */
    dwidth = ((width+31) & ~31) >> 3;
    pixelshift = 1;
  } else if ( num_palette <= 16 ) {
    /* 4 bits/pixel */
    dwidth = ((width+7) & ~7) >> 1;
    pixelshift = 4;
  } else {
    /* 8 bits/pixel */
    dwidth = (width+3) & ~3;
    pixelshift = 8;
  }
  max_colors = 1 << pixelshift;
  awidth = ((width+31) & ~31) >> 3;

  write_le16(hdr->bitcount,  pixelshift);
  write_le16(info->bitcount, pixelshift);

  frag->data  = data  = zalloc(datasize  = dwidth*height);
  frag->data_size = datasize;
  frag->alpha = alpha = zalloc(alphasize = awidth*height);
  frag->alpha_size = alphasize;

  hdr->colorcount = max_colors & 0xff;
  frag->pal = pal = zalloc(max_colors * sizeof(*pal));
  frag->pal_size = max_colors * sizeof(*pal);

  write_le32(hdr->sizeinbytes,
	     sizeof(*info)+frag->pal_size+datasize+alphasize);

  for ( i = 0 ; i < min(num_palette, max_colors) ; i++ ) {
    pal[i].r = palette[i].red;
    pal[i].g = palette[i].green;
    pal[i].b = palette[i].blue;
  }
  
  for ( y = 0 ; y < height ; y++ ) {
    dbyte = data  + (height-y-1)*dwidth;
    abyte = alpha + (height-y-1)*awidth;
    dshift = 8-pixelshift;  ashift = 7;

    for ( x = 0 ; x < width ; x++ ) {
      uint8_t index = row_pointers[y][x];
      uint8_t pixel = index;
      uint8_t ptran = (index < num_trans) ? trans[index] : 255;

      if ( ptran == 0 ) {
	pixel = 0;
      } else if ( ptran != 255 ) {
	  fprintf(stderr, "%s: Warning: partial transparency (%d/255) ignored\n",
		  file, ptran);
      }

      *dbyte |= pixel << dshift;
      dshift -= pixelshift;
      if ( dshift < 0 ) {
	dshift += 8;
	dbyte++;
      }

      *abyte |= (ptran ? 0 : 1) << ashift;
      ashift--;
      if ( ashift < 0 ) {
	ashift += 8;
	abyte++;
      }
    }
    
    /* Done with this row */
    free(row_pointers[y]);
  }
  free(row_pointers);

  /* Okay, we're done, clean up and return success */

  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  fclose(fp);
  return frag;

 perrexit:
  perror(file);
 errexit:
  if ( png_ptr ) {
    png_destroy_read_struct(png_ptr ? &png_ptr : NULL,
			    info_ptr ? &info_ptr : NULL,
			    end_info ? &end_info : NULL);
  }
  if ( fp ) fclose(fp);
  if ( pal )   free(pal);
  if ( data )  free(data);
  if ( alpha ) free(alpha);
  free(frag);
  return NULL;
}

/*
 * Main program: read all files, then stitch the ICO file together.
 */

int main(int argc, char *argv[])
{
  struct ico_fragment *frag;
  struct ico_fragment *list = NULL;
  struct ico_fragment **tail = &list;
  struct ico_common common;
  int count = 0;
  int i;
  uint32_t offset;

  for ( i = 1 ; i < argc ; i++ ) {
    if ( !(frag = read_png(argv[i])) )
      return 1;

    count++;
    *tail = frag;
    tail = &frag->next;
  }

  memset(&common, 0, sizeof(common));
  write_le16(common.type, 1);
  write_le16(common.count, count);

  /* Write header */
  fwrite(&common, sizeof(common), 1, stdout);

  offset = sizeof(common) + count*sizeof(struct ico_hdr);

  for ( frag = list ; frag ; frag = frag->next ) {
    write_le32(frag->hdr.fileoffset, offset);
    fwrite(&frag->hdr, sizeof(struct ico_hdr), 1, stdout);

    offset += sizeof(struct ico_info) + frag->pal_size + frag->data_size + frag->alpha_size;
  }

  for ( frag = list ; frag ; frag = frag->next ) {
    fwrite(&frag->info, sizeof(struct ico_info), 1, stdout);
    fwrite(frag->pal, 1, frag->pal_size, stdout);
    fwrite(frag->data, 1, frag->data_size, stdout);
    fwrite(frag->alpha, 1, frag->alpha_size, stdout);
  }

  return 0;
}