made a few functions static
[swftools.git] / lib / png.c
index f114e91..96fd463 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -30,7 +30,7 @@ typedef struct _COL {
     U8 a,r,g,b;
 } COL;
 
-int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
+static int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
 {
     unsigned int len;
     unsigned char blen[4];
@@ -63,7 +63,7 @@ int png_read_chunk(char (*head)[4], int*destlen, U8**destdata, FILE*fi)
     return 1;
 }
 
-unsigned int png_get_dword(FILE*fi)
+static unsigned int png_get_dword(FILE*fi)
 {
     unsigned int a;
     unsigned char b[4];
@@ -79,7 +79,7 @@ struct png_header
     int mode;
 };
 
-int png_read_header(FILE*fi, struct png_header*header)
+static int png_read_header(FILE*fi, struct png_header*header)
 {
     char id[4];
     int len;
@@ -139,7 +139,7 @@ int png_read_header(FILE*fi, struct png_header*header)
 
 typedef unsigned char byte;
 #define ABS(a) ((a)>0?(a):(-(a)))
-byte inline PaethPredictor (byte a,byte b,byte c)
+static inline byte PaethPredictor (byte a,byte b,byte c)
 {
         // a = left, b = above, c = upper left
         int p = a + b - c;        // initial estimate
@@ -155,7 +155,7 @@ byte inline PaethPredictor (byte a,byte b,byte c)
         else return c;
 }
 
-void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
+static void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
 {
     int x;
     unsigned char last=0;
@@ -205,7 +205,7 @@ void applyfilter1(int mode, U8*src, U8*old, U8*dest, int width)
 
 }
 
-void applyfilter2(int mode, U8*src, U8*old, U8*dest, int width)
+static void applyfilter2(int mode, U8*src, U8*old, U8*dest, int width)
 {
     int x;
     unsigned char lasta=0;
@@ -268,7 +268,7 @@ void applyfilter2(int mode, U8*src, U8*old, U8*dest, int width)
 
 
 /* also performs 24 bit conversion! */
-void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
+static void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
 {
     int x;
     unsigned char lastr=0;
@@ -345,7 +345,7 @@ void applyfilter3(int mode, U8*src, U8*old, U8*dest, int width)
     }    
 }
 
-void inline applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
+static void inline applyfilter4(int mode, U8*src, U8*old, U8*dest, int width)
 {
     int x;
     unsigned char lastr=0;
@@ -545,8 +545,9 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
            }
            printf("\n");*/
        }
-       if(data)
-           free(data);
+       if(data) {
+           free(data); data=0;
+        }
     }
     
     if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
@@ -602,6 +603,7 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
            }
        }
        free(old);
+        free(imagedata);
     } else if(header.mode == 6 || header.mode == 2) {
        int i,s=0;
        int x,y;
@@ -632,11 +634,24 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
            } 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
+           } else { // header.mode = 2
                applyfilter3(mode, src, old, dest, header.width);
+               /* replace alpha color */
+               if(hasalphacolor) {
+                   int x;
+                   for(x=0;x<header.width;x++) {
+                       if(dest[x*4+1] == alphacolor[0] &&
+                          dest[x*4+2] == alphacolor[1] &&
+                          dest[x*4+3] == alphacolor[2]) {
+                           *(U32*)&dest[x*4] = 0;
+                       }
+                   }
+               }
+           }
        }
+        free(imagedata);
     } else if(header.mode == 0 || header.mode == 3) {
        COL*rgba = 0;
        U8*tmpline = (U8*)malloc(header.width+1);
@@ -726,6 +741,7 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata)
        free(tmpline);
        free(destline);
        free(rgba);
+        free(imagedata);
     } else {
        printf("expected PNG mode to be 2, 3 or 6 (is:%d)\n", header.mode);
        return 0;
@@ -883,7 +899,7 @@ void writePNG(char*filename, unsigned char*data, int width, int height)
     }
 
     datalen2 = datalen3;
-    data2 = malloc(datalen2);
+    data2 = (U8*)malloc(datalen2);
 
     if((ret = compress (data2, &datalen2, data3, datalen3)) != Z_OK) {
        fprintf(stderr, "zlib error in pic %d\n", ret);