Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <limits.h>
21 :
22 : #include <vcl/bmpacc.hxx>
23 :
24 : #include <impoct.hxx>
25 :
26 : #include "octree.hxx"
27 :
28 : // - pMask -
29 :
30 : static const sal_uInt8 pImplMask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
31 :
32 : // - NodeCache -
33 :
34 0 : ImpNodeCache::ImpNodeCache( const sal_uLong nInitSize ) :
35 0 : pActNode( NULL )
36 : {
37 0 : const sal_uLong nSize = nInitSize + 4;
38 :
39 0 : for( sal_uLong i = 0; i < nSize; i++ )
40 : {
41 0 : OctreeNode* pNewNode = new NODE;
42 :
43 0 : pNewNode->pNextInCache = pActNode;
44 0 : pActNode = pNewNode;
45 : }
46 0 : }
47 :
48 0 : ImpNodeCache::~ImpNodeCache()
49 : {
50 0 : while( pActNode )
51 : {
52 0 : OctreeNode* pNode = pActNode;
53 :
54 0 : pActNode = pNode->pNextInCache;
55 0 : delete pNode;
56 : }
57 0 : }
58 :
59 : // - Octree -
60 :
61 0 : Octree::Octree( const BitmapReadAccess& rReadAcc, sal_uLong nColors ) :
62 : nMax ( nColors ),
63 : nLeafCount ( 0L ),
64 : pTree ( NULL ),
65 0 : pAcc ( &rReadAcc )
66 : {
67 0 : pNodeCache = new ImpNodeCache( nColors );
68 0 : memset( pReduce, 0, ( OCTREE_BITS + 1 ) * sizeof( PNODE ) );
69 0 : ImplCreateOctree();
70 0 : }
71 :
72 0 : Octree::~Octree()
73 : {
74 0 : ImplDeleteOctree( &pTree );
75 0 : delete pNodeCache;
76 0 : }
77 :
78 0 : void Octree::ImplCreateOctree()
79 : {
80 0 : if( !!*pAcc )
81 : {
82 0 : const long nWidth = pAcc->Width();
83 0 : const long nHeight = pAcc->Height();
84 :
85 0 : if( pAcc->HasPalette() )
86 : {
87 0 : for( long nY = 0; nY < nHeight; nY++ )
88 : {
89 0 : for( long nX = 0; nX < nWidth; nX++ )
90 : {
91 0 : pColor = &(BitmapColor&) pAcc->GetPaletteColor( pAcc->GetPixelIndex( nY, nX ) );
92 0 : nLevel = 0L;
93 0 : ImplAdd( &pTree );
94 :
95 0 : while( nLeafCount > nMax )
96 0 : ImplReduce();
97 : }
98 : }
99 : }
100 : else
101 : {
102 0 : BitmapColor aColor;
103 :
104 0 : pColor = &aColor;
105 :
106 0 : for( long nY = 0; nY < nHeight; nY++ )
107 : {
108 0 : for( long nX = 0; nX < nWidth; nX++ )
109 : {
110 0 : aColor = pAcc->GetPixel( nY, nX );
111 0 : nLevel = 0L;
112 0 : ImplAdd( &pTree );
113 :
114 0 : while( nLeafCount > nMax )
115 0 : ImplReduce();
116 : }
117 0 : }
118 : }
119 : }
120 0 : }
121 :
122 0 : void Octree::ImplDeleteOctree( PPNODE ppNode )
123 : {
124 0 : for ( sal_uLong i = 0UL; i < 8UL; i++ )
125 : {
126 0 : if ( (*ppNode)->pChild[ i ] )
127 0 : ImplDeleteOctree( &(*ppNode)->pChild[ i ] );
128 : }
129 :
130 0 : pNodeCache->ImplReleaseNode( *ppNode );
131 0 : *ppNode = NULL;
132 0 : }
133 :
134 0 : void Octree::ImplAdd( PPNODE ppNode )
135 : {
136 : // ggf. neuen Knoten erzeugen
137 0 : if( !*ppNode )
138 : {
139 0 : *ppNode = pNodeCache->ImplGetFreeNode();
140 0 : (*ppNode)->bLeaf = ( OCTREE_BITS == nLevel );
141 :
142 0 : if( (*ppNode)->bLeaf )
143 0 : nLeafCount++;
144 : else
145 : {
146 0 : (*ppNode)->pNext = pReduce[ nLevel ];
147 0 : pReduce[ nLevel ] = *ppNode;
148 : }
149 : }
150 :
151 0 : if( (*ppNode)->bLeaf )
152 : {
153 0 : (*ppNode)->nCount++;
154 0 : (*ppNode)->nRed += pColor->GetRed();
155 0 : (*ppNode)->nGreen += pColor->GetGreen();
156 0 : (*ppNode)->nBlue += pColor->GetBlue();
157 : }
158 : else
159 : {
160 0 : const sal_uLong nShift = 7 - nLevel;
161 0 : const sal_uInt8 cMask = pImplMask[ nLevel ];
162 0 : const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
163 0 : ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
164 0 : ( ( pColor->GetBlue() & cMask ) >> nShift );
165 :
166 0 : nLevel++;
167 0 : ImplAdd( &(*ppNode)->pChild[ nIndex ] );
168 : }
169 0 : }
170 :
171 0 : void Octree::ImplReduce()
172 : {
173 : sal_uLong i;
174 : PNODE pNode;
175 0 : sal_uLong nRedSum = 0L;
176 0 : sal_uLong nGreenSum = 0L;
177 0 : sal_uLong nBlueSum = 0L;
178 0 : sal_uLong nChildren = 0L;
179 :
180 0 : for ( i = OCTREE_BITS - 1; i && !pReduce[i]; i-- ) {}
181 :
182 0 : pNode = pReduce[ i ];
183 0 : pReduce[ i ] = pNode->pNext;
184 :
185 0 : for ( i = 0; i < 8; i++ )
186 : {
187 0 : if ( pNode->pChild[ i ] )
188 : {
189 0 : PNODE pChild = pNode->pChild[ i ];
190 :
191 0 : nRedSum += pChild->nRed;
192 0 : nGreenSum += pChild->nGreen;
193 0 : nBlueSum += pChild->nBlue;
194 0 : pNode->nCount += pChild->nCount;
195 :
196 0 : pNodeCache->ImplReleaseNode( pNode->pChild[ i ] );
197 0 : pNode->pChild[ i ] = NULL;
198 0 : nChildren++;
199 : }
200 : }
201 :
202 0 : pNode->bLeaf = true;
203 0 : pNode->nRed = nRedSum;
204 0 : pNode->nGreen = nGreenSum;
205 0 : pNode->nBlue = nBlueSum;
206 0 : nLeafCount -= --nChildren;
207 0 : }
208 :
209 0 : void Octree::CreatePalette( PNODE pNode )
210 : {
211 0 : if( pNode->bLeaf )
212 : {
213 0 : pNode->nPalIndex = nPalIndex;
214 0 : aPal[ nPalIndex++ ] = BitmapColor( (sal_uInt8) ( (double) pNode->nRed / pNode->nCount ),
215 0 : (sal_uInt8) ( (double) pNode->nGreen / pNode->nCount ),
216 0 : (sal_uInt8) ( (double) pNode->nBlue / pNode->nCount ) );
217 : }
218 0 : else for( sal_uLong i = 0UL; i < 8UL; i++ )
219 0 : if( pNode->pChild[ i ] )
220 0 : CreatePalette( pNode->pChild[ i ] );
221 :
222 0 : }
223 :
224 0 : void Octree::GetPalIndex( PNODE pNode )
225 : {
226 0 : if ( pNode->bLeaf )
227 0 : nPalIndex = pNode->nPalIndex;
228 : else
229 : {
230 0 : const sal_uLong nShift = 7 - nLevel;
231 0 : const sal_uInt8 cMask = pImplMask[ nLevel++ ];
232 0 : const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
233 0 : ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
234 0 : ( ( pColor->GetBlue() & cMask ) >> nShift );
235 :
236 0 : GetPalIndex( pNode->pChild[ nIndex ] );
237 : }
238 0 : }
239 :
240 : // - InverseColorMap -
241 :
242 0 : InverseColorMap::InverseColorMap( const BitmapPalette& rPal ) :
243 0 : nBits( 8 - OCTREE_BITS )
244 : {
245 : sal_uLong* cdp;
246 : sal_uInt8* crgbp;
247 0 : const sal_uLong nColorMax = 1 << OCTREE_BITS;
248 0 : const sal_uLong xsqr = 1 << ( nBits << 1 );
249 0 : const sal_uLong xsqr2 = xsqr << 1;
250 0 : const sal_uLong nColors = rPal.GetEntryCount();
251 0 : const long x = 1L << nBits;
252 0 : const long x2 = x >> 1L;
253 : sal_uLong r, g, b;
254 : long rxx, gxx, bxx;
255 : long rdist, gdist, bdist;
256 : long crinc, cginc, cbinc;
257 :
258 0 : ImplCreateBuffers( nColorMax );
259 :
260 0 : for( sal_uLong nIndex = 0; nIndex < nColors; nIndex++ )
261 : {
262 0 : const BitmapColor& rColor = rPal[ (sal_uInt16) nIndex ];
263 0 : const sal_uInt8 cRed = rColor.GetRed();
264 0 : const sal_uInt8 cGreen = rColor.GetGreen();
265 0 : const sal_uInt8 cBlue = rColor.GetBlue();
266 :
267 0 : rdist = cRed - x2;
268 0 : gdist = cGreen - x2;
269 0 : bdist = cBlue - x2;
270 0 : rdist = rdist*rdist + gdist*gdist + bdist*bdist;
271 :
272 0 : crinc = ( xsqr - ( cRed << nBits ) ) << 1L;
273 0 : cginc = ( xsqr - ( cGreen << nBits ) ) << 1L;
274 0 : cbinc = ( xsqr - ( cBlue << nBits ) ) << 1L;
275 :
276 0 : cdp = (sal_uLong*) pBuffer;
277 0 : crgbp = pMap;
278 :
279 0 : for( r = 0, rxx = crinc; r < nColorMax; rdist += rxx, r++, rxx += xsqr2 )
280 : {
281 0 : for( g = 0, gdist = rdist, gxx = cginc; g < nColorMax; gdist += gxx, g++, gxx += xsqr2 )
282 : {
283 0 : for( b = 0, bdist = gdist, bxx = cbinc; b < nColorMax; bdist += bxx, b++, cdp++, crgbp++, bxx += xsqr2 )
284 0 : if ( !nIndex || ( (long) *cdp ) > bdist )
285 : {
286 0 : *cdp = bdist;
287 0 : *crgbp = (sal_uInt8) nIndex;
288 : }
289 : }
290 : }
291 : }
292 0 : }
293 :
294 0 : InverseColorMap::~InverseColorMap()
295 : {
296 0 : rtl_freeMemory( pBuffer );
297 0 : rtl_freeMemory( pMap );
298 0 : }
299 :
300 0 : void InverseColorMap::ImplCreateBuffers( const sal_uLong nMax )
301 : {
302 0 : const sal_uLong nCount = nMax * nMax * nMax;
303 0 : const sal_uLong nSize = nCount * sizeof( sal_uLong );
304 :
305 0 : pMap = (sal_uInt8*) rtl_allocateMemory( nCount );
306 0 : memset( pMap, 0x00, nCount );
307 :
308 0 : pBuffer = (sal_uInt8*) rtl_allocateMemory( nSize );
309 0 : memset( pBuffer, 0xff, nSize );
310 0 : }
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|