| 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 |
| 51 |
|
|
| 52 |
|
#define setskip(sp,n) ((sp)[0]=(n)>>16,(sp)[1]=((n)>>8)&255,(sp)[2]=(n)&255) |
| 53 |
|
|
| 54 |
+ |
static void initnet(void); |
| 55 |
+ |
static void inxbuild(void); |
| 56 |
+ |
static int inxsearch(int b, int g, int r); |
| 57 |
+ |
static int contest(int b, int g, int r); |
| 58 |
+ |
static void altersingle(int alpha, int i, int b, int g, int r); |
| 59 |
+ |
static void alterneigh(int rad, int i, int b, int g, int r); |
| 60 |
+ |
static void learn(void); |
| 61 |
+ |
static void unbiasnet(void); |
| 62 |
+ |
static void cpyclrtab(void); |
| 63 |
|
|
| 64 |
< |
neu_init(npixels) /* initialize our sample array */ |
| 65 |
< |
long npixels; |
| 64 |
> |
|
| 65 |
> |
extern int |
| 66 |
> |
neu_init( /* initialize our sample array */ |
| 67 |
> |
long npixels |
| 68 |
> |
) |
| 69 |
|
{ |
| 70 |
|
register int nsleft; |
| 71 |
|
register long sv; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
< |
neu_pixel(col) /* add pixel to our samples */ |
| 106 |
< |
register BYTE col[]; |
| 105 |
> |
extern void |
| 106 |
> |
neu_pixel( /* add pixel to our samples */ |
| 107 |
> |
register BYTE col[] |
| 108 |
> |
) |
| 109 |
|
{ |
| 110 |
|
if (!skipcount--) { |
| 111 |
|
skipcount = nskip(cursamp); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
< |
neu_colrs(cs, n) /* add a scanline to our samples */ |
| 121 |
< |
register COLR *cs; |
| 122 |
< |
register int n; |
| 120 |
> |
extern void |
| 121 |
> |
neu_colrs( /* add a scanline to our samples */ |
| 122 |
> |
register COLR *cs, |
| 123 |
> |
register int n |
| 124 |
> |
) |
| 125 |
|
{ |
| 126 |
|
while (n > skipcount) { |
| 127 |
|
cs += skipcount; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
< |
neu_clrtab(ncolors) /* make new color table using ncolors */ |
| 141 |
< |
int ncolors; |
| 140 |
> |
extern int |
| 141 |
> |
neu_clrtab( /* make new color table using ncolors */ |
| 142 |
> |
int ncolors |
| 143 |
> |
) |
| 144 |
|
{ |
| 145 |
|
clrtabsiz = ncolors; |
| 146 |
|
if (clrtabsiz > 256) clrtabsiz = 256; |
| 150 |
|
cpyclrtab(); |
| 151 |
|
inxbuild(); |
| 152 |
|
/* we're done with our samples */ |
| 153 |
< |
free((char *)thesamples); |
| 153 |
> |
free((void *)thesamples); |
| 154 |
|
/* reset dithering function */ |
| 155 |
|
neu_dith_colrs((BYTE *)NULL, (COLR *)NULL, 0); |
| 156 |
|
/* return new color table size */ |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
< |
int |
| 162 |
< |
neu_map_pixel(col) /* get pixel for color */ |
| 163 |
< |
register BYTE col[]; |
| 161 |
> |
extern int |
| 162 |
> |
neu_map_pixel( /* get pixel for color */ |
| 163 |
> |
register BYTE col[] |
| 164 |
> |
) |
| 165 |
|
{ |
| 166 |
|
return(inxsearch(col[BLU],col[GRN],col[RED])); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
|
| 170 |
< |
neu_map_colrs(bs, cs, n) /* convert a scanline to color index values */ |
| 171 |
< |
register BYTE *bs; |
| 172 |
< |
register COLR *cs; |
| 173 |
< |
register int n; |
| 170 |
> |
extern void |
| 171 |
> |
neu_map_colrs( /* convert a scanline to color index values */ |
| 172 |
> |
register BYTE *bs, |
| 173 |
> |
register COLR *cs, |
| 174 |
> |
register int n |
| 175 |
> |
) |
| 176 |
|
{ |
| 177 |
|
while (n-- > 0) { |
| 178 |
|
*bs++ = inxsearch(cs[0][BLU],cs[0][GRN],cs[0][RED]); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
< |
neu_dith_colrs(bs, cs, n) /* convert scanline to dithered index values */ |
| 185 |
< |
register BYTE *bs; |
| 186 |
< |
register COLR *cs; |
| 187 |
< |
int n; |
| 184 |
> |
extern void |
| 185 |
> |
neu_dith_colrs( /* convert scanline to dithered index values */ |
| 186 |
> |
register BYTE *bs, |
| 187 |
> |
register COLR *cs, |
| 188 |
> |
int n |
| 189 |
> |
) |
| 190 |
|
{ |
| 191 |
|
static short (*cerr)[3] = NULL; |
| 192 |
|
static int N = 0; |
| 195 |
|
|
| 196 |
|
if (n != N) { /* get error propogation array */ |
| 197 |
|
if (N) { |
| 198 |
< |
free((char *)cerr); |
| 198 |
> |
free((void *)cerr); |
| 199 |
|
cerr = NULL; |
| 200 |
|
} |
| 201 |
|
if (n) |
| 206 |
|
return; |
| 207 |
|
} |
| 208 |
|
N = n; |
| 209 |
< |
bzero((char *)cerr, 3*N*sizeof(short)); |
| 209 |
> |
memset((char *)cerr, '\0', 3*N*sizeof(short)); |
| 210 |
|
} |
| 211 |
|
err[0] = err[1] = err[2] = 0; |
| 212 |
|
for (x = 0; x < n; x++) { |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
/* The following was adapted and modified from the original (GW) */ |
| 234 |
+ |
|
| 235 |
+ |
/* cheater definitions (GW) */ |
| 236 |
+ |
#define thepicture thesamples |
| 237 |
+ |
#define lengthcount (nsamples*3) |
| 238 |
+ |
#define samplefac 1 |
| 239 |
+ |
|
| 240 |
|
/*----------------------------------------------------------------------*/ |
| 241 |
|
/* */ |
| 242 |
|
/* NeuQuant */ |
| 243 |
|
/* -------- */ |
| 244 |
|
/* */ |
| 245 |
< |
/* Copyright: Anthony Dekker, June 1994 */ |
| 245 |
> |
/* Copyright: Anthony Dekker, November 1994 */ |
| 246 |
|
/* */ |
| 247 |
|
/* This program performs colour quantization of graphics images (SUN */ |
| 248 |
|
/* raster files). It uses a Kohonen Neural Network. It produces */ |
| 275 |
|
/* Email: [email protected] */ |
| 276 |
|
/*----------------------------------------------------------------------*/ |
| 277 |
|
|
| 278 |
< |
#define bool int |
| 279 |
< |
#define false 0 |
| 280 |
< |
#define true 1 |
| 278 |
> |
#define bool int |
| 279 |
> |
#define false 0 |
| 280 |
> |
#define true 1 |
| 281 |
|
|
| 282 |
< |
#define initrad 32 |
| 283 |
< |
#define radiusdec 30 |
| 284 |
< |
#define alphadec 30 |
| 282 |
> |
/* network defs */ |
| 283 |
> |
#define netsize clrtabsiz /* number of colours - can change this */ |
| 284 |
> |
#define maxnetpos (netsize-1) |
| 285 |
> |
#define netbiasshift 4 /* bias for colour values */ |
| 286 |
> |
#define ncycles 100 /* no. of learning cycles */ |
| 287 |
|
|
| 288 |
|
/* defs for freq and bias */ |
| 289 |
< |
#define gammashift 10 |
| 290 |
< |
#define betashift gammashift |
| 291 |
< |
#define intbiasshift 16 |
| 292 |
< |
#define intbias (1<<intbiasshift) |
| 293 |
< |
#define gamma (1<<gammashift) |
| 294 |
< |
#define beta (intbias>>betashift) |
| 289 |
> |
#define intbiasshift 16 /* bias for fractions */ |
| 290 |
> |
#define intbias (((int) 1)<<intbiasshift) |
| 291 |
> |
#define gammashift 10 /* gamma = 1024 */ |
| 292 |
> |
#define gamma (((int) 1)<<gammashift) |
| 293 |
> |
#define betashift 10 |
| 294 |
> |
#define beta (intbias>>betashift) /* beta = 1/1024 */ |
| 295 |
|
#define betagamma (intbias<<(gammashift-betashift)) |
| 265 |
– |
#define gammaphi (intbias<<(gammashift-8)) |
| 296 |
|
|
| 297 |
< |
/* defs for rad and alpha */ |
| 298 |
< |
#define maxrad (initrad+1) |
| 299 |
< |
#define radiusbiasshift 6 |
| 300 |
< |
#define radiusbias (1<<radiusbiasshift) |
| 301 |
< |
#define initradius ((int) (initrad*radiusbias)) |
| 302 |
< |
#define alphabiasshift 10 |
| 303 |
< |
#define initalpha (1<<alphabiasshift) |
| 297 |
> |
/* defs for decreasing radius factor */ |
| 298 |
> |
#define initrad (256>>3) /* for 256 cols, radius starts */ |
| 299 |
> |
#define radiusbiasshift 6 /* at 32.0 biased by 6 bits */ |
| 300 |
> |
#define radiusbias (((int) 1)<<radiusbiasshift) |
| 301 |
> |
#define initradius (initrad*radiusbias) /* and decreases by a */ |
| 302 |
> |
#define radiusdec 30 /* factor of 1/30 each cycle */ |
| 303 |
> |
|
| 304 |
> |
/* defs for decreasing alpha factor */ |
| 305 |
> |
#define alphabiasshift 10 /* alpha starts at 1.0 */ |
| 306 |
> |
#define initalpha (((int) 1)<<alphabiasshift) |
| 307 |
> |
int alphadec; /* biased by 10 bits */ |
| 308 |
> |
|
| 309 |
> |
/* radbias and alpharadbias used for radpower calculation */ |
| 310 |
|
#define radbiasshift 8 |
| 311 |
< |
#define radbias (1<<radbiasshift) |
| 311 |
> |
#define radbias (((int) 1)<<radbiasshift) |
| 312 |
|
#define alpharadbshift (alphabiasshift+radbiasshift) |
| 313 |
< |
#define alpharadbias (1<<alpharadbshift) |
| 313 |
> |
#define alpharadbias (((int) 1)<<alpharadbshift) |
| 314 |
|
|
| 315 |
< |
/* other defs */ |
| 316 |
< |
#define netbiasshift 4 |
| 317 |
< |
#define funnyshift (intbiasshift-netbiasshift) |
| 318 |
< |
#define maxnetval ((256<<netbiasshift)-1) |
| 319 |
< |
#define ncycles 100 |
| 320 |
< |
#define jump1 499 /* prime */ |
| 285 |
< |
#define jump2 491 /* prime */ |
| 286 |
< |
#define jump3 487 /* any pic whose size was divisible by all */ |
| 287 |
< |
#define jump4 503 /* four primes would be simply enormous */ |
| 315 |
> |
/* four primes near 500 - assume no image has a length so large */ |
| 316 |
> |
/* that it is divisible by all four primes */ |
| 317 |
> |
#define prime1 499 |
| 318 |
> |
#define prime2 491 |
| 319 |
> |
#define prime3 487 |
| 320 |
> |
#define prime4 503 |
| 321 |
|
|
| 289 |
– |
/* cheater definitions (GW) */ |
| 290 |
– |
#define thepicture thesamples |
| 291 |
– |
#define lengthcount (nsamples*3) |
| 292 |
– |
#define samplefac 1 |
| 293 |
– |
|
| 322 |
|
typedef int pixel[4]; /* BGRc */ |
| 323 |
+ |
pixel network[256]; |
| 324 |
|
|
| 325 |
< |
static pixel network[256]; |
| 325 |
> |
int netindex[256]; /* for network lookup - really 256 */ |
| 326 |
|
|
| 327 |
< |
static int netindex[256]; |
| 327 |
> |
int bias [256]; /* bias and freq arrays for learning */ |
| 328 |
> |
int freq [256]; |
| 329 |
> |
int radpower[initrad]; /* radpower for precomputation */ |
| 330 |
|
|
| 300 |
– |
static int bias [256]; |
| 301 |
– |
static int freq [256]; |
| 302 |
– |
static int radpower[256]; /* actually need only go up to maxrad */ |
| 331 |
|
|
| 332 |
< |
/* fixed space overhead 256*4+256+256+256+256 words = 256*8 = 8kB */ |
| 332 |
> |
/* initialise network in range (0,0,0) to (255,255,255) */ |
| 333 |
|
|
| 334 |
< |
|
| 335 |
< |
static |
| 308 |
< |
initnet() |
| 334 |
> |
static void |
| 335 |
> |
initnet(void) |
| 336 |
|
{ |
| 337 |
|
register int i; |
| 338 |
|
register int *p; |
| 339 |
|
|
| 340 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 340 |
> |
for (i=0; i<netsize; i++) { |
| 341 |
|
p = network[i]; |
| 342 |
< |
p[0] = |
| 343 |
< |
p[1] = |
| 317 |
< |
p[2] = (i<<(netbiasshift+8))/clrtabsiz; |
| 318 |
< |
freq[i] = intbias/clrtabsiz; /* 1/256 */ |
| 342 |
> |
p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize; |
| 343 |
> |
freq[i] = intbias/netsize; /* 1/netsize */ |
| 344 |
|
bias[i] = 0; |
| 345 |
|
} |
| 346 |
|
} |
| 347 |
|
|
| 348 |
|
|
| 349 |
< |
static |
| 350 |
< |
inxbuild() |
| 349 |
> |
/* do after unbias - insertion sort of network and build netindex[0..255] */ |
| 350 |
> |
|
| 351 |
> |
static void |
| 352 |
> |
inxbuild(void) |
| 353 |
|
{ |
| 354 |
|
register int i,j,smallpos,smallval; |
| 355 |
|
register int *p,*q; |
| 356 |
< |
int start,previous; |
| 356 |
> |
int previouscol,startpos; |
| 357 |
|
|
| 358 |
< |
previous = 0; |
| 359 |
< |
start = 0; |
| 360 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 358 |
> |
previouscol = 0; |
| 359 |
> |
startpos = 0; |
| 360 |
> |
for (i=0; i<netsize; i++) { |
| 361 |
|
p = network[i]; |
| 362 |
|
smallpos = i; |
| 363 |
|
smallval = p[1]; /* index on g */ |
| 364 |
< |
/* find smallest in i+1..clrtabsiz-1 */ |
| 365 |
< |
for (j=i+1; j<clrtabsiz; j++) { |
| 364 |
> |
/* find smallest in i..netsize-1 */ |
| 365 |
> |
for (j=i+1; j<netsize; j++) { |
| 366 |
|
q = network[j]; |
| 367 |
|
if (q[1] < smallval) { /* index on g */ |
| 368 |
|
smallpos = j; |
| 370 |
|
} |
| 371 |
|
} |
| 372 |
|
q = network[smallpos]; |
| 373 |
+ |
/* swap p (i) and q (smallpos) entries */ |
| 374 |
|
if (i != smallpos) { |
| 375 |
|
j = q[0]; q[0] = p[0]; p[0] = j; |
| 376 |
|
j = q[1]; q[1] = p[1]; p[1] = j; |
| 378 |
|
j = q[3]; q[3] = p[3]; p[3] = j; |
| 379 |
|
} |
| 380 |
|
/* smallval entry is now in position i */ |
| 381 |
< |
if (smallval != previous) { |
| 382 |
< |
netindex[previous] = (start+i)>>1; |
| 383 |
< |
for (j=previous+1; j<smallval; j++) netindex[j] = i; |
| 384 |
< |
previous = smallval; |
| 385 |
< |
start = i; |
| 381 |
> |
if (smallval != previouscol) { |
| 382 |
> |
netindex[previouscol] = (startpos+i)>>1; |
| 383 |
> |
for (j=previouscol+1; j<smallval; j++) netindex[j] = i; |
| 384 |
> |
previouscol = smallval; |
| 385 |
> |
startpos = i; |
| 386 |
|
} |
| 387 |
|
} |
| 388 |
< |
netindex[previous] = (start+255)>>1; |
| 389 |
< |
for (j=previous+1; j<256; j++) netindex[j] = 255; |
| 388 |
> |
netindex[previouscol] = (startpos+maxnetpos)>>1; |
| 389 |
> |
for (j=previouscol+1; j<256; j++) netindex[j] = maxnetpos; /* really 256 */ |
| 390 |
|
} |
| 391 |
|
|
| 392 |
|
|
| 393 |
|
static int |
| 394 |
< |
inxsearch(b,g,r) /* accepts real BGR values after net is unbiased */ |
| 395 |
< |
register int b,g,r; |
| 394 |
> |
inxsearch( /* accepts real BGR values after net is unbiased */ |
| 395 |
> |
register int b, |
| 396 |
> |
register int g, |
| 397 |
> |
register int r |
| 398 |
> |
) |
| 399 |
|
{ |
| 400 |
< |
register int i,j,best,x,y,bestd; |
| 400 |
> |
register int i,j,dist,a,bestd; |
| 401 |
|
register int *p; |
| 402 |
+ |
int best; |
| 403 |
|
|
| 404 |
|
bestd = 1000; /* biggest possible dist is 256*3 */ |
| 405 |
|
best = -1; |
| 406 |
|
i = netindex[g]; /* index on g */ |
| 407 |
< |
j = i-1; |
| 407 |
> |
j = i-1; /* start at netindex[g] and work outwards */ |
| 408 |
|
|
| 409 |
< |
while ((i<clrtabsiz) || (j>=0)) { |
| 410 |
< |
if (i<clrtabsiz) { |
| 409 |
> |
while ((i<netsize) || (j>=0)) { |
| 410 |
> |
if (i<netsize) { |
| 411 |
|
p = network[i]; |
| 412 |
< |
x = p[1] - g; /* inx key */ |
| 413 |
< |
if (x >= bestd) i = clrtabsiz; /* stop iter */ |
| 412 |
> |
dist = p[1] - g; /* inx key */ |
| 413 |
> |
if (dist >= bestd) i = netsize; /* stop iter */ |
| 414 |
|
else { |
| 415 |
|
i++; |
| 416 |
< |
if (x<0) x = -x; |
| 417 |
< |
y = p[0] - b; |
| 418 |
< |
if (y<0) y = -y; |
| 419 |
< |
x += y; |
| 420 |
< |
if (x<bestd) { |
| 421 |
< |
y = p[2] - r; |
| 422 |
< |
if (y<0) y = -y; |
| 391 |
< |
x += y; /* x holds distance */ |
| 392 |
< |
if (x<bestd) {bestd=x; best=p[3];} |
| 416 |
> |
if (dist<0) dist = -dist; |
| 417 |
> |
a = p[0] - b; if (a<0) a = -a; |
| 418 |
> |
dist += a; |
| 419 |
> |
if (dist<bestd) { |
| 420 |
> |
a = p[2] - r; if (a<0) a = -a; |
| 421 |
> |
dist += a; |
| 422 |
> |
if (dist<bestd) {bestd=dist; best=p[3];} |
| 423 |
|
} |
| 424 |
|
} |
| 425 |
|
} |
| 426 |
|
if (j>=0) { |
| 427 |
|
p = network[j]; |
| 428 |
< |
x = g - p[1]; /* inx key - reverse dif */ |
| 429 |
< |
if (x >= bestd) j = -1; /* stop iter */ |
| 428 |
> |
dist = g - p[1]; /* inx key - reverse dif */ |
| 429 |
> |
if (dist >= bestd) j = -1; /* stop iter */ |
| 430 |
|
else { |
| 431 |
|
j--; |
| 432 |
< |
if (x<0) x = -x; |
| 433 |
< |
y = p[0] - b; |
| 434 |
< |
if (y<0) y = -y; |
| 435 |
< |
x += y; |
| 436 |
< |
if (x<bestd) { |
| 437 |
< |
y = p[2] - r; |
| 438 |
< |
if (y<0) y = -y; |
| 409 |
< |
x += y; /* x holds distance */ |
| 410 |
< |
if (x<bestd) {bestd=x; best=p[3];} |
| 432 |
> |
if (dist<0) dist = -dist; |
| 433 |
> |
a = p[0] - b; if (a<0) a = -a; |
| 434 |
> |
dist += a; |
| 435 |
> |
if (dist<bestd) { |
| 436 |
> |
a = p[2] - r; if (a<0) a = -a; |
| 437 |
> |
dist += a; |
| 438 |
> |
if (dist<bestd) {bestd=dist; best=p[3];} |
| 439 |
|
} |
| 440 |
|
} |
| 441 |
|
} |
| 444 |
|
} |
| 445 |
|
|
| 446 |
|
|
| 447 |
+ |
/* finds closest neuron (min dist) and updates freq */ |
| 448 |
+ |
/* finds best neuron (min dist-bias) and returns position */ |
| 449 |
+ |
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
| 450 |
+ |
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
| 451 |
+ |
|
| 452 |
|
static int |
| 453 |
< |
contest(b,g,r) /* accepts biased BGR values */ |
| 454 |
< |
register int b,g,r; |
| 453 |
> |
contest( /* accepts biased BGR values */ |
| 454 |
> |
register int b, |
| 455 |
> |
register int g, |
| 456 |
> |
register int r |
| 457 |
> |
) |
| 458 |
|
{ |
| 459 |
< |
register int i,best,bestbias,x,y,bestd,bestbiasd; |
| 460 |
< |
register int *p,*q, *pp; |
| 459 |
> |
register int i,dist,a,biasdist,betafreq; |
| 460 |
> |
int bestpos,bestbiaspos,bestd,bestbiasd; |
| 461 |
> |
register int *p,*f, *n; |
| 462 |
|
|
| 463 |
< |
bestd = ~(1<<31); |
| 463 |
> |
bestd = ~(((int) 1)<<31); |
| 464 |
|
bestbiasd = bestd; |
| 465 |
< |
best = -1; |
| 466 |
< |
bestbias = best; |
| 467 |
< |
q = bias; |
| 468 |
< |
p = freq; |
| 469 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 470 |
< |
pp = network[i]; |
| 471 |
< |
x = pp[0] - b; |
| 472 |
< |
if (x<0) x = -x; |
| 473 |
< |
y = pp[1] - g; |
| 474 |
< |
if (y<0) y = -y; |
| 475 |
< |
x += y; |
| 476 |
< |
y = pp[2] - r; |
| 477 |
< |
if (y<0) y = -y; |
| 478 |
< |
x += y; /* x holds distance */ |
| 479 |
< |
/* >> netbiasshift not needed if funnyshift used */ |
| 480 |
< |
if (x<bestd) {bestd=x; best=i;} |
| 481 |
< |
y = x - ((*q)>>funnyshift); /* y holds biasd */ |
| 482 |
< |
if (y<bestbiasd) {bestbiasd=y; bestbias=i;} |
| 446 |
< |
y = (*p >> betashift); /* y holds beta*freq */ |
| 447 |
< |
*p -= y; |
| 448 |
< |
*q += (y<<gammashift); |
| 449 |
< |
p++; |
| 450 |
< |
q++; |
| 465 |
> |
bestpos = -1; |
| 466 |
> |
bestbiaspos = bestpos; |
| 467 |
> |
p = bias; |
| 468 |
> |
f = freq; |
| 469 |
> |
|
| 470 |
> |
for (i=0; i<netsize; i++) { |
| 471 |
> |
n = network[i]; |
| 472 |
> |
dist = n[0] - b; if (dist<0) dist = -dist; |
| 473 |
> |
a = n[1] - g; if (a<0) a = -a; |
| 474 |
> |
dist += a; |
| 475 |
> |
a = n[2] - r; if (a<0) a = -a; |
| 476 |
> |
dist += a; |
| 477 |
> |
if (dist<bestd) {bestd=dist; bestpos=i;} |
| 478 |
> |
biasdist = dist - ((*p)>>(intbiasshift-netbiasshift)); |
| 479 |
> |
if (biasdist<bestbiasd) {bestbiasd=biasdist; bestbiaspos=i;} |
| 480 |
> |
betafreq = (*f >> betashift); |
| 481 |
> |
*f++ -= betafreq; |
| 482 |
> |
*p++ += (betafreq<<gammashift); |
| 483 |
|
} |
| 484 |
< |
freq[best] += beta; |
| 485 |
< |
bias[best] -= betagamma; |
| 486 |
< |
return(bestbias); |
| 484 |
> |
freq[bestpos] += beta; |
| 485 |
> |
bias[bestpos] -= betagamma; |
| 486 |
> |
return(bestbiaspos); |
| 487 |
|
} |
| 488 |
|
|
| 489 |
|
|
| 490 |
< |
static |
| 491 |
< |
alterneigh(rad,i,b,g,r) /* accepts biased BGR values */ |
| 492 |
< |
int rad,i; |
| 493 |
< |
register int b,g,r; |
| 490 |
> |
/* move neuron i towards (b,g,r) by factor alpha */ |
| 491 |
> |
|
| 492 |
> |
static void |
| 493 |
> |
altersingle( /* accepts biased BGR values */ |
| 494 |
> |
register int alpha, |
| 495 |
> |
register int i, |
| 496 |
> |
register int b, |
| 497 |
> |
register int g, |
| 498 |
> |
register int r |
| 499 |
> |
) |
| 500 |
|
{ |
| 501 |
+ |
register int *n; |
| 502 |
+ |
|
| 503 |
+ |
n = network[i]; /* alter hit neuron */ |
| 504 |
+ |
*n -= (alpha*(*n - b)) / initalpha; |
| 505 |
+ |
n++; |
| 506 |
+ |
*n -= (alpha*(*n - g)) / initalpha; |
| 507 |
+ |
n++; |
| 508 |
+ |
*n -= (alpha*(*n - r)) / initalpha; |
| 509 |
+ |
} |
| 510 |
+ |
|
| 511 |
+ |
|
| 512 |
+ |
/* move neurons adjacent to i towards (b,g,r) by factor */ |
| 513 |
+ |
/* alpha*(1-((i-j)^2/[r]^2)) precomputed as radpower[|i-j|]*/ |
| 514 |
+ |
|
| 515 |
+ |
static void |
| 516 |
+ |
alterneigh( /* accents biased BGR values */ |
| 517 |
+ |
int rad, |
| 518 |
+ |
int i, |
| 519 |
+ |
register int b, |
| 520 |
+ |
register int g, |
| 521 |
+ |
register int r |
| 522 |
+ |
) |
| 523 |
+ |
{ |
| 524 |
|
register int j,k,lo,hi,a; |
| 525 |
|
register int *p, *q; |
| 526 |
|
|
| 527 |
< |
lo = i-rad; |
| 528 |
< |
if (lo<-1) lo= -1; |
| 468 |
< |
hi = i+rad; |
| 469 |
< |
if (hi>clrtabsiz) hi=clrtabsiz; |
| 527 |
> |
lo = i-rad; if (lo<-1) lo= -1; |
| 528 |
> |
hi = i+rad; if (hi>netsize) hi=netsize; |
| 529 |
|
|
| 530 |
|
j = i+1; |
| 531 |
|
k = i-1; |
| 554 |
|
} |
| 555 |
|
|
| 556 |
|
|
| 557 |
< |
static |
| 558 |
< |
altersingle(alpha,j,b,g,r) /* accepts biased BGR values */ |
| 500 |
< |
register int alpha,j,b,g,r; |
| 557 |
> |
static void |
| 558 |
> |
learn(void) |
| 559 |
|
{ |
| 502 |
– |
register int *q; |
| 503 |
– |
|
| 504 |
– |
q = network[j]; /* alter hit neuron */ |
| 505 |
– |
*q -= (alpha*(*q - b)) / initalpha; |
| 506 |
– |
q++; |
| 507 |
– |
*q -= (alpha*(*q - g)) / initalpha; |
| 508 |
– |
q++; |
| 509 |
– |
*q -= (alpha*(*q - r)) / initalpha; |
| 510 |
– |
} |
| 511 |
– |
|
| 512 |
– |
|
| 513 |
– |
static |
| 514 |
– |
learn() |
| 515 |
– |
{ |
| 560 |
|
register int i,j,b,g,r; |
| 561 |
< |
int radius,rad,alpha,step,delta,upto; |
| 561 |
> |
int radius,rad,alpha,step,delta,samplepixels; |
| 562 |
|
register unsigned char *p; |
| 563 |
|
unsigned char *lim; |
| 564 |
|
|
| 565 |
< |
upto = lengthcount/(3*samplefac); |
| 522 |
< |
delta = upto/ncycles; |
| 523 |
< |
lim = thepicture + lengthcount; |
| 565 |
> |
alphadec = 30 + ((samplefac-1)/3); |
| 566 |
|
p = thepicture; |
| 567 |
+ |
lim = thepicture + lengthcount; |
| 568 |
+ |
samplepixels = lengthcount/(3*samplefac); |
| 569 |
+ |
delta = samplepixels/ncycles; |
| 570 |
|
alpha = initalpha; |
| 571 |
|
radius = initradius; |
| 572 |
+ |
|
| 573 |
|
rad = radius >> radiusbiasshift; |
| 574 |
|
if (rad <= 1) rad = 0; |
| 575 |
|
for (i=0; i<rad; i++) |
| 576 |
|
radpower[i] = alpha*(((rad*rad - i*i)*radbias)/(rad*rad)); |
| 577 |
< |
|
| 578 |
< |
if ((lengthcount%jump1) != 0) step = 3*jump1; |
| 577 |
> |
|
| 578 |
> |
if ((lengthcount%prime1) != 0) step = 3*prime1; |
| 579 |
|
else { |
| 580 |
< |
if ((lengthcount%jump2) !=0) step = 3*jump2; |
| 580 |
> |
if ((lengthcount%prime2) !=0) step = 3*prime2; |
| 581 |
|
else { |
| 582 |
< |
if ((lengthcount%jump3) !=0) step = 3*jump3; |
| 583 |
< |
else step = 3*jump4; |
| 582 |
> |
if ((lengthcount%prime3) !=0) step = 3*prime3; |
| 583 |
> |
else step = 3*prime4; |
| 584 |
|
} |
| 585 |
|
} |
| 586 |
+ |
|
| 587 |
|
i = 0; |
| 588 |
< |
while (i < upto) { |
| 588 |
> |
while (i < samplepixels) { |
| 589 |
|
b = p[0] << netbiasshift; |
| 590 |
|
g = p[1] << netbiasshift; |
| 591 |
|
r = p[2] << netbiasshift; |
| 592 |
|
j = contest(b,g,r); |
| 593 |
|
|
| 594 |
|
altersingle(alpha,j,b,g,r); |
| 595 |
< |
if (rad) alterneigh(rad,j,b,g,r); |
| 549 |
< |
/* alter neighbours */ |
| 595 |
> |
if (rad) alterneigh(rad,j,b,g,r); /* alter neighbours */ |
| 596 |
|
|
| 597 |
|
p += step; |
| 598 |
|
if (p >= lim) p -= lengthcount; |
| 609 |
|
} |
| 610 |
|
} |
| 611 |
|
|
| 612 |
< |
static |
| 613 |
< |
unbiasnet() |
| 612 |
> |
/* unbias network to give 0..255 entries */ |
| 613 |
> |
/* which can then be used for colour map */ |
| 614 |
> |
/* and record position i to prepare for sort */ |
| 615 |
> |
|
| 616 |
> |
static void |
| 617 |
> |
unbiasnet(void) |
| 618 |
|
{ |
| 619 |
|
int i,j; |
| 620 |
|
|
| 621 |
< |
for (i=0; i<clrtabsiz; i++) { |
| 621 |
> |
for (i=0; i<netsize; i++) { |
| 622 |
|
for (j=0; j<3; j++) |
| 623 |
|
network[i][j] >>= netbiasshift; |
| 624 |
|
network[i][3] = i; /* record colour no */ |
| 625 |
|
} |
| 626 |
|
} |
| 627 |
|
|
| 628 |
< |
/* Don't do this until the network has been unbiased */ |
| 628 |
> |
|
| 629 |
> |
/* Don't do this until the network has been unbiased (GW) */ |
| 630 |
|
|
| 631 |
< |
static |
| 632 |
< |
cpyclrtab() |
| 631 |
> |
static void |
| 632 |
> |
cpyclrtab(void) |
| 633 |
|
{ |
| 634 |
|
register int i,j,k; |
| 635 |
|
|
| 636 |
< |
for (j=0; j<clrtabsiz; j++) { |
| 636 |
> |
for (j=0; j<netsize; j++) { |
| 637 |
|
k = network[j][3]; |
| 638 |
|
for (i = 0; i < 3; i++) |
| 639 |
|
clrtab[k][i] = network[j][2-i]; |