1 |
– |
/* Copyright (c) 1994 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 |
|
* Neural-Net quantization algorithm based on work of Anthony Dekker |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "standard.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
< |
#include "color.h" |
10 |
> |
#include <string.h> |
11 |
|
|
12 |
+ |
#include "standard.h" |
13 |
+ |
#include "color.h" |
14 |
|
#include "random.h" |
15 |
+ |
#include "clrtab.h" |
16 |
|
|
17 |
|
#ifdef COMPAT_MODE |
18 |
|
#define neu_init new_histo |
24 |
|
#define neu_dith_colrs dith_colrs |
25 |
|
#endif |
26 |
|
/* our color table (global) */ |
27 |
< |
extern BYTE clrtab[256][3]; |
27 |
> |
extern uby8 clrtab[256][3]; |
28 |
|
static int clrtabsiz; |
29 |
|
|
30 |
|
#ifndef DEFSMPFAC |
31 |
< |
#ifdef SPEED |
32 |
< |
#define DEFSMPFAC (240/SPEED+3) |
33 |
< |
#else |
34 |
< |
#define DEFSMPFAC 30 |
31 |
> |
#define DEFSMPFAC 3 |
32 |
|
#endif |
36 |
– |
#endif |
33 |
|
|
34 |
|
int samplefac = DEFSMPFAC; /* sampling factor */ |
35 |
|
|
36 |
|
/* Samples array starts off holding spacing between adjacent |
37 |
|
* samples, and ends up holding actual BGR sample values. |
38 |
|
*/ |
39 |
< |
static BYTE *thesamples; |
39 |
> |
static uby8 *thesamples; |
40 |
|
static int nsamples; |
41 |
< |
static BYTE *cursamp; |
41 |
> |
static uby8 *cursamp; |
42 |
|
static long skipcount; |
43 |
|
|
44 |
|
#define MAXSKIP (1<<24-1) |
47 |
|
|
48 |
|
#define setskip(sp,n) ((sp)[0]=(n)>>16,(sp)[1]=((n)>>8)&255,(sp)[2]=(n)&255) |
49 |
|
|
50 |
+ |
static void initnet(void); |
51 |
+ |
static void inxbuild(void); |
52 |
+ |
static int inxsearch(int b, int g, int r); |
53 |
+ |
static int contest(int b, int g, int r); |
54 |
+ |
static void altersingle(int alpha, int i, int b, int g, int r); |
55 |
+ |
static void alterneigh(int rad, int i, int b, int g, int r); |
56 |
+ |
static void learn(void); |
57 |
+ |
static void unbiasnet(void); |
58 |
+ |
static void cpyclrtab(void); |
59 |
|
|
60 |
< |
neu_init(npixels) /* initialize our sample array */ |
61 |
< |
long npixels; |
60 |
> |
|
61 |
> |
extern int |
62 |
> |
neu_init( /* initialize our sample array */ |
63 |
> |
long npixels |
64 |
> |
) |
65 |
|
{ |
66 |
|
register int nsleft; |
67 |
|
register long sv; |
71 |
|
nsamples = npixels/samplefac; |
72 |
|
if (nsamples < 600) |
73 |
|
return(-1); |
74 |
< |
thesamples = (BYTE *)malloc(nsamples*3); |
74 |
> |
thesamples = (uby8 *)malloc(nsamples*3); |
75 |
|
if (thesamples == NULL) |
76 |
|
return(-1); |
77 |
|
cursamp = thesamples; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
< |
neu_pixel(col) /* add pixel to our samples */ |
102 |
< |
register BYTE col[]; |
101 |
> |
extern void |
102 |
> |
neu_pixel( /* add pixel to our samples */ |
103 |
> |
register uby8 col[] |
104 |
> |
) |
105 |
|
{ |
106 |
|
if (!skipcount--) { |
107 |
|
skipcount = nskip(cursamp); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
< |
neu_colrs(cs, n) /* add a scanline to our samples */ |
117 |
< |
register COLR *cs; |
118 |
< |
register int n; |
116 |
> |
extern void |
117 |
> |
neu_colrs( /* add a scanline to our samples */ |
118 |
> |
register COLR *cs, |
119 |
> |
register int n |
120 |
> |
) |
121 |
|
{ |
122 |
|
while (n > skipcount) { |
123 |
|
cs += skipcount; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
< |
neu_clrtab(ncolors) /* make new color table using ncolors */ |
137 |
< |
int ncolors; |
136 |
> |
extern int |
137 |
> |
neu_clrtab( /* make new color table using ncolors */ |
138 |
> |
int ncolors |
139 |
> |
) |
140 |
|
{ |
141 |
|
clrtabsiz = ncolors; |
142 |
|
if (clrtabsiz > 256) clrtabsiz = 256; |
146 |
|
cpyclrtab(); |
147 |
|
inxbuild(); |
148 |
|
/* we're done with our samples */ |
149 |
< |
free((char *)thesamples); |
149 |
> |
free((void *)thesamples); |
150 |
|
/* reset dithering function */ |
151 |
< |
neu_dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
151 |
> |
neu_dith_colrs((uby8 *)NULL, (COLR *)NULL, 0); |
152 |
|
/* return new color table size */ |
153 |
|
return(clrtabsiz); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
< |
int |
158 |
< |
neu_map_pixel(col) /* get pixel for color */ |
159 |
< |
register BYTE col[]; |
157 |
> |
extern int |
158 |
> |
neu_map_pixel( /* get pixel for color */ |
159 |
> |
register uby8 col[] |
160 |
> |
) |
161 |
|
{ |
162 |
|
return(inxsearch(col[BLU],col[GRN],col[RED])); |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
< |
neu_map_colrs(bs, cs, n) /* convert a scanline to color index values */ |
167 |
< |
register BYTE *bs; |
168 |
< |
register COLR *cs; |
169 |
< |
register int n; |
166 |
> |
extern void |
167 |
> |
neu_map_colrs( /* convert a scanline to color index values */ |
168 |
> |
register uby8 *bs, |
169 |
> |
register COLR *cs, |
170 |
> |
register int n |
171 |
> |
) |
172 |
|
{ |
173 |
|
while (n-- > 0) { |
174 |
|
*bs++ = inxsearch(cs[0][BLU],cs[0][GRN],cs[0][RED]); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
< |
neu_dith_colrs(bs, cs, n) /* convert scanline to dithered index values */ |
181 |
< |
register BYTE *bs; |
182 |
< |
register COLR *cs; |
183 |
< |
int n; |
180 |
> |
extern void |
181 |
> |
neu_dith_colrs( /* convert scanline to dithered index values */ |
182 |
> |
register uby8 *bs, |
183 |
> |
register COLR *cs, |
184 |
> |
int n |
185 |
> |
) |
186 |
|
{ |
187 |
|
static short (*cerr)[3] = NULL; |
188 |
|
static int N = 0; |
191 |
|
|
192 |
|
if (n != N) { /* get error propogation array */ |
193 |
|
if (N) { |
194 |
< |
free((char *)cerr); |
194 |
> |
free((void *)cerr); |
195 |
|
cerr = NULL; |
196 |
|
} |
197 |
|
if (n) |
202 |
|
return; |
203 |
|
} |
204 |
|
N = n; |
205 |
< |
bzero((char *)cerr, 3*N*sizeof(short)); |
205 |
> |
memset((char *)cerr, '\0', 3*N*sizeof(short)); |
206 |
|
} |
207 |
|
err[0] = err[1] = err[2] = 0; |
208 |
|
for (x = 0; x < n; x++) { |
233 |
|
#define lengthcount (nsamples*3) |
234 |
|
#define samplefac 1 |
235 |
|
|
236 |
< |
/*----------------------------------------------------------------------*/ |
237 |
< |
/* */ |
238 |
< |
/* NeuQuant */ |
239 |
< |
/* -------- */ |
240 |
< |
/* */ |
241 |
< |
/* Copyright: Anthony Dekker, November 1994 */ |
242 |
< |
/* */ |
243 |
< |
/* This program performs colour quantization of graphics images (SUN */ |
244 |
< |
/* raster files). It uses a Kohonen Neural Network. It produces */ |
245 |
< |
/* better results than existing methods and runs faster, using minimal */ |
246 |
< |
/* space (8kB plus the image itself). The algorithm is described in */ |
247 |
< |
/* the paper "Kohonen Neural Networks for Optimal Colour Quantization" */ |
248 |
< |
/* to appear in the journal "Network: Computation in Neural Systems". */ |
249 |
< |
/* It is a significant improvement of an earlier algorithm. */ |
250 |
< |
/* */ |
251 |
< |
/* This program is distributed free for academic use or for evaluation */ |
252 |
< |
/* by commercial organizations. */ |
253 |
< |
/* */ |
254 |
< |
/* Usage: NeuQuant -n inputfile > outputfile */ |
255 |
< |
/* */ |
237 |
< |
/* where n is a sampling factor for neural learning. */ |
238 |
< |
/* */ |
239 |
< |
/* Program performance compared with other methods is as follows: */ |
240 |
< |
/* */ |
241 |
< |
/* Algorithm | Av. CPU Time | Quantization Error */ |
242 |
< |
/* ------------------------------------------------------------- */ |
243 |
< |
/* NeuQuant -3 | 314 | 5.55 */ |
244 |
< |
/* NeuQuant -10 | 119 | 5.97 */ |
245 |
< |
/* NeuQuant -30 | 65 | 6.53 */ |
246 |
< |
/* Oct-Trees | 141 | 8.96 */ |
247 |
< |
/* Median Cut (XV -best) | 420 | 9.28 */ |
248 |
< |
/* Median Cut (XV -slow) | 72 | 12.15 */ |
249 |
< |
/* */ |
250 |
< |
/* Author's address: Dept of ISCS, National University of Singapore */ |
251 |
< |
/* Kent Ridge, Singapore 0511 */ |
252 |
< |
/* Email: [email protected] */ |
253 |
< |
/*----------------------------------------------------------------------*/ |
236 |
> |
/* NeuQuant Neural-Net Quantization Algorithm Interface |
237 |
> |
* ---------------------------------------------------- |
238 |
> |
* |
239 |
> |
* Copyright (c) 1994 Anthony Dekker |
240 |
> |
* |
241 |
> |
* NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. |
242 |
> |
* See "Kohonen neural networks for optimal colour quantization" |
243 |
> |
* in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367. |
244 |
> |
* for a discussion of the algorithm. |
245 |
> |
* See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML |
246 |
> |
* |
247 |
> |
* Any party obtaining a copy of these files from the author, directly or |
248 |
> |
* indirectly, is granted, free of charge, a full and unrestricted irrevocable, |
249 |
> |
* world-wide, paid up, royalty-free, nonexclusive right and license to deal |
250 |
> |
* in this software and documentation files (the "Software"), including without |
251 |
> |
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
252 |
> |
* and/or sell copies of the Software, and to permit persons who receive |
253 |
> |
* copies from any such party to do so, with the only requirement being |
254 |
> |
* that this copyright notice remain intact. |
255 |
> |
*/ |
256 |
|
|
257 |
|
#define bool int |
258 |
|
#define false 0 |
259 |
|
#define true 1 |
260 |
|
|
261 |
|
/* network defs */ |
262 |
< |
#define netsize 256 /* number of colours - can change this */ |
262 |
> |
#define netsize clrtabsiz /* number of colours - can change this */ |
263 |
|
#define maxnetpos (netsize-1) |
264 |
|
#define netbiasshift 4 /* bias for colour values */ |
265 |
|
#define ncycles 100 /* no. of learning cycles */ |
274 |
|
#define betagamma (intbias<<(gammashift-betashift)) |
275 |
|
|
276 |
|
/* defs for decreasing radius factor */ |
277 |
< |
#define initrad (netsize>>3) /* for 256 cols, radius starts */ |
277 |
> |
#define initrad (256>>3) /* for 256 cols, radius starts */ |
278 |
|
#define radiusbiasshift 6 /* at 32.0 biased by 6 bits */ |
279 |
|
#define radiusbias (((int) 1)<<radiusbiasshift) |
280 |
|
#define initradius (initrad*radiusbias) /* and decreases by a */ |
299 |
|
#define prime4 503 |
300 |
|
|
301 |
|
typedef int pixel[4]; /* BGRc */ |
302 |
< |
pixel network[netsize]; |
302 |
> |
pixel network[256]; |
303 |
|
|
304 |
|
int netindex[256]; /* for network lookup - really 256 */ |
305 |
|
|
306 |
< |
int bias [netsize]; /* bias and freq arrays for learning */ |
307 |
< |
int freq [netsize]; |
306 |
> |
int bias [256]; /* bias and freq arrays for learning */ |
307 |
> |
int freq [256]; |
308 |
|
int radpower[initrad]; /* radpower for precomputation */ |
309 |
|
|
310 |
|
|
311 |
|
/* initialise network in range (0,0,0) to (255,255,255) */ |
312 |
|
|
313 |
< |
initnet() |
313 |
> |
static void |
314 |
> |
initnet(void) |
315 |
|
{ |
316 |
|
register int i; |
317 |
|
register int *p; |
318 |
|
|
319 |
< |
for (i=0; i<clrtabsiz; i++) { |
319 |
> |
for (i=0; i<netsize; i++) { |
320 |
|
p = network[i]; |
321 |
< |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz; |
322 |
< |
freq[i] = intbias/clrtabsiz; /* 1/clrtabsiz */ |
321 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
322 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
323 |
|
bias[i] = 0; |
324 |
|
} |
325 |
|
} |
327 |
|
|
328 |
|
/* do after unbias - insertion sort of network and build netindex[0..255] */ |
329 |
|
|
330 |
< |
inxbuild() |
330 |
> |
static void |
331 |
> |
inxbuild(void) |
332 |
|
{ |
333 |
|
register int i,j,smallpos,smallval; |
334 |
|
register int *p,*q; |
336 |
|
|
337 |
|
previouscol = 0; |
338 |
|
startpos = 0; |
339 |
< |
for (i=0; i<clrtabsiz; i++) { |
339 |
> |
for (i=0; i<netsize; i++) { |
340 |
|
p = network[i]; |
341 |
|
smallpos = i; |
342 |
|
smallval = p[1]; /* index on g */ |
343 |
< |
/* find smallest in i..clrtabsiz-1 */ |
344 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
343 |
> |
/* find smallest in i..netsize-1 */ |
344 |
> |
for (j=i+1; j<netsize; j++) { |
345 |
|
q = network[j]; |
346 |
|
if (q[1] < smallval) { /* index on g */ |
347 |
|
smallpos = j; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
int inxsearch(b,g,r) /* accepts real BGR values after net is unbiased */ |
373 |
< |
register int b,g,r; |
372 |
> |
static int |
373 |
> |
inxsearch( /* accepts real BGR values after net is unbiased */ |
374 |
> |
register int b, |
375 |
> |
register int g, |
376 |
> |
register int r |
377 |
> |
) |
378 |
|
{ |
379 |
|
register int i,j,dist,a,bestd; |
380 |
|
register int *p; |
385 |
|
i = netindex[g]; /* index on g */ |
386 |
|
j = i-1; /* start at netindex[g] and work outwards */ |
387 |
|
|
388 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
389 |
< |
if (i<clrtabsiz) { |
388 |
> |
while ((i<netsize) || (j>=0)) { |
389 |
> |
if (i<netsize) { |
390 |
|
p = network[i]; |
391 |
|
dist = p[1] - g; /* inx key */ |
392 |
< |
if (dist >= bestd) i = clrtabsiz; /* stop iter */ |
392 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
393 |
|
else { |
394 |
|
i++; |
395 |
|
if (dist<0) dist = -dist; |
426 |
|
/* finds closest neuron (min dist) and updates freq */ |
427 |
|
/* finds best neuron (min dist-bias) and returns position */ |
428 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
429 |
< |
/* bias[i] = gamma*((1/clrtabsiz)-freq[i]) */ |
429 |
> |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
430 |
|
|
431 |
< |
int contest(b,g,r) /* accepts biased BGR values */ |
432 |
< |
register int b,g,r; |
431 |
> |
static int |
432 |
> |
contest( /* accepts biased BGR values */ |
433 |
> |
register int b, |
434 |
> |
register int g, |
435 |
> |
register int r |
436 |
> |
) |
437 |
|
{ |
438 |
|
register int i,dist,a,biasdist,betafreq; |
439 |
|
int bestpos,bestbiaspos,bestd,bestbiasd; |
446 |
|
p = bias; |
447 |
|
f = freq; |
448 |
|
|
449 |
< |
for (i=0; i<clrtabsiz; i++) { |
449 |
> |
for (i=0; i<netsize; i++) { |
450 |
|
n = network[i]; |
451 |
|
dist = n[0] - b; if (dist<0) dist = -dist; |
452 |
|
a = n[1] - g; if (a<0) a = -a; |
468 |
|
|
469 |
|
/* move neuron i towards (b,g,r) by factor alpha */ |
470 |
|
|
471 |
< |
altersingle(alpha,i,b,g,r) /* accepts biased BGR values */ |
472 |
< |
register int alpha,i,b,g,r; |
471 |
> |
static void |
472 |
> |
altersingle( /* accepts biased BGR values */ |
473 |
> |
register int alpha, |
474 |
> |
register int i, |
475 |
> |
register int b, |
476 |
> |
register int g, |
477 |
> |
register int r |
478 |
> |
) |
479 |
|
{ |
480 |
|
register int *n; |
481 |
|
|
491 |
|
/* move neurons adjacent to i towards (b,g,r) by factor */ |
492 |
|
/* alpha*(1-((i-j)^2/[r]^2)) precomputed as radpower[|i-j|]*/ |
493 |
|
|
494 |
< |
alterneigh(rad,i,b,g,r) /* accents biased BGR values */ |
495 |
< |
int rad,i; |
496 |
< |
register int b,g,r; |
494 |
> |
static void |
495 |
> |
alterneigh( /* accents biased BGR values */ |
496 |
> |
int rad, |
497 |
> |
int i, |
498 |
> |
register int b, |
499 |
> |
register int g, |
500 |
> |
register int r |
501 |
> |
) |
502 |
|
{ |
503 |
|
register int j,k,lo,hi,a; |
504 |
|
register int *p, *q; |
505 |
|
|
506 |
|
lo = i-rad; if (lo<-1) lo= -1; |
507 |
< |
hi = i+rad; if (hi>clrtabsiz) hi=clrtabsiz; |
507 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
508 |
|
|
509 |
|
j = i+1; |
510 |
|
k = i-1; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
learn() |
536 |
> |
static void |
537 |
> |
learn(void) |
538 |
|
{ |
539 |
|
register int i,j,b,g,r; |
540 |
|
int radius,rad,alpha,step,delta,samplepixels; |
592 |
|
/* which can then be used for colour map */ |
593 |
|
/* and record position i to prepare for sort */ |
594 |
|
|
595 |
< |
unbiasnet() |
595 |
> |
static void |
596 |
> |
unbiasnet(void) |
597 |
|
{ |
598 |
|
int i,j; |
599 |
|
|
600 |
< |
for (i=0; i<clrtabsiz; i++) { |
600 |
> |
for (i=0; i<netsize; i++) { |
601 |
|
for (j=0; j<3; j++) |
602 |
|
network[i][j] >>= netbiasshift; |
603 |
|
network[i][3] = i; /* record colour no */ |
607 |
|
|
608 |
|
/* Don't do this until the network has been unbiased (GW) */ |
609 |
|
|
610 |
< |
static |
611 |
< |
cpyclrtab() |
610 |
> |
static void |
611 |
> |
cpyclrtab(void) |
612 |
|
{ |
613 |
|
register int i,j,k; |
614 |
|
|
615 |
< |
for (j=0; j<clrtabsiz; j++) { |
615 |
> |
for (j=0; j<netsize; j++) { |
616 |
|
k = network[j][3]; |
617 |
|
for (i = 0; i < 3; i++) |
618 |
|
clrtab[k][i] = network[j][2-i]; |