inverted monochrome handler
[swftools.git] / lib / png.c
index fac4f77..e290b6f 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -99,7 +99,7 @@ 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))
     {
@@ -197,6 +197,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++;
@@ -448,7 +449,6 @@ EXPORT int getPNGdimensions(const 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;
     }
 
@@ -485,7 +485,6 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
     }
 
     if(!png_read_header(fi, &header)) {
-       printf("Error reading header from file %s\n", sname);
        return 0;
     }
 
@@ -619,6 +618,9 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
        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,8 +641,7 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
            }
 
            if(!y) {
-               memset(data2,0,header.width*4);
-               old = &data2[y*header.width*4];
+               old = firstline;
            } else {
                old = &data2[(y-1)*header.width*4];
            }
@@ -661,6 +662,7 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
                }
            }
        }
+       free(firstline);
         free(imagedata);
     } else if(header.mode == 0 || header.mode == 3) {
        COL*rgba = 0;
@@ -1080,7 +1082,7 @@ EXPORT void writePNG(const char*filename, unsigned char*data, int width, int hei
     zs.opaque = Z_NULL;
     zs.next_out = writebuf;
     zs.avail_out = ZLIB_BUFFER_SIZE;
-    ret = deflateInit(&zs, 9);
+    ret = deflateInit(&zs, Z_NO_COMPRESSION);
     if (ret != Z_OK) {
        fprintf(stderr, "error in deflateInit(): %s", zs.msg?zs.msg:"unknown");
        return;
@@ -1098,7 +1100,7 @@ EXPORT void writePNG(const char*filename, unsigned char*data, int width, int hei
        {
            int filtermode;
            int bestsize = 0x7fffffff;
-           for(filtermode=0;filtermode<5;filtermode++) {
+           for(filtermode=0;filtermode<=0;filtermode++) {
 
                if(!y && filtermode>=2)
                    continue; // don't do y direction filters in the first row