1 |
< |
/* |
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 |
13 |
|
Greg Ward 1 March 88, modified for arbitrary picture sizes |
14 |
|
*/ |
15 |
|
|
16 |
+ |
#include "standard.h" |
17 |
|
#include "ciq.h" |
18 |
|
|
19 |
|
#define table(m,r,g,b) hist[m[0][r]|m[1][g]|m[2][b]] /* histogram/pvtable */ |
23 |
|
colormap color; /* quantization colormap */ |
24 |
|
int n; /* number of colors in it */ |
25 |
|
|
26 |
+ |
#define ERRMAX 20 /* maximum allowed propagated error, |
27 |
+ |
if >=255 then no clamping */ |
28 |
+ |
|
29 |
|
/*----------------------------------------------------------------------*/ |
30 |
|
|
31 |
|
ciq(dith,nw,synth,cm) |
44 |
|
if (synth) |
45 |
|
n = makecm(nw,&na); /* analyze histogram and synthesize colormap */ |
46 |
|
else { |
47 |
< |
bcopy(cm,color,sizeof color); |
47 |
> |
bcopy((char *)cm,(char *)color,sizeof color); |
48 |
|
n = nw; |
49 |
|
na = 0; |
50 |
|
for (i=0; i<len; i++) if (hist[i]) na++; |
52 |
|
} |
53 |
|
picwritecm(color); |
54 |
|
|
55 |
+ |
initializeclosest(); |
56 |
|
if (dith) |
57 |
|
draw_dith(ocm); |
58 |
|
else { |
61 |
|
draw_nodith(ocm); |
62 |
|
} |
63 |
|
|
64 |
< |
bcopy(color,cm,sizeof color); |
64 |
> |
bcopy((char *)color,(char *)cm,sizeof color); |
65 |
|
/*endclosest();*/ |
66 |
|
} |
67 |
|
|
75 |
|
rgbpixel *line; |
76 |
|
colormap map; |
77 |
|
|
78 |
+ |
for (x = 0; x < len; x++) /* clear histogram */ |
79 |
+ |
hist[x] = 0; |
80 |
+ |
|
81 |
|
line = line3alloc(xmax); |
82 |
|
convertmap(ocm,map); |
83 |
|
|
144 |
|
Q[0] = Q[1] = Q[2] = 0; |
145 |
|
picreadline3(y,iline); |
146 |
|
for (p=P, q=Q, x=0; x<xmax; x++, p+=3, q+=3) { |
147 |
< |
rr = ocm[0][iline[x].r]+p[0]; |
148 |
< |
gg = ocm[1][iline[x].g]+p[1]; /* ideal */ |
149 |
< |
bb = ocm[2][iline[x].b]+p[2]; |
147 |
> |
rr = p[0]; |
148 |
> |
if (rr<-ERRMAX) rr = -ERRMAX; else if (rr>ERRMAX) rr = ERRMAX; |
149 |
> |
gg = p[1]; |
150 |
> |
if (gg<-ERRMAX) gg = -ERRMAX; else if (gg>ERRMAX) gg = ERRMAX; |
151 |
> |
bb = p[2]; |
152 |
> |
if (bb<-ERRMAX) bb = -ERRMAX; else if (bb>ERRMAX) bb = ERRMAX; |
153 |
> |
/* now rr,gg,bb is propagated color */ |
154 |
> |
rr += ocm[0][iline[x].r]; |
155 |
> |
gg += ocm[1][iline[x].g]; /* ideal */ |
156 |
> |
bb += ocm[2][iline[x].b]; |
157 |
|
if (rr<0) rr = 0; else if (rr>255) rr = 255; |
158 |
|
if (gg<0) gg = 0; else if (gg>255) gg = 255; |
159 |
|
if (bb<0) bb = 0; else if (bb>255) bb = 255; |