xref: /haiku/headers/private/shared/ColorQuantizer.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /* === C R E D I T S  &  D I S C L A I M E R S ==============
2  * Permission is given by the author to freely redistribute and include
3  * this code in any program as long as this credit is given where due.
4  *
5  * CQuantizer (c)  1996-1997 Jeff Prosise
6  *
7  * 31/08/2003 Davide Pizzolato - www.xdp.it
8  * - fixed minor bug in ProcessImage when bpp<=8
9  * - better color reduction to less than 16 colors
10  *
11  * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT
12  * WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT
13  * LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF DEFECTS,
14  * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
15  * RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU.
16  * SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL
17  * DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
18  * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN
19  * ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
20  * HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
21  *
22  * Use at your own risk!
23  * ==========================================================
24  *
25  * Modified for use with Haiku by David Powell & Stephan Aßmus.
26  */
27 #ifndef COLOR_QUANTIZER_H
28 #define COLOR_QUANTIZER_H
29 
30 
31 #include <SupportDefs.h>
32 
33 
34 namespace BPrivate {
35 
36 typedef struct _RGBA {
37 	uint8	b;
38 	uint8	g;
39 	uint8	r;
40 	uint8	a;
41 } RGBA;
42 
43 class BColorQuantizer {
44 public:
45 							BColorQuantizer(uint32 maxColors,
46 								uint32 bitsPerColor);
47 	virtual					~BColorQuantizer();
48 
49 			bool			ProcessImage(const uint8* const * rowPtrs, int width,
50 								int height);
51 
52 			uint32			GetColorCount() const;
53 			void			GetColorTable(RGBA* table) const;
54 
55 private:
56 			struct Node;
57 
58 private:
59 			void			_AddColor(Node** _node, uint8 r, uint8 g, uint8 b,
60 								uint8 a, uint32 bitsPerColor, uint32 level,
61 								uint32* _leafCount, Node** reducibleNodes);
62 			Node*			_CreateNode(uint32 level, uint32 bitsPerColor,
63 								uint32* _leafCount, Node** reducibleNodes);
64 			void		    _ReduceTree(uint32 bitsPerColor, uint32* _leafCount,
65 								Node** reducibleNodes);
66 			void		    _DeleteTree(Node** _node);
67 
68 			void		    _GetPaletteColors(Node* node, RGBA* table,
69 								uint32* pIndex, uint32* pSum) const;
70 
71 private:
72 			Node*			fTree;
73 			uint32			fLeafCount;
74 			Node*			fReducibleNodes[9];
75 			uint32			fMaxColors;
76 			uint32			fOutputMaxColors;
77 			uint32			fBitsPerColor;
78 };
79 
80 }	// namespace BPrivate
81 
82 using BPrivate::BColorQuantizer;
83 using BPrivate::RGBA;
84 
85 #endif // COLOR_QUANTIZER_H
86