-
Notifications
You must be signed in to change notification settings - Fork 6
/
png2ico.cpp
645 lines (553 loc) · 19.7 KB
/
png2ico.cpp
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
/*
TODO:
>When feeding the image ms26.png (see message "what's wrong with this
>image") to png2ico I get the message
>
>libpng error: Read error
>
>How do I suppress the outputting of this message? I was surprised that
>libpng outputs a message in addition to jumping to the setjmp error
>handler (which outputs its own message). Do I need to write my own error
>handling functions or is there a simpler way (of course I don't want to
>recompile libpng itself)?
That message is the message printed by the setjmp error handler.
You can replace the error handler without recompiling libpng. Just
include a pointer to your error handler in png_read_create_struct().
See for example pngtest.c which illustrates replacing the error handler.
*/
/* Copyright (C) 2002 Matthias S. Benkmann <matthias@winterdrache.de>
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; version 2
of the License (ONLY THIS 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.
*/
/*
Notes about transparent and inverted pixels:
Handling of transparent pixels is inconsistent in Windows. Sometimes a
pixel with an AND mask value of 1 is just transparent (i.e. its color
value is ignored), sometimes the color value is XORed with the background to
give some kind of inverted effect. A closer look at bmp.txt suggests that
the latter behaviour is the correct one but because it often doesn't happen
it's de facto undefined behaviour.
Furthermore, sometimes the AND mask entry seems to be interpreted as a
color index, i.e. a value of 1 will AND the background with color 1.
Conclusion: The most robust solution seems to be:
-color 0 always 0,0,0
-color 1 always 255,255,255
-all transparent pixels get color 0
*/
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#if __GNUC__ > 2
#include <ext/hash_map>
#else
#include <hash_map>
#endif
#include <png.h>
#include "VERSION"
using namespace std;
namespace __gnu_cxx{};
using namespace __gnu_cxx;
const int word_max=65535;
const int transparency_threshold=196;
const int color_reduce_warning_threshold=512; //maximum quadratic euclidean distance in RGB color space that a palette color may have to a source color assigned to it before a warning is issued
const unsigned int slow_reduction_warn_threshold=262144; //number of colors in source image times number of colors in target image that triggers the warning that the reduction may take a while
void writeWord(FILE* f, int word)
{
char data[2];
data[0]=(word&255);
data[1]=(word>>8)&255;
if (fwrite(data,2,1,f)!=1) {perror("Write error"); exit(1);};
};
void writeDWord(FILE* f, unsigned int dword)
{
char data[4];
data[0]=(dword&255);
data[1]=(dword>>8)&255;
data[2]=(dword>>16)&255;
data[3]=(dword>>24)&255;
if (fwrite(data,4,1,f)!=1) {perror("Write error"); exit(1);};
};
void writeByte(FILE* f, int byte)
{
char data[1];
data[0]=(byte&255);
if (fwrite(data,1,1,f)!=1) {perror("Write error"); exit(1);};
};
struct png_data
{
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
png_uint_32 width, height;
png_colorp palette;
png_bytepp transMap;
int num_palette;
int requested_colors;
int col_bits;
png_data():png_ptr(NULL),info_ptr(NULL),end_info(NULL),width(0),height(0),
palette(NULL),transMap(NULL),num_palette(0),requested_colors(0),col_bits(0){};
};
int andMaskLineLen(const png_data& img)
{
int len=(img.width+7)>>3;
return (len+3)&~3;
};
int xorMaskLineLen(const png_data& img)
{
int pixelsPerByte=(8/img.col_bits);
return ((img.width+pixelsPerByte-1)/pixelsPerByte+3)&~3;
};
typedef bool (*checkTransparent_t)(png_bytep, png_data&);
bool checkTransparent1(png_bytep data, png_data&)
{
return (data[3]<transparency_threshold);
};
bool checkTransparent3(png_bytep, png_data&)
{
return false;
};
//returns true if color reduction resulted in at least one of the image's colors
//being mapped to a palette color with a quadratic distance of more than
//color_reduce_warning_threshold
bool convertToIndexed(png_data& img, bool hasAlpha)
{
int maxColors=img.requested_colors;
size_t palSize=sizeof(png_color)*256; //must reserve space for 256 entries here because write loop in main() expects it
img.palette=(png_colorp)malloc(palSize);
memset(img.palette,0,palSize); //must initialize whole palette because write loop in main() expects it
img.num_palette=0;
checkTransparent_t checkTrans=checkTransparent1;
int bytesPerPixel=4;
if (!hasAlpha)
{
bytesPerPixel=3;
checkTrans=checkTransparent3;
};
//first pass: gather all colors, make sure
//alpha channel (if present) contains only 0 and 255
//if an alpha channel is present, set all transparent pixels to RGBA (0,0,0,0)
//transparent pixels will already be mapped to palette entry 0, non-transparent
//pixels will not get a mapping yet (-1)
hash_map<unsigned int,signed int> mapQuadToPalEntry;
png_bytep* row_pointers=png_get_rows(img.png_ptr, img.info_ptr);
for (int y=img.height-1; y>=0; --y)
{
png_bytep pixel=row_pointers[y];
for (unsigned i=0; i<img.width; ++i)
{
unsigned int quad=pixel[0]+(pixel[1]<<8)+(pixel[2]<<16);
bool trans=(*checkTrans)(pixel,img);
if (hasAlpha)
{
if (trans)
{
pixel[0]=0;
pixel[1]=0;
pixel[2]=0;
pixel[3]=0;
quad=0;
}
else pixel[3]=255;
quad+=(pixel[3]<<24);
}
else if (!trans) quad+=(255<<24);
if (trans)
mapQuadToPalEntry[quad]=0;
else
mapQuadToPalEntry[quad]=-1;
pixel+=bytesPerPixel;
};
};
//always allocate entry 0 to black and entry 1 to white because
//sometimes AND mask is interpreted as color index
img.num_palette=2;
img.palette[0].red=0;
img.palette[0].green=0;
img.palette[0].blue=0;
img.palette[1].red=255;
img.palette[1].green=255;
img.palette[1].blue=255;
mapQuadToPalEntry[255<<24]=0; //map (non-transparent) black to entry 0
mapQuadToPalEntry[255+(255<<8)+(255<<16)+(255<<24)]=1; //map (non-transparent) white to entry 1
if (mapQuadToPalEntry.size()*img.requested_colors>slow_reduction_warn_threshold)
{
fprintf(stdout,"Please be patient. My color reduction algorithm is really slow.\n");
};
//Now fill up the palette with colors from the image by repeatedly picking the
//color most different from the previously picked colors and adding this to the
//palette. This is done to make sure that in case there are more image colors than
//palette entries, palette entries are not wasted on similar colors.
while(img.num_palette<maxColors)
{
unsigned int mostDifferentQuad=0;
int mdqMinDist=-1; //smallest distance to an entry in the palette for mostDifferentQuad
int mdqDistSum=-1; //sum over all distances to palette entries for mostDifferentQuad
hash_map<unsigned int,signed int>::iterator stop=mapQuadToPalEntry.end();
hash_map<unsigned int,signed int>::iterator iter=mapQuadToPalEntry.begin();
while(iter!=stop)
{
hash_map<unsigned int,signed int>::value_type& mapping=*iter++;
if (mapping.second<0)
{
unsigned int quad=mapping.first;
int red=quad&255; //must be signed
int green=(quad>>8)&255;
int blue=(quad>>16)&255;
int distSum=0;
int minDist=INT_MAX;
for (int i=0; i<img.num_palette; ++i)
{
int dist=(red-img.palette[i].red);
dist*=dist;
int temp=(green-img.palette[i].green);
dist+=temp*temp;
temp=(blue-img.palette[i].blue);
dist+=temp*temp;
if (dist<minDist) minDist=dist;
distSum+=dist;
};
if (minDist>mdqMinDist || (minDist==mdqMinDist && distSum>mdqDistSum))
{
mostDifferentQuad=quad;
mdqMinDist=minDist;
mdqDistSum=distSum;
};
};
};
if (mdqMinDist>0) //if we have found a most different quad, add it to the palette
{ //and map it to the new palette entry
int palentry=img.num_palette;
img.palette[palentry].red=mostDifferentQuad&255;
img.palette[palentry].green=(mostDifferentQuad>>8)&255;
img.palette[palentry].blue=(mostDifferentQuad>>16)&255;
mapQuadToPalEntry[mostDifferentQuad]=palentry;
++img.num_palette;
}
else break; //otherwise (i.e. all quads are mapped) the palette is finished
};
//Now map all yet unmapped colors to the most appropriate palette entry
hash_map<unsigned int,signed int>::iterator stop=mapQuadToPalEntry.end();
hash_map<unsigned int,signed int>::iterator iter=mapQuadToPalEntry.begin();
while(iter!=stop)
{
hash_map<unsigned int,signed int>::value_type& mapping=*iter++;
if (mapping.second<0)
{
unsigned int quad=mapping.first;
int red=quad&255; //must be signed
int green=(quad>>8)&255;
int blue=(quad>>16)&255;
int minDist=INT_MAX;
int bestIndex=0;
for (int i=0; i<img.num_palette; ++i)
{
int dist=(red-img.palette[i].red);
dist*=dist;
int temp=(green-img.palette[i].green);
dist+=temp*temp;
temp=(blue-img.palette[i].blue);
dist+=temp*temp;
if (dist<minDist) { minDist=dist; bestIndex=i; };
};
mapping.second=bestIndex;
};
};
//Adjust all palette entries (except for 0 and 1) to be the mean of all
//colors mapped to it
for (int i=2; i<img.num_palette; ++i)
{
int red=0;
int green=0;
int blue=0;
int numMappings=0;
hash_map<unsigned int,signed int>::iterator stop=mapQuadToPalEntry.end();
hash_map<unsigned int,signed int>::iterator iter=mapQuadToPalEntry.begin();
while(iter!=stop)
{
hash_map<unsigned int,signed int>::value_type& mapping=*iter++;
if (mapping.second==i)
{
unsigned int quad=mapping.first;
red+=quad&255;
green+=(quad>>8)&255;
blue+=(quad>>16)&255;
++numMappings;
};
};
if (numMappings>0)
{
img.palette[i].red=(red+red+numMappings)/(numMappings+numMappings);
img.palette[i].green=(green+green+numMappings)/(numMappings+numMappings);
img.palette[i].blue=(blue+blue+numMappings)/(numMappings+numMappings);
};
};
//Now determine if a non-transparent source color got mapped to a target color that
//has a distance that exceeds the threshold
bool tooManyColors=false;
stop=mapQuadToPalEntry.end();
iter=mapQuadToPalEntry.begin();
while(iter!=stop)
{
hash_map<unsigned int,signed int>::value_type& mapping=*iter++;
unsigned int quad=mapping.first;
if ((quad>>24)!=0) //if color is not transparent
{
int red=quad&255;
int green=(quad>>8)&255;
int blue=(quad>>16)&255;
int i=mapping.second;
int dist=(red-img.palette[i].red);
dist*=dist;
int temp=(green-img.palette[i].green);
dist+=temp*temp;
temp=(blue-img.palette[i].blue);
dist+=temp*temp;
if (dist>color_reduce_warning_threshold) tooManyColors=true;
};
};
int transLineLen=andMaskLineLen(img);
int transLinePad=transLineLen - ((img.width+7)/8);
img.transMap=(png_bytepp)malloc(img.height*sizeof(png_bytep));
//second pass: convert RGB to palette entries
for (int y=img.height-1; y>=0; --y)
{
png_bytep row=row_pointers[y];
png_bytep pixel=row;
int count8=0;
int transbyte=0;
png_bytep transPtr=img.transMap[y]=(png_bytep)malloc(transLineLen);
for (unsigned i=0; i<img.width; ++i)
{
bool trans=((*checkTrans)(pixel,img));
unsigned int quad=pixel[0]+(pixel[1]<<8)+(pixel[2]<<16);
if (!trans) quad+=(255<<24); //NOTE: alpha channel has already been set to 255 for non-transparent pixels, so this is correct even for images with alpha channel
if (trans) ++transbyte;
if (++count8==8)
{
*transPtr++ = transbyte;
count8=0;
transbyte=0;
};
transbyte+=transbyte; //shift left 1
int palentry=mapQuadToPalEntry[quad];
row[i]=palentry;
pixel+=bytesPerPixel;
};
for(int i=0; i<transLinePad; ++i) *transPtr++ = 0;
};
return tooManyColors;
};
//packs a line of width pixels (1 byte per pixel) in row, with 8/nbits pixels packed
//into each byte
//returns the new number of bytes in row
int pack(png_bytep row,int width,int nbits)
{
int pixelsPerByte=8/nbits;
if (pixelsPerByte<=1) return width;
int ander=(1<<nbits)-1;
int outByte=0;
int count=0;
int outIndex=0;
for (int i=0; i<width; ++i)
{
outByte+=(row[i]&ander);
if (++count==pixelsPerByte)
{
row[outIndex]=outByte;
count=0;
++outIndex;
outByte=0;
};
outByte<<=nbits;
};
if (count>0)
{
outByte<<=nbits*(pixelsPerByte-count);
row[outIndex]=outByte;
++outIndex;
};
return outIndex;
};
void usage()
{
fprintf(stderr,version"\n");
fprintf(stderr,"USAGE: png2ico icofile [--colors <num>] pngfile1 [pngfile2 ...]\n");
exit(1);
};
int main(int argc, char* argv[])
{
if (argc<3) usage();
if (argc-2 > word_max)
{
fprintf(stderr,"Too many PNG files\n");
exit(1);
};
vector<png_data> pngdata;
static int numColors=256; //static to get rid of longjmp() clobber warning
static const char* outfileName=NULL;
//i is static because used in a setjmp() block
for (static int i=1; i<argc; ++i)
{
if (strcmp(argv[i],"--colors")==0)
{
++i;
if (i>=argc)
{
fprintf(stderr,"Number missing after --colors\n");
exit(1);
};
char* endptr;
long num=strtol(argv[i],&endptr,10);
if (*(argv[i])==0 || *endptr!=0 || (num!=2 && num!=16 && num!=256))
{
fprintf(stderr,"Illegal number of colors\n");
exit(1);
};
numColors=num;
continue;
};
if (outfileName==NULL) { outfileName=argv[i]; continue; };
FILE* pngfile=fopen(argv[i],"rb");
if (pngfile==NULL) {perror(argv[i]); exit(1);};
png_byte header[8];
if (fread(header,8,1,pngfile)!=1) {perror(argv[i]); exit(1);};
if (png_sig_cmp(header,0,8))
{
fprintf(stderr,"%s: Not a PNG file\n",argv[i]);
exit(1);
};
png_data data;
data.requested_colors=numColors;
for (data.col_bits=1; (1<<data.col_bits)<numColors; ++data.col_bits);
data.png_ptr=png_create_read_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!data.png_ptr)
{
fprintf(stderr,"png_create_read_struct error\n");
exit(1);
};
data.info_ptr=png_create_info_struct(data.png_ptr);
if (!data.info_ptr)
{
png_destroy_read_struct(&data.png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fprintf(stderr,"png_create_info_struct error\n");
exit(1);
};
data.end_info=png_create_info_struct(data.png_ptr);
if (!data.end_info)
{
png_destroy_read_struct(&data.png_ptr, &data.info_ptr, (png_infopp)NULL);
fprintf(stderr,"png_create_info_struct error\n");
exit(1);
};
if (setjmp(png_jmpbuf(data.png_ptr)))
{
png_destroy_read_struct(&data.png_ptr, &data.info_ptr, &data.end_info);
fprintf(stderr,"%s: PNG error\n",argv[i]);
exit(1);
};
png_init_io(data.png_ptr, pngfile);
png_set_sig_bytes(data.png_ptr,8);
int trafo=PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16|PNG_TRANSFORM_EXPAND;
png_read_png(data.png_ptr, data.info_ptr, trafo , NULL);
int bit_depth, color_type, interlace_type, compression_type, filter_method;
png_get_IHDR(data.png_ptr, data.info_ptr, &data.width, &data.height, &bit_depth, &color_type,
&interlace_type, &compression_type, &filter_method);
if ( (data.width&7)!=0 || data.width>=256 || data.height>=256)
{
//I don't know if the following is really a requirement (bmp.txt says that
//only 16x16, 32x32 and 64x64 are allowed but that doesn't seem right) but
//if the width is not a multiple of 8, then the loop creating the and mask later
//doesn't work properly because it doesn't shift in padding bits
fprintf(stderr,"%s: Width must be multiple of 8 and <256. Height must be <256.\n",argv[i]);
exit(1);
};
if ((color_type & PNG_COLOR_MASK_COLOR)==0)
{
fprintf(stderr,"%s: Grayscale image not supported\n",argv[i]);
exit(1);
};
if (color_type==PNG_COLOR_TYPE_PALETTE)
{
fprintf(stderr,"This can't happen. PNG_TRANSFORM_EXPAND transforms image to RGB.\n");
exit(1);
}
else
{
if (convertToIndexed(data, ((color_type & PNG_COLOR_MASK_ALPHA)!=0)))
{
fprintf(stderr,"%s: Warning! Color reduction may not be optimal!\nIf the result is not satisfactory, reduce the number of colors\nbefore using png2ico.\n",argv[i]);
};
};
pngdata.push_back(data);
fclose(pngfile);
};
if (outfileName==NULL || pngdata.size()<1) usage();
FILE* outfile=fopen(outfileName,"wb");
if (outfile==NULL) {perror(argv[1]); exit(1);};
writeWord(outfile,0); //idReserved
writeWord(outfile,1); //idType
writeWord(outfile,pngdata.size()); //idCount
int offset=6+pngdata.size()*16;
vector<png_data>::const_iterator img;
for(img=pngdata.begin(); img!=pngdata.end(); ++img)
{
writeByte(outfile,img->width); //bWidth
writeByte(outfile,img->height); //bHeight
writeByte(outfile,img->requested_colors&255); //bColorCount
writeByte(outfile,0); //bReserved
writeWord(outfile,0); //wPlanes
writeWord(outfile,0); //wBitCount
int resSize=40+img->requested_colors*4+(andMaskLineLen(*img)+xorMaskLineLen(*img))*img->height;
writeDWord(outfile,resSize); //dwBytesInRes
writeDWord(outfile,offset); //dwImageOffset
offset+=resSize;
};
for(img=pngdata.begin(); img!=pngdata.end(); ++img)
{
writeDWord(outfile,40); //biSize
writeDWord(outfile,img->width); //biWidth
writeDWord(outfile,2*img->height); //biHeight (2 times because the 2 masks are counted)
writeWord(outfile,1); //biPlanes
writeWord(outfile,img->col_bits); //biBitCount
writeDWord(outfile,0); //biCompression
writeDWord(outfile,(andMaskLineLen(*img)+xorMaskLineLen(*img))*img->height); //biSizeImage
writeDWord(outfile,0); //biXPelsPerMeter
writeDWord(outfile,0); //biYPelsPerMeter
writeDWord(outfile,0); //biClrUsed (MUST BE 0 ACCORDING TO bmp.txt!!! I tried putting the real number here, but this breaks icons in some places)
writeDWord(outfile,0); //biClrImportant
for (int i=0; i<img->requested_colors; ++i)
{
char col[4];
col[0]=img->palette[i].blue;
col[1]=img->palette[i].green;
col[2]=img->palette[i].red;
col[3]=0;
if (fwrite(col,4,1,outfile)!=1) {perror("Write error"); exit(1);};
};
png_bytep* row_pointers=png_get_rows(img->png_ptr, img->info_ptr);
for (int y=img->height-1; y>=0; --y)
{
png_bytep row=row_pointers[y];
int newLength=pack(row,img->width,img->col_bits);
if (fwrite(row,newLength,1,outfile)!=1) {perror("Write error"); exit(1);};
for(int i=0; i<xorMaskLineLen(*img)-newLength; ++i) writeByte(outfile,0);
};
for (int y=img->height-1; y>=0; --y)
{
png_bytep transPtr=img->transMap[y];
if (fwrite(transPtr,andMaskLineLen(*img),1,outfile)!=1) {perror("Write error"); exit(1);};
};
};
fclose(outfile);
};