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++) { |
331 |
|
|
332 |
|
/* initialise network in range (0,0,0) to (255,255,255) */ |
333 |
|
|
334 |
< |
initnet() |
334 |
> |
static void |
335 |
> |
initnet(void) |
336 |
|
{ |
337 |
|
register int i; |
338 |
|
register int *p; |
348 |
|
|
349 |
|
/* do after unbias - insertion sort of network and build netindex[0..255] */ |
350 |
|
|
351 |
< |
inxbuild() |
351 |
> |
static void |
352 |
> |
inxbuild(void) |
353 |
|
{ |
354 |
|
register int i,j,smallpos,smallval; |
355 |
|
register int *p,*q; |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
< |
int inxsearch(b,g,r) /* accepts real BGR values after net is unbiased */ |
394 |
< |
register int b,g,r; |
393 |
> |
static int |
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,dist,a,bestd; |
401 |
|
register int *p; |
449 |
|
/* for frequently chosen neurons, freq[i] is high and bias[i] is negative */ |
450 |
|
/* bias[i] = gamma*((1/netsize)-freq[i]) */ |
451 |
|
|
452 |
< |
int contest(b,g,r) /* accepts biased BGR values */ |
453 |
< |
register int b,g,r; |
452 |
> |
static int |
453 |
> |
contest( /* accepts biased BGR values */ |
454 |
> |
register int b, |
455 |
> |
register int g, |
456 |
> |
register int r |
457 |
> |
) |
458 |
|
{ |
459 |
|
register int i,dist,a,biasdist,betafreq; |
460 |
|
int bestpos,bestbiaspos,bestd,bestbiasd; |
489 |
|
|
490 |
|
/* move neuron i towards (b,g,r) by factor alpha */ |
491 |
|
|
492 |
< |
altersingle(alpha,i,b,g,r) /* accepts biased BGR values */ |
493 |
< |
register int alpha,i,b,g,r; |
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 |
|
|
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 |
< |
alterneigh(rad,i,b,g,r) /* accents biased BGR values */ |
516 |
< |
int rad,i; |
517 |
< |
register int b,g,r; |
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; |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
< |
learn() |
557 |
> |
static void |
558 |
> |
learn(void) |
559 |
|
{ |
560 |
|
register int i,j,b,g,r; |
561 |
|
int radius,rad,alpha,step,delta,samplepixels; |
613 |
|
/* which can then be used for colour map */ |
614 |
|
/* and record position i to prepare for sort */ |
615 |
|
|
616 |
< |
unbiasnet() |
616 |
> |
static void |
617 |
> |
unbiasnet(void) |
618 |
|
{ |
619 |
|
int i,j; |
620 |
|
|
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 |
|
|