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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines