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.8 by greg, Thu Dec 9 11:57:15 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 96 | Line 107 | register BYTE  *bs;
107   register COLR   *cs;
108   int     n;
109   {
110 <        static short    (*cerr)[3];
110 >        static short    (*cerr)[3] = NULL;
111          static int      N = 0;
112          int     err[3], errp[3];
113          register int    x, i;
114  
115          if (n != N) {           /* get error propogation array */
116 <                if (N)
117 <                        cerr = (short (*)[3])realloc((char *)cerr,
118 <                                        3*n*sizeof(short));
119 <                else
116 >                if (N) {
117 >                        free((char *)cerr);
118 >                        cerr = NULL;
119 >                }
120 >                if (n)
121                          cerr = (short (*)[3])malloc(3*n*sizeof(short));
122                  if (cerr == NULL) {
123                          N = 0;
# Line 170 | Line 182 | register int   box[3][2];
182   #define c0      r
183          register int    r, g, b;
184          int     pri;
185 <        int     t[HMAX], med;
185 >        long    t[HMAX], med;
186                                          /* find dominant axis */
187          pri = RED;
188          if (box[GRN][1]-box[GRN][0] > box[pri][1]-box[pri][0])
# Line 226 | Line 238 | mktabent(p, box)       /* compute average color for box and
238   int     p;
239   register int    box[3][2];
240   {
241 <        long    sum[3];
242 <        int     r, g, n;
243 <        register int    b, c;
241 >        unsigned long   sum[3];
242 >        unsigned        r, g;
243 >        unsigned long   n;
244 >        register unsigned       b, c;
245                                                  /* sum pixels in box */
246          n = 0;
247          sum[RED] = sum[GRN] = sum[BLU] = 0;
# Line 243 | Line 256 | register int   box[3][2];
256                      }
257                      histo[r][g][b] = p;         /* assign pixel */
258                  }
259 +        if (n >= (1L<<23)/HMAX) {               /* avoid overflow */
260 +                sum[RED] /= n;
261 +                sum[GRN] /= n;
262 +                sum[BLU] /= n;
263 +                n = 1;
264 +        }
265          if (n) {                                /* compute average */
266                  clrtab[p][RED] = sum[RED]*256/NRED/n;
267                  clrtab[p][GRN] = sum[GRN]*256/NGRN/n;
# Line 253 | Line 272 | register int   box[3][2];
272                  clrtab[p][BLU] = (box[BLU][0]+box[BLU][1])*256/NBLU/2;
273          }
274   }
275 +
276 +
277 + #ifdef CLOSEST
278 + #define NBSIZ           32
279 + static
280 + closest(n)                      /* make sure we have the closest colors */
281 + int     n;
282 + {
283 +        BYTE    *neigh[256];
284 +        register int    r, g, b;
285 + #define i r
286 +                                        /* get space for neighbor lists */
287 +        for (i = 0; i < n; i++) {
288 +                if ((neigh[i] = (BYTE *)malloc(NBSIZ)) == NULL) {
289 +                        while (i--)
290 +                                free(neigh[i]);
291 +                        return;                 /* ENOMEM -- abandon effort */
292 +                }
293 +                neigh[i][0] = i;                /* identity is terminator */
294 +        }
295 +                                        /* make neighbor lists */
296 +        for (r = 0; r < NRED; r++)
297 +            for (g = 0; g < NGRN; g++)
298 +                for (b = 0; b < NBLU; b++) {
299 +                    if (r < NRED-1 && histo[r][g][b] != histo[r+1][g][b])
300 +                        addneigh(neigh, histo[r][g][b], histo[r+1][g][b]);
301 +                    if (g < NGRN-1 && histo[r][g][b] != histo[r][g+1][b])
302 +                        addneigh(neigh, histo[r][g][b], histo[r][g+1][b]);
303 +                    if (b < NBLU-1 && histo[r][g][b] != histo[r][g][b+1])
304 +                        addneigh(neigh, histo[r][g][b], histo[r][g][b+1]);
305 +                }
306 +                                        /* assign closest values */
307 +        for (r = 0; r < NRED; r++)
308 +            for (g = 0; g < NGRN; g++)
309 +                for (b = 0; b < NBLU; b++)
310 +                    setclosest(neigh, r, g, b);
311 +                                        /* free neighbor lists */
312 +        for (i = 0; i < n; i++)
313 +                free(neigh[i]);
314 + #undef i
315 + }
316 +
317 +
318 + static
319 + addneigh(nl, i, j)              /* i and j are neighbors; add them to list */
320 + register BYTE   *nl[];
321 + register int    i;
322 + int     j;
323 + {
324 +        int     nc;
325 +        char    *nnl;
326 +        register int    t;
327 +        
328 +        for (nc = 0; nc < 2; nc++) {            /* do both neighbors */
329 +                for (t = 0; nl[i][t] != i; t++)
330 +                        if (nl[i][t] == j)
331 +                                break;          /* in list already */
332 +                if (nl[i][t] == i) {            /* add to list */
333 +                        nl[i][t++] = j;
334 +                        if (t % NBSIZ == 0) {   /* enlarge list */
335 +                                if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL)
336 +                                        t--;
337 +                                else
338 +                                        nl[i] = (BYTE *)nnl;
339 +                        }
340 +                        nl[i][t] = i;           /* terminator */
341 +                }
342 +                t = i; i = j; j = t;            /* swap and do it again */
343 +        }
344 + }
345 +
346 +
347 + static unsigned
348 + dist(col, r, g, b)              /* find distance from clrtab entry to r,g,b */
349 + register BYTE   col[3];
350 + int     r, g, b;
351 + {
352 +        register int    tmp;
353 +        register unsigned       sum;
354 +        
355 +        tmp = col[RED]*NRED/256 - r;
356 +        sum = tmp*tmp;
357 +        tmp = col[GRN]*NGRN/256 - g;
358 +        sum += tmp*tmp;
359 +        tmp = col[BLU]*NBLU/256 - b;
360 +        sum += tmp*tmp;
361 +        return(sum);
362 + }
363 +
364 +
365 + static
366 + setclosest(nl, r, g, b)         /* find index closest to color and assign */
367 + BYTE    *nl[];
368 + int     r, g, b;
369 + {
370 +        int     ident;
371 +        unsigned        min;
372 +        register unsigned       d;
373 +        register BYTE   *p;
374 +                                        /* get starting value */
375 +        min = dist(clrtab[ident=histo[r][g][b]], r, g, b);
376 +                                        /* find minimum */
377 +        for (p = nl[ident]; *p != ident; p++)
378 +                if ((d = dist(clrtab[*p], r, g, b)) < min) {
379 +                        min = d;
380 +                        histo[r][g][b] = *p;
381 +                }
382 + }
383 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines