1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include "Imlib.h"
#include "Imlib_private.h"
void _PaletteAlloc(ImlibData *id, int num, int *cols)
{
XColor xcl;
int i;
int r,g,b;
if (id->palette) free(id->palette);
id->palette=malloc(sizeof(ImlibColor)*num);
if (id->palette_orig) free(id->palette_orig);
id->palette_orig=malloc(sizeof(ImlibColor)*num);
for(i=0;i<num;i++)
{
r=cols[(i*3)+0];
g=cols[(i*3)+1];
b=cols[(i*3)+2];
xcl.red=(unsigned short)((r<<8)|(r));
xcl.green=(unsigned short)((g<<8)|(g));
xcl.blue=(unsigned short)((b<<8)|(b));
xcl.flags=DoRed|DoGreen|DoBlue;
XAllocColor(id->x.disp,id->x.root_cmap,&xcl);
id->palette[i].r=xcl.red>>8;
id->palette[i].g=xcl.green>>8;
id->palette[i].b=xcl.blue>>8;
id->palette[i].pixel=xcl.pixel;
id->palette_orig[i].r=r;
id->palette_orig[i].g=g;
id->palette_orig[i].b=b;
id->palette_orig[i].pixel=xcl.pixel;
}
id->num_colors=num;
}
int Imlib_load_colors(ImlibData *id, char *file)
{
FILE *f;
char s[256];
int i;
int pal[768];
int r,g,b;
int rr,gg,bb;
f=fopen(file,"r");
if (!f)
{
fprintf(stderr,"ImLib ERROR: Cannot find palette file %s\n",file);
return 0;
}
i=0;
while(fgets(s,256,f))
{
if (s[0]=='0')
{
sscanf(s,"%x %x %x",&r,&g,&b);
if (r<0) r=0; if (r>255) r=255;
if (g<0) g=0; if (g>255) g=255;
if (b<0) b=0; if (b>255) b=255;
pal[i++]=r;
pal[i++]=g;
pal[i++]=b;
}
if (i>=768) break;
}
fclose(f);
_PaletteAlloc(id,(i/3),pal);
if (id->fast_rgb) free(id->fast_rgb);
id->fast_rgb=malloc(sizeof(int)*32*32*32);
if (id->fast_err) free(id->fast_err);
id->fast_err=malloc(sizeof(int)*32*32*32);
if (id->fast_erg) free(id->fast_erg);
id->fast_erg=malloc(sizeof(int)*32*32*32);
if (id->fast_erb) free(id->fast_erb);
id->fast_erb=malloc(sizeof(int)*32*32*32);
for (r=0;r<32;r++)
{
for (g=0;g<32;g++)
{
for (b=0;b<32;b++)
{
rr=(r<<3)|(r>>2);
gg=(g<<3)|(g>>2);
bb=(b<<3)|(b>>2);
id->FAST_RGB(r,g,b)=Imlib_best_color_match(id,&rr,&gg,&bb);
id->FAST_ERR(r,g,b)=rr;
id->FAST_ERG(r,g,b)=gg;
id->FAST_ERB(r,g,b)=bb;
}
}
}
return 1;
}
void Imlib_free_colors(ImlibData *id)
{
int i;
unsigned long pixels[256];
for (i=0;i<id->num_colors;i++) pixels[i]=id->palette[i].pixel;
XFreeColors(id->x.disp,id->x.root_cmap,pixels,id->num_colors,0);
id->num_colors=0;
}
|