1 /************************************************************************/
2 /*									*/
3 /*  Color allocation, mainly for bitmap images.				*/
4 /*									*/
5 /************************************************************************/
6 
7 #   ifndef	BMCOLOR_H
8 #   define	BMCOLOR_H
9 
10 #   include	<utilColor.h>
11 
12 /************************************************************************/
13 /*									*/
14 /*  For color approximation, anti aliasing and dithering		*/
15 /*									*/
16 /************************************************************************/
17 
18 typedef struct AllocatorColor
19     {
20     RGB16Color		acColorValues;
21     unsigned long	acColorNumber;
22     void *		acColorPrivate;
23     unsigned char	acAllocated;
24 #			define		AC_UNALLOCATED		0
25 #			define		AC_ALLOCATED		1
26 #			define		AC_CALCULATED		2
27 #			define		AC_COPIED		3
28     } AllocatorColor;
29 
30 #   define acRed	acColorValues.rgb16Red
31 #   define acGreen	acColorValues.rgb16Green
32 #   define acBlue	acColorValues.rgb16Blue
33 
34 struct ColorAllocator;
35 
36 typedef int (*SystemAllocator)(		AllocatorColor *	ac,
37 					struct ColorAllocator *	ca,
38 					unsigned int		r,
39 					unsigned int		g,
40 					unsigned int		b );
41 
42 typedef void (*SystemCleanup)(		struct ColorAllocator *	ca );
43 
44 typedef struct ColorAllocator
45     {
46     AllocatorColor	ca222Colors[64];
47     AllocatorColor *	caColors;
48     int			caColorCount;
49     int			caDepth;
50     unsigned int	caRedApproxShift;
51     unsigned int	caGreenApproxShift;
52     unsigned int	caBlueApproxShift;
53     unsigned int	caRedPixelShift;
54     unsigned int	caGreenPixelShift;
55     unsigned int	caBluePixelShift;
56     unsigned int	caRedMask;
57     unsigned int	caGreenMask;
58     unsigned int	caBlueMask;
59 
60     AllocatorColor	caColorLastAllocated;
61 
62     int			caAllocationType;
63 #			define		CA_UNKNOWN		0
64 #			define		CA_INDEX_ALLOC		1
65 #			define		CA_CALCULATED		2
66 #			define		CA_ALLOCATOR		3
67     void *		caSystemPrivate;
68     SystemAllocator	caSystemAllocator;
69     SystemCleanup	caSystemCleanup;
70 
71     } ColorAllocator;
72 
73 /************************************************************************/
74 /*									*/
75 /*  Macros for indexing colors in an array of colors, usually in an	*/
76 /*  ColorAllocator structure.						*/
77 /*									*/
78 /************************************************************************/
79 
80 #   define	C222(r,g,b)	( ( ( (r) >> 2 ) & 0x30 ) | \
81 				  ( ( (g) >> 4 ) & 0x0c ) | \
82 				  ( ( (b) >> 6 ) & 0x03 ) )
83 
84 #   define bmColorRgbDirect( ac, ca, r, g, b )			\
85     {								\
86     unsigned int	rr= (r) >> (ca)->caRedApproxShift;	\
87     unsigned int	gg= (g) >> (ca)->caGreenApproxShift;	\
88     unsigned int	bb= (b) >> (ca)->caBlueApproxShift;	\
89     \
90     (ac)->acRed=   ( 65535* rr )/ (ca)->caRedMask;		\
91     (ac)->acGreen= ( 65535* gg )/ (ca)->caGreenMask;		\
92     (ac)->acBlue=  ( 65535* bb )/ (ca)->caBlueMask;		\
93     \
94     (ac)->acColorNumber= ( rr << (ca)->caRedPixelShift ) +	\
95 		 ( gg << (ca)->caGreenPixelShift )	+	\
96 		 ( bb << (ca)->caBluePixelShift )	;	\
97     }
98 
99 
100 /************************************************************************/
101 /*									*/
102 /*  Routine declarations:						*/
103 /*									*/
104 /************************************************************************/
105 
106 extern void bmSetCalculatedShifts(	ColorAllocator *	ca,
107 					unsigned long		redMask,
108 					unsigned long		greenMask,
109 					unsigned long		blueMask );
110 
111 extern void bmInitColorAllocator(	ColorAllocator *	ca );
112 extern void bmCleanColorAllocator(	ColorAllocator *	ca );
113 
114 extern int bmFindNearestColorRgb(	AllocatorColor *	acRet,
115 					const ColorAllocator *	ca,
116 					unsigned int		r,
117 					unsigned int		g,
118 					unsigned int		b );
119 
120 extern int bmAllocateAllocatorColors(	ColorAllocator *	ca,
121 					int			count );
122 
123 #   endif	/*  BMCOLOR_H  */
124