X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fpng.c;h=17381514f46cd9876043a221cb2a579203d990e8;hp=c30f77e898f7a91dd7fe14c3150bc5cf3145b2df;hb=3d73649bf0e39778e715a07da902d0a858065a43;hpb=fae20a521d8326fe5e1a2bd07375c1cdd0a34066 diff --git a/lib/png.c b/lib/png.c index c30f77e..1738151 100644 --- 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; } @@ -779,10 +780,10 @@ typedef struct { u32 color; } colornum_t; -int compare_colors(const void*_c1, const void*_c2) { +static int compare_colors(const void*_c1, const void*_c2) { colornum_t*c1 = (colornum_t*)_c1; colornum_t*c2 = (colornum_t*)_c2; - return c1->num - c2->num; + return c2->num - c1->num; } static colornum_t* getColors(COL*image, int size, int*num) @@ -817,6 +818,7 @@ static colornum_t* getColors(COL*image, int size, int*num) int col = (image[t].r)|(image[t].g)<<8|(image[t].b)<<16; int min,max,i,l; for(min=0, max=count, i=count/2, l=count; i != l; l=i,i=(min+max)/2) { + // binary search if(colors[i].color >= col) max=i; else min=i+1; } @@ -852,6 +854,12 @@ static COL* getOptimalPalette(COL*image, int size, int palettesize) return ret; } + if(num>2048) { + /* if there are too many different colors, pick the ones that + occur most often */ + num = 2048; + } + colornum_t*centers = malloc(sizeof(colornum_t)*palettesize); int t; for(t=0;t