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