made embeddable
[swftools.git] / lib / png.c
1 /*  png.c
2    
3    Copyright (c) 2003/2004/2005 Matthias Kramm <kramm@quiss.org>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <fcntl.h>
24 #include <zlib.h>
25
26 #ifdef EXPORT
27 #undef EXPORT
28 #endif
29
30 #ifdef PNG_INLINE_EXPORTS
31 #define EXPORT static
32 #else
33 #define EXPORT
34 #include "png.h"
35 #endif
36
37 typedef unsigned u32;
38
39 typedef struct _COL {
40     unsigned char a,r,g,b;
41 } COL;
42
43 static int png_read_chunk(char (*head)[4], int*destlen, unsigned char**destdata, FILE*fi)
44 {
45     unsigned int len;
46     unsigned char blen[4];
47     if(destlen) *destlen=0;
48     if(destdata) *destdata=0;
49     if(!fread(&blen, 4, 1, fi)) {
50         return 0;
51     }
52     if(!fread(head, 4, 1, fi)) {
53         return 0;
54     }
55     len = blen[0]<<24|blen[1]<<16|blen[2]<<8|blen[3];
56     if(destlen) *destlen = len;
57     if(destdata) {
58         if(!len) {
59             *destdata = 0;
60         } else {
61             *destdata = (unsigned char*)malloc(len);
62             if(!fread(*destdata, len, 1, fi)) {
63                 *destdata = 0;
64                 if(destlen) *destlen=0;
65                 return 0;
66             }
67         }
68         fseek(fi, 4, SEEK_CUR);
69
70     } else {
71         fseek(fi, len+4, SEEK_CUR);
72     }
73     return 1;
74 }
75
76 static unsigned int png_get_dword(FILE*fi)
77 {
78     unsigned int a;
79     unsigned char b[4];
80     fread(&b,4,1,fi);
81     return b[0]<<24|b[1]<<16|b[2]<<8|b[3];
82 }
83
84 struct png_header
85 {
86     int width;
87     int height;
88     int bpp;
89     int mode;
90 };
91
92 static int png_read_header(FILE*fi, struct png_header*header)
93 {
94     char id[4];
95     int len;
96     int ok=0;
97     unsigned char head[8] = {137,80,78,71,13,10,26,10};
98     unsigned char head2[8];
99     unsigned char*data;
100     fread(head2,8,1,fi);
101     if(strncmp((const char*)head,(const char*)head2,4))
102         return 0;
103     
104     while(png_read_chunk(&id, &len, &data, fi))
105     {
106         //printf("Chunk: %c%c%c%c (len:%d)\n", id[0],id[1],id[2],id[3], len);
107         if(!strncasecmp(id, "IHDR", 4)) {
108             char a,b,c,f,i;
109             if(len < 8) exit(1);
110             header->width = data[0]<<24|data[1]<<16|data[2]<<8|data[3];
111             header->height = data[4]<<24|data[5]<<16|data[6]<<8|data[7];
112             a = data[8];      // should be 8
113             b = data[9];      // should be 3(indexed) or 2(rgb)
114
115             c = data[10];     // compression mode (0)
116             f = data[11];     // filter mode (0)
117             i = data[12];     // interlace mode (0)
118
119             if(b!=0 && b!=4 && b!=2 && b!=3 && b!=6) {
120                 fprintf(stderr, "Image mode %d not supported!\n", b);
121                 return 0;
122             }
123             if(a!=8 && (b==2 || b==6)) {
124                 printf("Bpp %d in mode %d not supported!\n", a);
125                 return 0;
126             }
127             if(c!=0) {
128                 printf("Compression mode %d not supported!\n", c);
129                 return 0;
130             }
131             if(f!=0) {
132                 printf("Filter mode %d not supported!\n", f);
133                 return 0;
134             }
135             if(i!=0) {
136                 printf("Interlace mode %d not supported!\n", i);
137                 return 0;
138             }
139             //printf("%dx%d bpp:%d mode:%d comp:%d filter:%d interlace:%d\n",header->width, header->height, a,b,c,f,i);
140             header->bpp = a;
141             header->mode = b;
142             ok = 1;
143         } 
144         
145         free(data);
146     }
147     return ok;
148 }
149
150 typedef unsigned char byte;
151 #define ABS(a) ((a)>0?(a):(-(a)))
152 static inline byte PaethPredictor (byte a,byte b,byte c)
153 {
154         // a = left, b = above, c = upper left
155         int p = a + b - c;        // initial estimate
156         int pa = ABS(p - a);      // distances to a, b, c
157         int pb = ABS(p - b);
158         int pc = ABS(p - c);
159         // return nearest of a,b,c,
160         // breaking ties in order a,b,c.
161         if (pa <= pb && pa <= pc) 
162                 return a;
163         else if (pb <= pc) 
164                 return b;
165         else return c;
166 }
167
168 static void applyfilter1(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
169 {
170     int x;
171     unsigned char last=0;
172     unsigned char upperlast=0;
173
174     if(mode==0) {
175         for(x=0;x<width;x++) {
176             *dest = *src;
177             dest++;
178             src++;
179         }
180     }
181     else if(mode==1) {
182         for(x=0;x<width;x++) {
183             *dest = *src+last;
184             last = *dest;
185             dest++;
186             src++;
187         }
188     }
189     else if(mode==2) {
190         for(x=0;x<width;x++) {
191             *dest = *src+*old;
192             dest++;
193             old++;
194             src++;
195         }
196     }
197     else if(mode==3) {
198         for(x=0;x<width;x++) {
199             *dest = *src+(*old+last)/2;
200             dest++;
201             old++;
202             src++;
203         }
204     }
205     else if(mode==4) {
206         for(x=0;x<width;x++) {
207             *dest = *src+PaethPredictor(last,*old,upperlast);
208             last = *dest;
209             upperlast = *old;
210             dest++;
211             old++;
212             src++;
213         }
214     }    
215
216 }
217
218 static void applyfilter2(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
219 {
220     int x;
221     unsigned char lasta=0;
222     unsigned char lastb=0;
223     unsigned char upperlasta=0;
224     unsigned char upperlastb=0;
225
226     if(mode==0) {
227         for(x=0;x<width;x++) {
228             dest[0] = src[0];
229             dest[1] = src[1];
230             dest+=2;
231             src+=2;
232         }
233     }
234     else if(mode==1) {
235         for(x=0;x<width;x++) {
236             dest[0] = src[0]+lasta;
237             dest[1] = src[1]+lastb;
238             lasta = dest[0];
239             lastb = dest[1];
240             dest+=2;
241             src+=2;
242         }
243     }
244     else if(mode==2) {
245         for(x=0;x<width;x++) {
246             dest[0] = src[0]+old[0];
247             dest[1] = src[1]+old[1];
248             dest+=2;
249             old+=2;
250             src+=2;
251         }
252     }
253     else if(mode==3) {
254         for(x=0;x<width;x++) {
255             dest[0] = src[0]+(old[0]+lasta)/2;
256             dest[1] = src[1]+(old[1]+lastb)/2;
257             lasta = dest[0];
258             lastb = dest[1];
259             dest+=2;
260             old+=2;
261             src+=2;
262         }
263     }
264     else if(mode==4) {
265         for(x=0;x<width;x++) {
266             dest[0] = src[0]+PaethPredictor(lasta,old[0],upperlasta);
267             dest[1] = src[1]+PaethPredictor(lastb,old[1],upperlastb);
268             lasta = dest[0];
269             lastb = dest[1];
270             upperlasta = old[0];
271             upperlastb = old[1];
272             dest+=2;
273             old+=2;
274             src+=2;
275         }
276     }    
277 }
278
279
280 /* also performs 24 bit conversion! */
281 static void applyfilter3(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
282 {
283     int x;
284     unsigned char lastr=0;
285     unsigned char lastg=0;
286     unsigned char lastb=0;
287     unsigned char upperlastr=0;
288     unsigned char upperlastg=0;
289     unsigned char upperlastb=0;
290
291     if(mode==0) {
292         for(x=0;x<width;x++) {
293             dest[0] = 255;
294             dest[1] = src[0];
295             dest[2] = src[1];
296             dest[3] = src[2];
297             dest+=4;
298             src+=3;
299         }
300     }
301     else if(mode==1) {
302         for(x=0;x<width;x++) {
303             dest[0] = 255;
304             dest[1] = src[0]+lastr;
305             dest[2] = src[1]+lastg;
306             dest[3] = src[2]+lastb;
307             lastr = dest[1];
308             lastg = dest[2];
309             lastb = dest[3];
310             dest+=4;
311             src+=3;
312         }
313     }
314     else if(mode==2) {
315         for(x=0;x<width;x++) {
316             dest[0] = 255;
317             dest[1] = src[0]+old[1];
318             dest[2] = src[1]+old[2];
319             dest[3] = src[2]+old[3];
320             dest+=4;
321             old+=4;
322             src+=3;
323         }
324     }
325     else if(mode==3) {
326         for(x=0;x<width;x++) {
327             dest[0] = 255;
328             dest[1] = src[0]+(old[1]+lastr)/2;
329             dest[2] = src[1]+(old[2]+lastg)/2;
330             dest[3] = src[2]+(old[3]+lastb)/2;
331             lastr = dest[1];
332             lastg = dest[2];
333             lastb = dest[3];
334             dest+=4;
335             old+=4;
336             src+=3;
337         }
338     }
339     else if(mode==4) {
340         for(x=0;x<width;x++) {
341             dest[0] = 255;
342             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
343             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
344             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
345             lastr = dest[1];
346             lastg = dest[2];
347             lastb = dest[3];
348             upperlastr = old[1];
349             upperlastg = old[2];
350             upperlastb = old[3];
351             dest+=4;
352             old+=4;
353             src+=3;
354         }
355     }    
356 }
357
358 static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
359 {
360     int x;
361     unsigned char lastr=0;
362     unsigned char lastg=0;
363     unsigned char lastb=0;
364     unsigned char lasta=0;
365     unsigned char upperlastr=0;
366     unsigned char upperlastg=0;
367     unsigned char upperlastb=0;
368     unsigned char upperlasta=0;
369
370     if(mode==0) {
371         for(x=0;x<width;x++) {
372             dest[0] = src[3];
373             dest[1] = src[0];
374             dest[2] = src[1];
375             dest[3] = src[2];
376             dest+=4;
377             src+=4;
378         }
379     }
380     else if(mode==1) {
381         for(x=0;x<width;x++) {
382             dest[0] = src[3]+lasta;
383             dest[1] = src[0]+lastr;
384             dest[2] = src[1]+lastg;
385             dest[3] = src[2]+lastb;
386             lasta = dest[0];
387             lastr = dest[1];
388             lastg = dest[2];
389             lastb = dest[3];
390             dest+=4;
391             src+=4;
392         }
393     }
394     else if(mode==2) {
395         for(x=0;x<width;x++) {
396             dest[0] = src[3]+old[0];
397             dest[1] = src[0]+old[1];
398             dest[2] = src[1]+old[2];
399             dest[3] = src[2]+old[3];
400             dest+=4;
401             old+=4;
402             src+=4;
403         }
404     }
405     else if(mode==3) {
406         for(x=0;x<width;x++) {
407             dest[0] = src[3]+(old[0]+lasta)/2;
408             dest[1] = src[0]+(old[1]+lastr)/2;
409             dest[2] = src[1]+(old[2]+lastg)/2;
410             dest[3] = src[2]+(old[3]+lastb)/2;
411             lasta = dest[0];
412             lastr = dest[1];
413             lastg = dest[2];
414             lastb = dest[3];
415             dest+=4;
416             old+=4;
417             src+=4;
418         }
419     }
420     else if(mode==4) {
421         for(x=0;x<width;x++) {
422             dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
423             dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
424             dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
425             dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
426             lasta = dest[0];
427             lastr = dest[1];
428             lastg = dest[2];
429             lastb = dest[3];
430             upperlasta = old[0];
431             upperlastr = old[1];
432             upperlastg = old[2];
433             upperlastb = old[3];
434             dest+=4;
435             old+=4;
436             src+=4;
437         }
438     }    
439 }
440
441
442 EXPORT int getPNGdimensions(char*sname, int*destwidth, int*destheight)
443 {
444     FILE*fi;
445     struct png_header header;
446     if ((fi = fopen(sname, "rb")) == NULL) {
447         fprintf(stderr, "Couldn't open %s\n", sname);
448         return 0;
449     }
450     if(!png_read_header(fi, &header)) {
451         fprintf(stderr, "Error reading header from file %s\n", sname);
452         return 0;
453     }
454
455     *destwidth = header.width;
456     *destheight = header.height;
457     return 1;
458 }
459
460 EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
461 {
462     char tagid[4];
463     int len;
464     unsigned char*data;
465     unsigned char*imagedata;
466     unsigned char*zimagedata=0;
467     unsigned long int imagedatalen;
468     unsigned long int zimagedatalen=0;
469     unsigned char*palette = 0;
470     int palettelen = 0;
471     unsigned char*alphapalette = 0;
472     int alphapalettelen = 0;
473     struct png_header header;
474     int bypp;
475     unsigned char*data2 = 0;
476     unsigned char alphacolor[3];
477     int hasalphacolor=0;
478
479     FILE *fi;
480     unsigned char *scanline;
481
482     if ((fi = fopen(sname, "rb")) == NULL) {
483         printf("Couldn't open %s\n", sname);
484         return 0;
485     }
486
487     if(!png_read_header(fi, &header)) {
488         printf("Error reading header from file %s\n", sname);
489         return 0;
490     }
491
492     if(header.mode == 3 || header.mode == 0) bypp = 1;
493     else if(header.mode == 4) bypp = 2;
494     else if(header.mode == 2) bypp = 3;
495     else if(header.mode == 6) bypp = 4;
496     else {
497         printf("ERROR: mode:%d\n", header.mode);
498         return 0;
499     }
500
501     imagedatalen = bypp * header.width * header.height + 65536;
502     imagedata = (unsigned char*)malloc(imagedatalen);
503
504     fseek(fi,8,SEEK_SET);
505     while(!feof(fi))
506     {
507         if(!png_read_chunk(&tagid, &len, &data, fi))
508             break;
509         if(!strncmp(tagid, "IEND", 4)) {
510             break;
511         }
512         if(!strncmp(tagid, "PLTE", 4)) {
513             palette = data;
514             palettelen = len/3;
515             data = 0; //don't free data
516             //printf("%d colors in palette\n", palettelen);
517         }
518         if(!strncmp(tagid, "tRNS", 4)) {
519             if(header.mode == 3) {
520                 alphapalette = data;
521                 alphapalettelen = len;
522                 data = 0; //don't free data
523                 //printf("found %d alpha colors\n", alphapalettelen);
524             } else if(header.mode == 0 || header.mode == 2) {
525                 int t;
526                 if(header.mode == 2) {
527                     alphacolor[0] = data[1];
528                     alphacolor[1] = data[3];
529                     alphacolor[2] = data[5];
530                 } else {
531                     alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
532                 }
533                 hasalphacolor = 1;
534             }
535         }
536         if(!strncmp(tagid, "IDAT", 4)) {
537             if(!zimagedata) {
538                 zimagedatalen = len;
539                 zimagedata = (unsigned char*)malloc(len);
540                 memcpy(zimagedata,data,len);
541             } else {
542                 zimagedata = (unsigned char*)realloc(zimagedata, zimagedatalen+len);
543                 memcpy(&zimagedata[zimagedatalen], data, len);
544                 zimagedatalen += len;
545             }
546         }
547         if(!strncmp(tagid, "tEXt", 4)) {
548             /*int t;
549             printf("Image Text: ");
550             for(t=0;t<len;t++) {
551                 if(data[t]>=32 && data[t]<128)
552                     printf("%c", data[t]);
553                 else
554                     printf("?");
555             }
556             printf("\n");*/
557         }
558         if(data) {
559             free(data); data=0;
560         }
561     }
562     
563     if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
564         printf("Couldn't uncompress %s!\n", sname);
565         if(zimagedata)
566             free(zimagedata);
567         return 0;
568     }
569     free(zimagedata);
570     fclose(fi);
571
572     *destwidth = header.width;
573     *destheight = header.height;
574         
575     data2 = (unsigned char*)malloc(header.width*header.height*4);
576
577     if(header.mode == 4)
578     {
579         int i,s=0;
580         int x,y;
581         int pos=0;
582         unsigned char* old= (unsigned char*)malloc(header.width*2);
583         memset(old, 0, header.width*2);
584         *destdata = data2;
585         for(y=0;y<header.height;y++) {
586             int mode = imagedata[pos++]; //filter mode
587             unsigned char*src;
588             unsigned char*dest;
589             int x;
590             dest = &data2[(y*header.width)*4];
591
592             if(header.bpp == 8) {
593                 /* one byte per pixel */
594                 src = &imagedata[pos];
595                 pos+=header.width*2;
596             } else {
597                 /* not implemented yet */
598                 fprintf(stderr, "ERROR: mode=4 bpp:%d\n", header.bpp);
599                 free(data2);
600                 return 0;
601             }
602
603             applyfilter2(mode, src, old, dest, header.width);
604             memcpy(old, dest, header.width*2);
605
606             for(x=header.width-1;x>=0;x--) {
607                 unsigned char gray = dest[x*2+0];
608                 unsigned char alpha = dest[x*2+1];
609                 dest[x*4+0] = alpha;
610                 dest[x*4+1] = gray;
611                 dest[x*4+2] = gray;
612                 dest[x*4+3] = gray;
613             }
614         }
615         free(old);
616         free(imagedata);
617     } else if(header.mode == 6 || header.mode == 2) {
618         int i,s=0;
619         int x,y;
620         int pos=0;
621         *destdata = data2;
622         for(y=0;y<header.height;y++) {
623             int mode = imagedata[pos++]; //filter mode
624             unsigned char*src;
625             unsigned char*dest;
626             unsigned char*old;
627             dest = &data2[(y*header.width)*4];
628
629             if(header.bpp == 8)
630             {
631                 /* one byte per pixel */
632                 src = &imagedata[pos];
633                 pos+=header.width*(header.mode==6?4:3);
634             } else {
635                 /* not implemented yet */
636                 fprintf(stderr, "ERROR: bpp:%d\n", header.bpp);
637                 free(data2);
638                 return 0;
639             }
640
641             if(!y) {
642                 memset(data2,0,header.width*4);
643                 old = &data2[y*header.width*4];
644             } else {
645                 old = &data2[(y-1)*header.width*4];
646             }
647             if(header.mode == 6) {
648                 applyfilter4(mode, src, old, dest, header.width);
649             } else { // header.mode = 2
650                 applyfilter3(mode, src, old, dest, header.width);
651                 /* replace alpha color */
652                 if(hasalphacolor) {
653                     int x;
654                     for(x=0;x<header.width;x++) {
655                         if(dest[x*4+1] == alphacolor[0] &&
656                            dest[x*4+2] == alphacolor[1] &&
657                            dest[x*4+3] == alphacolor[2]) {
658                             *(u32*)&dest[x*4] = 0;
659                         }
660                     }
661                 }
662             }
663         }
664         free(imagedata);
665     } else if(header.mode == 0 || header.mode == 3) {
666         COL*rgba = 0;
667         unsigned char*tmpline = (unsigned char*)malloc(header.width+1);
668         unsigned char*destline = (unsigned char*)malloc(header.width+1);
669         int i,x,y;
670         int pos=0;
671         
672         *destdata = data2;
673         
674         if(header.mode == 0) { // grayscale palette
675             int mult = (0x1ff>>header.bpp);
676             palettelen = 1<<header.bpp;
677             rgba = (COL*)malloc(palettelen*sizeof(COL));
678             for(i=0;i<palettelen;i++) {
679                 rgba[i].a = 255;
680                 rgba[i].r = i*mult;
681                 rgba[i].g = i*mult;
682                 rgba[i].b = i*mult;
683                 if(hasalphacolor) {
684                     if(rgba[i].r == alphacolor[0])
685                         rgba[i].a = 0;
686                 }
687             }
688         } else {
689             if(!palette) {
690                 fprintf(stderr, "Error: No palette found!\n");
691                 exit(1);
692             }
693             rgba = (COL*)malloc(palettelen*4);
694             /* 24->32 bit conversion */
695             for(i=0;i<palettelen;i++) {
696                 rgba[i].r = palette[i*3+0];
697                 rgba[i].g = palette[i*3+1];
698                 rgba[i].b = palette[i*3+2];
699                 if(alphapalette && i<alphapalettelen) {
700                     rgba[i].a = alphapalette[i];
701                     /*rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
702                     rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
703                     rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;*/
704                 } else {
705                     rgba[i].a = 255;
706                 }
707                 if(hasalphacolor) {
708                     if(rgba[i].r == alphacolor[0] &&
709                        rgba[i].g == alphacolor[1] &&
710                        rgba[i].b == alphacolor[2])
711                         rgba[i].a = 0;
712                 }
713             }
714         }
715
716         for(y=0;y<header.height;y++) {
717             int mode = imagedata[pos++]; //filter mode
718             int x;
719             unsigned char*old;
720             unsigned char*src;
721             src = &imagedata[pos];
722             if(header.bpp == 8) {
723                 pos+=header.width;
724             } else {
725                 int x,s=0;
726                 int bitpos = 0;
727                 u32 v = (1<<header.bpp)-1;
728                 for(x=0;x<header.width;x++) {
729                     u32 r = src[s/8]<<8 | 
730                             src[s/8+1];
731                     int t;
732                     tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
733                     s+=header.bpp;
734                 }
735                 src = tmpline;
736                 pos+=(header.width*header.bpp+7)/8;
737             }
738
739             if(!y) {
740                 memset(destline,0,header.width);
741                 old = &destline[y*header.width];
742             } else {
743                 old = tmpline;
744             }
745             applyfilter1(mode, src, old, destline, header.width);
746             memcpy(tmpline,destline,header.width);
747             for(x=0;x<header.width;x++) {
748                 *(COL*)&data2[y*header.width*4+x*4+0] = rgba[destline[x]];
749             }
750         }
751         free(tmpline);
752         free(destline);
753         free(rgba);
754         free(imagedata);
755     } else {
756         printf("expected PNG mode to be 2, 3 or 6 (is:%d)\n", header.mode);
757         return 0;
758     }
759
760     return 1;
761 }
762
763 static u32 mycrc32;
764
765 static u32*crc32_table = 0;
766 static void make_crc32_table(void)
767 {
768   int t;
769   if(crc32_table) 
770       return;
771   crc32_table = (u32*)malloc(1024);
772
773   for (t = 0; t < 256; t++) {
774     u32 c = t;
775     int s;
776     for (s = 0; s < 8; s++) {
777       c = (0xedb88320L*(c&1)) ^ (c >> 1);
778     }
779     crc32_table[t] = c;
780   }
781 }
782 static inline void png_write_byte(FILE*fi, unsigned char byte)
783 {
784     fwrite(&byte,1,1,fi);
785     mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
786 }
787 static void png_start_chunk(FILE*fi, char*type, int len)
788 {
789     unsigned char mytype[4]={0,0,0,0};
790     unsigned char mylen[4];
791     mylen[0] = len>>24;
792     mylen[1] = len>>16;
793     mylen[2] = len>>8;
794     mylen[3] = len;
795     memcpy(mytype,type,strlen(type));
796     fwrite(&mylen, 4, 1, fi);
797     mycrc32=0xffffffff;
798     png_write_byte(fi,mytype[0]);
799     png_write_byte(fi,mytype[1]);
800     png_write_byte(fi,mytype[2]);
801     png_write_byte(fi,mytype[3]);
802 }
803 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
804 {
805     int t;
806     for(t=0;t<len;t++)
807         png_write_byte(fi,bytes[t]);
808 }
809 static void png_write_dword(FILE*fi, u32 dword)
810 {
811     png_write_byte(fi,dword>>24);
812     png_write_byte(fi,dword>>16);
813     png_write_byte(fi,dword>>8);
814     png_write_byte(fi,dword);
815 }
816 static void png_end_chunk(FILE*fi)
817 {
818     u32 tmp = mycrc32^0xffffffff;
819     unsigned char tmp2[4];
820     tmp2[0] = tmp>>24;
821     tmp2[1] = tmp>>16;
822     tmp2[2] = tmp>>8;
823     tmp2[3] = tmp;
824     fwrite(&tmp2,4,1,fi);
825 }
826
827 EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
828 {
829     FILE*fi;
830     int crc;
831     int t;
832     unsigned char format;
833     unsigned char tmp;
834     unsigned char* data2=0;
835     unsigned char* data3=0;
836     u32 datalen;
837     u32 datalen2;
838     u32 datalen3;
839     unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
840     int cols;
841     char alpha = 1;
842     int pos = 0;
843     int error;
844     u32 tmp32;
845     int bpp;
846     int ret;
847
848     make_crc32_table();
849
850     bpp = 32;
851     cols = 0;
852     format = 5;
853
854     datalen = (width*height*bpp/8+cols*8);
855     
856     fi = fopen(filename, "wb");
857     if(!fi) {
858         perror("open");
859         return;
860     }
861     fwrite(head,sizeof(head),1,fi);     
862
863     png_start_chunk(fi, "IHDR", 13);
864      png_write_dword(fi,width);
865      png_write_dword(fi,height);
866      png_write_byte(fi,8);
867      if(format == 3)
868      png_write_byte(fi,3); //indexed
869      else if(format == 5 && alpha==0)
870      png_write_byte(fi,2); //rgb
871      else if(format == 5 && alpha==1)
872      png_write_byte(fi,6); //rgba
873      else return;
874
875      png_write_byte(fi,0); //compression mode
876      png_write_byte(fi,0); //filter mode
877      png_write_byte(fi,0); //interlace mode
878     png_end_chunk(fi);
879    
880 /*    if(format == 3) {
881         png_start_chunk(fi, "PLTE", 768);
882          
883          for(t=0;t<256;t++) {
884              png_write_byte(fi,palette[t].r);
885              png_write_byte(fi,palette[t].g);
886              png_write_byte(fi,palette[t].b);
887          }
888         png_end_chunk(fi);
889     }*/
890     {
891         int pos2 = 0;
892         int x,y;
893         int srcwidth = width * (bpp/8);
894         datalen3 = (width*4+5)*height;
895         data3 = (unsigned char*)malloc(datalen3);
896         for(y=0;y<height;y++)
897         {
898            data3[pos2++]=0; //filter type
899            for(x=0;x<width;x++) {
900                data3[pos2++]=data[pos+1];
901                data3[pos2++]=data[pos+2];
902                data3[pos2++]=data[pos+3];
903                data3[pos2++]=data[pos+0]; //alpha
904                pos+=4;
905            }
906            pos+=((srcwidth+3)&~3)-srcwidth; //align
907         }
908         datalen3=pos2;
909     }
910
911     datalen2 = datalen3;
912     data2 = (unsigned char*)malloc(datalen2);
913
914     if((ret = compress (data2, &datalen2, data3, datalen3)) != Z_OK) {
915         fprintf(stderr, "zlib error in pic %d\n", ret);
916         return;
917     }
918     png_start_chunk(fi, "IDAT", datalen2);
919     png_write_bytes(fi,data2,datalen2);
920     png_end_chunk(fi);
921     png_start_chunk(fi, "IEND", 0);
922     png_end_chunk(fi);
923
924     free(data2);
925     free(data3);
926 }