1 |
– |
/* Copyright (c) 1993 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 |
|
* Simple median-cut color quantization based on colortab.c |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "standard.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include <string.h> |
11 |
+ |
|
12 |
+ |
#include "standard.h" |
13 |
|
#include "color.h" |
14 |
+ |
#include "clrtab.h" |
15 |
+ |
|
16 |
|
/* histogram resolution */ |
17 |
|
#define NRED 36 |
18 |
|
#define NGRN 48 |
25 |
|
#define part(cn) ((cn)>>2) |
26 |
|
#define prim(cn) ((cn)&3) |
27 |
|
/* our color table (global) */ |
28 |
< |
BYTE clrtab[256][3]; |
28 |
> |
extern BYTE clrtab[256][3]; |
29 |
|
/* histogram of colors / color assignments */ |
30 |
|
static unsigned histo[NRED][NGRN][NBLU]; |
31 |
|
#define cndx(c) histo[((c)[RED]*NRED)>>8][((c)[GRN]*NGRN)>>8][((c)[BLU]*NBLU)>>8] |
32 |
|
/* initial color cube boundary */ |
33 |
< |
static int CLRCUBE[3][2] = {0,NRED,0,NGRN,0,NBLU}; |
33 |
> |
static int CLRCUBE[3][2] = {{0,NRED},{0,NGRN},{0,NBLU}}; |
34 |
|
/* maximum propagated error during dithering */ |
35 |
|
#define MAXERR 20 |
36 |
|
/* define CLOSEST to get closest colors */ |
43 |
|
#endif |
44 |
|
|
45 |
|
|
46 |
< |
new_histo() /* clear our histogram */ |
46 |
> |
#ifdef CLOSEST |
47 |
> |
static void closest(int n); |
48 |
> |
static void setclosest(BYTE *nl[], int r, int g, int b); |
49 |
> |
static void addneigh(BYTE *nl[], int i, int j); |
50 |
> |
static unsigned int dist(BYTE col[3], int r, int g, int b); |
51 |
> |
#endif |
52 |
> |
static void cut(int box[3][2], int c0, int c1); |
53 |
> |
static int split(int box[3][2]); |
54 |
> |
static void mktabent(int p, int box[3][2]); |
55 |
> |
|
56 |
> |
|
57 |
> |
extern int |
58 |
> |
new_histo( /* clear our histogram */ |
59 |
> |
int n |
60 |
> |
) |
61 |
|
{ |
62 |
< |
bzero((char *)histo, sizeof(histo)); |
62 |
> |
memset((void *)histo, '\0', sizeof(histo)); |
63 |
> |
return(0); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
< |
cnt_pixel(col) /* add pixel to our histogram */ |
68 |
< |
register BYTE col[]; |
67 |
> |
extern void |
68 |
> |
cnt_pixel( /* add pixel to our histogram */ |
69 |
> |
register BYTE col[] |
70 |
> |
) |
71 |
|
{ |
72 |
|
cndx(col)++; |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
< |
cnt_colrs(cs, n) /* add a scanline to our histogram */ |
77 |
< |
register COLR *cs; |
78 |
< |
register int n; |
76 |
> |
extern void |
77 |
> |
cnt_colrs( /* add a scanline to our histogram */ |
78 |
> |
register COLR *cs, |
79 |
> |
register int n |
80 |
> |
) |
81 |
|
{ |
82 |
|
while (n-- > 0) { |
83 |
|
cndx(cs[0])++; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
< |
new_clrtab(ncolors) /* make new color table using ncolors */ |
90 |
< |
int ncolors; |
89 |
> |
extern int |
90 |
> |
new_clrtab( /* make new color table using ncolors */ |
91 |
> |
int ncolors |
92 |
> |
) |
93 |
|
{ |
94 |
|
if (ncolors < 1) |
95 |
|
return(0); |
100 |
|
#ifdef CLOSEST |
101 |
|
closest(ncolors); /* ensure colors picked are closest */ |
102 |
|
#endif |
103 |
+ |
/* reset dithering function */ |
104 |
+ |
dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
105 |
|
/* return new color table size */ |
106 |
|
return(ncolors); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
< |
int |
111 |
< |
map_pixel(col) /* get pixel for color */ |
112 |
< |
register BYTE col[]; |
110 |
> |
extern int |
111 |
> |
map_pixel( /* get pixel for color */ |
112 |
> |
register BYTE col[] |
113 |
> |
) |
114 |
|
{ |
115 |
|
return(cndx(col)); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
< |
map_colrs(bs, cs, n) /* convert a scanline to color index values */ |
120 |
< |
register BYTE *bs; |
121 |
< |
register COLR *cs; |
122 |
< |
register int n; |
119 |
> |
extern void |
120 |
> |
map_colrs( /* convert a scanline to color index values */ |
121 |
> |
register BYTE *bs, |
122 |
> |
register COLR *cs, |
123 |
> |
register int n |
124 |
> |
) |
125 |
|
{ |
126 |
|
while (n-- > 0) { |
127 |
|
*bs++ = cndx(cs[0]); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
< |
dith_colrs(bs, cs, n) /* convert scanline to dithered index values */ |
134 |
< |
register BYTE *bs; |
135 |
< |
register COLR *cs; |
136 |
< |
int n; |
133 |
> |
extern void |
134 |
> |
dith_colrs( /* convert scanline to dithered index values */ |
135 |
> |
register BYTE *bs, |
136 |
> |
register COLR *cs, |
137 |
> |
int n |
138 |
> |
) |
139 |
|
{ |
140 |
< |
static short (*cerr)[3]; |
140 |
> |
static short (*cerr)[3] = NULL; |
141 |
|
static int N = 0; |
142 |
|
int err[3], errp[3]; |
143 |
|
register int x, i; |
144 |
|
|
145 |
|
if (n != N) { /* get error propogation array */ |
146 |
< |
if (N) |
147 |
< |
cerr = (short (*)[3])realloc((char *)cerr, |
148 |
< |
3*n*sizeof(short)); |
149 |
< |
else |
146 |
> |
if (N) { |
147 |
> |
free((void *)cerr); |
148 |
> |
cerr = NULL; |
149 |
> |
} |
150 |
> |
if (n) |
151 |
|
cerr = (short (*)[3])malloc(3*n*sizeof(short)); |
152 |
|
if (cerr == NULL) { |
153 |
|
N = 0; |
155 |
|
return; |
156 |
|
} |
157 |
|
N = n; |
158 |
< |
bzero((char *)cerr, 3*N*sizeof(short)); |
158 |
> |
memset((void *)cerr, '\0', 3*N*sizeof(short)); |
159 |
|
} |
160 |
|
err[0] = err[1] = err[2] = 0; |
161 |
|
for (x = 0; x < n; x++) { |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
< |
static |
184 |
< |
cut(box, c0, c1) /* partition color space */ |
185 |
< |
register int box[3][2]; |
186 |
< |
int c0, c1; |
183 |
> |
static void |
184 |
> |
cut( /* partition color space */ |
185 |
> |
register int box[3][2], |
186 |
> |
int c0, |
187 |
> |
int c1 |
188 |
> |
) |
189 |
|
{ |
190 |
|
register int branch; |
191 |
|
int kb[3][2]; |
196 |
|
} |
197 |
|
/* split box */ |
198 |
|
branch = split(box); |
199 |
< |
bcopy((char *)box, (char *)kb, sizeof(kb)); |
199 |
> |
memcpy((void *)kb, (void *)box, sizeof(kb)); |
200 |
|
/* do left (lesser) branch */ |
201 |
|
kb[prim(branch)][1] = part(branch); |
202 |
|
cut(kb, c0, (c0+c1)>>1); |
208 |
|
|
209 |
|
|
210 |
|
static int |
211 |
< |
split(box) /* find median cut for box */ |
212 |
< |
register int box[3][2]; |
211 |
> |
split( /* find median cut for box */ |
212 |
> |
register int box[3][2] |
213 |
> |
) |
214 |
|
{ |
215 |
|
#define c0 r |
216 |
|
register int r, g, b; |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
< |
static |
270 |
< |
mktabent(p, box) /* compute average color for box and assign */ |
271 |
< |
int p; |
272 |
< |
register int box[3][2]; |
269 |
> |
static void |
270 |
> |
mktabent( /* compute average color for box and assign */ |
271 |
> |
int p, |
272 |
> |
register int box[3][2] |
273 |
> |
) |
274 |
|
{ |
275 |
|
unsigned long sum[3]; |
276 |
< |
unsigned r, g, n; |
276 |
> |
unsigned r, g; |
277 |
> |
unsigned long n; |
278 |
|
register unsigned b, c; |
279 |
|
/* sum pixels in box */ |
280 |
|
n = 0; |
282 |
|
for (r = box[RED][0]; r < box[RED][1]; r++) |
283 |
|
for (g = box[GRN][0]; g < box[GRN][1]; g++) |
284 |
|
for (b = box[BLU][0]; b < box[BLU][1]; b++) { |
285 |
< |
if (c = histo[r][g][b]) { |
285 |
> |
if ( (c = histo[r][g][b]) ) { |
286 |
|
n += c; |
287 |
|
sum[RED] += (long)c*r; |
288 |
|
sum[GRN] += (long)c*g; |
290 |
|
} |
291 |
|
histo[r][g][b] = p; /* assign pixel */ |
292 |
|
} |
293 |
< |
if (n >= (1<<23)/HMAX) { /* avoid overflow */ |
293 |
> |
if (n >= (1L<<23)/HMAX) { /* avoid overflow */ |
294 |
|
sum[RED] /= n; |
295 |
|
sum[GRN] /= n; |
296 |
|
sum[BLU] /= n; |
310 |
|
|
311 |
|
#ifdef CLOSEST |
312 |
|
#define NBSIZ 32 |
313 |
< |
static |
314 |
< |
closest(n) /* make sure we have the closest colors */ |
315 |
< |
int n; |
313 |
> |
static void |
314 |
> |
closest( /* make sure we have the closest colors */ |
315 |
> |
int n |
316 |
> |
) |
317 |
|
{ |
318 |
|
BYTE *neigh[256]; |
319 |
|
register int r, g, b; |
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
< |
static |
354 |
< |
addneigh(nl, i, j) /* i and j are neighbors; add them to list */ |
355 |
< |
register BYTE *nl[]; |
356 |
< |
register int i; |
357 |
< |
int j; |
353 |
> |
static void |
354 |
> |
addneigh( /* i and j are neighbors; add them to list */ |
355 |
> |
register BYTE *nl[], |
356 |
> |
register int i, |
357 |
> |
int j |
358 |
> |
) |
359 |
|
{ |
360 |
|
int nc; |
361 |
|
char *nnl; |
368 |
|
if (nl[i][t] == i) { /* add to list */ |
369 |
|
nl[i][t++] = j; |
370 |
|
if (t % NBSIZ == 0) { /* enlarge list */ |
371 |
< |
if ((nnl = realloc(nl[i], t+NBSIZ)) == NULL) |
371 |
> |
if ((nnl = realloc((void *)nl[i], |
372 |
> |
t+NBSIZ)) == NULL) |
373 |
|
t--; |
374 |
|
else |
375 |
|
nl[i] = (BYTE *)nnl; |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
< |
static unsigned |
385 |
< |
dist(col, r, g, b) /* find distance from clrtab entry to r,g,b */ |
386 |
< |
register BYTE col[3]; |
387 |
< |
int r, g, b; |
384 |
> |
static unsigned int |
385 |
> |
dist( /* find distance from clrtab entry to r,g,b */ |
386 |
> |
register BYTE col[3], |
387 |
> |
int r, |
388 |
> |
int g, |
389 |
> |
int b |
390 |
> |
) |
391 |
|
{ |
392 |
|
register int tmp; |
393 |
|
register unsigned sum; |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
< |
static |
406 |
< |
setclosest(nl, r, g, b) /* find index closest to color and assign */ |
407 |
< |
BYTE *nl[]; |
408 |
< |
int r, g, b; |
405 |
> |
static void |
406 |
> |
setclosest( /* find index closest to color and assign */ |
407 |
> |
BYTE *nl[], |
408 |
> |
int r, |
409 |
> |
int g, |
410 |
> |
int b |
411 |
> |
) |
412 |
|
{ |
413 |
|
int ident; |
414 |
|
unsigned min; |