removed parameter_t
[swftools.git] / lib / png.c
index e305872..8578d2d 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -99,12 +99,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];
@@ -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++;
@@ -439,7 +440,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 +449,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 +457,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 +485,6 @@ 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);
        return 0;
     }
 
@@ -619,6 +618,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,8 +641,7 @@ 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];
            }
@@ -661,6 +662,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;
@@ -1009,7 +1011,7 @@ static void filter_line(int filtermode, unsigned char*dest, unsigned char*src, i
     }
 }
 
-EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
+EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
 {
     FILE*fi;
     int crc;
@@ -1074,7 +1076,7 @@ EXPORT void writePNG(char*filename, unsigned char*data, int width, int height)
     long idatpos = png_start_chunk(fi, "IDAT", 0);
     
     memset(&zs,0,sizeof(z_stream));
-    Bytef*writebuf = malloc(ZLIB_BUFFER_SIZE);
+    Bytef*writebuf = (Bytef*)malloc(ZLIB_BUFFER_SIZE);
     zs.zalloc = Z_NULL;
     zs.zfree  = Z_NULL;
     zs.opaque = Z_NULL;