fixed drawlink() ruby callback
[swftools.git] / lib / png.c
index c548aa2..1738151 100644 (file)
--- a/lib/png.c
+++ b/lib/png.c
@@ -122,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) {
@@ -486,6 +486,7 @@ EXPORT int getPNG(const char*sname, int*destwidth, int*destheight, unsigned char
     }
 
     if(!png_read_header(fi, &header)) {
+       fclose(fi);
        return 0;
     }
 
@@ -1342,8 +1343,15 @@ EXPORT void savePNG(const char*filename, unsigned char*data, int width, int heig
     long idatsize = 0;
     {
        int x,y;
-       int srcwidth = width * (bpp/8);
-       int linelen = 1 + ((srcwidth+3)&~3);
+        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);