| 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 |
| 29 |
< |
#define DEFSMPFAC (240/SPEED+3) |
| 30 |
< |
#else |
| 31 |
< |
#define DEFSMPFAC 30 |
| 31 |
> |
#define DEFSMPFAC 3 |
| 32 |
|
#endif |
| 33 |
– |
#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 cpyclrtab(); |
| 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 |
|
|
| 61 |
< |
neu_init(npixels) /* initialize our sample array */ |
| 62 |
< |
long npixels; |
| 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; |
| 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; |
| 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++) { |
| 327 |
|
|
| 328 |
|
/* initialise network in range (0,0,0) to (255,255,255) */ |
| 329 |
|
|
| 330 |
< |
initnet() |
| 330 |
> |
static void |
| 331 |
> |
initnet(void) |
| 332 |
|
{ |
| 333 |
|
register int i; |
| 334 |
|
register int *p; |
| 344 |
|
|
| 345 |
|
/* do after unbias - insertion sort of network and build netindex[0..255] */ |
| 346 |
|
|
| 347 |
< |
inxbuild() |
| 347 |
> |
static void |
| 348 |
> |
inxbuild(void) |
| 349 |
|
{ |
| 350 |
|
register int i,j,smallpos,smallval; |
| 351 |
|
register int *p,*q; |
| 386 |
|
} |
| 387 |
|
|
| 388 |
|
|
| 389 |
< |
int inxsearch(b,g,r) /* accepts real BGR values after net is unbiased */ |
| 390 |
< |
register int b,g,r; |
| 389 |
> |
static int |
| 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,dist,a,bestd; |
| 397 |
|
register int *p; |
| 445 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
| 446 |
|
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
| 447 |
|
|
| 448 |
< |
int contest(b,g,r) /* accepts biased BGR values */ |
| 449 |
< |
register int b,g,r; |
| 448 |
> |
static int |
| 449 |
> |
contest( /* accepts biased BGR values */ |
| 450 |
> |
register int b, |
| 451 |
> |
register int g, |
| 452 |
> |
register int r |
| 453 |
> |
) |
| 454 |
|
{ |
| 455 |
|
register int i,dist,a,biasdist,betafreq; |
| 456 |
|
int bestpos,bestbiaspos,bestd,bestbiasd; |
| 485 |
|
|
| 486 |
|
/* move neuron i towards (b,g,r) by factor alpha */ |
| 487 |
|
|
| 488 |
< |
altersingle(alpha,i,b,g,r) /* accepts biased BGR values */ |
| 489 |
< |
register int alpha,i,b,g,r; |
| 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 |
|
|
| 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 |
< |
alterneigh(rad,i,b,g,r) /* accents biased BGR values */ |
| 512 |
< |
int rad,i; |
| 513 |
< |
register int b,g,r; |
| 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; |
| 550 |
|
} |
| 551 |
|
|
| 552 |
|
|
| 553 |
< |
learn() |
| 553 |
> |
static void |
| 554 |
> |
learn(void) |
| 555 |
|
{ |
| 556 |
|
register int i,j,b,g,r; |
| 557 |
|
int radius,rad,alpha,step,delta,samplepixels; |
| 609 |
|
/* which can then be used for colour map */ |
| 610 |
|
/* and record position i to prepare for sort */ |
| 611 |
|
|
| 612 |
< |
unbiasnet() |
| 612 |
> |
static void |
| 613 |
> |
unbiasnet(void) |
| 614 |
|
{ |
| 615 |
|
int i,j; |
| 616 |
|
|
| 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 |
|
|