3 Copyright (c) 2003/2004/2005 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
31 #ifdef PNG_INLINE_EXPORTS
41 unsigned char a,r,g,b;
44 static int png_read_chunk(char (*head)[4], int*destlen, unsigned char**destdata, FILE*fi)
47 unsigned char blen[4];
48 if(destlen) *destlen=0;
49 if(destdata) *destdata=0;
50 if(!fread(&blen, 4, 1, fi)) {
53 if(!fread(head, 4, 1, fi)) {
56 len = blen[0]<<24|blen[1]<<16|blen[2]<<8|blen[3];
57 if(destlen) *destlen = len;
62 *destdata = (unsigned char*)malloc(len);
63 if(!fread(*destdata, len, 1, fi)) {
65 if(destlen) *destlen=0;
69 fseek(fi, 4, SEEK_CUR);
72 fseek(fi, len+4, SEEK_CUR);
77 static unsigned int png_get_dword(FILE*fi)
82 return b[0]<<24|b[1]<<16|b[2]<<8|b[3];
93 static int png_read_header(FILE*fi, struct png_header*header)
98 unsigned char head[8] = {137,80,78,71,13,10,26,10};
99 unsigned char head2[8];
102 if(strncmp((const char*)head,(const char*)head2,4))
103 return 0; // not a png file
105 while(png_read_chunk(&id, &len, &data, fi))
107 //printf("Chunk: %c%c%c%c (len:%d)\n", id[0],id[1],id[2],id[3], len);
108 if(!strncmp(id, "IHDR", 4)) {
111 header->width = data[0]<<24|data[1]<<16|data[2]<<8|data[3];
112 header->height = data[4]<<24|data[5]<<16|data[6]<<8|data[7];
113 a = data[8]; // should be 8
114 b = data[9]; // should be 3(indexed) or 2(rgb)
116 c = data[10]; // compression mode (0)
117 f = data[11]; // filter mode (0)
118 i = data[12]; // interlace mode (0)
120 if(b!=0 && b!=4 && b!=2 && b!=3 && b!=6) {
121 fprintf(stderr, "Image mode %d not supported!\n", b);
124 if(a!=8 && (b==2 || b==6)) {
125 printf("Bpp %d in mode %d not supported!\n", a);
129 printf("Compression mode %d not supported!\n", c);
133 printf("Filter mode %d not supported!\n", f);
137 printf("Interlace mode %d not supported!\n", i);
140 //printf("%dx%d bpp:%d mode:%d comp:%d filter:%d interlace:%d\n",header->width, header->height, a,b,c,f,i);
151 typedef unsigned char byte;
152 #define ABS(a) ((a)>0?(a):(-(a)))
153 static inline byte PaethPredictor (byte a,byte b,byte c)
155 // a = left, b = above, c = upper left
156 int p = a + b - c; // initial estimate
157 int pa = ABS(p - a); // distances to a, b, c
160 // return nearest of a,b,c,
161 // breaking ties in order a,b,c.
162 if (pa <= pb && pa <= pc)
169 static void applyfilter1(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
172 unsigned char last=0;
173 unsigned char upperlast=0;
176 for(x=0;x<width;x++) {
183 for(x=0;x<width;x++) {
191 for(x=0;x<width;x++) {
199 for(x=0;x<width;x++) {
200 *dest = *src+(*old+last)/2;
208 for(x=0;x<width;x++) {
209 *dest = *src+PaethPredictor(last,*old,upperlast);
220 static void applyfilter2(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
223 unsigned char lasta=0;
224 unsigned char lastb=0;
225 unsigned char upperlasta=0;
226 unsigned char upperlastb=0;
229 for(x=0;x<width;x++) {
237 for(x=0;x<width;x++) {
238 dest[0] = src[0]+lasta;
239 dest[1] = src[1]+lastb;
247 for(x=0;x<width;x++) {
248 dest[0] = src[0]+old[0];
249 dest[1] = src[1]+old[1];
256 for(x=0;x<width;x++) {
257 dest[0] = src[0]+(old[0]+lasta)/2;
258 dest[1] = src[1]+(old[1]+lastb)/2;
267 for(x=0;x<width;x++) {
268 dest[0] = src[0]+PaethPredictor(lasta,old[0],upperlasta);
269 dest[1] = src[1]+PaethPredictor(lastb,old[1],upperlastb);
282 /* also performs 24 bit conversion! */
283 static void applyfilter3(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
286 unsigned char lastr=0;
287 unsigned char lastg=0;
288 unsigned char lastb=0;
289 unsigned char upperlastr=0;
290 unsigned char upperlastg=0;
291 unsigned char upperlastb=0;
294 for(x=0;x<width;x++) {
304 for(x=0;x<width;x++) {
306 dest[1] = src[0]+lastr;
307 dest[2] = src[1]+lastg;
308 dest[3] = src[2]+lastb;
317 for(x=0;x<width;x++) {
319 dest[1] = src[0]+old[1];
320 dest[2] = src[1]+old[2];
321 dest[3] = src[2]+old[3];
328 for(x=0;x<width;x++) {
330 dest[1] = src[0]+(old[1]+lastr)/2;
331 dest[2] = src[1]+(old[2]+lastg)/2;
332 dest[3] = src[2]+(old[3]+lastb)/2;
342 for(x=0;x<width;x++) {
344 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
345 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
346 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
360 static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
363 unsigned char lastr=0;
364 unsigned char lastg=0;
365 unsigned char lastb=0;
366 unsigned char lasta=0;
367 unsigned char upperlastr=0;
368 unsigned char upperlastg=0;
369 unsigned char upperlastb=0;
370 unsigned char upperlasta=0;
373 for(x=0;x<width;x++) {
383 for(x=0;x<width;x++) {
384 dest[0] = src[3]+lasta;
385 dest[1] = src[0]+lastr;
386 dest[2] = src[1]+lastg;
387 dest[3] = src[2]+lastb;
397 for(x=0;x<width;x++) {
398 dest[0] = src[3]+old[0];
399 dest[1] = src[0]+old[1];
400 dest[2] = src[1]+old[2];
401 dest[3] = src[2]+old[3];
408 for(x=0;x<width;x++) {
409 dest[0] = src[3]+(old[0]+lasta)/2;
410 dest[1] = src[0]+(old[1]+lastr)/2;
411 dest[2] = src[1]+(old[2]+lastg)/2;
412 dest[3] = src[2]+(old[3]+lastb)/2;
423 for(x=0;x<width;x++) {
424 dest[0] = src[3]+PaethPredictor(lasta,old[0],upperlasta);
425 dest[1] = src[0]+PaethPredictor(lastr,old[1],upperlastr);
426 dest[2] = src[1]+PaethPredictor(lastg,old[2],upperlastg);
427 dest[3] = src[2]+PaethPredictor(lastb,old[3],upperlastb);
444 EXPORT int getPNGdimensions(const char*sname, int*destwidth, int*destheight)
447 struct png_header header;
448 if ((fi = fopen(sname, "rb")) == NULL) {
449 fprintf(stderr, "Couldn't open %s\n", sname);
452 if(!png_read_header(fi, &header)) {
456 *destwidth = header.width;
457 *destheight = header.height;
461 EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char**destdata)
466 unsigned char*imagedata;
467 unsigned char*zimagedata=0;
468 unsigned long int imagedatalen;
469 unsigned long int zimagedatalen=0;
470 unsigned char*palette = 0;
472 unsigned char*alphapalette = 0;
473 int alphapalettelen = 0;
474 struct png_header header;
476 unsigned char*data2 = 0;
477 unsigned char alphacolor[3];
481 unsigned char *scanline;
483 if ((fi = fopen(sname, "rb")) == NULL) {
484 printf("Couldn't open %s\n", sname);
488 if(!png_read_header(fi, &header)) {
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;
497 printf("ERROR: mode:%d\n", header.mode);
501 imagedatalen = bypp * header.width * header.height + 65536;
502 imagedata = (unsigned char*)malloc(imagedatalen);
504 fseek(fi,8,SEEK_SET);
507 if(!png_read_chunk(&tagid, &len, &data, fi))
509 if(!strncmp(tagid, "IEND", 4)) {
512 if(!strncmp(tagid, "PLTE", 4)) {
515 data = 0; //don't free data
516 //printf("%d colors in palette\n", palettelen);
518 if(!strncmp(tagid, "tRNS", 4)) {
519 if(header.mode == 3) {
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) {
526 if(header.mode == 2) {
527 alphacolor[0] = data[1];
528 alphacolor[1] = data[3];
529 alphacolor[2] = data[5];
531 alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
536 if(!strncmp(tagid, "IDAT", 4)) {
539 zimagedata = (unsigned char*)malloc(len);
540 memcpy(zimagedata,data,len);
542 zimagedata = (unsigned char*)realloc(zimagedata, zimagedatalen+len);
543 memcpy(&zimagedata[zimagedatalen], data, len);
544 zimagedatalen += len;
547 if(!strncmp(tagid, "tEXt", 4)) {
549 printf("Image Text: ");
551 if(data[t]>=32 && data[t]<128)
552 printf("%c", data[t]);
563 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
564 printf("Couldn't uncompress %s!\n", sname);
572 *destwidth = header.width;
573 *destheight = header.height;
575 data2 = (unsigned char*)malloc(header.width*header.height*4);
582 unsigned char* old= (unsigned char*)malloc(header.width*2);
583 memset(old, 0, header.width*2);
585 for(y=0;y<header.height;y++) {
586 int mode = imagedata[pos++]; //filter mode
590 dest = &data2[(y*header.width)*4];
592 if(header.bpp == 8) {
593 /* one byte per pixel */
594 src = &imagedata[pos];
597 /* not implemented yet */
598 fprintf(stderr, "ERROR: mode=4 bpp:%d\n", header.bpp);
603 applyfilter2(mode, src, old, dest, header.width);
604 memcpy(old, dest, header.width*2);
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];
617 } else if(header.mode == 6 || header.mode == 2) {
623 unsigned char* firstline = malloc(header.width*4);
624 memset(firstline,0,header.width*4);
625 for(y=0;y<header.height;y++) {
626 int mode = imagedata[pos++]; //filter mode
630 dest = &data2[(y*header.width)*4];
634 /* one byte per pixel */
635 src = &imagedata[pos];
636 pos+=header.width*(header.mode==6?4:3);
638 /* not implemented yet */
639 fprintf(stderr, "ERROR: bpp:%d\n", header.bpp);
647 old = &data2[(y-1)*header.width*4];
649 if(header.mode == 6) {
650 applyfilter4(mode, src, old, dest, header.width);
651 } else { // header.mode = 2
652 applyfilter3(mode, src, old, dest, header.width);
653 /* replace alpha color */
656 for(x=0;x<header.width;x++) {
657 if(dest[x*4+1] == alphacolor[0] &&
658 dest[x*4+2] == alphacolor[1] &&
659 dest[x*4+3] == alphacolor[2]) {
660 *(u32*)&dest[x*4] = 0;
668 } else if(header.mode == 0 || header.mode == 3) {
670 unsigned char*tmpline = (unsigned char*)malloc(header.width+1);
671 unsigned char*destline = (unsigned char*)malloc(header.width+1);
677 if(header.mode == 0) { // grayscale palette
678 int mult = (0x1ff>>header.bpp);
679 palettelen = 1<<header.bpp;
680 rgba = (COL*)malloc(palettelen*sizeof(COL));
681 for(i=0;i<palettelen;i++) {
687 if(rgba[i].r == alphacolor[0])
693 fprintf(stderr, "Error: No palette found!\n");
696 rgba = (COL*)malloc(palettelen*4);
697 /* 24->32 bit conversion */
698 for(i=0;i<palettelen;i++) {
699 rgba[i].r = palette[i*3+0];
700 rgba[i].g = palette[i*3+1];
701 rgba[i].b = palette[i*3+2];
702 if(alphapalette && i<alphapalettelen) {
703 rgba[i].a = alphapalette[i];
704 /*rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
705 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
706 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;*/
711 if(rgba[i].r == alphacolor[0] &&
712 rgba[i].g == alphacolor[1] &&
713 rgba[i].b == alphacolor[2])
719 for(y=0;y<header.height;y++) {
720 int mode = imagedata[pos++]; //filter mode
724 src = &imagedata[pos];
725 if(header.bpp == 8) {
730 u32 v = (1<<header.bpp)-1;
731 for(x=0;x<header.width;x++) {
732 u32 r = src[s/8]<<8 |
735 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
739 pos+=(header.width*header.bpp+7)/8;
743 memset(destline,0,header.width);
744 old = &destline[y*header.width];
748 applyfilter1(mode, src, old, destline, header.width);
749 memcpy(tmpline,destline,header.width);
750 for(x=0;x<header.width;x++) {
751 *(COL*)&data2[y*header.width*4+x*4+0] = rgba[destline[x]];
759 printf("expected PNG mode to be 2, 3 or 6 (is:%d)\n", header.mode);
766 static char hasAlpha(unsigned char*_image, int size)
768 COL*image = (COL*)_image;
770 for(t=0;t<size;t++) {
782 static int compare_colors(const void*_c1, const void*_c2) {
783 colornum_t*c1 = (colornum_t*)_c1;
784 colornum_t*c2 = (colornum_t*)_c2;
785 return c2->num - c1->num;
788 static colornum_t* getColors(COL*image, int size, int*num)
790 unsigned char*colexists = malloc((256*256*256)/8);
791 memset(colexists, 0, (256*256*256)/8);
795 /* find all different colors in the image */
796 for(t=0;t<size;t++) {
797 int index = (image[t].r)|(image[t].g)<<8|(image[t].b)<<16;
798 if(!(colexists[index/8]&(1<<(index&7)))) {
800 colexists[index/8]|=(1<<(index&7));
804 /* now store them in an array */
805 colornum_t*colors=(colornum_t*)malloc(sizeof(colornum_t)*count);
807 for(t=0;t<256*256*256;t++) {
808 if(colexists[t/8]&(1<<(t&7))) {
809 colors[pos].color = t;
815 /* next, count how often each color occurs */
816 for(t=0;t<size;t++) {
817 int col = (image[t].r)|(image[t].g)<<8|(image[t].b)<<16;
819 for(min=0, max=count, i=count/2, l=count; i != l; l=i,i=(min+max)/2) {
821 if(colors[i].color >= col) max=i;
824 assert(colors[i].color==col);
832 static COL* getOptimalPalette(COL*image, int size, int palettesize)
835 COL* ret = malloc(sizeof(COL)*palettesize);
836 memset(ret, 0, sizeof(COL)*palettesize);
837 colornum_t*colors = getColors(image, size, &num);
839 assert(palettesize<=256);
841 qsort(colors, num, sizeof(colornum_t), compare_colors);
843 if(num<=palettesize) {
844 /* if there are not more than palettesize different colors in
845 the image anyway, we are done */
848 ret[t].r = colors[t].color;
849 ret[t].g = colors[t].color>>8;
850 ret[t].b = colors[t].color>>16;
857 /* if there are too many different colors, pick the ones that
862 colornum_t*centers = malloc(sizeof(colornum_t)*palettesize);
864 for(t=0;t<palettesize;t++) {
865 centers[t].color = colors[t].color;
867 unsigned char*belongsto = (unsigned char*)malloc(num);
868 memset(belongsto, 0, num);
869 /* do a k-means clustering on the colors */
873 if(tries++ >= (palettesize+num)*2) {
874 fprintf(stderr, "Warning: didn't find optimal palette\n");
879 for(s=0;s<palettesize;s++) {
885 for(s=0;s<palettesize;s++) {
887 distance += abs((centers[s].color>>0&0xff) - (colors[t].color>>0&0xff));
888 distance += abs((centers[s].color>>8&0xff) - (colors[t].color>>8&0xff));
889 distance += abs((centers[s].color>>16&0xff) - (colors[t].color>>16&0xff));
890 distance *= colors[t].num;
896 if(bestpos!=belongsto[t])
898 belongsto[t] = bestpos;
900 for(s=0;s<palettesize;s++) {
906 if(belongsto[t]==s) {
907 r += ((colors[t].color>>0)&0xff)*colors[t].num;
908 g += ((colors[t].color>>8)&0xff)*colors[t].num;
909 b += ((colors[t].color>>16)&0xff)*colors[t].num;
910 count+=colors[t].num;
914 int random = rand()%num;
915 centers[s].color = colors[random].color;
922 centers[s].color = r|g<<8|b<<16;
923 centers[s].num = count;
929 for(t=0;t<palettesize;t++) {
930 ret[t].r = centers[t].color;
931 ret[t].g = centers[t].color>>8;
932 ret[t].b = centers[t].color>>16;
939 static int sqr(const int x) {return x*x;}
941 static void quantizeImage(unsigned char*_image, int size, int numcolors, unsigned char**newimage, COL**palette)
943 COL*image = (COL*)_image;
944 COL*pal= getOptimalPalette(image, size, numcolors);
946 *newimage = (unsigned char*)malloc(size);
948 for(t=0;t<size;t++) {
952 for(s=0;s<numcolors;s++) {
954 distance += sqr((pal[s].r) - (image[t].r))*5;
955 distance += sqr((pal[s].g) - (image[t].g))*6;
956 distance += sqr((pal[s].b) - (image[t].b))*4;
962 //image[t] = pal[bestcol];
963 (*newimage)[t] = bestcol;
969 static u32*crc32_table = 0;
970 static void make_crc32_table(void)
975 crc32_table = (u32*)malloc(1024);
977 for (t = 0; t < 256; t++) {
980 for (s = 0; s < 8; s++) {
981 c = (0xedb88320L*(c&1)) ^ (c >> 1);
986 static inline void png_write_byte(FILE*fi, unsigned char byte)
988 fwrite(&byte,1,1,fi);
989 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
991 static long png_start_chunk(FILE*fi, char*type, int len)
993 unsigned char mytype[4]={0,0,0,0};
994 unsigned char mylen[4];
1000 memcpy(mytype,type,strlen(type));
1001 filepos = ftell(fi);
1002 fwrite(&mylen, 4, 1, fi);
1004 png_write_byte(fi,mytype[0]);
1005 png_write_byte(fi,mytype[1]);
1006 png_write_byte(fi,mytype[2]);
1007 png_write_byte(fi,mytype[3]);
1010 static void png_patch_len(FILE*fi, int pos, int len)
1012 unsigned char mylen[4];
1018 fseek(fi, pos, SEEK_SET);
1019 fwrite(&mylen, 4, 1, fi);
1020 fseek(fi, 0, SEEK_END);
1022 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
1026 png_write_byte(fi,bytes[t]);
1028 static void png_write_dword(FILE*fi, u32 dword)
1030 png_write_byte(fi,dword>>24);
1031 png_write_byte(fi,dword>>16);
1032 png_write_byte(fi,dword>>8);
1033 png_write_byte(fi,dword);
1035 static void png_end_chunk(FILE*fi)
1037 u32 tmp = mycrc32^0xffffffff;
1038 unsigned char tmp2[4];
1043 fwrite(&tmp2,4,1,fi);
1046 #define ZLIB_BUFFER_SIZE 16384
1048 static long compress_line(z_stream*zs, Bytef*line, int len, FILE*fi)
1055 int ret = deflate(zs, Z_NO_FLUSH);
1057 fprintf(stderr, "error in deflate(): %s", zs->msg?zs->msg:"unknown");
1060 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
1061 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
1063 png_write_bytes(fi, zs->next_out - consumed , consumed);
1064 zs->next_out = zs->next_out - consumed;
1065 zs->avail_out = ZLIB_BUFFER_SIZE;
1074 static int test_line(z_stream*zs_orig, Bytef*line, int linelen)
1077 int ret = deflateCopy(&zs, zs_orig);
1079 fprintf(stderr, "Couldn't copy stream\n");
1084 zs.avail_in = linelen;
1088 int mode = Z_SYNC_FLUSH;
1090 int ret = deflate(&zs, mode);
1091 if (ret != Z_OK && ret != Z_STREAM_END) {
1092 fprintf(stderr, "error in deflate(): %s (mode %s, %d bytes remaining)\n", zs.msg?zs.msg:"unknown",
1093 mode==Z_SYNC_FLUSH?"Z_SYNC_FLUSH":"Z_FINISH", zs.avail_in);
1096 if(zs.avail_out != ZLIB_BUFFER_SIZE) {
1097 int consumed = ZLIB_BUFFER_SIZE - zs.avail_out;
1099 zs.next_out = zs.next_out - consumed;
1100 zs.avail_out = ZLIB_BUFFER_SIZE;
1102 if (ret == Z_STREAM_END) {
1109 ret = deflateEnd(&zs);
1111 fprintf(stderr, "error in deflateEnd(): %s\n", zs.msg?zs.msg:"unknown");
1117 static int finishzlib(z_stream*zs, FILE*fi)
1122 ret = deflate(zs, Z_FINISH);
1124 ret != Z_STREAM_END) {
1125 fprintf(stderr, "error in deflate(finish): %s\n", zs->msg?zs->msg:"unknown");
1129 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
1130 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
1132 png_write_bytes(fi, zs->next_out - consumed , consumed);
1133 zs->next_out = zs->next_out - consumed;
1134 zs->avail_out = ZLIB_BUFFER_SIZE;
1136 if (ret == Z_STREAM_END) {
1140 ret = deflateEnd(zs);
1142 fprintf(stderr, "error in deflateEnd(): %s\n", zs->msg?zs->msg:"unknown");
1148 static void filter_line8(int filtermode, unsigned char*dest, unsigned char*src, int width)
1152 int srcwidth = width;
1154 if(filtermode == 0) {
1155 for(x=0;x<width;x++) {
1156 dest[pos2++]=src[pos++]; //alpha
1158 } else if(filtermode == 1) {
1159 /* x difference filter */
1160 dest[pos2++]=src[pos++];
1161 for(x=1;x<width;x++) {
1162 dest[pos2++]=src[pos] - src[pos-1];
1165 } else if(filtermode == 2) {
1166 /* y difference filter */
1167 for(x=0;x<width;x++) {
1168 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
1171 } else if(filtermode == 3) {
1172 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
1174 /* x+y difference filter */
1175 for(x=1;x<width;x++) {
1176 dest[pos2++]=src[pos+0] - (src[pos-1+0] + src[pos-srcwidth+0])/2; //alpha
1179 } else if(filtermode == 4) {
1180 dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
1182 /* paeth difference filter */
1183 for(x=1;x<width;x++) {
1184 dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-1+0], src[pos-srcwidth+0], src[pos-1-srcwidth+0]);
1190 static void filter_line32(int filtermode, unsigned char*dest, unsigned char*src, int width)
1194 int srcwidth = width*4;
1196 if(filtermode == 0) {
1197 for(x=0;x<width;x++) {
1198 dest[pos2++]=src[pos+1];
1199 dest[pos2++]=src[pos+2];
1200 dest[pos2++]=src[pos+3];
1201 dest[pos2++]=src[pos+0]; //alpha
1204 } else if(filtermode == 1) {
1205 /* x difference filter */
1206 dest[pos2++]=src[pos+1];
1207 dest[pos2++]=src[pos+2];
1208 dest[pos2++]=src[pos+3];
1209 dest[pos2++]=src[pos+0];
1211 for(x=1;x<width;x++) {
1212 dest[pos2++]=src[pos+1] - src[pos-4+1];
1213 dest[pos2++]=src[pos+2] - src[pos-4+2];
1214 dest[pos2++]=src[pos+3] - src[pos-4+3];
1215 dest[pos2++]=src[pos+0] - src[pos-4+0]; //alpha
1218 } else if(filtermode == 2) {
1219 /* y difference filter */
1220 for(x=0;x<width;x++) {
1221 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1];
1222 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2];
1223 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3];
1224 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
1227 } else if(filtermode == 3) {
1228 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1]/2;
1229 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2]/2;
1230 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3]/2;
1231 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
1233 /* x+y difference filter */
1234 for(x=1;x<width;x++) {
1235 dest[pos2++]=src[pos+1] - (src[pos-4+1] + src[pos-srcwidth+1])/2;
1236 dest[pos2++]=src[pos+2] - (src[pos-4+2] + src[pos-srcwidth+2])/2;
1237 dest[pos2++]=src[pos+3] - (src[pos-4+3] + src[pos-srcwidth+3])/2;
1238 dest[pos2++]=src[pos+0] - (src[pos-4+0] + src[pos-srcwidth+0])/2; //alpha
1241 } else if(filtermode == 4) {
1242 dest[pos2++]=src[pos+1] - PaethPredictor(0, src[pos-srcwidth+1], 0);
1243 dest[pos2++]=src[pos+2] - PaethPredictor(0, src[pos-srcwidth+2], 0);
1244 dest[pos2++]=src[pos+3] - PaethPredictor(0, src[pos-srcwidth+3], 0);
1245 dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
1247 /* paeth difference filter */
1248 for(x=1;x<width;x++) {
1249 dest[pos2++]=src[pos+1] - PaethPredictor(src[pos-4+1], src[pos-srcwidth+1], src[pos-4-srcwidth+1]);
1250 dest[pos2++]=src[pos+2] - PaethPredictor(src[pos-4+2], src[pos-srcwidth+2], src[pos-4-srcwidth+2]);
1251 dest[pos2++]=src[pos+3] - PaethPredictor(src[pos-4+3], src[pos-srcwidth+3], src[pos-4-srcwidth+3]);
1252 dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-4+0], src[pos-srcwidth+0], src[pos-4-srcwidth+0]);
1258 EXPORT void savePNG(const char*filename, unsigned char*data, int width, int height, int numcolors)
1263 unsigned char format;
1265 unsigned char* data2=0;
1268 unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
1289 quantizeImage(data, width*height, numcolors, &data, &palette);
1292 datalen = (width*height*bpp/8+cols*8);
1294 fi = fopen(filename, "wb");
1299 fwrite(head,sizeof(head),1,fi);
1301 png_start_chunk(fi, "IHDR", 13);
1302 png_write_dword(fi,width);
1303 png_write_dword(fi,height);
1304 png_write_byte(fi,8);
1306 png_write_byte(fi,3); //indexed
1307 else if(format == 5 && alpha==0)
1308 png_write_byte(fi,2); //rgb
1309 else if(format == 5 && alpha==1)
1310 png_write_byte(fi,6); //rgba
1313 png_write_byte(fi,0); //compression mode
1314 png_write_byte(fi,0); //filter mode
1315 png_write_byte(fi,0); //interlace mode
1319 png_start_chunk(fi, "PLTE", 768);
1320 for(t=0;t<cols;t++) {
1321 png_write_byte(fi,palette[t].r);
1322 png_write_byte(fi,palette[t].g);
1323 png_write_byte(fi,palette[t].b);
1327 long idatpos = png_start_chunk(fi, "IDAT", 0);
1329 memset(&zs,0,sizeof(z_stream));
1330 Bytef*writebuf = (Bytef*)malloc(ZLIB_BUFFER_SIZE);
1334 zs.next_out = writebuf;
1335 zs.avail_out = ZLIB_BUFFER_SIZE;
1336 ret = deflateInit(&zs, 9);
1338 fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
1345 int srcwidth = width * (bpp/8);
1346 int linelen = 1 + ((srcwidth+3)&~3);
1347 unsigned char* line = (unsigned char*)malloc(linelen);
1348 unsigned char* bestline = (unsigned char*)malloc(linelen);
1349 memset(line, 0, linelen);
1350 for(y=0;y<height;y++)
1353 int bestsize = 0x7fffffff;
1354 for(filtermode=0;filtermode<=0;filtermode++) {
1355 if(!y && filtermode>=2)
1356 continue; // don't do y direction filters in the first row
1358 line[0]=filtermode; //filter type
1360 filter_line8(filtermode, line+1, &data[y*srcwidth], width);
1362 filter_line32(filtermode, line+1, &data[y*srcwidth], width);
1364 int size = test_line(&zs, line, linelen);
1365 if(size < bestsize) {
1366 memcpy(bestline, line, linelen);
1370 idatsize += compress_line(&zs, bestline, linelen, fi);
1372 free(line);free(bestline);
1374 idatsize += finishzlib(&zs, fi);
1375 png_patch_len(fi, idatpos, idatsize);
1378 png_start_chunk(fi, "IEND", 0);
1386 EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
1388 savePNG(filename, data, width, height, 0);
1390 EXPORT void writePalettePNG(const char*filename, unsigned char*data, int width, int height)
1392 savePNG(filename, data, width, height, 256);