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.10 by greg, Fri Jun 10 12:51:16 1994 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 23 | Line 23 | static char SCCSid[] = "$SunId$ LBL";
23   #define part(cn)        ((cn)>>2)
24   #define prim(cn)        ((cn)&3)
25                                  /* our color table (global) */
26 < BYTE    clrtab[256][3];
26 > extern BYTE     clrtab[256][3];
27                                  /* histogram of colors / color assignments */
28   static unsigned histo[NRED][NGRN][NBLU];
29   #define cndx(c)         histo[((c)[RED]*NRED)>>8][((c)[GRN]*NGRN)>>8][((c)[BLU]*NBLU)>>8]
# 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 */
44 > new_histo(n)            /* clear our histogram */
45 > int     n;
46   {
47          bzero((char *)histo, sizeof(histo));
48 +        return(0);
49   }
50  
51  
# Line 66 | Line 76 | int    ncolors;
76                  ncolors = 256;
77                                  /* partition color space */
78          cut(CLRCUBE, 0, ncolors);
79 + #ifdef CLOSEST
80 +        closest(ncolors);       /* ensure colors picked are closest */
81 + #endif
82 +                                /* reset dithering function */
83 +        dith_colrs((BYTE *)NULL, (COLR *)NULL, 0);
84                                  /* return new color table size */
85          return(ncolors);
86   }
# Line 96 | Line 111 | register BYTE  *bs;
111   register COLR   *cs;
112   int     n;
113   {
114 <        static short    (*cerr)[3];
114 >        static short    (*cerr)[3] = NULL;
115          static int      N = 0;
116          int     err[3], errp[3];
117          register int    x, i;
118  
119          if (n != N) {           /* get error propogation array */
120 <                if (N)
121 <                        cerr = (short (*)[3])realloc((char *)cerr,
122 <                                        3*n*sizeof(short));
123 <                else
120 >                if (N) {
121 >                        free((char *)cerr);
122 >                        cerr = NULL;
123 >                }
124 >                if (n)
125                          cerr = (short (*)[3])malloc(3*n*sizeof(short));
126                  if (cerr == NULL) {
127                          N = 0;
# Line 170 | Line 186 | register int   box[3][2];
186   #define c0      r
187          register int    r, g, b;
188          int     pri;
189 <        int     t[HMAX], med;
189 >        long    t[HMAX], med;
190                                          /* find dominant axis */
191          pri = RED;
192          if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0])
# Line 226 | Line 242 | mktabent(p, box)       /* compute average color for box and
242   int     p;
243   register int    box[3][2];
244   {
245 <        long    sum[3];
246 <        int     r, g, n;
247 <        register int    b, c;
245 >        unsigned long   sum[3];
246 >        unsigned        r, g;
247 >        unsigned long   n;
248 >        register unsigned       b, c;
249                                                  /* sum pixels in box */
250          n = 0;
251          sum[RED] = sum[GRN] = sum[BLU] = 0;
# Line 243 | Line 260 | register int   box[3][2];
260                      }
261                      histo[r][g][b] = p;         /* assign pixel */
262                  }
263 +        if (n >= (1L<<23)/HMAX) {               /* avoid overflow */
264 +                sum[RED] /= n;
265 +                sum[GRN] /= n;
266 +                sum[BLU] /= n;
267 +                n = 1;
268 +        }
269          if (n) {                                /* compute average */
270                  clrtab[p][RED] = sum[RED]*256/NRED/n;
271                  clrtab[p][GRN] = sum[GRN]*256/NGRN/n;
# Line 253 | Line 276 | register int   box[3][2];
276                  clrtab[p][BLU] = (box[BLU][0]+box[BLU][1])*256/NBLU/2;
277          }
278   }
279 +
280 +
281 + #ifdef CLOSEST
282 + #define NBSIZ           32
283 + static
284 + closest(n)                      /* make sure we have the closest colors */
285 + int     n;
286 + {
287 +        BYTE    *neigh[256];
288 +        register int    r, g, b;
289 + #define i r
290 +                                        /* get space for neighbor lists */
291 +        for (i = 0; i < n; i++) {
292 +                if ((neigh[i] = (BYTE *)malloc(NBSIZ)) == NULL) {
293 +                        while (i--)
294 +                                free(neigh[i]);
295 +                        return;                 /* ENOMEM -- abandon effort */
296 +                }
297 +                neigh[i][0] = i;                /* identity is terminator */
298 +        }
299 +                                        /* make neighbor lists */
300 +        for (r = 0; r < NRED; r++)
301 +            for (g = 0; g < NGRN; g++)
302 +                for (b = 0; b < NBLU; b++) {
303 +                    if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b])
304 +                        addneigh(neigh, histo[r][g][b], histo[r+1][g][b]);
305 +                    if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b])
306 +                        addneigh(neigh, histo[r][g][b], histo[r][g+1][b]);
307 +                    if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1])
308 +                        addneigh(neigh, histo[r][g][b], histo[r][g][b+1]);
309 +                }
310 +                                        /* assign closest values */
311 +        for (r = 0; r < NRED; r++)
312 +            for (g = 0; g < NGRN; g++)
313 +                for (b = 0; b < NBLU; b++)
314 +                    setclosest(neigh, r, g, b);
315 +                                        /* free neighbor lists */
316 +        for (i = 0; i < n; i++)
317 +                free(neigh[i]);
318 + #undef i
319 + }
320 +
321 +
322 + static
323 + addneigh(nl, i, j)              /* i and j are neighbors; add them to list */
324 + register BYTE   *nl[];
325 + register int    i;
326 + int     j;
327 + {
328 +        int     nc;
329 +        char    *nnl;
330 +        register int    t;
331 +        
332 +        for (nc = 0; nc < 2; nc++) {            /* do both neighbors */
333 +                for (t = 0; nl[i][t] != i; t++)
334 +                        if (nl[i][t] == j)
335 +                                break;          /* in list already */
336 +                if (nl[i][t] == i) {            /* add to list */
337 +                        nl[i][t++] = j;
338 +                        if (t % NBSIZ == 0) {   /* enlarge list */
339 +                                if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL)
340 +                                        t--;
341 +                                else
342 +                                        nl[i] = (BYTE *)nnl;
343 +                        }
344 +                        nl[i][t] = i;           /* terminator */
345 +                }
346 +                t = i; i = j; j = t;            /* swap and do it again */
347 +        }
348 + }
349 +
350 +
351 + static unsigned
352 + dist(col, r, g, b)              /* find distance from clrtab entry to r,g,b */
353 + register BYTE   col[3];
354 + int     r, g, b;
355 + {
356 +        register int    tmp;
357 +        register unsigned       sum;
358 +        
359 +        tmp = col[RED]*NRED/256 - r;
360 +        sum = tmp*tmp;
361 +        tmp = col[GRN]*NGRN/256 - g;
362 +        sum += tmp*tmp;
363 +        tmp = col[BLU]*NBLU/256 - b;
364 +        sum += tmp*tmp;
365 +        return(sum);
366 + }
367 +
368 +
369 + static
370 + setclosest(nl, r, g, b)         /* find index closest to color and assign */
371 + BYTE    *nl[];
372 + int     r, g, b;
373 + {
374 +        int     ident;
375 +        unsigned        min;
376 +        register unsigned       d;
377 +        register BYTE   *p;
378 +                                        /* get starting value */
379 +        min = dist(clrtab[ident=histo[r][g][b]], r, g, b);
380 +                                        /* find minimum */
381 +        for (p = nl[ident]; *p != ident; p++)
382 +                if ((d = dist(clrtab[*p], r, g, b)) < min) {
383 +                        min = d;
384 +                        histo[r][g][b] = *p;
385 +                }
386 + }
387 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines