ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ciq.c
Revision: 1.2
Committed: Thu Feb 2 14:10:33 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -1 lines
Log Message:
Fixed SCCSid

File Contents

# Content
1 /* Copyright 1988 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 CIQ - main program for color image quantization
9 options for Floyd-Steinberg dithering by minimization of accumulated error
10 or undithered quantization
11
12 Paul Heckbert 16 April 82, cleaned up 8 June 86
13 Greg Ward 1 March 88, modified for arbitrary picture sizes
14 */
15
16 #include "ciq.h"
17
18 #define table(m,r,g,b) hist[m[0][r]|m[1][g]|m[2][b]] /* histogram/pvtable */
19
20 int hist[len]; /* list of frequencies or pixelvalues for coded color */
21
22 colormap color; /* quantization colormap */
23 int n; /* number of colors in it */
24
25 /*----------------------------------------------------------------------*/
26
27 ciq(dith,nw,synth,cm)
28 int dith; /* is dithering desired? 0=no, 1=yes */
29 int nw; /* number of colors wanted in output image */
30 int synth; /* synthesize colormap? 0=no, 1=yes */
31 colormap cm; /* quantization colormap */
32 /* read if synth=0; always written */
33 {
34 int na,i;
35 colormap ocm;
36
37 picreadcm(ocm); /* read original picture's colormap (usually identity)*/
38 sample(ocm); /* find histogram */
39
40 if (synth)
41 n = makecm(nw,&na); /* analyze histogram and synthesize colormap */
42 else {
43 bcopy(cm,color,sizeof color);
44 n = nw;
45 na = 0;
46 for (i=0; i<len; i++) if (hist[i]) na++;
47 /*printf("from %d to %d colors\n",na,n);*/
48 }
49 picwritecm(color);
50
51 if (dith)
52 draw_dith(ocm);
53 else {
54 for (i=0; i<len; i++)
55 if (hist[i]) hist[i] = closest(red(i),gre(i),blu(i));
56 draw_nodith(ocm);
57 }
58
59 bcopy(color,cm,sizeof color);
60 /*endclosest();*/
61 }
62
63 /*----------------------------------------------------------------------*/
64
65 sample(ocm) /* make color frequency table (histogram) */
66 colormap ocm;
67 {
68 register int x,y;
69 register rgbpixel *lp;
70 rgbpixel *line;
71 colormap map;
72
73 line = line3alloc(xmax);
74 convertmap(ocm,map);
75
76 for (y=0; y<ymax; y++) {
77 picreadline3(y,line);
78 for (lp=line, x=0; x<xmax; x++, lp++)
79 table(map,lp->r,lp->g,lp->b)++;
80 }
81 free((char *)line);
82 }
83
84 convertmap(in,out) /* convert to shifted color map */
85 register colormap in,out;
86 {
87 register int j;
88
89 for (j=0; j<256; j++) {
90 out[0][j] = (in[0][j]&0xf8)<<7;
91 out[1][j] = (in[1][j]&0xf8)<<2;
92 out[2][j] = (in[2][j]&0xf8)>>3;
93 }
94 }
95
96 draw_nodith(ocm) /* quantize without dithering */
97 colormap ocm; /* colormap for orig */
98 {
99 register int x,y;
100 register rgbpixel *lp;
101 rgbpixel *iline;
102 pixel *oline;
103 colormap map;
104
105 iline = line3alloc(xmax);
106 oline = linealloc(xmax);
107 convertmap(ocm,map);
108
109 for (y=0; y<ymax; y++) {
110 picreadline3(y,iline);
111 for (lp=iline, x=0; x<xmax; x++, lp++)
112 oline[x] = table(map,lp->r,lp->g,lp->b);
113 picwriteline(y,oline);
114 }
115 free((char *)iline);
116 free((char *)oline);
117 }
118
119 draw_dith(ocm) /* quantize with dithering */
120 colormap ocm; /* colormap for orig */
121 {
122 register int *p,*q,rr,gg,bb,v,x,y,*P,*Q,*R;
123 int *buf;
124 rgbpixel *iline; /* input scanline */
125 pixel *oline; /* output scanline */
126
127 buf = (int *)ecalloc(2,3*sizeof(int)*(xmax+1));
128 iline = line3alloc(xmax);
129 oline = linealloc(xmax);
130 /* reuse hist array as quantization table */
131 for (v=0; v<len; v++) hist[v] = -1;
132
133 P = &buf[0];
134 Q = &buf[3*(xmax+1)];
135 for (y=0; y<ymax; y++, R=P, P=Q, Q=R) {
136 Q[0] = Q[1] = Q[2] = 0;
137 picreadline3(y,iline);
138 for (p=P, q=Q, x=0; x<xmax; x++, p+=3, q+=3) {
139 rr = ocm[0][iline[x].r]+p[0];
140 gg = ocm[1][iline[x].g]+p[1]; /* ideal */
141 bb = ocm[2][iline[x].b]+p[2];
142 if (rr<0) rr = 0; else if (rr>255) rr = 255;
143 if (gg<0) gg = 0; else if (gg>255) gg = 255;
144 if (bb<0) bb = 0; else if (bb>255) bb = 255;
145 v = (rr&0xf8)<<7 | (gg&0xf8)<<2 | (bb&0xf8)>>3;
146 if (hist[v]<0)
147 hist[v] = closest(rr,gg,bb); /* nearest to ideal */
148 oline[x] = v = hist[v];
149 rr -= color[0][v];
150 gg -= color[1][v]; /* error */
151 bb -= color[2][v];
152 v = rr*3>>3; p[3] += v; q[0] += v; q[3] = rr-(v<<1);
153 v = gg*3>>3; p[4] += v; q[1] += v; q[4] = gg-(v<<1);
154 v = bb*3>>3; p[5] += v; q[2] += v; q[5] = bb-(v<<1);
155 }
156 picwriteline(y,oline);
157 }
158 free((char *)iline);
159 free((char *)oline);
160 free((char *)buf);
161 }