fixed format warnings
[swftools.git] / lib / png.c
index 141218e..1738151 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 #include <math.h>
 #include <fcntl.h>
 #include <zlib.h>
@@ -99,12 +100,12 @@ static int png_read_header(FILE*fi, struct png_header*header)
     unsigned char*data;
     fread(head2,8,1,fi);
     if(strncmp((const char*)head,(const char*)head2,4))
-       return 0;
+       return 0; // not a png file
     
     while(png_read_chunk(&id, &len, &data, fi))
     {
        //printf("Chunk: %c%c%c%c (len:%d)\n", id[0],id[1],id[2],id[3], len);
-       if(!strncasecmp(id, "IHDR", 4)) {
+       if(!strncmp(id, "IHDR", 4)) {
            char a,b,c,f,i;
            if(len < 8) exit(1);
            header->width = data[0]<<24|data[1]<<16|data[2]<<8|data[3];
@@ -121,7 +122,7 @@ static int png_read_header(FILE*fi, struct png_header*header)
                return 0;
            }
            if(a!=8 && (b==2 || b==6)) {
-               printf("Bpp %d in mode %d not supported!\n", a);
+               printf("Bpp %d in mode %d not supported!\n", b, a);
                return 0;
            }
            if(c!=0) {
@@ -197,6 +198,7 @@ static void applyfilter1(int mode, unsigned char*src, unsigned char*old, unsigne
     else if(mode==3) {
        for(x=0;x<width;x++) {
            *dest = *src+(*old+last)/2;
+           last = *dest;
            dest++;
            old++;
            src++;
@@ -439,7 +441,7 @@ static void inline applyfilter4(int mode, unsigned char*src, unsigned char*old,
 }
 
 
-EXPORT int getPNGdimensions(char*sname, int*destwidth, int*destheight)
+EXPORT int getPNGdimensions(const char*sname, int*destwidth, int*destheight)
 {
     FILE*fi;
     struct png_header header;
@@ -448,7 +450,6 @@ EXPORT int getPNGdimensions(char*sname, int*destwidth, int*destheight)
        return 0;
     }
     if(!png_read_header(fi, &header)) {
-       fprintf(stderr, "Error reading header from file %s\n", sname);
        return 0;
     }
 
@@ -457,7 +458,7 @@ EXPORT int getPNGdimensions(char*sname, int*destwidth, int*destheight)
     return 1;
 }
 
-EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
+EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char**destdata)
 {
     char tagid[4];
     int len;
@@ -485,7 +486,7 @@ EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**dest
     }
 
     if(!png_read_header(fi, &header)) {
-       printf("Error reading header from file %s\n", sname);
+       fclose(fi);
        return 0;
     }
 
@@ -619,6 +620,9 @@ EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**dest
        int x,y;
        int pos=0;
        *destdata = data2;
+       
+       unsigned char* firstline = malloc(header.width*4);
+       memset(firstline,0,header.width*4);
        for(y=0;y<header.height;y++) {
            int mode = imagedata[pos++]; //filter mode
            unsigned char*src;
@@ -639,12 +643,11 @@ EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**dest
            }
 
            if(!y) {
-               memset(data2,0,header.width*4);
-               old = &data2[y*header.width*4];
+               old = firstline;
            } else {
                old = &data2[(y-1)*header.width*4];
            }
-           if(header.mode == 6) {
+           if(header.mode == 6) { 
                applyfilter4(mode, src, old, dest, header.width);
            } else { // header.mode = 2
                applyfilter3(mode, src, old, dest, header.width);
@@ -661,6 +664,7 @@ EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**dest
                }
            }
        }
+       free(firstline);
         free(imagedata);
     } else if(header.mode == 0 || header.mode == 3) {
        COL*rgba = 0;
@@ -760,6 +764,207 @@ EXPORT int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**dest
     return 1;
 }
 
+static char hasAlpha(unsigned char*_image, int size)
+{
+    COL*image = (COL*)_image;
+    int t;
+    for(t=0;t<size;t++) {
+        if(image[t].a!=255)
+            return 1;
+    }
+    return 0;
+}
+
+typedef struct {
+    u32 num;
+    u32 color;
+} colornum_t;
+
+static int compare_colors(const void*_c1, const void*_c2) {
+    colornum_t*c1 = (colornum_t*)_c1;
+    colornum_t*c2 = (colornum_t*)_c2;
+    return c2->num - c1->num;
+}
+
+static colornum_t* getColors(COL*image, int size, int*num)
+{
+    unsigned char*colexists = malloc((256*256*256)/8);
+    memset(colexists, 0, (256*256*256)/8);
+    int t;
+    int count=0;
+
+    /* find all different colors in the image */
+    for(t=0;t<size;t++) {
+        int index = (image[t].r)|(image[t].g)<<8|(image[t].b)<<16;
+        if(!(colexists[index/8]&(1<<(index&7)))) {
+            count++;
+            colexists[index/8]|=(1<<(index&7));
+        }
+    }
+    
+    /* now store them in an array */
+    colornum_t*colors=(colornum_t*)malloc(sizeof(colornum_t)*count);
+    int pos=0;
+    for(t=0;t<256*256*256;t++) {
+        if(colexists[t/8]&(1<<(t&7))) {
+            colors[pos].color = t;
+            colors[pos].num = 0;
+            pos++;
+        }
+    }
+
+    /* next, count how often each color occurs */
+    for(t=0;t<size;t++) {
+        int col = (image[t].r)|(image[t].g)<<8|(image[t].b)<<16;
+        int min,max,i,l;
+        for(min=0, max=count, i=count/2, l=count; i != l; l=i,i=(min+max)/2) {
+           // binary search
+            if(colors[i].color >= col) max=i;
+            else min=i+1;
+        }
+        assert(colors[i].color==col);
+        colors[i].num++;
+    }
+    free(colexists);
+    *num = count;
+    return colors;
+}
+
+static COL* getOptimalPalette(COL*image, int size, int palettesize)
+{
+    int num;
+    COL* ret = malloc(sizeof(COL)*palettesize);
+    memset(ret, 0, sizeof(COL)*palettesize);
+    colornum_t*colors = getColors(image, size, &num);
+
+    assert(palettesize<=256);
+
+    qsort(colors, num, sizeof(colornum_t), compare_colors);
+
+    if(num<=palettesize) {
+        /* if there are not more than palettesize different colors in 
+           the image anyway, we are done */
+        int t;
+        for(t=0;t<num;t++) {
+            ret[t].r = colors[t].color;
+            ret[t].g = colors[t].color>>8;
+            ret[t].b = colors[t].color>>16;
+            ret[t].a = 255;
+        }
+        return ret;
+    }
+
+    if(num>2048) {
+       /* if there are too many different colors, pick the ones that
+          occur most often */
+       num = 2048;
+    }
+
+    colornum_t*centers = malloc(sizeof(colornum_t)*palettesize);
+    int t;
+    for(t=0;t<palettesize;t++) {
+        centers[t].color = colors[t].color;
+    }
+    unsigned char*belongsto = (unsigned char*)malloc(num);
+    memset(belongsto, 0, num);
+    /* do a k-means clustering on the colors */
+    char change = 1;
+    int tries = 0;
+    while(change) {
+        if(tries++ >= (palettesize+num)*2) {
+            fprintf(stderr, "Warning: didn't find optimal palette\n");
+            break;
+        }
+        change = 0;
+        int s,t;
+        for(s=0;s<palettesize;s++) {
+            centers[s].num = 0;
+        }
+        for(t=0;t<num;t++) {
+            int best=0x7fffffff;
+            int bestpos=0;
+            for(s=0;s<palettesize;s++) {
+                int distance = 0;
+                distance += abs((centers[s].color>>0&0xff) - (colors[t].color>>0&0xff));
+                distance += abs((centers[s].color>>8&0xff) - (colors[t].color>>8&0xff));
+                distance += abs((centers[s].color>>16&0xff) - (colors[t].color>>16&0xff));
+                distance *= colors[t].num;
+                if(distance<best) {
+                    best = distance;
+                    bestpos = s;
+                }
+            }
+            if(bestpos!=belongsto[t])
+                change = 1;
+            belongsto[t] = bestpos;
+        }
+        for(s=0;s<palettesize;s++) {
+            int r=0;
+            int g=0;
+            int b=0;
+            int count=0;
+            for(t=0;t<num;t++) {
+                if(belongsto[t]==s) {
+                    r += ((colors[t].color>>0)&0xff)*colors[t].num;
+                    g += ((colors[t].color>>8)&0xff)*colors[t].num;
+                    b += ((colors[t].color>>16)&0xff)*colors[t].num;
+                    count+=colors[t].num;
+                }
+            }
+            if(!count) {
+                int random = rand()%num;
+                centers[s].color = colors[random].color;
+                centers[s].num = 0;
+                change = 1;
+            } else {
+                r /= count;
+                g /= count;
+                b /= count;
+                centers[s].color = r|g<<8|b<<16;
+                centers[s].num = count;
+            }
+        }
+    }
+    free(belongsto);
+    free(colors);
+    for(t=0;t<palettesize;t++) {
+        ret[t].r = centers[t].color;
+        ret[t].g = centers[t].color>>8;
+        ret[t].b = centers[t].color>>16;
+        ret[t].a = 255;
+    }
+    free(centers);
+    return ret;
+}
+
+static int sqr(const int x) {return x*x;}
+
+static void quantizeImage(unsigned char*_image, int size, int numcolors, unsigned char**newimage, COL**palette) 
+{
+    COL*image = (COL*)_image;
+    COL*pal= getOptimalPalette(image, size, numcolors);
+    *palette = pal;
+    *newimage = (unsigned char*)malloc(size);
+    int t;
+    for(t=0;t<size;t++) {
+        int best=0x7fffffff;
+        int bestcol = 0;
+        int s;
+        for(s=0;s<numcolors;s++) {
+            int distance = 0;
+            distance += sqr((pal[s].r) - (image[t].r))*5;
+            distance += sqr((pal[s].g) - (image[t].g))*6;
+            distance += sqr((pal[s].b) - (image[t].b))*4;
+            if(distance<best) {
+                best = distance;
+                bestcol = s;
+            }
+        }
+        //image[t] = pal[bestcol];
+        (*newimage)[t] = bestcol;
+    }
+}
+
 static u32 mycrc32;
 
 static u32*crc32_table = 0;
@@ -784,21 +989,36 @@ static inline void png_write_byte(FILE*fi, unsigned char byte)
     fwrite(&byte,1,1,fi);
     mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
 }
-static void png_start_chunk(FILE*fi, char*type, int len)
+static long png_start_chunk(FILE*fi, char*type, int len)
 {
     unsigned char mytype[4]={0,0,0,0};
     unsigned char mylen[4];
+    long filepos;
     mylen[0] = len>>24;
     mylen[1] = len>>16;
     mylen[2] = len>>8;
     mylen[3] = len;
     memcpy(mytype,type,strlen(type));
+    filepos = ftell(fi);
     fwrite(&mylen, 4, 1, fi);
     mycrc32=0xffffffff;
     png_write_byte(fi,mytype[0]);
     png_write_byte(fi,mytype[1]);
     png_write_byte(fi,mytype[2]);
     png_write_byte(fi,mytype[3]);
+    return filepos;
+}
+static void png_patch_len(FILE*fi, int pos, int len)
+{
+    unsigned char mylen[4];
+    long filepos;
+    mylen[0] = len>>24;
+    mylen[1] = len>>16;
+    mylen[2] = len>>8;
+    mylen[3] = len;
+    fseek(fi, pos, SEEK_SET);
+    fwrite(&mylen, 4, 1, fi);
+    fseek(fi, 0, SEEK_END);
 }
 static void png_write_bytes(FILE*fi, unsigned char*bytes, int len)
 {
@@ -824,7 +1044,219 @@ static void png_end_chunk(FILE*fi)
     fwrite(&tmp2,4,1,fi);
 }
 
-EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
+#define ZLIB_BUFFER_SIZE 16384
+
+static long compress_line(z_stream*zs, Bytef*line, int len, FILE*fi)
+{
+    long size = 0;
+    zs->next_in = line;
+    zs->avail_in = len;
+
+    while(1) {
+       int ret = deflate(zs, Z_NO_FLUSH);
+       if (ret != Z_OK) {
+           fprintf(stderr, "error in deflate(): %s", zs->msg?zs->msg:"unknown");
+           return 0;
+       }
+       if(zs->avail_out != ZLIB_BUFFER_SIZE) {
+           int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
+           size += consumed;
+           png_write_bytes(fi, zs->next_out - consumed , consumed);
+           zs->next_out = zs->next_out - consumed;
+           zs->avail_out = ZLIB_BUFFER_SIZE;
+       }
+       if(!zs->avail_in) {
+           break;
+       }
+    }
+    return size;
+}
+
+static int test_line(z_stream*zs_orig, Bytef*line, int linelen)
+{
+    z_stream zs;
+    int ret = deflateCopy(&zs, zs_orig);
+    if(ret != Z_OK) {
+       fprintf(stderr, "Couldn't copy stream\n");
+       return 0;
+    }
+
+    zs.next_in = line;
+    zs.avail_in = linelen;
+
+    long size = 0;
+
+    int mode = Z_SYNC_FLUSH;
+    while(1) {
+       int ret = deflate(&zs, mode);
+       if (ret != Z_OK && ret != Z_STREAM_END) {
+           fprintf(stderr, "error in deflate(): %s (mode %s, %d bytes remaining)\n", zs.msg?zs.msg:"unknown", 
+                   mode==Z_SYNC_FLUSH?"Z_SYNC_FLUSH":"Z_FINISH", zs.avail_in);
+           return 0;
+       }
+       if(zs.avail_out != ZLIB_BUFFER_SIZE) {
+           int consumed = ZLIB_BUFFER_SIZE - zs.avail_out;
+           size += consumed;
+           zs.next_out = zs.next_out - consumed;
+           zs.avail_out = ZLIB_BUFFER_SIZE;
+       }
+        if (ret == Z_STREAM_END) {
+            break;
+        }
+       if(!zs.avail_in) {
+           mode = Z_FINISH;
+       }
+    }
+    ret = deflateEnd(&zs);
+    if (ret != Z_OK) {
+       fprintf(stderr, "error in deflateEnd(): %s\n", zs.msg?zs.msg:"unknown");
+       return 0;
+    }
+    return size;
+}
+
+static int finishzlib(z_stream*zs, FILE*fi)
+{
+    int size = 0;
+    int ret;
+    while(1) {
+        ret = deflate(zs, Z_FINISH);
+        if (ret != Z_OK &&
+            ret != Z_STREAM_END) {
+           fprintf(stderr, "error in deflate(finish): %s\n", zs->msg?zs->msg:"unknown");
+           return 0;
+       }
+
+       if(zs->avail_out != ZLIB_BUFFER_SIZE) {
+           int consumed = ZLIB_BUFFER_SIZE - zs->avail_out;
+           size += consumed;
+           png_write_bytes(fi, zs->next_out - consumed , consumed);
+           zs->next_out = zs->next_out - consumed;
+           zs->avail_out = ZLIB_BUFFER_SIZE;
+       }
+        if (ret == Z_STREAM_END) {
+            break;
+        }
+    }
+    ret = deflateEnd(zs);
+    if (ret != Z_OK) {
+       fprintf(stderr, "error in deflateEnd(): %s\n", zs->msg?zs->msg:"unknown");
+       return 0;
+    }
+    return size;
+}
+
+static void filter_line8(int filtermode, unsigned char*dest, unsigned char*src, int width)
+{
+    int pos2 = 0;
+    int pos = 0;
+    int srcwidth = width;
+    int x;
+    if(filtermode == 0) {
+       for(x=0;x<width;x++) {
+           dest[pos2++]=src[pos++]; //alpha
+       }
+    } else if(filtermode == 1) {
+       /* x difference filter */
+       dest[pos2++]=src[pos++];
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos] - src[pos-1];
+           pos++;
+       }
+    } else if(filtermode == 2) {
+       /* y difference filter */
+       for(x=0;x<width;x++) {
+           dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
+           pos++;
+       }
+    } else if(filtermode == 3) {
+       dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
+       pos++;
+       /* x+y difference filter */
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos+0] - (src[pos-1+0] + src[pos-srcwidth+0])/2; //alpha
+           pos++;
+       }
+    } else if(filtermode == 4) {
+       dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
+       pos++;
+       /* paeth difference filter */
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-1+0], src[pos-srcwidth+0], src[pos-1-srcwidth+0]);
+           pos++;
+       }
+    }
+}
+
+static void filter_line32(int filtermode, unsigned char*dest, unsigned char*src, int width)
+{
+    int pos2 = 0;
+    int pos = 0;
+    int srcwidth = width*4;
+    int x;
+    if(filtermode == 0) {
+       for(x=0;x<width;x++) {
+           dest[pos2++]=src[pos+1];
+           dest[pos2++]=src[pos+2];
+           dest[pos2++]=src[pos+3];
+           dest[pos2++]=src[pos+0]; //alpha
+           pos+=4;
+       }
+    } else if(filtermode == 1) {
+       /* x difference filter */
+       dest[pos2++]=src[pos+1];
+       dest[pos2++]=src[pos+2];
+       dest[pos2++]=src[pos+3];
+       dest[pos2++]=src[pos+0];
+       pos+=4;
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos+1] - src[pos-4+1];
+           dest[pos2++]=src[pos+2] - src[pos-4+2];
+           dest[pos2++]=src[pos+3] - src[pos-4+3];
+           dest[pos2++]=src[pos+0] - src[pos-4+0]; //alpha
+           pos+=4;
+       }
+    } else if(filtermode == 2) {
+       /* y difference filter */
+       for(x=0;x<width;x++) {
+           dest[pos2++]=src[pos+1] - src[pos-srcwidth+1];
+           dest[pos2++]=src[pos+2] - src[pos-srcwidth+2];
+           dest[pos2++]=src[pos+3] - src[pos-srcwidth+3];
+           dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]; //alpha
+           pos+=4;
+       }
+    } else if(filtermode == 3) {
+       dest[pos2++]=src[pos+1] - src[pos-srcwidth+1]/2;
+       dest[pos2++]=src[pos+2] - src[pos-srcwidth+2]/2;
+       dest[pos2++]=src[pos+3] - src[pos-srcwidth+3]/2;
+       dest[pos2++]=src[pos+0] - src[pos-srcwidth+0]/2;
+       pos+=4;
+       /* x+y difference filter */
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos+1] - (src[pos-4+1] + src[pos-srcwidth+1])/2;
+           dest[pos2++]=src[pos+2] - (src[pos-4+2] + src[pos-srcwidth+2])/2;
+           dest[pos2++]=src[pos+3] - (src[pos-4+3] + src[pos-srcwidth+3])/2;
+           dest[pos2++]=src[pos+0] - (src[pos-4+0] + src[pos-srcwidth+0])/2; //alpha
+           pos+=4;
+       }
+    } else if(filtermode == 4) {
+       dest[pos2++]=src[pos+1] - PaethPredictor(0, src[pos-srcwidth+1], 0);
+       dest[pos2++]=src[pos+2] - PaethPredictor(0, src[pos-srcwidth+2], 0);
+       dest[pos2++]=src[pos+3] - PaethPredictor(0, src[pos-srcwidth+3], 0);
+       dest[pos2++]=src[pos+0] - PaethPredictor(0, src[pos-srcwidth+0], 0);
+       pos+=4;
+       /* paeth difference filter */
+       for(x=1;x<width;x++) {
+           dest[pos2++]=src[pos+1] - PaethPredictor(src[pos-4+1], src[pos-srcwidth+1], src[pos-4-srcwidth+1]);
+           dest[pos2++]=src[pos+2] - PaethPredictor(src[pos-4+2], src[pos-srcwidth+2], src[pos-4-srcwidth+2]);
+           dest[pos2++]=src[pos+3] - PaethPredictor(src[pos-4+3], src[pos-srcwidth+3], src[pos-4-srcwidth+3]);
+           dest[pos2++]=src[pos+0] - PaethPredictor(src[pos-4+0], src[pos-srcwidth+0], src[pos-4-srcwidth+0]);
+           pos+=4;
+       }
+    }
+}
+
+EXPORT void savePNG(const char*filename, unsigned char*data, int width, int height, int numcolors)
 {
     FILE*fi;
     int crc;
@@ -832,10 +1264,8 @@ EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
     unsigned char format;
     unsigned char tmp;
     unsigned char* data2=0;
-    unsigned char* data3=0;
     u32 datalen;
     u32 datalen2;
-    u32 datalen3;
     unsigned char head[] = {137,80,78,71,13,10,26,10}; // PNG header
     int cols;
     char alpha = 1;
@@ -844,12 +1274,21 @@ EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
     u32 tmp32;
     int bpp;
     int ret;
+    z_stream zs;
+    COL*palette=0;
 
     make_crc32_table();
 
-    bpp = 32;
-    cols = 0;
-    format = 5;
+    if(!numcolors) {
+        bpp = 32;
+        cols = 0;
+        format = 5;
+    } else {
+        bpp = 8;
+        cols = numcolors;
+        format = 3;
+        quantizeImage(data, width*height, numcolors, &data, &palette);
+    }
 
     datalen = (width*height*bpp/8+cols*8);
     
@@ -876,51 +1315,87 @@ EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
      png_write_byte(fi,0); //filter mode
      png_write_byte(fi,0); //interlace mode
     png_end_chunk(fi);
-   
-/*    if(format == 3) {
+
+    if(format == 3) {
        png_start_chunk(fi, "PLTE", 768);
-        
-        for(t=0;t<256;t++) {
+        for(t=0;t<cols;t++) {
             png_write_byte(fi,palette[t].r);
             png_write_byte(fi,palette[t].g);
             png_write_byte(fi,palette[t].b);
         }
        png_end_chunk(fi);
-    }*/
+    }
+    long idatpos = png_start_chunk(fi, "IDAT", 0);
+    
+    memset(&zs,0,sizeof(z_stream));
+    Bytef*writebuf = (Bytef*)malloc(ZLIB_BUFFER_SIZE);
+    zs.zalloc = Z_NULL;
+    zs.zfree  = Z_NULL;
+    zs.opaque = Z_NULL;
+    zs.next_out = writebuf;
+    zs.avail_out = ZLIB_BUFFER_SIZE;
+    ret = deflateInit(&zs, 9);
+    if (ret != Z_OK) {
+       fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
+       return;
+    }
+
+    long idatsize = 0;
     {
-       int pos2 = 0;
        int x,y;
-       int srcwidth = width * (bpp/8);
-       datalen3 = (width*4+5)*height;
-       data3 = (unsigned char*)malloc(datalen3);
+        int bypp = bpp/8;
+       int srcwidth = width * bypp;
+       int linelen = 1 + srcwidth;
+        if(bypp==2) 
+            linelen = 1 + ((srcwidth+1)&~1);
+        else if(bypp==3) 
+            linelen = 1 + ((srcwidth+2)/3)*3;
+        else if(bypp==4) 
+            linelen = 1 + ((srcwidth+3)&~3);
+       unsigned char* line = (unsigned char*)malloc(linelen);
+       unsigned char* bestline = (unsigned char*)malloc(linelen);
+       memset(line, 0, linelen);
        for(y=0;y<height;y++)
        {
-          data3[pos2++]=0; //filter type
-          for(x=0;x<width;x++) {
-              data3[pos2++]=data[pos+1];
-              data3[pos2++]=data[pos+2];
-              data3[pos2++]=data[pos+3];
-              data3[pos2++]=data[pos+0]; //alpha
-              pos+=4;
-          }
-          pos+=((srcwidth+3)&~3)-srcwidth; //align
-       }
-       datalen3=pos2;
-    }
+           int filtermode;
+           int bestsize = 0x7fffffff;
+           for(filtermode=0;filtermode<=0;filtermode++) {
+               if(!y && filtermode>=2)
+                   continue; // don't do y direction filters in the first row
+               
+               line[0]=filtermode; //filter type
+                if(bpp==8)
+                   filter_line8(filtermode, line+1, &data[y*srcwidth], width);
+                else
+                   filter_line32(filtermode, line+1, &data[y*srcwidth], width);
 
-    datalen2 = datalen3;
-    data2 = (unsigned char*)malloc(datalen2);
-
-    if((ret = compress (data2, &datalen2, data3, datalen3)) != Z_OK) {
-       fprintf(stderr, "zlib error in pic %d\n", ret);
-       return;
+               int size = test_line(&zs, line, linelen);
+               if(size < bestsize) {
+                   memcpy(bestline, line, linelen);
+                   bestsize = size;
+               }
+           }
+           idatsize += compress_line(&zs, bestline, linelen, fi);
+       }
+       free(line);free(bestline);
     }
-    png_start_chunk(fi, "IDAT", datalen2);
-    png_write_bytes(fi,data2,datalen2);
+    idatsize += finishzlib(&zs, fi);
+    png_patch_len(fi, idatpos, idatsize);
     png_end_chunk(fi);
+
     png_start_chunk(fi, "IEND", 0);
     png_end_chunk(fi);
 
+    free(writebuf);
     free(data2);
-    free(data3);
+    fclose(fi);
+}
+
+EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
+{
+    savePNG(filename, data, width, height, 0);
+}
+EXPORT void writePalettePNG(const char*filename, unsigned char*data, int width, int height)
+{
+    savePNG(filename, data, width, height, 256);
 }