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 */
30 #ifdef PNG_INLINE_EXPORTS
40 unsigned char a,r,g,b;
43 static int png_read_chunk(char (*head)[4], int*destlen, unsigned char**destdata, FILE*fi)
46 unsigned char blen[4];
47 if(destlen) *destlen=0;
48 if(destdata) *destdata=0;
49 if(!fread(&blen, 4, 1, fi)) {
52 if(!fread(head, 4, 1, fi)) {
55 len = blen[0]<<24|blen[1]<<16|blen[2]<<8|blen[3];
56 if(destlen) *destlen = len;
61 *destdata = (unsigned char*)malloc(len);
62 if(!fread(*destdata, len, 1, fi)) {
64 if(destlen) *destlen=0;
68 fseek(fi, 4, SEEK_CUR);
71 fseek(fi, len+4, SEEK_CUR);
76 static unsigned int png_get_dword(FILE*fi)
81 return b[0]<<24|b[1]<<16|b[2]<<8|b[3];
92 static int png_read_header(FILE*fi, struct png_header*header)
97 unsigned char head[8] = {137,80,78,71,13,10,26,10};
98 unsigned char head2[8];
101 if(strncmp((const char*)head,(const char*)head2,4))
102 return 0; // not a png file
104 while(png_read_chunk(&id, &len, &data, fi))
106 //printf("Chunk: %c%c%c%c (len:%d)\n", id[0],id[1],id[2],id[3], len);
107 if(!strncmp(id, "IHDR", 4)) {
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)
115 c = data[10]; // compression mode (0)
116 f = data[11]; // filter mode (0)
117 i = data[12]; // interlace mode (0)
119 if(b!=0 && b!=4 && b!=2 && b!=3 && b!=6) {
120 fprintf(stderr, "Image mode %d not supported!\n", b);
123 if(a!=8 && (b==2 || b==6)) {
124 printf("Bpp %d in mode %d not supported!\n", a);
128 printf("Compression mode %d not supported!\n", c);
132 printf("Filter mode %d not supported!\n", f);
136 printf("Interlace mode %d not supported!\n", i);
139 //printf("%dx%d bpp:%d mode:%d comp:%d filter:%d interlace:%d\n",header->width, header->height, a,b,c,f,i);
150 typedef unsigned char byte;
151 #define ABS(a) ((a)>0?(a):(-(a)))
152 static inline byte PaethPredictor (byte a,byte b,byte c)
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
159 // return nearest of a,b,c,
160 // breaking ties in order a,b,c.
161 if (pa <= pb && pa <= pc)
168 static void applyfilter1(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
171 unsigned char last=0;
172 unsigned char upperlast=0;
175 for(x=0;x<width;x++) {
182 for(x=0;x<width;x++) {
190 for(x=0;x<width;x++) {
198 for(x=0;x<width;x++) {
199 *dest = *src+(*old+last)/2;
206 for(x=0;x<width;x++) {
207 *dest = *src+PaethPredictor(last,*old,upperlast);
218 static void applyfilter2(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
221 unsigned char lasta=0;
222 unsigned char lastb=0;
223 unsigned char upperlasta=0;
224 unsigned char upperlastb=0;
227 for(x=0;x<width;x++) {
235 for(x=0;x<width;x++) {
236 dest[0] = src[0]+lasta;
237 dest[1] = src[1]+lastb;
245 for(x=0;x<width;x++) {
246 dest[0] = src[0]+old[0];
247 dest[1] = src[1]+old[1];
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;
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);
280 /* also performs 24 bit conversion! */
281 static void applyfilter3(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
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;
292 for(x=0;x<width;x++) {
302 for(x=0;x<width;x++) {
304 dest[1] = src[0]+lastr;
305 dest[2] = src[1]+lastg;
306 dest[3] = src[2]+lastb;
315 for(x=0;x<width;x++) {
317 dest[1] = src[0]+old[1];
318 dest[2] = src[1]+old[2];
319 dest[3] = src[2]+old[3];
326 for(x=0;x<width;x++) {
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;
340 for(x=0;x<width;x++) {
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);
358 static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old, unsigned char*dest, int width)
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;
371 for(x=0;x<width;x++) {
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;
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];
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;
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);
442 EXPORT int getPNGdimensions(const char*sname, int*destwidth, int*destheight)
445 struct png_header header;
446 if ((fi = fopen(sname, "rb")) == NULL) {
447 fprintf(stderr, "Couldn't open %s\n", sname);
450 if(!png_read_header(fi, &header)) {
454 *destwidth = header.width;
455 *destheight = header.height;
459 EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char**destdata)
464 unsigned char*imagedata;
465 unsigned char*zimagedata=0;
466 unsigned long int imagedatalen;
467 unsigned long int zimagedatalen=0;
468 unsigned char*palette = 0;
470 unsigned char*alphapalette = 0;
471 int alphapalettelen = 0;
472 struct png_header header;
474 unsigned char*data2 = 0;
475 unsigned char alphacolor[3];
479 unsigned char *scanline;
481 if ((fi = fopen(sname, "rb")) == NULL) {
482 printf("Couldn't open %s\n", sname);
486 if(!png_read_header(fi, &header)) {
490 if(header.mode == 3 || header.mode == 0) bypp = 1;
491 else if(header.mode == 4) bypp = 2;
492 else if(header.mode == 2) bypp = 3;
493 else if(header.mode == 6) bypp = 4;
495 printf("ERROR: mode:%d\n", header.mode);
499 imagedatalen = bypp * header.width * header.height + 65536;
500 imagedata = (unsigned char*)malloc(imagedatalen);
502 fseek(fi,8,SEEK_SET);
505 if(!png_read_chunk(&tagid, &len, &data, fi))
507 if(!strncmp(tagid, "IEND", 4)) {
510 if(!strncmp(tagid, "PLTE", 4)) {
513 data = 0; //don't free data
514 //printf("%d colors in palette\n", palettelen);
516 if(!strncmp(tagid, "tRNS", 4)) {
517 if(header.mode == 3) {
519 alphapalettelen = len;
520 data = 0; //don't free data
521 //printf("found %d alpha colors\n", alphapalettelen);
522 } else if(header.mode == 0 || header.mode == 2) {
524 if(header.mode == 2) {
525 alphacolor[0] = data[1];
526 alphacolor[1] = data[3];
527 alphacolor[2] = data[5];
529 alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1];
534 if(!strncmp(tagid, "IDAT", 4)) {
537 zimagedata = (unsigned char*)malloc(len);
538 memcpy(zimagedata,data,len);
540 zimagedata = (unsigned char*)realloc(zimagedata, zimagedatalen+len);
541 memcpy(&zimagedata[zimagedatalen], data, len);
542 zimagedatalen += len;
545 if(!strncmp(tagid, "tEXt", 4)) {
547 printf("Image Text: ");
549 if(data[t]>=32 && data[t]<128)
550 printf("%c", data[t]);
561 if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
562 printf("Couldn't uncompress %s!\n", sname);
570 *destwidth = header.width;
571 *destheight = header.height;
573 data2 = (unsigned char*)malloc(header.width*header.height*4);
580 unsigned char* old= (unsigned char*)malloc(header.width*2);
581 memset(old, 0, header.width*2);
583 for(y=0;y<header.height;y++) {
584 int mode = imagedata[pos++]; //filter mode
588 dest = &data2[(y*header.width)*4];
590 if(header.bpp == 8) {
591 /* one byte per pixel */
592 src = &imagedata[pos];
595 /* not implemented yet */
596 fprintf(stderr, "ERROR: mode=4 bpp:%d\n", header.bpp);
601 applyfilter2(mode, src, old, dest, header.width);
602 memcpy(old, dest, header.width*2);
604 for(x=header.width-1;x>=0;x--) {
605 unsigned char gray = dest[x*2+0];
606 unsigned char alpha = dest[x*2+1];
615 } else if(header.mode == 6 || header.mode == 2) {
621 unsigned char* firstline = malloc(header.width*4);
622 memset(firstline,0,header.width*4);
623 for(y=0;y<header.height;y++) {
624 int mode = imagedata[pos++]; //filter mode
628 dest = &data2[(y*header.width)*4];
632 /* one byte per pixel */
633 src = &imagedata[pos];
634 pos+=header.width*(header.mode==6?4:3);
636 /* not implemented yet */
637 fprintf(stderr, "ERROR: bpp:%d\n", header.bpp);
645 old = &data2[(y-1)*header.width*4];
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 */
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;
666 } else if(header.mode == 0 || header.mode == 3) {
668 unsigned char*tmpline = (unsigned char*)malloc(header.width+1);
669 unsigned char*destline = (unsigned char*)malloc(header.width+1);
675 if(header.mode == 0) { // grayscale palette
676 int mult = (0x1ff>>header.bpp);
677 palettelen = 1<<header.bpp;
678 rgba = (COL*)malloc(palettelen*sizeof(COL));
679 for(i=0;i<palettelen;i++) {
685 if(rgba[i].r == alphacolor[0])
691 fprintf(stderr, "Error: No palette found!\n");
694 rgba = (COL*)malloc(palettelen*4);
695 /* 24->32 bit conversion */
696 for(i=0;i<palettelen;i++) {
697 rgba[i].r = palette[i*3+0];
698 rgba[i].g = palette[i*3+1];
699 rgba[i].b = palette[i*3+2];
700 if(alphapalette && i<alphapalettelen) {
701 rgba[i].a = alphapalette[i];
702 /*rgba[i].r = ((int)rgba[i].r*rgba[i].a)/255;
703 rgba[i].g = ((int)rgba[i].g*rgba[i].a)/255;
704 rgba[i].b = ((int)rgba[i].b*rgba[i].a)/255;*/
709 if(rgba[i].r == alphacolor[0] &&
710 rgba[i].g == alphacolor[1] &&
711 rgba[i].b == alphacolor[2])
717 for(y=0;y<header.height;y++) {
718 int mode = imagedata[pos++]; //filter mode
722 src = &imagedata[pos];
723 if(header.bpp == 8) {
728 u32 v = (1<<header.bpp)-1;
729 for(x=0;x<header.width;x++) {
730 u32 r = src[s/8]<<8 |
733 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
737 pos+=(header.width*header.bpp+7)/8;
741 memset(destline,0,header.width);
742 old = &destline[y*header.width];
746 applyfilter1(mode, src, old, destline, header.width);
747 memcpy(tmpline,destline,header.width);
748 for(x=0;x<header.width;x++) {
749 *(COL*)&data2[y*header.width*4+x*4+0] = rgba[destline[x]];
757 printf("expected PNG mode to be 2, 3 or 6 (is:%d)\n", header.mode);
766 static u32*crc32_table = 0;
767 static void make_crc32_table(void)
772 crc32_table = (u32*)malloc(1024);
774 for (t = 0; t < 256; t++) {
777 for (s = 0; s < 8; s++) {
778 c = (0xedb88320L*(c&1)) ^ (c >> 1);
783 static inline void png_write_byte(FILE*fi, unsigned char byte)
785 fwrite(&byte,1,1,fi);
786 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
788 static long png_start_chunk(FILE*fi, char*type, int len)
790 unsigned char mytype[4]={0,0,0,0};
791 unsigned char mylen[4];
797 memcpy(mytype,type,strlen(type));
799 fwrite(&mylen, 4, 1, fi);
801 png_write_byte(fi,mytype[0]);
802 png_write_byte(fi,mytype[1]);
803 png_write_byte(fi,mytype[2]);
804 png_write_byte(fi,mytype[3]);
807 static void png_patch_len(FILE*fi, int pos, int len)
809 unsigned char mylen[4];
815 fseek(fi, pos, SEEK_SET);
816 fwrite(&mylen, 4, 1, fi);
817 fseek(fi, 0, SEEK_END);
819 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
823 png_write_byte(fi,bytes[t]);
825 static void png_write_dword(FILE*fi, u32 dword)
827 png_write_byte(fi,dword>>24);
828 png_write_byte(fi,dword>>16);
829 png_write_byte(fi,dword>>8);
830 png_write_byte(fi,dword);
832 static void png_end_chunk(FILE*fi)
834 u32 tmp = mycrc32^0xffffffff;
835 unsigned char tmp2[4];
840 fwrite(&tmp2,4,1,fi);
843 #define ZLIB_BUFFER_SIZE 16384
845 static long compress_line(z_stream*zs, Bytef*line, int len, FILE*fi)
852 int ret = deflate(zs, Z_NO_FLUSH);
854 fprintf(stderr, "error in deflate(): %s", zs->msg?zs->msg:"unknown");
857 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
858 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
860 png_write_bytes(fi, zs->next_out - consumed , consumed);
861 zs->next_out = zs->next_out - consumed;
862 zs->avail_out = ZLIB_BUFFER_SIZE;
871 static int test_line(z_stream*zs_orig, Bytef*line, int linelen)
874 int ret = deflateCopy(&zs, zs_orig);
876 fprintf(stderr, "Couldn't copy stream\n");
881 zs.avail_in = linelen;
885 int mode = Z_SYNC_FLUSH;
887 int ret = deflate(&zs, mode);
888 if (ret != Z_OK && ret != Z_STREAM_END) {
889 fprintf(stderr, "error in deflate(): %s (mode %s, %d bytes remaining)\n", zs.msg?zs.msg:"unknown",
890 mode==Z_SYNC_FLUSH?"Z_SYNC_FLUSH":"Z_FINISH", zs.avail_in);
893 if(zs.avail_out != ZLIB_BUFFER_SIZE) {
894 int consumed = ZLIB_BUFFER_SIZE - zs.avail_out;
896 zs.next_out = zs.next_out - consumed;
897 zs.avail_out = ZLIB_BUFFER_SIZE;
899 if (ret == Z_STREAM_END) {
906 ret = deflateEnd(&zs);
908 fprintf(stderr, "error in deflateEnd(): %s\n", zs.msg?zs.msg:"unknown");
914 static int finishzlib(z_stream*zs, FILE*fi)
919 ret = deflate(zs, Z_FINISH);
921 ret != Z_STREAM_END) {
922 fprintf(stderr, "error in deflate(finish): %s\n", zs->msg?zs->msg:"unknown");
926 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
927 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
929 png_write_bytes(fi, zs->next_out - consumed , consumed);
930 zs->next_out = zs->next_out - consumed;
931 zs->avail_out = ZLIB_BUFFER_SIZE;
933 if (ret == Z_STREAM_END) {
937 ret = deflateEnd(zs);
939 fprintf(stderr, "error in deflateEnd(): %s\n", zs->msg?zs->msg:"unknown");
945 static void filter_line(int filtermode, unsigned char*dest, unsigned char*src, int width)
949 int srcwidth = width*4;
951 if(filtermode == 0) {
952 for(x=0;x<width;x++) {
953 dest[pos2++]=src[pos+1];
954 dest[pos2++]=src[pos+2];
955 dest[pos2++]=src[pos+3];
956 dest[pos2++]=src[pos+0]; //alpha
959 } else if(filtermode == 1) {
960 /* x difference filter */
961 dest[pos2++]=src[pos+1];
962 dest[pos2++]=src[pos+2];
963 dest[pos2++]=src[pos+3];
964 dest[pos2++]=src[pos+0];
966 for(x=1;x<width;x++) {
967 dest[pos2++]=src[pos+1] - src[pos-4+1];
968 dest[pos2++]=src[pos+2] - src[pos-4+2];
969 dest[pos2++]=src[pos+3] - src[pos-4+3];
970 dest[pos2++]=src[pos+0] - src[pos-4+0]; //alpha
973 } else if(filtermode == 2) {
974 /* y difference filter */
975 for(x=0;x<width;x++) {
976 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1];
977 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2];
978 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3];
979 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
982 } else if(filtermode == 3) {
983 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1]/2;
984 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2]/2;
985 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3]/2;
986 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
988 /* x+y difference filter */
989 for(x=1;x<width;x++) {
990 dest[pos2++]=src[pos+1] - (src[pos-4+1] + src[pos-srcwidth+1])/2;
991 dest[pos2++]=src[pos+2] - (src[pos-4+2] + src[pos-srcwidth+2])/2;
992 dest[pos2++]=src[pos+3] - (src[pos-4+3] + src[pos-srcwidth+3])/2;
993 dest[pos2++]=src[pos+0] - (src[pos-4+0] + src[pos-srcwidth+0])/2; //alpha
996 } else if(filtermode == 4) {
997 dest[pos2++]=src[pos+1] - PaethPredictor(0, src[pos-srcwidth+1], 0);
998 dest[pos2++]=src[pos+2] - PaethPredictor(0, src[pos-srcwidth+2], 0);
999 dest[pos2++]=src[pos+3] - PaethPredictor(0, src[pos-srcwidth+3], 0);
1000 dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
1002 /* paeth difference filter */
1003 for(x=1;x<width;x++) {
1004 dest[pos2++]=src[pos+1] - PaethPredictor(src[pos-4+1], src[pos-srcwidth+1], src[pos-4-srcwidth+1]);
1005 dest[pos2++]=src[pos+2] - PaethPredictor(src[pos-4+2], src[pos-srcwidth+2], src[pos-4-srcwidth+2]);
1006 dest[pos2++]=src[pos+3] - PaethPredictor(src[pos-4+3], src[pos-srcwidth+3], src[pos-4-srcwidth+3]);
1007 dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-4+0], src[pos-srcwidth+0], src[pos-4-srcwidth+0]);
1013 EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
1018 unsigned char format;
1020 unsigned char* data2=0;
1023 unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
1039 datalen = (width*height*bpp/8+cols*8);
1041 fi = fopen(filename, "wb");
1046 fwrite(head,sizeof(head),1,fi);
1048 png_start_chunk(fi, "IHDR", 13);
1049 png_write_dword(fi,width);
1050 png_write_dword(fi,height);
1051 png_write_byte(fi,8);
1053 png_write_byte(fi,3); //indexed
1054 else if(format == 5 && alpha==0)
1055 png_write_byte(fi,2); //rgb
1056 else if(format == 5 && alpha==1)
1057 png_write_byte(fi,6); //rgba
1060 png_write_byte(fi,0); //compression mode
1061 png_write_byte(fi,0); //filter mode
1062 png_write_byte(fi,0); //interlace mode
1065 /* if(format == 3) {
1066 png_start_chunk(fi, "PLTE", 768);
1068 for(t=0;t<256;t++) {
1069 png_write_byte(fi,palette[t].r);
1070 png_write_byte(fi,palette[t].g);
1071 png_write_byte(fi,palette[t].b);
1075 long idatpos = png_start_chunk(fi, "IDAT", 0);
1077 memset(&zs,0,sizeof(z_stream));
1078 Bytef*writebuf = (Bytef*)malloc(ZLIB_BUFFER_SIZE);
1082 zs.next_out = writebuf;
1083 zs.avail_out = ZLIB_BUFFER_SIZE;
1084 ret = deflateInit(&zs, 9);
1086 fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
1093 int srcwidth = width * (bpp/8);
1094 int linelen = 1 + ((srcwidth+3)&~3);
1095 unsigned char* line = (unsigned char*)malloc(linelen);
1096 unsigned char* bestline = (unsigned char*)malloc(linelen);
1097 memset(line, 0, linelen);
1098 for(y=0;y<height;y++)
1101 int bestsize = 0x7fffffff;
1102 for(filtermode=0;filtermode<5;filtermode++) {
1104 if(!y && filtermode>=2)
1105 continue; // don't do y direction filters in the first row
1107 line[0]=filtermode; //filter type
1108 filter_line(filtermode, line+1, &data[y*srcwidth], width);
1110 int size = test_line(&zs, line, linelen);
1111 if(size < bestsize) {
1112 memcpy(bestline, line, linelen);
1116 idatsize += compress_line(&zs, bestline, linelen, fi);
1118 free(line);free(bestline);
1120 idatsize += finishzlib(&zs, fi);
1121 png_patch_len(fi, idatpos, idatsize);
1124 png_start_chunk(fi, "IEND", 0);