xref: /aoo42x/main/vcl/inc/vcl/octree.hxx (revision 0d63794c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_OCTREE_HXX
25 #define _SV_OCTREE_HXX
26 
27 #include <vcl/salbtype.hxx>
28 #include <vcl/dllapi.h>
29 
30 // -----------
31 // - Defines -
32 // -----------
33 
34 #define OCTREE_BITS		5
35 #define OCTREE_BITS_1	10
36 
37 // --------------
38 // - OctreeNode -
39 // --------------
40 
41 typedef struct OctreeNode
42 {
43 	sal_uLong		nCount;
44 	sal_uLong		nRed;
45 	sal_uLong		nGreen;
46 	sal_uLong		nBlue;
47 	OctreeNode* pChild[ 8 ];
48 	OctreeNode*	pNext;
49 	OctreeNode*	pNextInCache;
50 	sal_uInt16		nPalIndex;
51 	sal_Bool		bLeaf;
52 } NODE;
53 
54 typedef NODE*		PNODE;
55 typedef PNODE*		PPNODE;
56 
57 // ----------
58 // - Octree -
59 // ----------
60 
61 class ImpNodeCache;
62 class BitmapReadAccess;
63 
64 class VCL_PLUGIN_PUBLIC Octree
65 {
66 private:
67 
68 	BitmapPalette				aPal;
69 	sal_uLong						nMax;
70 	sal_uLong						nLeafCount;
71 	sal_uLong						nLevel;
72 	PNODE						pTree;
73 	PNODE						pReduce[ OCTREE_BITS + 1 ];
74 	BitmapColor*				pColor;
75 	ImpNodeCache*				pNodeCache;
76 	const BitmapReadAccess*		pAcc;
77 	sal_uInt16						nPalIndex;
78 
Octree()79 								Octree() {};
80 
81 	void						CreatePalette( PNODE pNode );
82 	void						GetPalIndex( PNODE pNode );
83 
84 	SAL_DLLPRIVATE void         ImplCreateOctree();
85 	SAL_DLLPRIVATE void			ImplDeleteOctree( PPNODE ppNode );
86 	SAL_DLLPRIVATE void			ImplAdd( PPNODE ppNode );
87 	SAL_DLLPRIVATE void			ImplReduce();
88 
89 public:
90 
91 								Octree( const BitmapReadAccess& rReadAcc, sal_uLong nColors );
92 								Octree( sal_uLong nColors );
93 								~Octree();
94 
95 	void						AddColor( const BitmapColor& rColor );
96 
97 	inline const BitmapPalette&	GetPalette();
98 	inline sal_uInt16				GetBestPaletteIndex( const BitmapColor& rColor );
99 };
100 
101 // ------------------------------------------------------------------------
102 
GetPalette()103 inline const BitmapPalette& Octree::GetPalette()
104 {
105 	aPal.SetEntryCount( (sal_uInt16) nLeafCount );
106 	nPalIndex = 0;
107 	CreatePalette( pTree );
108 	return aPal;
109 }
110 
111 // ------------------------------------------------------------------------
112 
GetBestPaletteIndex(const BitmapColor & rColor)113 inline sal_uInt16 Octree::GetBestPaletteIndex( const BitmapColor& rColor )
114 {
115 	pColor = &(BitmapColor&) rColor;
116 	nPalIndex = 65535;
117 	nLevel = 0L;
118 	GetPalIndex( pTree );
119 	return nPalIndex;
120 }
121 
122 // -------------------
123 // - InverseColorMap -
124 // -------------------
125 
126 class VCL_PLUGIN_PUBLIC InverseColorMap
127 {
128 private:
129 
130 	sal_uInt8*				pBuffer;
131 	sal_uInt8*				pMap;
132 	const sal_uLong			nBits;
133 
134 //#if 0 // _SOLAR__PRIVATE
135 
136 	SAL_DLLPRIVATE void	ImplCreateBuffers( const sal_uLong nMax );
137 
138 //#endif // __PRIVATE
139 
140 public:
141 
142 	explicit			InverseColorMap( const BitmapPalette& rPal );
143 						~InverseColorMap();
144 
145 	inline sal_uInt16		GetBestPaletteIndex( const BitmapColor& rColor );
146 };
147 
148 // ------------------------------------------------------------------------
149 
GetBestPaletteIndex(const BitmapColor & rColor)150 inline sal_uInt16 InverseColorMap::GetBestPaletteIndex( const BitmapColor& rColor )
151 {
152 	return pMap[ ( ( (sal_uLong) rColor.GetRed() >> nBits ) << OCTREE_BITS_1 ) |
153 				 ( ( (sal_uLong) rColor.GetGreen() >> nBits ) << OCTREE_BITS ) |
154 				 ( (sal_uLong) rColor.GetBlue() >> nBits ) ];
155 }
156 
157 #endif // _SV_OCTREE_HXX
158