git.asbjorn.biz
/
swftools.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
added import{.c/.h} dependencies
[swftools.git]
/
lib
/
png.c
diff --git
a/lib/png.c
b/lib/png.c
index
c30f77e
..
d71c19b
100644
(file)
--- a/
lib/png.c
+++ b/
lib/png.c
@@
-779,10
+779,10
@@
typedef struct {
u32 color;
} colornum_t;
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;
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)
}
static colornum_t* getColors(COL*image, int size, int*num)
@@
-817,6
+817,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) {
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;
}
if(colors[i].color >= col) max=i;
else min=i+1;
}
@@
-852,6
+853,12
@@
static COL* getOptimalPalette(COL*image, int size, int palettesize)
return ret;
}
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;t++) {
colornum_t*centers = malloc(sizeof(colornum_t)*palettesize);
int t;
for(t=0;t<palettesize;t++) {
@@
-904,7
+911,7
@@
static COL* getOptimalPalette(COL*image, int size, int palettesize)
}
}
if(!count) {
}
}
if(!count) {
- int random = lrand48()%num;
+ int random = rand()%num;
centers[s].color = colors[random].color;
centers[s].num = 0;
change = 1;
centers[s].color = colors[random].color;
centers[s].num = 0;
change = 1;
@@
-1335,8
+1342,15
@@
EXPORT void savePNG(const char*filename, unsigned char*data, int width, int heig
long idatsize = 0;
{
int x,y;
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);
unsigned char* line = (unsigned char*)malloc(linelen);
unsigned char* bestline = (unsigned char*)malloc(linelen);
memset(line, 0, linelen);
@@
-1376,11
+1390,11
@@
EXPORT void savePNG(const char*filename, unsigned char*data, int width, int heig
fclose(fi);
}
fclose(fi);
}
-void writePNG(const char*filename, unsigned char*data, int width, int height)
+EXPORT void writePNG(const char*filename, unsigned char*data, int width, int height)
{
savePNG(filename, data, width, height, 0);
}
{
savePNG(filename, data, width, height, 0);
}
-void writePalettePNG(const char*filename, unsigned char*data, int width, int height)
+EXPORT void writePalettePNG(const char*filename, unsigned char*data, int width, int height)
{
savePNG(filename, data, width, height, 256);
}
{
savePNG(filename, data, width, height, 256);
}