X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fpng.c;h=17381514f46cd9876043a221cb2a579203d990e8;hp=e30587204e14ed8571b98ab6b84598ef06721cac;hb=2c719855eac434f01d47ba0717d76de65939d74e;hpb=4513e3be4df534f961bdca64480631fe798c67fb diff --git a/lib/png.c b/lib/png.c index e305872..1738151 100644 --- a/lib/png.c +++ b/lib/png.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -99,12 +100,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]; @@ -121,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) { @@ -197,6 +198,7 @@ static void applyfilter1(int mode, unsigned char*src, unsigned char*old, unsigne else if(mode==3) { for(x=0;xnum - c1->num; +} + +static colornum_t* getColors(COL*image, int size, int*num) +{ + unsigned char*colexists = malloc((256*256*256)/8); + memset(colexists, 0, (256*256*256)/8); + int t; + int count=0; + + /* find all different colors in the image */ + for(t=0;t= col) max=i; + else min=i+1; + } + assert(colors[i].color==col); + colors[i].num++; + } + free(colexists); + *num = count; + return colors; +} + +static COL* getOptimalPalette(COL*image, int size, int palettesize) +{ + int num; + COL* ret = malloc(sizeof(COL)*palettesize); + memset(ret, 0, sizeof(COL)*palettesize); + colornum_t*colors = getColors(image, size, &num); + + assert(palettesize<=256); + + qsort(colors, num, sizeof(colornum_t), compare_colors); + + if(num<=palettesize) { + /* if there are not more than palettesize different colors in + the image anyway, we are done */ + int t; + for(t=0;t>8; + ret[t].b = colors[t].color>>16; + ret[t].a = 255; + } + 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= (palettesize+num)*2) { + fprintf(stderr, "Warning: didn't find optimal palette\n"); + break; + } + change = 0; + int s,t; + for(s=0;s>0&0xff) - (colors[t].color>>0&0xff)); + distance += abs((centers[s].color>>8&0xff) - (colors[t].color>>8&0xff)); + distance += abs((centers[s].color>>16&0xff) - (colors[t].color>>16&0xff)); + distance *= colors[t].num; + if(distance>0)&0xff)*colors[t].num; + g += ((colors[t].color>>8)&0xff)*colors[t].num; + b += ((colors[t].color>>16)&0xff)*colors[t].num; + count+=colors[t].num; + } + } + if(!count) { + int random = rand()%num; + centers[s].color = colors[random].color; + centers[s].num = 0; + change = 1; + } else { + r /= count; + g /= count; + b /= count; + centers[s].color = r|g<<8|b<<16; + centers[s].num = count; + } + } + } + free(belongsto); + free(colors); + for(t=0;t>8; + ret[t].b = centers[t].color>>16; + ret[t].a = 255; + } + free(centers); + return ret; +} + +static int sqr(const int x) {return x*x;} + +static void quantizeImage(unsigned char*_image, int size, int numcolors, unsigned char**newimage, COL**palette) +{ + COL*image = (COL*)_image; + COL*pal= getOptimalPalette(image, size, numcolors); + *palette = pal; + *newimage = (unsigned char*)malloc(size); + int t; + for(t=0;t=2) continue; // don't do y direction filters in the first row line[0]=filtermode; //filter type - filter_line(filtermode, line+1, &data[y*srcwidth], width); + if(bpp==8) + filter_line8(filtermode, line+1, &data[y*srcwidth], width); + else + filter_line32(filtermode, line+1, &data[y*srcwidth], width); int size = test_line(&zs, line, linelen); if(size < bestsize) { @@ -1128,3 +1391,11 @@ EXPORT void writePNG(char*filename, unsigned char*data, int width, int height) fclose(fi); } +EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height) +{ + savePNG(filename, data, width, height, 0); +} +EXPORT void writePalettePNG(const char*filename, unsigned char*data, int width, int height) +{ + savePNG(filename, data, width, height, 256); +}