PageRenderTime 37ms CodeModel.GetById 20ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/src/FreeImage/Source/Quantizers.h

https://bitbucket.org/cabalistic/ogredeps/
C++ Header | 225 lines | 82 code | 39 blank | 104 comment | 0 complexity | b9a30f2fa339c35009d4f3c8b91a66b1 MD5 | raw file
  1// =============================================================
  2// Quantizer objects and functions
  3//
  4// Design and implementation by:
  5// - Hervé Drolon <drolon@infonie.fr>
  6//
  7// This file is part of FreeImage 3
  8//
  9// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
 10// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 11// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
 12// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
 13// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
 14// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
 15// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
 16// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 17// THIS DISCLAIMER.
 18//
 19// Use at your own risk!
 20// =============================================================
 21
 22// 
 23////////////////////////////////////////////////////////////////
 24
 25#include "FreeImage.h"
 26
 27////////////////////////////////////////////////////////////////
 28
 29/**
 30  Xiaolin Wu color quantization algorithm
 31*/
 32class WuQuantizer
 33{
 34public:
 35
 36typedef struct tagBox {
 37    int r0;			 // min value, exclusive
 38    int r1;			 // max value, inclusive
 39    int g0;  
 40    int g1;  
 41    int b0;  
 42    int b1;
 43    int vol;
 44} Box;
 45
 46protected:
 47    float *gm2;
 48	LONG *wt, *mr, *mg, *mb;
 49	WORD *Qadd;
 50
 51	// DIB data
 52	unsigned width, height;
 53	unsigned pitch;
 54	FIBITMAP *m_dib;
 55
 56protected:
 57    void Hist3D(LONG *vwt, LONG *vmr, LONG *vmg, LONG *vmb, float *m2, int ReserveSize, RGBQUAD *ReservePalette);
 58	void M3D(LONG *vwt, LONG *vmr, LONG *vmg, LONG *vmb, float *m2);
 59	LONG Vol(Box *cube, LONG *mmt);
 60	LONG Bottom(Box *cube, BYTE dir, LONG *mmt);
 61	LONG Top(Box *cube, BYTE dir, int pos, LONG *mmt);
 62	float Var(Box *cube);
 63	float Maximize(Box *cube, BYTE dir, int first, int last , int *cut,
 64				   LONG whole_r, LONG whole_g, LONG whole_b, LONG whole_w);
 65	bool Cut(Box *set1, Box *set2);
 66	void Mark(Box *cube, int label, BYTE *tag);
 67
 68public:
 69	// Constructor - Input parameter: DIB 24-bit to be quantized
 70    WuQuantizer(FIBITMAP *dib);
 71	// Destructor
 72	~WuQuantizer();
 73	// Quantizer - Return value: quantized 8-bit (color palette) DIB
 74	FIBITMAP* Quantize(int PaletteSize, int ReserveSize, RGBQUAD *ReservePalette);
 75};
 76
 77
 78/**
 79  NEUQUANT Neural-Net quantization algorithm by Anthony Dekker
 80*/
 81
 82// ----------------------------------------------------------------
 83// Constant definitions
 84// ----------------------------------------------------------------
 85
 86/** number of colours used: 
 87	for 256 colours, fixed arrays need 8kb, plus space for the image
 88*/
 89//static const int netsize = 256;
 90
 91/**@name network definitions */
 92//@{
 93//static const int maxnetpos = (netsize - 1);
 94/// bias for colour values
 95static const int netbiasshift = 4;
 96/// no. of learning cycles
 97static const int ncycles = 100;
 98//@}
 99
100/**@name defs for freq and bias */
101//@{
102/// bias for fractions
103static const int intbiasshift = 16;
104static const int intbias = (((int)1) << intbiasshift);
105/// gamma = 1024
106static const int gammashift = 10;
107// static const int gamma = (((int)1) << gammashift);
108/// beta = 1 / 1024
109static const int betashift = 10;
110static const int beta = (intbias >> betashift);
111static const int betagamma = (intbias << (gammashift-betashift));
112//@}
113
114/**@name defs for decreasing radius factor */
115//@{
116/// for 256 cols, radius starts
117//static const int initrad = (netsize >> 3);
118/// at 32.0 biased by 6 bits
119static const int radiusbiasshift = 6;
120static const int radiusbias = (((int)1) << radiusbiasshift);
121/// and decreases by a 
122//static const int initradius	= (initrad * radiusbias);
123// factor of 1/30 each cycle
124static const int radiusdec = 30;
125//@}
126
127/**@name defs for decreasing alpha factor */
128//@{
129/// alpha starts at 1.0
130static const int alphabiasshift = 10;
131static const int initalpha = (((int)1) << alphabiasshift);
132//@}
133
134/**@name radbias and alpharadbias used for radpower calculation */
135//@{
136static const int radbiasshift = 8;
137static const int radbias = (((int)1) << radbiasshift);
138static const int alpharadbshift = (alphabiasshift+radbiasshift);
139static const int alpharadbias = (((int)1) << alpharadbshift);	
140//@}
141
142class NNQuantizer
143{
144protected:
145	/**@name image parameters */
146	//@{
147	/// pointer to input dib
148	FIBITMAP *dib_ptr;
149	/// image width
150	int img_width;
151	/// image height
152	int img_height;
153	/// image line length
154	int img_line;
155	//@}
156
157	/**@name network parameters */
158	//@{
159
160	int netsize, maxnetpos, initrad, initradius;
161
162	/// BGRc
163	typedef int pixel[4];
164	/// the network itself
165	pixel *network;
166
167	/// for network lookup - really 256
168	int netindex[256];
169
170	/// bias array for learning
171	int *bias;
172	/// freq array for learning
173	int *freq;
174	/// radpower for precomputation
175	int *radpower;
176	//@}
177
178protected:
179	/// Initialise network in range (0,0,0) to (255,255,255) and set parameters
180	void initnet();	
181
182	/// Unbias network to give byte values 0..255 and record position i to prepare for sort
183	void unbiasnet();
184
185	/// Insertion sort of network and building of netindex[0..255] (to do after unbias)
186	void inxbuild();
187
188	/// Search for BGR values 0..255 (after net is unbiased) and return colour index
189	int inxsearch(int b, int g, int r);
190
191	/// Search for biased BGR values
192	int contest(int b, int g, int r);
193	
194	/// Move neuron i towards biased (b,g,r) by factor alpha
195	void altersingle(int alpha, int i, int b, int g, int r);
196
197	/// Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in radpower[|i-j|]
198	void alterneigh(int rad, int i, int b, int g, int r);
199
200	/** Main Learning Loop
201	@param sampling_factor sampling factor in [1..30]
202	*/
203	void learn(int sampling_factor);
204
205	/// Get a pixel sample at position pos. Handle 4-byte boundary alignment.
206	void getSample(long pos, int *b, int *g, int *r);
207
208
209public:
210	/// Constructor
211	NNQuantizer(int PaletteSize);
212
213	/// Destructor
214	~NNQuantizer();
215
216	/** Quantizer
217	@param dib input 24-bit dib to be quantized
218	@param sampling a sampling factor in range 1..30. 
219	1 => slower (but better), 30 => faster. Default value is 1
220	@return returns the quantized 8-bit (color palette) DIB
221	*/
222	FIBITMAP* Quantize(FIBITMAP *dib, int ReserveSize, RGBQUAD *ReservePalette, int sampling = 1);
223
224};
225