1 |
– |
/* |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
+ |
/* |
5 |
|
CIQ - main program for color image quantization |
6 |
|
options for Floyd-Steinberg dithering by minimization of accumulated error |
7 |
|
or undithered quantization |
10 |
|
Greg Ward 1 March 88, modified for arbitrary picture sizes |
11 |
|
*/ |
12 |
|
|
13 |
+ |
#include "standard.h" |
14 |
|
#include "ciq.h" |
15 |
|
|
16 |
|
#define table(m,r,g,b) hist[m[0][r]|m[1][g]|m[2][b]] /* histogram/pvtable */ |
20 |
|
colormap color; /* quantization colormap */ |
21 |
|
int n; /* number of colors in it */ |
22 |
|
|
23 |
+ |
#define ERRMAX 20 /* maximum allowed propagated error, |
24 |
+ |
if >=255 then no clamping */ |
25 |
+ |
|
26 |
|
/*----------------------------------------------------------------------*/ |
27 |
|
|
28 |
|
ciq(dith,nw,synth,cm) |
41 |
|
if (synth) |
42 |
|
n = makecm(nw,&na); /* analyze histogram and synthesize colormap */ |
43 |
|
else { |
44 |
< |
bcopy(cm,color,sizeof color); |
44 |
> |
bcopy((void *)cm,(void *)color,sizeof color); |
45 |
|
n = nw; |
46 |
|
na = 0; |
47 |
|
for (i=0; i<len; i++) if (hist[i]) na++; |
49 |
|
} |
50 |
|
picwritecm(color); |
51 |
|
|
52 |
+ |
initializeclosest(); |
53 |
|
if (dith) |
54 |
|
draw_dith(ocm); |
55 |
|
else { |
58 |
|
draw_nodith(ocm); |
59 |
|
} |
60 |
|
|
61 |
< |
bcopy(color,cm,sizeof color); |
61 |
> |
bcopy((void *)color,(void *)cm,sizeof color); |
62 |
|
/*endclosest();*/ |
63 |
|
} |
64 |
|
|
72 |
|
rgbpixel *line; |
73 |
|
colormap map; |
74 |
|
|
75 |
+ |
for (x = 0; x < len; x++) /* clear histogram */ |
76 |
+ |
hist[x] = 0; |
77 |
+ |
|
78 |
|
line = line3alloc(xmax); |
79 |
|
convertmap(ocm,map); |
80 |
|
|
83 |
|
for (lp=line, x=0; x<xmax; x++, lp++) |
84 |
|
table(map,lp->r,lp->g,lp->b)++; |
85 |
|
} |
86 |
< |
free((char *)line); |
86 |
> |
free((void *)line); |
87 |
|
} |
88 |
|
|
89 |
|
convertmap(in,out) /* convert to shifted color map */ |
117 |
|
oline[x] = table(map,lp->r,lp->g,lp->b); |
118 |
|
picwriteline(y,oline); |
119 |
|
} |
120 |
< |
free((char *)iline); |
121 |
< |
free((char *)oline); |
120 |
> |
free((void *)iline); |
121 |
> |
free((void *)oline); |
122 |
|
} |
123 |
|
|
124 |
|
draw_dith(ocm) /* quantize with dithering */ |
141 |
|
Q[0] = Q[1] = Q[2] = 0; |
142 |
|
picreadline3(y,iline); |
143 |
|
for (p=P, q=Q, x=0; x<xmax; x++, p+=3, q+=3) { |
144 |
< |
rr = ocm[0][iline[x].r]+p[0]; |
145 |
< |
gg = ocm[1][iline[x].g]+p[1]; /* ideal */ |
146 |
< |
bb = ocm[2][iline[x].b]+p[2]; |
144 |
> |
rr = p[0]; |
145 |
> |
if (rr<-ERRMAX) rr = -ERRMAX; else if (rr>ERRMAX) rr = ERRMAX; |
146 |
> |
gg = p[1]; |
147 |
> |
if (gg<-ERRMAX) gg = -ERRMAX; else if (gg>ERRMAX) gg = ERRMAX; |
148 |
> |
bb = p[2]; |
149 |
> |
if (bb<-ERRMAX) bb = -ERRMAX; else if (bb>ERRMAX) bb = ERRMAX; |
150 |
> |
/* now rr,gg,bb is propagated color */ |
151 |
> |
rr += ocm[0][iline[x].r]; |
152 |
> |
gg += ocm[1][iline[x].g]; /* ideal */ |
153 |
> |
bb += ocm[2][iline[x].b]; |
154 |
|
if (rr<0) rr = 0; else if (rr>255) rr = 255; |
155 |
|
if (gg<0) gg = 0; else if (gg>255) gg = 255; |
156 |
|
if (bb<0) bb = 0; else if (bb>255) bb = 255; |
167 |
|
} |
168 |
|
picwriteline(y,oline); |
169 |
|
} |
170 |
< |
free((char *)iline); |
171 |
< |
free((char *)oline); |
172 |
< |
free((char *)buf); |
170 |
> |
free((void *)iline); |
171 |
> |
free((void *)oline); |
172 |
> |
free((void *)buf); |
173 |
|
} |