| 51 |
|
|
| 52 |
|
#define setskip(sp,n) ((sp)[0]=(n)>>16,(sp)[1]=((n)>>8)&255,(sp)[2]=(n)&255) |
| 53 |
|
|
| 54 |
+ |
static cpyclrtab(); |
| 55 |
|
|
| 56 |
+ |
|
| 57 |
|
neu_init(npixels) /* initialize our sample array */ |
| 58 |
|
long npixels; |
| 59 |
|
{ |
| 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 |
|
|
| 315 |
|
register int i; |
| 316 |
|
register int *p; |
| 317 |
|
|
| 318 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 318 |
> |
for (i=0; i<netsize; i++) { |
| 319 |
|
p = network[i]; |
| 320 |
< |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz; |
| 321 |
< |
freq[i] = intbias/clrtabsiz; /* 1/clrtabsiz */ |
| 320 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
| 321 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
| 322 |
|
bias[i] = 0; |
| 323 |
|
} |
| 324 |
|
} |
| 334 |
|
|
| 335 |
|
previouscol = 0; |
| 336 |
|
startpos = 0; |
| 337 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 337 |
> |
for (i=0; i<netsize; i++) { |
| 338 |
|
p = network[i]; |
| 339 |
|
smallpos = i; |
| 340 |
|
smallval = p[1]; /* index on g */ |
| 341 |
< |
/* find smallest in i..clrtabsiz-1 */ |
| 342 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
| 341 |
> |
/* find smallest in i..netsize-1 */ |
| 342 |
> |
for (j=i+1; j<netsize; j++) { |
| 343 |
|
q = network[j]; |
| 344 |
|
if (q[1] < smallval) { /* index on g */ |
| 345 |
|
smallpos = j; |
| 379 |
|
i = netindex[g]; /* index on g */ |
| 380 |
|
j = i-1; /* start at netindex[g] and work outwards */ |
| 381 |
|
|
| 382 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
| 383 |
< |
if (i<clrtabsiz) { |
| 382 |
> |
while ((i<netsize) || (j>=0)) { |
| 383 |
> |
if (i<netsize) { |
| 384 |
|
p = network[i]; |
| 385 |
|
dist = p[1] - g; /* inx key */ |
| 386 |
< |
if (dist >= bestd) i = clrtabsiz; /* stop iter */ |
| 386 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
| 387 |
|
else { |
| 388 |
|
i++; |
| 389 |
|
if (dist<0) dist = -dist; |
| 420 |
|
/* finds closest neuron (min dist) and updates freq */ |
| 421 |
|
/* finds best neuron (min dist-bias) and returns position */ |
| 422 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
| 423 |
< |
/* bias[i] = gamma*((1/clrtabsiz)-freq[i]) */ |
| 423 |
> |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
| 424 |
|
|
| 425 |
|
int contest(b,g,r) /* accepts biased BGR values */ |
| 426 |
|
register int b,g,r; |
| 436 |
|
p = bias; |
| 437 |
|
f = freq; |
| 438 |
|
|
| 439 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 439 |
> |
for (i=0; i<netsize; i++) { |
| 440 |
|
n = network[i]; |
| 441 |
|
dist = n[0] - b; if (dist<0) dist = -dist; |
| 442 |
|
a = n[1] - g; if (a<0) a = -a; |
| 483 |
|
register int *p, *q; |
| 484 |
|
|
| 485 |
|
lo = i-rad; if (lo<-1) lo= -1; |
| 486 |
< |
hi = i+rad; if (hi>clrtabsiz) hi=clrtabsiz; |
| 486 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
| 487 |
|
|
| 488 |
|
j = i+1; |
| 489 |
|
k = i-1; |
| 574 |
|
{ |
| 575 |
|
int i,j; |
| 576 |
|
|
| 577 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 577 |
> |
for (i=0; i<netsize; i++) { |
| 578 |
|
for (j=0; j<3; j++) |
| 579 |
|
network[i][j] >>= netbiasshift; |
| 580 |
|
network[i][3] = i; /* record colour no */ |
| 589 |
|
{ |
| 590 |
|
register int i,j,k; |
| 591 |
|
|
| 592 |
< |
for (j=0; j<clrtabsiz; j++) { |
| 592 |
> |
for (j=0; j<netsize; j++) { |
| 593 |
|
k = network[j][3]; |
| 594 |
|
for (i = 0; i < 3; i++) |
| 595 |
|
clrtab[k][i] = network[j][2-i]; |