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))
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(!strncasecmp(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(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)) {
451 fprintf(stderr, "Error reading header from file %s\n", sname);
455 *destwidth = header.width;
456 *destheight = header.height;
460 EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
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;
471 unsigned char*alphapalette = 0;
472 int alphapalettelen = 0;
473 struct png_header header;
475 unsigned char*data2 = 0;
476 unsigned char alphacolor[3];
480 unsigned char *scanline;
482 if ((fi = fopen(sname, "rb")) == NULL) {
483 printf("Couldn't open %s\n", sname);
487 if(!png_read_header(fi, &header)) {
488 printf("Error reading header from file %s\n", sname);
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) {
622 for(y=0;y<header.height;y++) {
623 int mode = imagedata[pos++]; //filter mode
627 dest = &data2[(y*header.width)*4];
631 /* one byte per pixel */
632 src = &imagedata[pos];
633 pos+=header.width*(header.mode==6?4:3);
635 /* not implemented yet */
636 fprintf(stderr, "ERROR: bpp:%d\n", header.bpp);
642 memset(data2,0,header.width*4);
643 old = &data2[y*header.width*4];
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;
665 } else if(header.mode == 0 || header.mode == 3) {
667 unsigned char*tmpline = (unsigned char*)malloc(header.width+1);
668 unsigned char*destline = (unsigned char*)malloc(header.width+1);
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++) {
684 if(rgba[i].r == alphacolor[0])
690 fprintf(stderr, "Error: No palette found!\n");
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;*/
708 if(rgba[i].r == alphacolor[0] &&
709 rgba[i].g == alphacolor[1] &&
710 rgba[i].b == alphacolor[2])
716 for(y=0;y<header.height;y++) {
717 int mode = imagedata[pos++]; //filter mode
721 src = &imagedata[pos];
722 if(header.bpp == 8) {
727 u32 v = (1<<header.bpp)-1;
728 for(x=0;x<header.width;x++) {
729 u32 r = src[s/8]<<8 |
732 tmpline[x] = (r>>(16-header.bpp-(s&7)))&v;
736 pos+=(header.width*header.bpp+7)/8;
740 memset(destline,0,header.width);
741 old = &destline[y*header.width];
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]];
756 printf("expected PNG mode to be 2, 3 or 6 (is:%d)\n", header.mode);
765 static u32*crc32_table = 0;
766 static void make_crc32_table(void)
771 crc32_table = (u32*)malloc(1024);
773 for (t = 0; t < 256; t++) {
776 for (s = 0; s < 8; s++) {
777 c = (0xedb88320L*(c&1)) ^ (c >> 1);
782 static inline void png_write_byte(FILE*fi, unsigned char byte)
784 fwrite(&byte,1,1,fi);
785 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
787 static long png_start_chunk(FILE*fi, char*type, int len)
789 unsigned char mytype[4]={0,0,0,0};
790 unsigned char mylen[4];
796 memcpy(mytype,type,strlen(type));
798 fwrite(&mylen, 4, 1, fi);
800 png_write_byte(fi,mytype[0]);
801 png_write_byte(fi,mytype[1]);
802 png_write_byte(fi,mytype[2]);
803 png_write_byte(fi,mytype[3]);
806 static void png_patch_len(FILE*fi, int pos, int len)
808 unsigned char mylen[4];
814 fseek(fi, pos, SEEK_SET);
815 fwrite(&mylen, 4, 1, fi);
816 fseek(fi, 0, SEEK_END);
818 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
822 png_write_byte(fi,bytes[t]);
824 static void png_write_dword(FILE*fi, u32 dword)
826 png_write_byte(fi,dword>>24);
827 png_write_byte(fi,dword>>16);
828 png_write_byte(fi,dword>>8);
829 png_write_byte(fi,dword);
831 static void png_end_chunk(FILE*fi)
833 u32 tmp = mycrc32^0xffffffff;
834 unsigned char tmp2[4];
839 fwrite(&tmp2,4,1,fi);
842 #define ZLIB_BUFFER_SIZE 16384
844 static long compress_line(z_stream*zs, Bytef*line, int len, FILE*fi)
851 int ret = deflate(zs, Z_NO_FLUSH);
853 fprintf(stderr, "error in deflate(): %s", zs->msg?zs->msg:"unknown");
856 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
857 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
859 png_write_bytes(fi, zs->next_out - consumed , consumed);
860 zs->next_out = zs->next_out - consumed;
861 zs->avail_out = ZLIB_BUFFER_SIZE;
870 static int test_line(z_stream*zs_orig, Bytef*line, int linelen)
873 int ret = deflateCopy(&zs, zs_orig);
875 fprintf(stderr, "Couldn't copy stream\n");
880 zs.avail_in = linelen;
884 int mode = Z_SYNC_FLUSH;
886 int ret = deflate(&zs, mode);
887 if (ret != Z_OK && ret != Z_STREAM_END) {
888 fprintf(stderr, "error in deflate(): %s (mode %s, %d bytes remaining)\n", zs.msg?zs.msg:"unknown",
889 mode==Z_SYNC_FLUSH?"Z_SYNC_FLUSH":"Z_FINISH", zs.avail_in);
892 if(zs.avail_out != ZLIB_BUFFER_SIZE) {
893 int consumed = ZLIB_BUFFER_SIZE - zs.avail_out;
895 zs.next_out = zs.next_out - consumed;
896 zs.avail_out = ZLIB_BUFFER_SIZE;
898 if (ret == Z_STREAM_END) {
905 ret = deflateEnd(&zs);
907 fprintf(stderr, "error in deflateEnd(): %s\n", zs.msg?zs.msg:"unknown");
913 static int finishzlib(z_stream*zs, FILE*fi)
918 ret = deflate(zs, Z_FINISH);
920 ret != Z_STREAM_END) {
921 fprintf(stderr, "error in deflate(finish): %s\n", zs->msg?zs->msg:"unknown");
925 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
926 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
928 png_write_bytes(fi, zs->next_out - consumed , consumed);
929 zs->next_out = zs->next_out - consumed;
930 zs->avail_out = ZLIB_BUFFER_SIZE;
932 if (ret == Z_STREAM_END) {
936 ret = deflateEnd(zs);
938 fprintf(stderr, "error in deflateEnd(): %s\n", zs->msg?zs->msg:"unknown");
944 static void filter_line(int filtermode, unsigned char*dest, unsigned char*src, int width)
948 int srcwidth = width*4;
950 if(filtermode == 0) {
951 for(x=0;x<width;x++) {
952 dest[pos2++]=src[pos+1];
953 dest[pos2++]=src[pos+2];
954 dest[pos2++]=src[pos+3];
955 dest[pos2++]=src[pos+0]; //alpha
958 } else if(filtermode == 1) {
959 /* x difference filter */
960 dest[pos2++]=src[pos+1];
961 dest[pos2++]=src[pos+2];
962 dest[pos2++]=src[pos+3];
963 dest[pos2++]=src[pos+0];
965 for(x=1;x<width;x++) {
966 dest[pos2++]=src[pos+1] - src[pos-4+1];
967 dest[pos2++]=src[pos+2] - src[pos-4+2];
968 dest[pos2++]=src[pos+3] - src[pos-4+3];
969 dest[pos2++]=src[pos+0] - src[pos-4+0]; //alpha
972 } else if(filtermode == 2) {
973 /* y difference filter */
974 for(x=0;x<width;x++) {
975 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1];
976 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2];
977 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3];
978 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
981 } else if(filtermode == 3) {
982 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1]/2;
983 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2]/2;
984 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3]/2;
985 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
987 /* x+y difference filter */
988 for(x=1;x<width;x++) {
989 dest[pos2++]=src[pos+1] - (src[pos-4+1] + src[pos-srcwidth+1])/2;
990 dest[pos2++]=src[pos+2] - (src[pos-4+2] + src[pos-srcwidth+2])/2;
991 dest[pos2++]=src[pos+3] - (src[pos-4+3] + src[pos-srcwidth+3])/2;
992 dest[pos2++]=src[pos+0] - (src[pos-4+0] + src[pos-srcwidth+0])/2; //alpha
995 } else if(filtermode == 4) {
996 dest[pos2++]=src[pos+1] - PaethPredictor(0, src[pos-srcwidth+1], 0);
997 dest[pos2++]=src[pos+2] - PaethPredictor(0, src[pos-srcwidth+2], 0);
998 dest[pos2++]=src[pos+3] - PaethPredictor(0, src[pos-srcwidth+3], 0);
999 dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
1001 /* paeth difference filter */
1002 for(x=1;x<width;x++) {
1003 dest[pos2++]=src[pos+1] - PaethPredictor(src[pos-4+1], src[pos-srcwidth+1], src[pos-4-srcwidth+1]);
1004 dest[pos2++]=src[pos+2] - PaethPredictor(src[pos-4+2], src[pos-srcwidth+2], src[pos-4-srcwidth+2]);
1005 dest[pos2++]=src[pos+3] - PaethPredictor(src[pos-4+3], src[pos-srcwidth+3], src[pos-4-srcwidth+3]);
1006 dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-4+0], src[pos-srcwidth+0], src[pos-4-srcwidth+0]);
1012 EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
1017 unsigned char format;
1019 unsigned char* data2=0;
1022 unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
1038 datalen = (width*height*bpp/8+cols*8);
1040 fi = fopen(filename, "wb");
1045 fwrite(head,sizeof(head),1,fi);
1047 png_start_chunk(fi, "IHDR", 13);
1048 png_write_dword(fi,width);
1049 png_write_dword(fi,height);
1050 png_write_byte(fi,8);
1052 png_write_byte(fi,3); //indexed
1053 else if(format == 5 && alpha==0)
1054 png_write_byte(fi,2); //rgb
1055 else if(format == 5 && alpha==1)
1056 png_write_byte(fi,6); //rgba
1059 png_write_byte(fi,0); //compression mode
1060 png_write_byte(fi,0); //filter mode
1061 png_write_byte(fi,0); //interlace mode
1064 /* if(format == 3) {
1065 png_start_chunk(fi, "PLTE", 768);
1067 for(t=0;t<256;t++) {
1068 png_write_byte(fi,palette[t].r);
1069 png_write_byte(fi,palette[t].g);
1070 png_write_byte(fi,palette[t].b);
1074 long idatpos = png_start_chunk(fi, "IDAT", 0);
1076 memset(&zs,0,sizeof(z_stream));
1077 Bytef*writebuf = malloc(ZLIB_BUFFER_SIZE);
1081 zs.next_out = writebuf;
1082 zs.avail_out = ZLIB_BUFFER_SIZE;
1083 ret = deflateInit(&zs, 9);
1085 fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
1092 int srcwidth = width * (bpp/8);
1093 int linelen = 1 + ((srcwidth+3)&~3);
1094 unsigned char* line = (unsigned char*)malloc(linelen);
1095 unsigned char* bestline = (unsigned char*)malloc(linelen);
1096 memset(line, 0, linelen);
1097 for(y=0;y<height;y++)
1100 int bestsize = 0x7fffffff;
1101 for(filtermode=0;filtermode<5;filtermode++) {
1103 if(!y && filtermode>=2)
1104 continue; // don't do y direction filters in the first row
1106 line[0]=filtermode; //filter type
1107 filter_line(filtermode, line+1, &data[y*srcwidth], width);
1109 int size = test_line(&zs, line, linelen);
1110 if(size < bestsize) {
1111 memcpy(bestline, line, linelen);
1115 idatsize += compress_line(&zs, bestline, linelen, fi);
1117 free(line);free(bestline);
1119 idatsize += finishzlib(&zs, fi);
1120 png_patch_len(fi, idatpos, idatsize);
1123 png_start_chunk(fi, "IEND", 0);