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

Comparing ray/src/px/ciq.c (file contents):
Revision 1.3 by greg, Fri Apr 7 13:30:09 1989 UTC vs.
Revision 2.5 by schorsch, Sun Mar 28 20:33:13 2004 UTC

# Line 1 | Line 1
1 /* Copyright 1988 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   CIQ - main program for color image quantization
6   options for Floyd-Steinberg dithering by minimization of accumulated error
# Line 13 | Line 10 | Paul Heckbert  16 April 82, cleaned up 8 June 86
10   Greg Ward       1 March 88, modified for arbitrary picture sizes
11   */
12  
13 + #include <string.h>
14 +
15 + #include "standard.h"
16   #include "ciq.h"
17  
18   #define table(m,r,g,b) hist[m[0][r]|m[1][g]|m[2][b]]  /* histogram/pvtable */
# Line 22 | Line 22 | int hist[len];         /* list of frequencies or pixelvalues
22   colormap color;         /* quantization colormap */
23   int n;                  /* number of colors in it */
24  
25 + #define ERRMAX 20       /* maximum allowed propagated error,
26 +                           if >=255 then no clamping */
27 +
28 + static void sample(colormap ocm);
29 + static void convertmap(colormap in, colormap out);
30 + static void draw_nodith(colormap ocm);
31 + static void draw_dith(colormap ocm);
32 +
33 +
34   /*----------------------------------------------------------------------*/
35  
36 < ciq(dith,nw,synth,cm)
37 < int dith;               /* is dithering desired? 0=no, 1=yes */
38 < int nw;                 /* number of colors wanted in output image */
39 < int synth;              /* synthesize colormap? 0=no, 1=yes */
40 < colormap cm;            /* quantization colormap */
41 <                        /* read if synth=0; always written */
36 > void
37 > ciq(
38 >        int dith,               /* is dithering desired? 0=no, 1=yes */
39 >        int nw,                 /* number of colors wanted in output image */
40 >        int synth,              /* synthesize colormap? 0=no, 1=yes */
41 >        colormap cm             /* quantization colormap */
42 > )                       /* read if synth=0; always written */
43   {
44      int na,i;
45      colormap ocm;
# Line 40 | Line 50 | colormap cm;           /* quantization colormap */
50      if (synth)
51          n = makecm(nw,&na);     /* analyze histogram and synthesize colormap */
52      else {
53 <        bcopy(cm,color,sizeof color);
53 >        memcpy((void *)color,(void *)cm,sizeof color);
54          n = nw;
55          na = 0;
56          for (i=0; i<len; i++) if (hist[i]) na++;
# Line 48 | Line 58 | colormap cm;           /* quantization colormap */
58      }
59      picwritecm(color);
60  
61 +    initializeclosest();
62      if (dith)
63          draw_dith(ocm);
64      else {
# Line 56 | Line 67 | colormap cm;           /* quantization colormap */
67          draw_nodith(ocm);
68      }
69  
70 <    bcopy(color,cm,sizeof color);
70 >    memcpy((void *)cm,(void *)color,sizeof color);
71      /*endclosest();*/
72   }
73  
74   /*----------------------------------------------------------------------*/
75  
76 < sample(ocm)             /* make color frequency table (histogram) */
77 < colormap ocm;
76 > static void
77 > sample(         /* make color frequency table (histogram) */
78 >        colormap ocm
79 > )
80   {
81      register int x,y;
82      register rgbpixel *lp;
# Line 81 | Line 94 | colormap ocm;
94          for (lp=line, x=0; x<xmax; x++, lp++)
95              table(map,lp->r,lp->g,lp->b)++;
96      }
97 <    free((char *)line);
97 >    free((void *)line);
98   }
99  
100 < convertmap(in,out)              /* convert to shifted color map */
101 < register colormap in,out;
100 > static void
101 > convertmap(             /* convert to shifted color map */
102 >        register colormap in,
103 >        register colormap out
104 > )
105   {
106      register int j;
107  
# Line 96 | Line 112 | register colormap in,out;
112      }
113   }
114  
115 < draw_nodith(ocm)        /* quantize without dithering */
116 < colormap ocm;                   /* colormap for orig */
115 > static void
116 > draw_nodith(    /* quantize without dithering */
117 >        colormap ocm                    /* colormap for orig */
118 > )
119   {
120      register int x,y;
121      register rgbpixel *lp;
# Line 115 | Line 133 | colormap ocm;                  /* colormap for orig */
133              oline[x] = table(map,lp->r,lp->g,lp->b);
134          picwriteline(y,oline);
135      }
136 <    free((char *)iline);
137 <    free((char *)oline);
136 >    free((void *)iline);
137 >    free((void *)oline);
138   }
139  
140 < draw_dith(ocm)                  /* quantize with dithering */
141 < colormap ocm;                   /* colormap for orig */
140 > static void
141 > draw_dith(                      /* quantize with dithering */
142 >        colormap ocm                    /* colormap for orig */
143 > )
144   {
145      register int *p,*q,rr,gg,bb,v,x,y,*P,*Q,*R;
146      int *buf;
# Line 139 | Line 159 | colormap ocm;                  /* colormap for orig */
159          Q[0] = Q[1] = Q[2] = 0;
160          picreadline3(y,iline);
161          for (p=P, q=Q, x=0; x<xmax; x++, p+=3, q+=3) {
162 <            rr = ocm[0][iline[x].r]+p[0];
163 <            gg = ocm[1][iline[x].g]+p[1];       /* ideal */
164 <            bb = ocm[2][iline[x].b]+p[2];
162 >            rr = p[0];
163 >            if (rr<-ERRMAX) rr = -ERRMAX; else if (rr>ERRMAX) rr = ERRMAX;
164 >            gg = p[1];
165 >            if (gg<-ERRMAX) gg = -ERRMAX; else if (gg>ERRMAX) gg = ERRMAX;
166 >            bb = p[2];
167 >            if (bb<-ERRMAX) bb = -ERRMAX; else if (bb>ERRMAX) bb = ERRMAX;
168 >            /* now rr,gg,bb is propagated color */
169 >            rr += ocm[0][iline[x].r];
170 >            gg += ocm[1][iline[x].g];   /* ideal */
171 >            bb += ocm[2][iline[x].b];
172              if (rr<0) rr = 0; else if (rr>255) rr = 255;
173              if (gg<0) gg = 0; else if (gg>255) gg = 255;
174              if (bb<0) bb = 0; else if (bb>255) bb = 255;
# Line 158 | Line 185 | colormap ocm;                  /* colormap for orig */
185          }
186          picwriteline(y,oline);
187      }
188 <    free((char *)iline);
189 <    free((char *)oline);
190 <    free((char *)buf);
188 >    free((void *)iline);
189 >    free((void *)oline);
190 >    free((void *)buf);
191   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines