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 |
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 */ |
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 |
|
/*----------------------------------------------------------------------*/ |
29 |
|
|
30 |
|
ciq(dith,nw,synth,cm) |
43 |
|
if (synth) |
44 |
|
n = makecm(nw,&na); /* analyze histogram and synthesize colormap */ |
45 |
|
else { |
46 |
< |
bcopy(cm,color,sizeof color); |
46 |
> |
memcpy((void *)color,(void *)cm,sizeof color); |
47 |
|
n = nw; |
48 |
|
na = 0; |
49 |
|
for (i=0; i<len; i++) if (hist[i]) na++; |
51 |
|
} |
52 |
|
picwritecm(color); |
53 |
|
|
54 |
+ |
initializeclosest(); |
55 |
|
if (dith) |
56 |
|
draw_dith(ocm); |
57 |
|
else { |
60 |
|
draw_nodith(ocm); |
61 |
|
} |
62 |
|
|
63 |
< |
bcopy(color,cm,sizeof color); |
63 |
> |
memcpy((void *)cm,(void *)color,sizeof color); |
64 |
|
/*endclosest();*/ |
65 |
|
} |
66 |
|
|
85 |
|
for (lp=line, x=0; x<xmax; x++, lp++) |
86 |
|
table(map,lp->r,lp->g,lp->b)++; |
87 |
|
} |
88 |
< |
free((char *)line); |
88 |
> |
free((void *)line); |
89 |
|
} |
90 |
|
|
91 |
|
convertmap(in,out) /* convert to shifted color map */ |
119 |
|
oline[x] = table(map,lp->r,lp->g,lp->b); |
120 |
|
picwriteline(y,oline); |
121 |
|
} |
122 |
< |
free((char *)iline); |
123 |
< |
free((char *)oline); |
122 |
> |
free((void *)iline); |
123 |
> |
free((void *)oline); |
124 |
|
} |
125 |
|
|
126 |
|
draw_dith(ocm) /* quantize with dithering */ |
143 |
|
Q[0] = Q[1] = Q[2] = 0; |
144 |
|
picreadline3(y,iline); |
145 |
|
for (p=P, q=Q, x=0; x<xmax; x++, p+=3, q+=3) { |
146 |
< |
rr = ocm[0][iline[x].r]+p[0]; |
147 |
< |
gg = ocm[1][iline[x].g]+p[1]; /* ideal */ |
148 |
< |
bb = ocm[2][iline[x].b]+p[2]; |
146 |
> |
rr = p[0]; |
147 |
> |
if (rr<-ERRMAX) rr = -ERRMAX; else if (rr>ERRMAX) rr = ERRMAX; |
148 |
> |
gg = p[1]; |
149 |
> |
if (gg<-ERRMAX) gg = -ERRMAX; else if (gg>ERRMAX) gg = ERRMAX; |
150 |
> |
bb = p[2]; |
151 |
> |
if (bb<-ERRMAX) bb = -ERRMAX; else if (bb>ERRMAX) bb = ERRMAX; |
152 |
> |
/* now rr,gg,bb is propagated color */ |
153 |
> |
rr += ocm[0][iline[x].r]; |
154 |
> |
gg += ocm[1][iline[x].g]; /* ideal */ |
155 |
> |
bb += ocm[2][iline[x].b]; |
156 |
|
if (rr<0) rr = 0; else if (rr>255) rr = 255; |
157 |
|
if (gg<0) gg = 0; else if (gg>255) gg = 255; |
158 |
|
if (bb<0) bb = 0; else if (bb>255) bb = 255; |
169 |
|
} |
170 |
|
picwriteline(y,oline); |
171 |
|
} |
172 |
< |
free((char *)iline); |
173 |
< |
free((char *)oline); |
174 |
< |
free((char *)buf); |
172 |
> |
free((void *)iline); |
173 |
> |
free((void *)oline); |
174 |
> |
free((void *)buf); |
175 |
|
} |