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 |
|
|
16 |
|
#ifdef COMPAT_MODE |
50 |
|
|
51 |
|
#define setskip(sp,n) ((sp)[0]=(n)>>16,(sp)[1]=((n)>>8)&255,(sp)[2]=(n)&255) |
52 |
|
|
53 |
+ |
static cpyclrtab(); |
54 |
|
|
55 |
+ |
|
56 |
|
neu_init(npixels) /* initialize our sample array */ |
57 |
|
long npixels; |
58 |
|
{ |
133 |
|
cpyclrtab(); |
134 |
|
inxbuild(); |
135 |
|
/* we're done with our samples */ |
136 |
< |
free((char *)thesamples); |
136 |
> |
free((void *)thesamples); |
137 |
|
/* reset dithering function */ |
138 |
|
neu_dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
139 |
|
/* return new color table size */ |
173 |
|
|
174 |
|
if (n != N) { /* get error propogation array */ |
175 |
|
if (N) { |
176 |
< |
free((char *)cerr); |
176 |
> |
free((void *)cerr); |
177 |
|
cerr = NULL; |
178 |
|
} |
179 |
|
if (n) |
184 |
|
return; |
185 |
|
} |
186 |
|
N = n; |
187 |
< |
bzero((char *)cerr, 3*N*sizeof(short)); |
187 |
> |
memset((char *)cerr, '\0', 3*N*sizeof(short)); |
188 |
|
} |
189 |
|
err[0] = err[1] = err[2] = 0; |
190 |
|
for (x = 0; x < n; x++) { |
258 |
|
#define true 1 |
259 |
|
|
260 |
|
/* network defs */ |
261 |
< |
#define netsize 256 /* number of colours - can change this */ |
261 |
> |
#define netsize clrtabsiz /* number of colours - can change this */ |
262 |
|
#define maxnetpos (netsize-1) |
263 |
|
#define netbiasshift 4 /* bias for colour values */ |
264 |
|
#define ncycles 100 /* no. of learning cycles */ |
273 |
|
#define betagamma (intbias<<(gammashift-betashift)) |
274 |
|
|
275 |
|
/* defs for decreasing radius factor */ |
276 |
< |
#define initrad (netsize>>3) /* for 256 cols, radius starts */ |
276 |
> |
#define initrad (256>>3) /* for 256 cols, radius starts */ |
277 |
|
#define radiusbiasshift 6 /* at 32.0 biased by 6 bits */ |
278 |
|
#define radiusbias (((int) 1)<<radiusbiasshift) |
279 |
|
#define initradius (initrad*radiusbias) /* and decreases by a */ |
298 |
|
#define prime4 503 |
299 |
|
|
300 |
|
typedef int pixel[4]; /* BGRc */ |
301 |
< |
pixel network[netsize]; |
301 |
> |
pixel network[256]; |
302 |
|
|
303 |
|
int netindex[256]; /* for network lookup - really 256 */ |
304 |
|
|
305 |
< |
int bias [netsize]; /* bias and freq arrays for learning */ |
306 |
< |
int freq [netsize]; |
305 |
> |
int bias [256]; /* bias and freq arrays for learning */ |
306 |
> |
int freq [256]; |
307 |
|
int radpower[initrad]; /* radpower for precomputation */ |
308 |
|
|
309 |
|
|
314 |
|
register int i; |
315 |
|
register int *p; |
316 |
|
|
317 |
< |
for (i=0; i<clrtabsiz; i++) { |
317 |
> |
for (i=0; i<netsize; i++) { |
318 |
|
p = network[i]; |
319 |
< |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz; |
320 |
< |
freq[i] = intbias/clrtabsiz; /* 1/clrtabsiz */ |
319 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
320 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
321 |
|
bias[i] = 0; |
322 |
|
} |
323 |
|
} |
333 |
|
|
334 |
|
previouscol = 0; |
335 |
|
startpos = 0; |
336 |
< |
for (i=0; i<clrtabsiz; i++) { |
336 |
> |
for (i=0; i<netsize; i++) { |
337 |
|
p = network[i]; |
338 |
|
smallpos = i; |
339 |
|
smallval = p[1]; /* index on g */ |
340 |
< |
/* find smallest in i..clrtabsiz-1 */ |
341 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
340 |
> |
/* find smallest in i..netsize-1 */ |
341 |
> |
for (j=i+1; j<netsize; j++) { |
342 |
|
q = network[j]; |
343 |
|
if (q[1] < smallval) { /* index on g */ |
344 |
|
smallpos = j; |
378 |
|
i = netindex[g]; /* index on g */ |
379 |
|
j = i-1; /* start at netindex[g] and work outwards */ |
380 |
|
|
381 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
382 |
< |
if (i<clrtabsiz) { |
381 |
> |
while ((i<netsize) || (j>=0)) { |
382 |
> |
if (i<netsize) { |
383 |
|
p = network[i]; |
384 |
|
dist = p[1] - g; /* inx key */ |
385 |
< |
if (dist >= bestd) i = clrtabsiz; /* stop iter */ |
385 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
386 |
|
else { |
387 |
|
i++; |
388 |
|
if (dist<0) dist = -dist; |
419 |
|
/* finds closest neuron (min dist) and updates freq */ |
420 |
|
/* finds best neuron (min dist-bias) and returns position */ |
421 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
422 |
< |
/* bias[i] = gamma*((1/clrtabsiz)-freq[i]) */ |
422 |
> |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
423 |
|
|
424 |
|
int contest(b,g,r) /* accepts biased BGR values */ |
425 |
|
register int b,g,r; |
435 |
|
p = bias; |
436 |
|
f = freq; |
437 |
|
|
438 |
< |
for (i=0; i<clrtabsiz; i++) { |
438 |
> |
for (i=0; i<netsize; i++) { |
439 |
|
n = network[i]; |
440 |
|
dist = n[0] - b; if (dist<0) dist = -dist; |
441 |
|
a = n[1] - g; if (a<0) a = -a; |
482 |
|
register int *p, *q; |
483 |
|
|
484 |
|
lo = i-rad; if (lo<-1) lo= -1; |
485 |
< |
hi = i+rad; if (hi>clrtabsiz) hi=clrtabsiz; |
485 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
486 |
|
|
487 |
|
j = i+1; |
488 |
|
k = i-1; |
573 |
|
{ |
574 |
|
int i,j; |
575 |
|
|
576 |
< |
for (i=0; i<clrtabsiz; i++) { |
576 |
> |
for (i=0; i<netsize; i++) { |
577 |
|
for (j=0; j<3; j++) |
578 |
|
network[i][j] >>= netbiasshift; |
579 |
|
network[i][3] = i; /* record colour no */ |
588 |
|
{ |
589 |
|
register int i,j,k; |
590 |
|
|
591 |
< |
for (j=0; j<clrtabsiz; j++) { |
591 |
> |
for (j=0; j<netsize; j++) { |
592 |
|
k = network[j][3]; |
593 |
|
for (i = 0; i < 3; i++) |
594 |
|
clrtab[k][i] = network[j][2-i]; |