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.7 by greg, Tue Nov 22 12:19:21 1994 UTC vs.
Revision 2.13 by greg, Sat Sep 8 19:17:52 2007 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 28 | Line 28 | extern BYTE    clrtab[256][3];
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  
# Line 51 | Line 47 | static long    skipcount;
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;
# Line 90 | Line 98 | long   npixels;
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);
# Line 103 | Line 113 | register BYTE  col[];
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;
# Line 121 | Line 133 | register int   n;
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;
# Line 132 | Line 146 | int    ncolors;
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 */
# Line 140 | Line 154 | int    ncolors;
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]);
# Line 160 | Line 177 | register int   n;
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;
# Line 172 | Line 191 | int    n;
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)
# Line 183 | Line 202 | int    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++) {
# Line 214 | Line 233 | int    n;
233   #define lengthcount     (nsamples*3)
234   #define samplefac       1
235  
236 < /*----------------------------------------------------------------------*/
237 < /*                                                                      */
238 < /*                              NeuQuant                                */
239 < /*                              --------                                */
240 < /*                                                                      */
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       */
245 < /* better results than existing methods and runs faster, using minimal  */
246 < /* space (8kB plus the image itself).  The algorithm is described in    */
247 < /* the paper "Kohonen Neural Networks for Optimal Colour Quantization"  */
248 < /* to appear in the journal "Network: Computation in Neural Systems".   */
249 < /* It is a significant improvement of an earlier algorithm.             */
250 < /*                                                                      */
251 < /* This program is distributed free for academic use or for evaluation  */
252 < /* by commercial organizations.                                         */
253 < /*                                                                      */
254 < /*      Usage:  NeuQuant -n inputfile > outputfile                      */
255 < /*                                                                      */
237 < /* where n is a sampling factor for neural learning.                    */
238 < /*                                                                      */
239 < /* Program performance compared with other methods is as follows:       */
240 < /*                                                                      */
241 < /*      Algorithm               |  Av. CPU Time |  Quantization Error   */
242 < /*      -------------------------------------------------------------   */
243 < /*      NeuQuant -3             |  314          |  5.55                 */
244 < /*      NeuQuant -10            |  119          |  5.97                 */
245 < /*      NeuQuant -30            |  65           |  6.53                 */
246 < /*      Oct-Trees               |  141          |  8.96                 */
247 < /*      Median Cut (XV -best)   |  420          |  9.28                 */
248 < /*      Median Cut (XV -slow)   |  72           |  12.15                */
249 < /*                                                                      */
250 < /* Author's address:    Dept of ISCS, National University of Singapore  */
251 < /*                      Kent Ridge, Singapore 0511                      */
252 < /* Email:       [email protected]                                     */
253 < /*----------------------------------------------------------------------*/
236 > /* NeuQuant Neural-Net Quantization Algorithm Interface
237 > * ----------------------------------------------------
238 > *
239 > * Copyright (c) 1994 Anthony Dekker
240 > *
241 > * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994.
242 > * See "Kohonen neural networks for optimal colour quantization"
243 > * in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
244 > * for a discussion of the algorithm.
245 > * See also  http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
246 > *
247 > * Any party obtaining a copy of these files from the author, directly or
248 > * indirectly, is granted, free of charge, a full and unrestricted irrevocable,
249 > * world-wide, paid up, royalty-free, nonexclusive right and license to deal
250 > * in this software and documentation files (the "Software"), including without
251 > * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
252 > * and/or sell copies of the Software, and to permit persons who receive
253 > * copies from any such party to do so, with the only requirement being
254 > * that this copyright notice remain intact.
255 > */
256  
257   #define bool            int
258   #define false           0
# Line 308 | Line 310 | int radpower[initrad]; /* radpower for precomputation
310  
311   /* initialise network in range (0,0,0) to (255,255,255) */
312  
313 < initnet()      
313 > static void
314 > initnet(void)  
315   {
316          register int i;
317          register int *p;
# Line 324 | Line 327 | initnet()      
327  
328   /* do after unbias - insertion sort of network and build netindex[0..255] */
329  
330 < inxbuild()
330 > static void
331 > inxbuild(void)
332   {
333          register int i,j,smallpos,smallval;
334          register int *p,*q;
# Line 365 | Line 369 | inxbuild()
369   }
370  
371  
372 < int inxsearch(b,g,r)  /* accepts real BGR values after net is unbiased */
373 < register int b,g,r;
372 > static int
373 > inxsearch(  /* accepts real BGR values after net is unbiased */
374 >        register int b,
375 >        register int g,
376 >        register int r
377 > )
378   {
379          register int i,j,dist,a,bestd;
380          register int *p;
# Line 420 | Line 428 | register int b,g,r;
428   /* for frequently chosen neurons, freq[i] is high and bias[i] is negative */
429   /* bias[i] = gamma*((1/netsize)-freq[i]) */
430  
431 < int contest(b,g,r)      /* accepts biased BGR values */
432 < register int b,g,r;
431 > static int
432 > contest(        /* accepts biased BGR values */
433 >        register int b,
434 >        register int g,
435 >        register int r
436 > )
437   {
438          register int i,dist,a,biasdist,betafreq;
439          int bestpos,bestbiaspos,bestd,bestbiasd;
# Line 456 | Line 468 | register int b,g,r;
468  
469   /* move neuron i towards (b,g,r) by factor alpha */
470  
471 < altersingle(alpha,i,b,g,r)      /* accepts biased BGR values */
472 < register int alpha,i,b,g,r;
471 > static void
472 > altersingle(    /* accepts biased BGR values */
473 >        register int alpha,
474 >        register int i,
475 >        register int b,
476 >        register int g,
477 >        register int r
478 > )
479   {
480          register int *n;
481  
# Line 473 | Line 491 | register int alpha,i,b,g,r;
491   /* move neurons adjacent to i towards (b,g,r) by factor */
492   /* alpha*(1-((i-j)^2/[r]^2)) precomputed as radpower[|i-j|]*/
493  
494 < alterneigh(rad,i,b,g,r) /* accents biased BGR values */
495 < int rad,i;
496 < register int b,g,r;
494 > static void
495 > alterneigh(     /* accents biased BGR values */
496 >        int rad,
497 >        int i,
498 >        register int b,
499 >        register int g,
500 >        register int r
501 > )
502   {
503          register int j,k,lo,hi,a;
504          register int *p, *q;
# Line 510 | Line 533 | register int b,g,r;
533   }
534  
535  
536 < learn()
536 > static void
537 > learn(void)
538   {
539          register int i,j,b,g,r;
540          int radius,rad,alpha,step,delta,samplepixels;
# Line 568 | Line 592 | learn()
592   /* which can then be used for colour map */
593   /* and record position i to prepare for sort */
594  
595 < unbiasnet()
595 > static void
596 > unbiasnet(void)
597   {
598          int i,j;
599  
# Line 582 | Line 607 | unbiasnet()
607  
608   /* Don't do this until the network has been unbiased (GW) */
609                  
610 < static
611 < cpyclrtab()
610 > static void
611 > cpyclrtab(void)
612   {
613          register int i,j,k;
614          

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines