ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/clrtab.c
(Generate patch)

Comparing ray/src/px/clrtab.c (file contents):
Revision 2.1 by greg, Mon Oct 12 12:58:39 1992 UTC vs.
Revision 2.4 by greg, Fri Feb 5 09:45:40 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 31 | Line 31 | static unsigned        histo[NRED][NGRN][NBLU];
31   static int      CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU};
32                                  /* maximum propagated error during dithering */
33   #define MAXERR          20
34 +                                /* define CLOSEST to get closest colors */
35 + #ifndef CLOSEST
36 + #ifdef SPEED
37 + #if  SPEED > 8
38 + #define CLOSEST         1       /* this step takes a little longer */
39 + #endif
40 + #endif
41 + #endif
42  
43  
44   new_histo()             /* clear our histogram */
# Line 66 | Line 74 | int    ncolors;
74                  ncolors = 256;
75                                  /* partition color space */
76          cut(CLRCUBE, 0, ncolors);
77 + #ifdef CLOSEST
78 +        closest(ncolors);       /* ensure colors picked are closest */
79 + #endif
80                                  /* return new color table size */
81          return(ncolors);
82   }
# Line 253 | Line 264 | register int   box[3][2];
264                  clrtab[p][BLU] = (box[BLU][0]+box[BLU][1])*256/NBLU/2;
265          }
266   }
267 +
268 +
269 + #ifdef CLOSEST
270 + #define NBSIZ           32
271 + static
272 + closest(n)                      /* make sure we have the closest colors */
273 + int     n;
274 + {
275 +        BYTE    *neigh[256];
276 +        register int    r, g, b;
277 + #define i r
278 +                                        /* get space for neighbor lists */
279 +        for (i = 0; i < n; i++) {
280 +                if ((neigh[i] = (BYTE *)malloc(NBSIZ)) == NULL) {
281 +                        while (i--)
282 +                                free(neigh[i]);
283 +                        return;                 /* ENOMEM -- abandon effort */
284 +                }
285 +                neigh[i][0] = i;                /* identity is terminator */
286 +        }
287 +                                        /* make neighbor lists */
288 +        for (r = 0; r < NRED; r++)
289 +            for (g = 0; g < NGRN; g++)
290 +                for (b = 0; b < NBLU; b++) {
291 +                    if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b])
292 +                        addneigh(neigh, histo[r][g][b], histo[r+1][g][b]);
293 +                    if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b])
294 +                        addneigh(neigh, histo[r][g][b], histo[r][g+1][b]);
295 +                    if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1])
296 +                        addneigh(neigh, histo[r][g][b], histo[r][g][b+1]);
297 +                }
298 +                                        /* assign closest values */
299 +        for (r = 0; r < NRED; r++)
300 +            for (g = 0; g < NGRN; g++)
301 +                for (b = 0; b < NBLU; b++)
302 +                    setclosest(neigh, r, g, b);
303 +                                        /* free neighbor lists */
304 +        for (i = 0; i < n; i++)
305 +                free(neigh[i]);
306 + #undef i
307 + }
308 +
309 +
310 + static
311 + addneigh(nl, i, j)              /* i and j are neighbors; add them to list */
312 + register BYTE   *nl[];
313 + register int    i;
314 + int     j;
315 + {
316 +        int     nc;
317 +        char    *nnl;
318 +        register int    t;
319 +        
320 +        for (nc = 0; nc < 2; nc++) {            /* do both neighbors */
321 +                for (t = 0; nl[i][t] != i; t++)
322 +                        if (nl[i][t] == j)
323 +                                break;          /* in list already */
324 +                if (nl[i][t] == i) {            /* add to list */
325 +                        nl[i][t++] = j;
326 +                        if (t % NBSIZ == 0) {   /* enlarge list */
327 +                                if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL)
328 +                                        t--;
329 +                                else
330 +                                        nl[i] = (BYTE *)nnl;
331 +                        }
332 +                        nl[i][t] = i;           /* terminator */
333 +                }
334 +                t = i; i = j; j = t;            /* swap and do it again */
335 +        }
336 + }
337 +
338 +
339 + static unsigned
340 + dist(col, r, g, b)              /* find distance from clrtab entry to r,g,b */
341 + register BYTE   col[3];
342 + int     r, g, b;
343 + {
344 +        register int    tmp;
345 +        register unsigned       sum;
346 +        
347 +        tmp = col[RED]*NRED/256 - r;
348 +        sum = tmp*tmp;
349 +        tmp = col[GRN]*NGRN/256 - g;
350 +        sum += tmp*tmp;
351 +        tmp = col[BLU]*NBLU/256 - b;
352 +        sum += tmp*tmp;
353 +        return(sum);
354 + }
355 +
356 +
357 + static
358 + setclosest(nl, r, g, b)         /* find index closest to color and assign */
359 + BYTE    *nl[];
360 + int     r, g, b;
361 + {
362 +        int     ident;
363 +        unsigned        min;
364 +        register unsigned       d;
365 +        register BYTE   *p;
366 +                                        /* get starting value */
367 +        min = dist(clrtab[ident=histo[r][g][b]], r, g, b);
368 +                                        /* find minimum */
369 +        for (p = nl[ident]; *p != ident; p++)
370 +                if ((d = dist(clrtab[*p], r, g, b)) < min) {
371 +                        min = d;
372 +                        histo[r][g][b] = *p;
373 +                }
374 + }
375 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines