ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/neuclrtab.c
(Generate patch)

Comparing ray/src/px/neuclrtab.c (file contents):
Revision 2.6 by greg, Mon Nov 21 15:56:20 1994 UTC vs.
Revision 2.11 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
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
# Line 51 | Line 51 | static long    skipcount;
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;
# Line 90 | Line 102 | long   npixels;
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);
# Line 103 | Line 117 | register BYTE  col[];
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;
# Line 121 | Line 137 | register int   n;
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;
# Line 132 | Line 150 | int    ncolors;
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 */
# Line 140 | Line 158 | int    ncolors;
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]);
# Line 160 | Line 181 | register int   n;
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;
# Line 172 | Line 195 | int    n;
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)
# Line 183 | Line 206 | int    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++) {
# Line 257 | Line 280 | int    n;
280   #define true            1
281  
282   /* network defs */
283 < #define netsize         256                     /* number of colours - can change this */
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 */
# Line 272 | Line 295 | int    n;
295   #define betagamma       (intbias<<(gammashift-betashift))
296  
297   /* defs for decreasing radius factor */
298 < #define initrad         (netsize>>3)            /* for 256 cols, radius starts */
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 */
# Line 297 | Line 320 | int alphadec;                                  /* biased by 10 bits */
320   #define prime4          503
321  
322   typedef int pixel[4];  /* BGRc */
323 < pixel network[netsize];
323 > pixel network[256];
324  
325   int netindex[256];      /* for network lookup - really 256 */
326  
327 < int bias [netsize];     /* bias and freq arrays for learning */
328 < int freq [netsize];
327 > int bias [256];         /* bias and freq arrays for learning */
328 > int freq [256];
329   int radpower[initrad];  /* radpower for precomputation */
330  
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;
339          
340 <        for (i=0; i<clrtabsiz; i++) {
340 >        for (i=0; i<netsize; i++) {
341                  p = network[i];
342 <                p[0] = p[1] = p[2] = (i << (netbiasshift+8))/clrtabsiz;
343 <                freq[i] = intbias/clrtabsiz;  /* 1/clrtabsiz */
342 >                p[0] = p[1] = p[2] = (i << (netbiasshift+8))/netsize;
343 >                freq[i] = intbias/netsize;  /* 1/netsize */
344                  bias[i] = 0;
345          }
346   }
# Line 324 | Line 348 | initnet()      
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;
# Line 332 | Line 357 | inxbuild()
357  
358          previouscol = 0;
359          startpos = 0;
360 <        for (i=0; i<clrtabsiz; i++) {
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..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;
# Line 365 | Line 390 | inxbuild()
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;
# Line 377 | Line 406 | register int b,g,r;
406          i = netindex[g]; /* index on g */
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                          dist = p[1] - g;        /* inx key */
413 <                        if (dist >= bestd) i = clrtabsiz; /* stop iter */
413 >                        if (dist >= bestd) i = netsize; /* stop iter */
414                          else {
415                                  i++;
416                                  if (dist<0) dist = -dist;
# Line 418 | Line 447 | register int b,g,r;
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/clrtabsiz)-freq[i]) */
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;
# Line 434 | Line 467 | register int b,g,r;
467          p = bias;
468          f = freq;
469  
470 <        for (i=0; i<clrtabsiz; i++) {
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;
# Line 456 | Line 489 | register int b,g,r;
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  
# Line 473 | Line 512 | register int alpha,i,b,g,r;
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;
526  
527          lo = i-rad;   if (lo<-1) lo= -1;
528 <        hi = i+rad;   if (hi>clrtabsiz) hi=clrtabsiz;
528 >        hi = i+rad;   if (hi>netsize) hi=netsize;
529  
530          j = i+1;
531          k = i-1;
# Line 510 | Line 554 | register int b,g,r;
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;
# Line 568 | Line 613 | learn()
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  
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 */
# Line 582 | Line 628 | unbiasnet()
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];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines