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 |
|
*/ |
48 |
|
|
49 |
|
#define setskip(sp,n) ((sp)[0]=(n)>>16,(sp)[1]=((n)>>8)&255,(sp)[2]=(n)&255) |
50 |
|
|
51 |
+ |
static cpyclrtab(); |
52 |
|
|
53 |
+ |
|
54 |
|
neu_init(npixels) /* initialize our sample array */ |
55 |
|
long npixels; |
56 |
|
{ |
131 |
|
cpyclrtab(); |
132 |
|
inxbuild(); |
133 |
|
/* we're done with our samples */ |
134 |
< |
free((char *)thesamples); |
134 |
> |
free((void *)thesamples); |
135 |
|
/* reset dithering function */ |
136 |
|
neu_dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
137 |
|
/* return new color table size */ |
171 |
|
|
172 |
|
if (n != N) { /* get error propogation array */ |
173 |
|
if (N) { |
174 |
< |
free((char *)cerr); |
174 |
> |
free((void *)cerr); |
175 |
|
cerr = NULL; |
176 |
|
} |
177 |
|
if (n) |
256 |
|
#define true 1 |
257 |
|
|
258 |
|
/* network defs */ |
259 |
< |
#define netsize 256 /* number of colours - can change this */ |
259 |
> |
#define netsize clrtabsiz /* number of colours - can change this */ |
260 |
|
#define maxnetpos (netsize-1) |
261 |
|
#define netbiasshift 4 /* bias for colour values */ |
262 |
|
#define ncycles 100 /* no. of learning cycles */ |
271 |
|
#define betagamma (intbias<<(gammashift-betashift)) |
272 |
|
|
273 |
|
/* defs for decreasing radius factor */ |
274 |
< |
#define initrad (netsize>>3) /* for 256 cols, radius starts */ |
274 |
> |
#define initrad (256>>3) /* for 256 cols, radius starts */ |
275 |
|
#define radiusbiasshift 6 /* at 32.0 biased by 6 bits */ |
276 |
|
#define radiusbias (((int) 1)<<radiusbiasshift) |
277 |
|
#define initradius (initrad*radiusbias) /* and decreases by a */ |
296 |
|
#define prime4 503 |
297 |
|
|
298 |
|
typedef int pixel[4]; /* BGRc */ |
299 |
< |
pixel network[netsize]; |
299 |
> |
pixel network[256]; |
300 |
|
|
301 |
|
int netindex[256]; /* for network lookup - really 256 */ |
302 |
|
|
303 |
< |
int bias [netsize]; /* bias and freq arrays for learning */ |
304 |
< |
int freq [netsize]; |
303 |
> |
int bias [256]; /* bias and freq arrays for learning */ |
304 |
> |
int freq [256]; |
305 |
|
int radpower[initrad]; /* radpower for precomputation */ |
306 |
|
|
307 |
|
|
312 |
|
register int i; |
313 |
|
register int *p; |
314 |
|
|
315 |
< |
for (i=0; i<clrtabsiz; i++) { |
315 |
> |
for (i=0; i<netsize; i++) { |
316 |
|
p = network[i]; |
317 |
< |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz; |
318 |
< |
freq[i] = intbias/clrtabsiz; /* 1/clrtabsiz */ |
317 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
318 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
319 |
|
bias[i] = 0; |
320 |
|
} |
321 |
|
} |
331 |
|
|
332 |
|
previouscol = 0; |
333 |
|
startpos = 0; |
334 |
< |
for (i=0; i<clrtabsiz; i++) { |
334 |
> |
for (i=0; i<netsize; i++) { |
335 |
|
p = network[i]; |
336 |
|
smallpos = i; |
337 |
|
smallval = p[1]; /* index on g */ |
338 |
< |
/* find smallest in i..clrtabsiz-1 */ |
339 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
338 |
> |
/* find smallest in i..netsize-1 */ |
339 |
> |
for (j=i+1; j<netsize; j++) { |
340 |
|
q = network[j]; |
341 |
|
if (q[1] < smallval) { /* index on g */ |
342 |
|
smallpos = j; |
376 |
|
i = netindex[g]; /* index on g */ |
377 |
|
j = i-1; /* start at netindex[g] and work outwards */ |
378 |
|
|
379 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
380 |
< |
if (i<clrtabsiz) { |
379 |
> |
while ((i<netsize) || (j>=0)) { |
380 |
> |
if (i<netsize) { |
381 |
|
p = network[i]; |
382 |
|
dist = p[1] - g; /* inx key */ |
383 |
< |
if (dist >= bestd) i = clrtabsiz; /* stop iter */ |
383 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
384 |
|
else { |
385 |
|
i++; |
386 |
|
if (dist<0) dist = -dist; |
417 |
|
/* finds closest neuron (min dist) and updates freq */ |
418 |
|
/* finds best neuron (min dist-bias) and returns position */ |
419 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
420 |
< |
/* bias[i] = gamma*((1/clrtabsiz)-freq[i]) */ |
420 |
> |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
421 |
|
|
422 |
|
int contest(b,g,r) /* accepts biased BGR values */ |
423 |
|
register int b,g,r; |
433 |
|
p = bias; |
434 |
|
f = freq; |
435 |
|
|
436 |
< |
for (i=0; i<clrtabsiz; i++) { |
436 |
> |
for (i=0; i<netsize; i++) { |
437 |
|
n = network[i]; |
438 |
|
dist = n[0] - b; if (dist<0) dist = -dist; |
439 |
|
a = n[1] - g; if (a<0) a = -a; |
480 |
|
register int *p, *q; |
481 |
|
|
482 |
|
lo = i-rad; if (lo<-1) lo= -1; |
483 |
< |
hi = i+rad; if (hi>clrtabsiz) hi=clrtabsiz; |
483 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
484 |
|
|
485 |
|
j = i+1; |
486 |
|
k = i-1; |
571 |
|
{ |
572 |
|
int i,j; |
573 |
|
|
574 |
< |
for (i=0; i<clrtabsiz; i++) { |
574 |
> |
for (i=0; i<netsize; i++) { |
575 |
|
for (j=0; j<3; j++) |
576 |
|
network[i][j] >>= netbiasshift; |
577 |
|
network[i][3] = i; /* record colour no */ |
586 |
|
{ |
587 |
|
register int i,j,k; |
588 |
|
|
589 |
< |
for (j=0; j<clrtabsiz; j++) { |
589 |
> |
for (j=0; j<netsize; j++) { |
590 |
|
k = network[j][3]; |
591 |
|
for (i = 0; i < 3; i++) |
592 |
|
clrtab[k][i] = network[j][2-i]; |