257 |
|
#define true 1 |
258 |
|
|
259 |
|
/* network defs */ |
260 |
< |
#define netsize 256 /* number of colours - can change this */ |
260 |
> |
#define netsize clrtabsiz /* number of colours - can change this */ |
261 |
|
#define maxnetpos (netsize-1) |
262 |
|
#define netbiasshift 4 /* bias for colour values */ |
263 |
|
#define ncycles 100 /* no. of learning cycles */ |
272 |
|
#define betagamma (intbias<<(gammashift-betashift)) |
273 |
|
|
274 |
|
/* defs for decreasing radius factor */ |
275 |
< |
#define initrad (netsize>>3) /* for 256 cols, radius starts */ |
275 |
> |
#define initrad (256>>3) /* for 256 cols, radius starts */ |
276 |
|
#define radiusbiasshift 6 /* at 32.0 biased by 6 bits */ |
277 |
|
#define radiusbias (((int) 1)<<radiusbiasshift) |
278 |
|
#define initradius (initrad*radiusbias) /* and decreases by a */ |
297 |
|
#define prime4 503 |
298 |
|
|
299 |
|
typedef int pixel[4]; /* BGRc */ |
300 |
< |
pixel network[netsize]; |
300 |
> |
pixel network[256]; |
301 |
|
|
302 |
|
int netindex[256]; /* for network lookup - really 256 */ |
303 |
|
|
304 |
< |
int bias [netsize]; /* bias and freq arrays for learning */ |
305 |
< |
int freq [netsize]; |
304 |
> |
int bias [256]; /* bias and freq arrays for learning */ |
305 |
> |
int freq [256]; |
306 |
|
int radpower[initrad]; /* radpower for precomputation */ |
307 |
|
|
308 |
|
|
313 |
|
register int i; |
314 |
|
register int *p; |
315 |
|
|
316 |
< |
for (i=0; i<clrtabsiz; i++) { |
316 |
> |
for (i=0; i<netsize; i++) { |
317 |
|
p = network[i]; |
318 |
< |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz; |
319 |
< |
freq[i] = intbias/clrtabsiz; /* 1/clrtabsiz */ |
318 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
319 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
320 |
|
bias[i] = 0; |
321 |
|
} |
322 |
|
} |
332 |
|
|
333 |
|
previouscol = 0; |
334 |
|
startpos = 0; |
335 |
< |
for (i=0; i<clrtabsiz; i++) { |
335 |
> |
for (i=0; i<netsize; i++) { |
336 |
|
p = network[i]; |
337 |
|
smallpos = i; |
338 |
|
smallval = p[1]; /* index on g */ |
339 |
< |
/* find smallest in i..clrtabsiz-1 */ |
340 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
339 |
> |
/* find smallest in i..netsize-1 */ |
340 |
> |
for (j=i+1; j<netsize; j++) { |
341 |
|
q = network[j]; |
342 |
|
if (q[1] < smallval) { /* index on g */ |
343 |
|
smallpos = j; |
377 |
|
i = netindex[g]; /* index on g */ |
378 |
|
j = i-1; /* start at netindex[g] and work outwards */ |
379 |
|
|
380 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
381 |
< |
if (i<clrtabsiz) { |
380 |
> |
while ((i<netsize) || (j>=0)) { |
381 |
> |
if (i<netsize) { |
382 |
|
p = network[i]; |
383 |
|
dist = p[1] - g; /* inx key */ |
384 |
< |
if (dist >= bestd) i = clrtabsiz; /* stop iter */ |
384 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
385 |
|
else { |
386 |
|
i++; |
387 |
|
if (dist<0) dist = -dist; |
418 |
|
/* finds closest neuron (min dist) and updates freq */ |
419 |
|
/* finds best neuron (min dist-bias) and returns position */ |
420 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
421 |
< |
/* bias[i] = gamma*((1/clrtabsiz)-freq[i]) */ |
421 |
> |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
422 |
|
|
423 |
|
int contest(b,g,r) /* accepts biased BGR values */ |
424 |
|
register int b,g,r; |
434 |
|
p = bias; |
435 |
|
f = freq; |
436 |
|
|
437 |
< |
for (i=0; i<clrtabsiz; i++) { |
437 |
> |
for (i=0; i<netsize; i++) { |
438 |
|
n = network[i]; |
439 |
|
dist = n[0] - b; if (dist<0) dist = -dist; |
440 |
|
a = n[1] - g; if (a<0) a = -a; |
481 |
|
register int *p, *q; |
482 |
|
|
483 |
|
lo = i-rad; if (lo<-1) lo= -1; |
484 |
< |
hi = i+rad; if (hi>clrtabsiz) hi=clrtabsiz; |
484 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
485 |
|
|
486 |
|
j = i+1; |
487 |
|
k = i-1; |
572 |
|
{ |
573 |
|
int i,j; |
574 |
|
|
575 |
< |
for (i=0; i<clrtabsiz; i++) { |
575 |
> |
for (i=0; i<netsize; i++) { |
576 |
|
for (j=0; j<3; j++) |
577 |
|
network[i][j] >>= netbiasshift; |
578 |
|
network[i][3] = i; /* record colour no */ |
587 |
|
{ |
588 |
|
register int i,j,k; |
589 |
|
|
590 |
< |
for (j=0; j<clrtabsiz; j++) { |
590 |
> |
for (j=0; j<netsize; j++) { |
591 |
|
k = network[j][3]; |
592 |
|
for (i = 0; i < 3; i++) |
593 |
|
clrtab[k][i] = network[j][2-i]; |