X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fpng.c;h=abb5a3e83460a3f0fcc7717eea68eb3cda517537;hb=7f237d6ab33b067711025e6d8f2d77b534dbe722;hp=c1ce8d0859d17ad2c27ada312c6f4330b48a8f4b;hpb=33a5595bc732637a910b85db62e71c2d9a1599c5;p=swftools.git diff --git a/lib/png.c b/lib/png.c index c1ce8d0..abb5a3e 100644 --- a/lib/png.c +++ b/lib/png.c @@ -463,6 +463,8 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata) struct png_header header; int bypp; U8*data2 = 0; + U8 alphacolor[3]; + int hasalphacolor=0; FILE *fi; U8 *scanline; @@ -509,6 +511,16 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata) alphapalettelen = len; data = 0; //don't free data //printf("found %d alpha colors\n", alphapalettelen); + } else if(header.mode == 0 || header.mode == 2) { + int t; + if(header.mode == 2) { + alphacolor[0] = data[1]; + alphacolor[1] = data[3]; + alphacolor[2] = data[5]; + } else { + alphacolor[0] = alphacolor[1] = alphacolor[2] = data[1]; + } + hasalphacolor = 1; } } if(!strncmp(tagid, "IDAT", 4)) { @@ -533,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) { @@ -590,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; @@ -625,6 +639,7 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata) else // header.mode = 2 applyfilter3(mode, src, old, dest, header.width); } + free(imagedata); } else if(header.mode == 0 || header.mode == 3) { COL*rgba = 0; U8*tmpline = (U8*)malloc(header.width+1); @@ -643,6 +658,10 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata) rgba[i].r = i*mult; rgba[i].g = i*mult; rgba[i].b = i*mult; + if(hasalphacolor) { + if(rgba[i].r == alphacolor[0]) + rgba[i].a = 0; + } } } else { if(!palette) { @@ -663,6 +682,12 @@ int getPNG(char*sname, int*destwidth, int*destheight, unsigned char**destdata) } else { rgba[i].a = 255; } + if(hasalphacolor) { + if(rgba[i].r == alphacolor[0] && + rgba[i].g == alphacolor[1] && + rgba[i].b == alphacolor[2]) + rgba[i].a = 0; + } } } @@ -704,6 +729,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; @@ -861,7 +887,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);