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(!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)) {
451 fprintf(stderr, "Error reading header from file %s\n", sname);
455 *destwidth = header.width;
456 *destheight = header.height;
460 EXPORT int getPNG(const 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) {
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);
768 static u32*crc32_table = 0;
769 static void make_crc32_table(void)
774 crc32_table = (u32*)malloc(1024);
776 for (t = 0; t < 256; t++) {
779 for (s = 0; s < 8; s++) {
780 c = (0xedb88320L*(c&1)) ^ (c >> 1);
785 static inline void png_write_byte(FILE*fi, unsigned char byte)
787 fwrite(&byte,1,1,fi);
788 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
790 static long png_start_chunk(FILE*fi, char*type, int len)
792 unsigned char mytype[4]={0,0,0,0};
793 unsigned char mylen[4];
799 memcpy(mytype,type,strlen(type));
801 fwrite(&mylen, 4, 1, fi);
803 png_write_byte(fi,mytype[0]);
804 png_write_byte(fi,mytype[1]);
805 png_write_byte(fi,mytype[2]);
806 png_write_byte(fi,mytype[3]);
809 static void png_patch_len(FILE*fi, int pos, int len)
811 unsigned char mylen[4];
817 fseek(fi, pos, SEEK_SET);
818 fwrite(&mylen, 4, 1, fi);
819 fseek(fi, 0, SEEK_END);
821 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
825 png_write_byte(fi,bytes[t]);
827 static void png_write_dword(FILE*fi, u32 dword)
829 png_write_byte(fi,dword>>24);
830 png_write_byte(fi,dword>>16);
831 png_write_byte(fi,dword>>8);
832 png_write_byte(fi,dword);
834 static void png_end_chunk(FILE*fi)
836 u32 tmp = mycrc32^0xffffffff;
837 unsigned char tmp2[4];
842 fwrite(&tmp2,4,1,fi);
845 #define ZLIB_BUFFER_SIZE 16384
847 static long compress_line(z_stream*zs, Bytef*line, int len, FILE*fi)
854 int ret = deflate(zs, Z_NO_FLUSH);
856 fprintf(stderr, "error in deflate(): %s", zs->msg?zs->msg:"unknown");
859 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
860 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
862 png_write_bytes(fi, zs->next_out - consumed , consumed);
863 zs->next_out = zs->next_out - consumed;
864 zs->avail_out = ZLIB_BUFFER_SIZE;
873 static int test_line(z_stream*zs_orig, Bytef*line, int linelen)
876 int ret = deflateCopy(&zs, zs_orig);
878 fprintf(stderr, "Couldn't copy stream\n");
883 zs.avail_in = linelen;
887 int mode = Z_SYNC_FLUSH;
889 int ret = deflate(&zs, mode);
890 if (ret != Z_OK && ret != Z_STREAM_END) {
891 fprintf(stderr, "error in deflate(): %s (mode %s, %d bytes remaining)\n", zs.msg?zs.msg:"unknown",
892 mode==Z_SYNC_FLUSH?"Z_SYNC_FLUSH":"Z_FINISH", zs.avail_in);
895 if(zs.avail_out != ZLIB_BUFFER_SIZE) {
896 int consumed = ZLIB_BUFFER_SIZE - zs.avail_out;
898 zs.next_out = zs.next_out - consumed;
899 zs.avail_out = ZLIB_BUFFER_SIZE;
901 if (ret == Z_STREAM_END) {
908 ret = deflateEnd(&zs);
910 fprintf(stderr, "error in deflateEnd(): %s\n", zs.msg?zs.msg:"unknown");
916 static int finishzlib(z_stream*zs, FILE*fi)
921 ret = deflate(zs, Z_FINISH);
923 ret != Z_STREAM_END) {
924 fprintf(stderr, "error in deflate(finish): %s\n", zs->msg?zs->msg:"unknown");
928 if(zs->avail_out != ZLIB_BUFFER_SIZE) {
929 int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
931 png_write_bytes(fi, zs->next_out - consumed , consumed);
932 zs->next_out = zs->next_out - consumed;
933 zs->avail_out = ZLIB_BUFFER_SIZE;
935 if (ret == Z_STREAM_END) {
939 ret = deflateEnd(zs);
941 fprintf(stderr, "error in deflateEnd(): %s\n", zs->msg?zs->msg:"unknown");
947 static void filter_line(int filtermode, unsigned char*dest, unsigned char*src, int width)
951 int srcwidth = width*4;
953 if(filtermode == 0) {
954 for(x=0;x<width;x++) {
955 dest[pos2++]=src[pos+1];
956 dest[pos2++]=src[pos+2];
957 dest[pos2++]=src[pos+3];
958 dest[pos2++]=src[pos+0]; //alpha
961 } else if(filtermode == 1) {
962 /* x difference filter */
963 dest[pos2++]=src[pos+1];
964 dest[pos2++]=src[pos+2];
965 dest[pos2++]=src[pos+3];
966 dest[pos2++]=src[pos+0];
968 for(x=1;x<width;x++) {
969 dest[pos2++]=src[pos+1] - src[pos-4+1];
970 dest[pos2++]=src[pos+2] - src[pos-4+2];
971 dest[pos2++]=src[pos+3] - src[pos-4+3];
972 dest[pos2++]=src[pos+0] - src[pos-4+0]; //alpha
975 } else if(filtermode == 2) {
976 /* y difference filter */
977 for(x=0;x<width;x++) {
978 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1];
979 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2];
980 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3];
981 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
984 } else if(filtermode == 3) {
985 dest[pos2++]=src[pos+1] - src[pos-srcwidth+1]/2;
986 dest[pos2++]=src[pos+2] - src[pos-srcwidth+2]/2;
987 dest[pos2++]=src[pos+3] - src[pos-srcwidth+3]/2;
988 dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
990 /* x+y difference filter */
991 for(x=1;x<width;x++) {
992 dest[pos2++]=src[pos+1] - (src[pos-4+1] + src[pos-srcwidth+1])/2;
993 dest[pos2++]=src[pos+2] - (src[pos-4+2] + src[pos-srcwidth+2])/2;
994 dest[pos2++]=src[pos+3] - (src[pos-4+3] + src[pos-srcwidth+3])/2;
995 dest[pos2++]=src[pos+0] - (src[pos-4+0] + src[pos-srcwidth+0])/2; //alpha
998 } else if(filtermode == 4) {
999 dest[pos2++]=src[pos+1] - PaethPredictor(0, src[pos-srcwidth+1], 0);
1000 dest[pos2++]=src[pos+2] - PaethPredictor(0, src[pos-srcwidth+2], 0);
1001 dest[pos2++]=src[pos+3] - PaethPredictor(0, src[pos-srcwidth+3], 0);
1002 dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
1004 /* paeth difference filter */
1005 for(x=1;x<width;x++) {
1006 dest[pos2++]=src[pos+1] - PaethPredictor(src[pos-4+1], src[pos-srcwidth+1], src[pos-4-srcwidth+1]);
1007 dest[pos2++]=src[pos+2] - PaethPredictor(src[pos-4+2], src[pos-srcwidth+2], src[pos-4-srcwidth+2]);
1008 dest[pos2++]=src[pos+3] - PaethPredictor(src[pos-4+3], src[pos-srcwidth+3], src[pos-4-srcwidth+3]);
1009 dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-4+0], src[pos-srcwidth+0], src[pos-4-srcwidth+0]);
1015 EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
1020 unsigned char format;
1022 unsigned char* data2=0;
1025 unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
1041 datalen = (width*height*bpp/8+cols*8);
1043 fi = fopen(filename, "wb");
1048 fwrite(head,sizeof(head),1,fi);
1050 png_start_chunk(fi, "IHDR", 13);
1051 png_write_dword(fi,width);
1052 png_write_dword(fi,height);
1053 png_write_byte(fi,8);
1055 png_write_byte(fi,3); //indexed
1056 else if(format == 5 && alpha==0)
1057 png_write_byte(fi,2); //rgb
1058 else if(format == 5 && alpha==1)
1059 png_write_byte(fi,6); //rgba
1062 png_write_byte(fi,0); //compression mode
1063 png_write_byte(fi,0); //filter mode
1064 png_write_byte(fi,0); //interlace mode
1067 /* if(format == 3) {
1068 png_start_chunk(fi, "PLTE", 768);
1070 for(t=0;t<256;t++) {
1071 png_write_byte(fi,palette[t].r);
1072 png_write_byte(fi,palette[t].g);
1073 png_write_byte(fi,palette[t].b);
1077 long idatpos = png_start_chunk(fi, "IDAT", 0);
1079 memset(&zs,0,sizeof(z_stream));
1080 Bytef*writebuf = (Bytef*)malloc(ZLIB_BUFFER_SIZE);
1084 zs.next_out = writebuf;
1085 zs.avail_out = ZLIB_BUFFER_SIZE;
1086 ret = deflateInit(&zs, 9);
1088 fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
1095 int srcwidth = width * (bpp/8);
1096 int linelen = 1 + ((srcwidth+3)&~3);
1097 unsigned char* line = (unsigned char*)malloc(linelen);
1098 unsigned char* bestline = (unsigned char*)malloc(linelen);
1099 memset(line, 0, linelen);
1100 for(y=0;y<height;y++)
1103 int bestsize = 0x7fffffff;
1104 for(filtermode=0;filtermode<5;filtermode++) {
1106 if(!y && filtermode>=2)
1107 continue; // don't do y direction filters in the first row
1109 line[0]=filtermode; //filter type
1110 filter_line(filtermode, line+1, &data[y*srcwidth], width);
1112 int size = test_line(&zs, line, linelen);
1113 if(size < bestsize) {
1114 memcpy(bestline, line, linelen);
1118 idatsize += compress_line(&zs, bestline, linelen, fi);
1120 free(line);free(bestline);
1122 idatsize += finishzlib(&zs, fi);
1123 png_patch_len(fi, idatpos, idatsize);
1126 png_start_chunk(fi, "IEND", 0);