1 /* NeuQuant Neural-Net Quantization Algorithm Interface 2 * ---------------------------------------------------- 3 * 4 * Copyright (c) 1994 Anthony Dekker 5 * 6 * NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. 7 * See "Kohonen neural networks for optimal colour quantization" 8 * in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367. 9 * for a discussion of the algorithm. 10 * See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML 11 * 12 * Any party obtaining a copy of these files from the author, directly or 13 * indirectly, is granted, free of charge, a full and unrestricted irrevocable, 14 * world-wide, paid up, royalty-free, nonexclusive right and license to deal 15 * in this software and documentation files (the "Software"), including without 16 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 17 * and/or sell copies of the Software, and to permit persons who receive 18 * copies from any such party to do so, with the only requirement being 19 * that this copyright notice remain intact. 20 */ 21 22 /* Modified to quantize 32bit RGBA images for the pngnq program. 23 * Also modified to accept a numebr of colors arguement. 24 * Copyright (c) Stuart Coyle 2004-2006 25 */ 26 27 /* 28 * Rewritten by Kornel Lesiński (2009) 29 * Euclidean distance, color matching dependent on alpha channel 30 * and with gamma correction. code refreshed for modern compilers/architectures: 31 * ANSI C, floats, removed pointer tricks and used arrays and structs. 32 */ 33 34 35 #include <stdio.h> 36 #include <string.h> 37 38 39 #define MAXNETSIZE 256 /* maximum number of colours that can be used. 40 actual number is now passed to initcolors */ 41 42 43 /* four primes near 500 - assume no image has a length so large */ 44 /* that it is divisible by all four primes */ 45 #define prime1 499 46 #define prime2 491 47 #define prime3 487 48 #define prime4 503 49 50 #define minpicturebytes (4*prime4) /* minimum size for input image */ 51 52 53 /* Initialise network in range (0,0,0,0) to (255,255,255,255) and set parameters 54 ----------------------------------------------------------------------- */ 55 void initnet(unsigned char *thepic, unsigned int len, unsigned int colours, double gamma); 56 57 /* Unbias network to give byte values 0..255 and record position i to prepare for sort 58 ----------------------------------------------------------------------------------- */ 59 static inline double biasvalue(unsigned int temp); 60 61 /* Output colour map 62 ----------------- */ 63 void getcolormap(unsigned char *map); 64 65 /* Insertion sort of network and building of netindex[0..255] (to do after unbias) 66 ------------------------------------------------------------------------------- */ 67 void inxbuild(); 68 69 /* Search for ABGR values 0..255 (after net is unbiased) and return colour index 70 ---------------------------------------------------------------------------- */ 71 unsigned int inxsearch( int al, int b, int g, int r); 72 unsigned int slowinxsearch( int al, int b, int g, int r); 73 74 /* Main Learning Loop 75 ------------------ */ 76 void learn(unsigned int samplefactor, unsigned int verbose); 77 78 /* Program Skeleton 79 ---------------- 80 [select samplefac in range 1..30] 81 pic = (unsigned char*) malloc(4*width*height); 82 [read image from input file into pic] 83 initnet(pic,4*width*height,samplefac,colors); 84 learn(); 85 unbiasnet(); 86 [write output image header, using writecolourmap(f), 87 possibly editing the loops in that function] 88 inxbuild(); 89 [write output image using inxsearch(a,b,g,r)] */ 90