Debian Codegraph @

gdraw/gimageclut.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "colorP.h"
31 #include "gdrawP.h"
32 #include "ustring.h"
33
34 static void RevColListFree(struct revcol *rc) {
35 struct revcol *next;
36
37 while ( rc!=NULL ) {
38 next = rc->next;
39 free(rc);
40 rc = next;
41 }
42 }
43
44 static const GCol white = { 0xff, 0xff, 0xff, 1 }, black = { 0, 0, 0, 0 };
45
46 const GCol *_GImage_GetIndexedPixel(Color col,RevCMap *rev) {
47 int val, t, best, r, g, b;
48 struct revitem *this;
49 struct revcol *test, *bestcol;
50
51 if ( rev==NULL ) { /* Mono */
52 if ( COLOR2WHITE(col) )
53 return( &white );
54 else
55 return( &black );
56 }
57 if ( rev->is_grey ) {
58 val = COLOR2GREY(col);
59 return( &rev->greys[val]);
60 }
61
62 reverse_clut:
63 r = COLOR_RED(col); g = COLOR_GREEN(col); b = COLOR_BLUE(col);
64 if ( rev->div_mul==1 ) {
65 val = r>>rev->div_shift;
66 val = (val<<rev->side_shift)+(g>>rev->div_shift);
67 val = (val<<rev->side_shift)+(b>>rev->div_shift);
68 } else {
69 val = ((r+rev->div_add)*rev->div_mul)>>rev->div_shift;
70 t = ((g+rev->div_add)*rev->div_mul)>>rev->div_shift;
71 val = (val*rev->side_cnt)+t;
72 t = ((b+rev->div_add)*rev->div_mul)>>rev->div_shift;
73 val = (val*rev->side_cnt)+t;
74 }
75 this = &rev->cube[val];
76 if ( this->sub!=NULL ) {
77 col &= rev->mask;
78 rev = this->sub;
79 goto reverse_clut;
80 }
81
82 test = this->cols[0];
83 if ( test->next==NULL )
84 return( (GCol *) test );
85
86 if (( best = (r-test->red))<0 ) best = -best;
87 if (( t = (g-test->green))<0 ) t = -t; best += t;
88 if (( t = (b-test->blue))<0 ) t = -t; best += t;
89 bestcol = test;
90 for ( test=test->next; test!=NULL; test = test->next ) {
91 if (( val = (r-test->red))<0 ) val = -val;
92 if (( t = (g-test->green))<0 ) t = -t; val += t;
93 if (( t = (b-test->blue))<0 ) t = -t; val += t;
94 if ( val<best ) {
95 val = best;
96 bestcol = test;
97 }
98 }
99 return( (GCol *) bestcol );
100 }
101
102 static int rccnt( struct revcol *cols) {
103 int cnt = 0;
104 while ( cols!=NULL ) {
105 ++cnt;
106 cols = cols->next;
107 }
108 return( cnt );
109 }
110
111 static struct revcol *add_adjacent(struct revcol *test, struct revcol *old, Color col, int dmax) {
112 int r=COLOR_RED(col), g=COLOR_GREEN(col), b=COLOR_BLUE(col);
113 int off, t, bestoff;
114 struct revcol *best=NULL;
115
116 if ( test==NULL || test->dist>dmax )
117 return( old );
118 bestoff = 3*255;
119 for ( test=test; test!=NULL; test = test->next ) {
120 if ( (off = (r-test->red))<0 ) off = -off;
121 if ( (t = (g-test->green))<0 ) t = -t; off +=t;
122 if ( (b = (g-test->blue))<0 ) t = -t; off +=t;
123 if ( off<bestoff ) {
124 bestoff = off;
125 best = test;
126 }
127 }
128 if ( old!=NULL ) {
129 if ( (off = (r-old->red))<0 ) off = -off;
130 if ( (t = (g-old->green))<0 ) t = -t; off +=t;
131 if ( (b = (g-old->blue))<0 ) t = -t; off +=t;
132 if ( off<bestoff ) {
133 bestoff = off;
134 best = old;
135 }
136 }
137 if ( best!=old ) {
138 if ( old==NULL ) old = calloc(1,sizeof(struct revcol));
139 *old = *best;
140 old->next = NULL;
141 ++old->dist;
142 }
143 return( old );
144 }
145
146 static struct revcol *addrevcol(struct revcol *copy,struct revcol *old, int dist) {
147 struct revcol *rc = malloc(sizeof(struct revcol));
148
149 *rc = *copy;
150 rc->next = old;
151 rc->dist = dist;
152 return( rc );
153 }
154
155 static RevCMap *_GClutReverse(int side_cnt,int range,struct revcol *basecol,
156 struct revcol *cols,struct revcol *outercols) {
157 RevCMap *rev;
158 int i, s2, s3, side_size;
159 struct revcol *test;
160 int changed, dmax, anynulls;
161
162 rev = calloc(1,sizeof(RevCMap));
163
164 rev->side_cnt = side_cnt;
165 rev->range = range;
166 if ( side_cnt<0 ) {
167 /* special cases ... */
168 side_cnt = -side_cnt;
169 rev->side_cnt = side_cnt;
170 if ( side_cnt==6 ) { /* Used for the inefficient standard 6 by cube */
171 rev->div_mul = div_tables[0x33][0];
172 rev->div_shift = div_tables[0x33][1];
173 rev->div_add = 0x19;
174 }
175 side_size = 0x33;
176 } else if ( div_tables[side_cnt][0]==1 ) {
177 rev->side_shift = div_tables[side_cnt][1];
178 rev->div_shift = div_tables[range][1]-rev->side_shift;
179 rev->div_mul = 1;
180 rev->mask = ( 0x010101 * ((-1<rev->div_shift)&0xff) );
181 side_size = 1<<(rev->div_shift);
182 } else {
183 side_size = (range+side_cnt-1)/side_cnt;
184 rev->div_mul = div_tables[side_cnt][0];
185 rev->div_shift = div_tables[side_cnt][1];
186 }
187
188 rev->cube = calloc(side_cnt*side_cnt*side_cnt,sizeof(struct revitem));
189 for ( test = cols; test!=NULL; test = test->next ) {
190 int pos, dist;
191 int r,g,b;
192 int rbase = (test->red-basecol->red)/side_size, gbase = (test->green-basecol->green)/side_size, bbase = (test->blue-basecol->blue)/side_size;
193 for ( r = (test->red-basecol->red-side_size/2)/side_size;
194 r<=(test->red-basecol->red+side_size/2)/side_size; ++r ) {
195 if ( r<0 || r==side_cnt )
196 continue;
197 for ( g = (test->green-basecol->green-side_size/2)/side_size;
198 g<=(test->green-basecol->green+side_size/2)/side_size; ++g ) {
199 if ( g<0 || g==side_cnt )
200 continue;
201 for ( b = (test->blue-basecol->blue-side_size/2)/side_size;
202 b<=(test->blue-basecol->blue+side_size/2)/side_size; ++b ) {
203 if ( b<0 || b==side_cnt )
204 continue;
205 pos = (r*side_cnt + g)*side_cnt + b;
206 dist = r!=rbase || g!=gbase || b!=bbase;
207 rev->cube[pos].cols[dist] = addrevcol(test,rev->cube[pos].cols[dist],
208 dist );
209 }
210 }
211 }
212 }
213
214 /* Ok. We just put every color into the sub-cube that it belongs in */
215 /* but that's not good enough. Say we are looking for a color that */
216 /* is on the edge of a sub-cube, then the best match for it may be */
217 /* in one of the adjacent sub-cubes, rather than within it's own */
218 /* So we also ran through the list of colors, casting our net a */
219 /* little wider. Essentially for every sub-cube we found all the colors */
220 /* within a subcube centered on the color */
221
222 /* Some sub-cubes will probably have no information in them. Fill them */
223 /* in by looking at near-by cubes */
224 s2 = side_cnt*side_cnt; s3 = s2*side_cnt;
225 for ( i=0; i<s3; ++i ) {
226 if ( rev->cube[i].cols[0]==NULL && rev->cube[i].cols[1]!=NULL ) {
227 rev->cube[i].cols[0] = rev->cube[i].cols[1];
228 rev->cube[i].cols[1] = NULL;
229 }
230 }
231 changed = true; anynulls = false; dmax = 0;
232 while ( changed || (anynulls && dmax<256) ) {
233 changed = false; anynulls = false;
234 for ( i=0; i<s3; ++i ) {
235 if ( rev->cube[i].cols[0]==NULL ) {
236 int r = i/s2, g = (i/side_cnt)%side_cnt, b = i%side_cnt;
237 int col = ((r*side_cnt+(side_cnt>>1))<<16) |
238 ((g*side_cnt+(side_cnt>>1))<<8) |
239 ((b*side_cnt+(side_cnt>>1))) ;
240 if ( r>0 ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i-s2].cols[0],NULL,col,dmax);
241 if ( r+1<side_cnt ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i+s2].cols[0],rev->cube[i].cols[0],col,dmax);
242 if ( g>0 ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i-side_cnt].cols[0],rev->cube[i].cols[0],col,dmax);
243 if ( g+1<side_cnt ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i+side_cnt].cols[0],rev->cube[i].cols[0],col,dmax);
244 if ( b>0 ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i-1].cols[0],rev->cube[i].cols[0],col,dmax);
245 if ( b+1<side_cnt ) rev->cube[i].cols[0] = add_adjacent(rev->cube[i+1].cols[0],rev->cube[i].cols[0],col,dmax);
246 if ( rev->cube[i].cols[0]!=NULL ) changed = true;
247 else anynulls = true;
248 }
249 }
250 ++dmax;
251 }
252
253 if ( anynulls ) {
254 fprintf(stderr, "I'm sorry I cannot use this visual, please reconfigure your display\n" );
255 exit(1);
256 }
257
258 if ( rev->side_shift!=0 ) {
259 range >>= rev->side_shift;
260 if ( range>8 ) for ( i=0; i<s3; ++i ) if ( rev->cube[i].cols[0]->dist==0 ) {
261 int ql = rccnt(rev->cube[i].cols[0]);
262 int nr= -1;
263 struct revcol nbase;
264 if ( ql>128 )
265 nr = 16;
266 else if ( ql>32 )
267 nr = 8;
268 else if ( ql>=8 )
269 nr = 4;
270 if ( nr!=-1 ) {
271 nbase.red = basecol->red+(i/s2)*range;
272 nbase.green = basecol->green+(i/side_cnt)%side_cnt*range;
273 nbase.blue = basecol->blue+(i%side_cnt)*range;
274 while ( nr>range ) nr >>= 1;
275 if ( nr!=1 )
276 rev->cube[i].sub = _GClutReverse(nr,range,&nbase,
277 rev->cube[i].cols[0],rev->cube[i].cols[1]);
278 }
279 }
280 }
281 return( rev );
282 }
283
284 RevCMap *GClutReverse(GClut *clut,int side_cnt) {
285 struct revcol *base = NULL;
286 int i;
287 RevCMap *ret;
288 struct revcol basecol;
289
290 if ( GImageGreyClut(clut) ) {
291 GCol *greys; int changed;
292 ret = calloc(1,sizeof(RevCMap));
293 ret->is_grey = 1;
294 greys = ret->greys = malloc(256*sizeof(GCol));
295 for ( i=0; i<256; ++i ) greys[i].pixel = 0x1000;
296 for ( i=0; i<clut->clut_len; ++i ) {
297 int g = clut->clut[i]&0xff;
298 greys[g].red = greys[g].green = greys[g].blue = g;
299 greys[g].pixel = i;
300 }
301 changed = true;
302 while ( changed ) {
303 changed = false;
304 for ( i=0; i<256; ++i ) {
305 if ( greys[i].pixel!=0x1000 ) {
306 if ( i!=0 && greys[i-1].pixel == 0x1000 ) {
307 greys[i-1] = greys[i]; changed = true; }
308 if ( i!=255 && greys[i+1].pixel == 0x1000 ) {
309 greys[i+1] = greys[i]; changed = true; }
310 }
311 }
312 }
313 return( ret );
314 }
315
316 for ( i=0; i<clut->clut_len; ++i ) {
317 struct revcol *rc = malloc(sizeof(struct revcol));
318 rc->red = COLOR_RED(clut->clut[i]);
319 rc->green = COLOR_GREEN(clut->clut[i]);
320 rc->blue = COLOR_BLUE(clut->clut[i]);
321 rc->index = i;
322 rc->dist = 0;
323 rc->next = base;
324 base = rc;
325 }
326 memset(&basecol,'\0',sizeof(basecol));
327 ret = _GClutReverse(side_cnt,256,&basecol,base,base);
328 while ( base!=NULL ) {
329 struct revcol *rc = base->next;
330 free(base);
331 base = rc;
332 }
333 return( ret );
334 }
335
336 void GClut_RevCMapFree(RevCMap *rev) {
337 int i;
338
339 for ( i=0; i<rev->side_cnt*rev->side_cnt*rev->side_cnt; ++i ) {
340 if ( rev->cube[i].sub!=NULL )
341 GClut_RevCMapFree(rev->cube[i].sub);
342 RevColListFree(rev->cube[i].cols[0]);
343 RevColListFree(rev->cube[i].cols[1]);
344 }
345 free(rev->cube);
346 free(rev);
347 }
348
349 int GImageGreyClut(GClut *clut) {
350 int i, r,b,g;
351
352 if ( clut==NULL )
353 return( true );
354 for ( i=0; i<clut->clut_len; ++i ) {
355 r = COLOR_RED(clut->clut[i]);
356 g = COLOR_GREEN(clut->clut[i]);
357 b = COLOR_BLUE(clut->clut[i]);
358 if ( r!=g || g!=b ) {
359 clut->is_grey = false;
360 return( false );
361 }
362 }
363 clut->is_grey = true;
364 return( true );
365 }
366
367 static struct { char *name; long value; } predefn[] = {
368 { "red", 0xff0000 },
369 { "green", 0x008000 },
370 { "blue", 0x0000ff },
371 { "cyan", 0x00ffff },
372 { "magenta", 0xff00ff },
373 { "yellow", 0xffff00 },
374 { "black", 0x000000 },
375 { "gray", 0x808080 },
376 { "grey", 0x808080 },
377 { "white", 0xffffff },
378 { "maroon", 0x800000 },
379 { "olive", 0x808000 },
380 { "navy", 0x000080 },
381 { "purple", 0x800080 },
382 { "lime", 0x00ff00 },
383 { "aqua", 0x00ffff },
384 { "teal", 0x008080 },
385 { "fuchsia", 0xff0080 },
386 { "silver", 0xcccccc },
387 { NULL, 0 }
388 };
389
390 Color _GImage_ColourFName(char *name) {
391 int i;
392 int r,g,b,a;
393 double dr,dg,db,da;
394 struct hslrgb hs;
395 Color col;
396
397 for ( i=0; predefn[i].name!=NULL; ++i )
398 if ( strmatch(name,predefn[i].name)==0 )
399 return( predefn[i].value );
400
401 if ( sscanf(name,"%d %d %d", &r, &g, &b )==3 ||
402 sscanf(name,"%x %x %x", (unsigned *) &r, (unsigned *) &g, (unsigned *) &b )==3 ||
403 (strlen(name)==7 && sscanf(name,"#%2x%2x%2x", (unsigned *) &r, (unsigned *) &g, (unsigned *) &b )==3) ) {
404 if ( r>255 ) r=255; else if ( r<0 ) r=0;
405 if ( g>255 ) g=255; else if ( g<0 ) g=0;
406 if ( b>255 ) b=255; else if ( b<0 ) b=0;
407 return( ((long) r<<16) | (g<<8) | b );
408 } else if ( (strlen(name)==9 && sscanf(name,"#%2x%2x%2x%2x",
409 (unsigned *) &a, (unsigned *) &r, (unsigned *) &g, (unsigned *) &b )==4) ) {
410 if ( a>255 ) a=255; else if ( a<0 ) a=0;
411 if ( r>255 ) r=255; else if ( r<0 ) r=0;
412 if ( g>255 ) g=255; else if ( g<0 ) g=0;
413 if ( b>255 ) b=255; else if ( b<0 ) b=0;
414 col = ((long) a<<24) | ((long) r<<16) | (g<<8) | b;
415 if ( (col&0xfffffff0)==0xfffffff0 )
416 col &= 0xffffff; /* I use these colors internally for things like "Undefined" */
417 /* but since I also treat colors with 0 alpha channel as fully */
418 /* opaque, I can just represent this that way */
419 return( col );
420 } else if ( sscanf(name,"rgb(%lg,%lg,%lg)", &dr, &dg, &db)==3 ) {
421 if ( dr>1.0 ) dr=1.0; else if ( dr<0 ) dr= 0;
422 if ( dg>1.0 ) dg=1.0; else if ( dg<0 ) dg= 0;
423 if ( db>1.0 ) db=1.0; else if ( db<0 ) db= 0;
424 r = dr*255 +.5;
425 g = dg*255 +.5;
426 b = db*255 +.5;
427 return( ((long) r<<16) | (g<<8) | b );
428 } else if ( sscanf(name,"argb(%lg,%lg,%lg,%lg)", &da, &dr, &dg, &db)==4 ) {
429 if ( da>1.0 ) da=1.0; else if ( da<0 ) da= 0;
430 if ( dr>1.0 ) dr=1.0; else if ( dr<0 ) dr= 0;
431 if ( dg>1.0 ) dg=1.0; else if ( dg<0 ) dg= 0;
432 if ( db>1.0 ) db=1.0; else if ( db<0 ) db= 0;
433 a = da*255 +.5;
434 r = dr*255 +.5;
435 g = dg*255 +.5;
436 b = db*255 +.5;
437 col = ((long) a<<24) | ((long) r<<16) | (g<<8) | b;
438 if ( (col&0xfffffff0)==0xfffffff0 )
439 col &= 0xffffff; /* I use these colors internally for things like "Undefined" */
440 /* but since I also treat colors with 0 alpha channel as fully */
441 /* opaque, I can just represent this that way */
442 return( col );
443 } else if ( sscanf(name,"hsv(%lg,%lg,%lg)", &hs.h, &hs.s, &hs.v)==3 ) {
444 /* Hue is an angle in degrees. HS?2RGB does appropriate clipping */
445 if ( hs.s>1.0 ) hs.s=1.0; else if ( hs.s<0 ) hs.s= 0;
446 if ( hs.v>1.0 ) hs.v=1.0; else if ( hs.v<0 ) hs.v= 0;
447 gHSV2RGB(&hs);
448 r = hs.r*255 +.5;
449 g = hs.g*255 +.5;
450 b = hs.b*255 +.5;
451 return( ((long) r<<16) | (g<<8) | b );
452 } else if ( sscanf(name,"hsl(%lg,%lg,%lg)", &hs.h, &hs.s, &hs.l)==3 ) {
453 /* Hue is an angle in degrees. HS?2RGB does appropriate clipping */
454 if ( hs.s>1.0 ) hs.s=1.0; else if ( hs.s<0 ) hs.s= 0;
455 if ( hs.l>1.0 ) hs.l=1.0; else if ( hs.l<0 ) hs.l= 0;
456 gHSL2RGB(&hs);
457 r = hs.r*255 +.5;
458 g = hs.g*255 +.5;
459 b = hs.b*255 +.5;
460 return( ((long) r<<16) | (g<<8) | b );
461 } else if ( (strlen(name)==4 && sscanf(name,"#%1x%1x%1x", (unsigned *) &r, (unsigned *) &g, (unsigned *) &b )==3) ) {
462 if ( r>15 ) r=15; else if ( r<0 ) r=0;
463 if ( g>15 ) g=15; else if ( g<0 ) g=0;
464 if ( b>15 ) b=15; else if ( b<0 ) b=0;
465 return( ((long) (r*0x110000)) | (g*0x1100) | (b*0x11) );
466 } else if ( (strlen(name)==17 && sscanf(name,"#%4x%4x%4x", (unsigned *) &r, (unsigned *) &g, (unsigned *) &b )==3) ) {
467 r>>=8; g>>=8; b>>=8;
468 if ( r>255 ) r=255; else if ( r<0 ) r=0;
469 if ( g>255 ) g=255; else if ( g<0 ) g=0;
470 if ( b>255 ) b=255; else if ( b<0 ) b=0;
471 return( ((long) r<<16) | (g<<8) | b );
472 } else if ( sscanf(name,"rgb(%lg%%,%lg%%,%lg%%)", &dr, &dg, &db)==3 ) {
473 if ( dr>100 ) dr=100; else if ( dr<0 ) dr= 0;
474 if ( dg>100 ) dg=100; else if ( dg<0 ) dg= 0;
475 if ( db>100 ) db=100; else if ( db<0 ) db= 0;
476 r = (dr*255+50)/100 +.5;
477 g = (dg*255+50)/100 +.5;
478 b = (db*255+50)/100 +.5;
479 return( ((long) r<<16) | (g<<8) | b );
480 }
481 return( -1 );
482 }
483
484 Color GDrawColorBrighten(Color col, int by) {
485 int r, g, b;
486 if (( r = COLOR_RED(col)+by )>255 ) r=255;
487 if (( g=COLOR_GREEN(col)+by )>255 ) g=255;
488 if (( b=COLOR_BLUE(col)+by )>255 ) b=255;
489 return( COLOR_CREATE(r,g,b));
490 }
491
492 Color GDrawColorDarken(Color col, int by) {
493 int r, g, b;
494 if (( r = COLOR_RED(col)-by )<0 ) r=0;
495 if (( g=COLOR_GREEN(col)-by )<0 ) g=0;
496 if (( b=COLOR_BLUE(col)-by )<0 ) b=0;
497 return( COLOR_CREATE(r,g,b));
498 }