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 559 : ImpNodeCache::ImpNodeCache( const sal_uLong nInitSize ) :
35 559 : pActNode( NULL )
36 : {
37 559 : const sal_uLong nSize = nInitSize + 4;
38 :
39 145415 : for( sal_uLong i = 0; i < nSize; i++ )
40 : {
41 144856 : OctreeNode* pNewNode = new NODE;
42 :
43 144856 : pNewNode->pNextInCache = pActNode;
44 144856 : pActNode = pNewNode;
45 : }
46 559 : }
47 :
48 559 : ImpNodeCache::~ImpNodeCache()
49 : {
50 188045 : while( pActNode )
51 : {
52 186927 : OctreeNode* pNode = pActNode;
53 :
54 186927 : pActNode = pNode->pNextInCache;
55 186927 : delete pNode;
56 : }
57 559 : }
58 :
59 : // - Octree -
60 :
61 559 : Octree::Octree( const BitmapReadAccess& rReadAcc, sal_uLong nColors ) :
62 : nMax ( nColors ),
63 : nLeafCount ( 0L ),
64 : pTree ( NULL ),
65 559 : pAcc ( &rReadAcc )
66 : {
67 559 : pNodeCache = new ImpNodeCache( nColors );
68 559 : memset( pReduce, 0, ( OCTREE_BITS + 1 ) * sizeof( PNODE ) );
69 559 : ImplCreateOctree();
70 559 : }
71 :
72 1118 : Octree::~Octree()
73 : {
74 559 : ImplDeleteOctree( &pTree );
75 559 : delete pNodeCache;
76 559 : }
77 :
78 559 : void Octree::ImplCreateOctree()
79 : {
80 559 : if( !!*pAcc )
81 : {
82 559 : const long nWidth = pAcc->Width();
83 559 : const long nHeight = pAcc->Height();
84 :
85 559 : if( pAcc->HasPalette() )
86 : {
87 56 : for( long nY = 0; nY < nHeight; nY++ )
88 : {
89 740 : for( long nX = 0; nX < nWidth; nX++ )
90 : {
91 688 : pColor = &(BitmapColor&) pAcc->GetPaletteColor( pAcc->GetPixelIndex( nY, nX ) );
92 688 : nLevel = 0L;
93 688 : ImplAdd( &pTree );
94 :
95 1376 : while( nLeafCount > nMax )
96 0 : ImplReduce();
97 : }
98 : }
99 : }
100 : else
101 : {
102 555 : BitmapColor aColor;
103 :
104 555 : pColor = &aColor;
105 :
106 54272 : for( long nY = 0; nY < nHeight; nY++ )
107 : {
108 7262204 : for( long nX = 0; nX < nWidth; nX++ )
109 : {
110 7208487 : aColor = pAcc->GetPixel( nY, nX );
111 7208487 : nLevel = 0L;
112 7208487 : ImplAdd( &pTree );
113 :
114 14435094 : while( nLeafCount > nMax )
115 18120 : ImplReduce();
116 : }
117 555 : }
118 : }
119 : }
120 559 : }
121 :
122 143020 : void Octree::ImplDeleteOctree( PPNODE ppNode )
123 : {
124 1287180 : for ( sal_uLong i = 0UL; i < 8UL; i++ )
125 : {
126 1144160 : if ( (*ppNode)->pChild[ i ] )
127 142461 : ImplDeleteOctree( &(*ppNode)->pChild[ i ] );
128 : }
129 :
130 143020 : pNodeCache->ImplReleaseNode( *ppNode );
131 143020 : *ppNode = NULL;
132 143020 : }
133 :
134 42071238 : void Octree::ImplAdd( PPNODE ppNode )
135 : {
136 : // ggf. neuen Knoten erzeugen
137 42071238 : if( !*ppNode )
138 : {
139 172370 : *ppNode = pNodeCache->ImplGetFreeNode();
140 172370 : (*ppNode)->bLeaf = ( OCTREE_BITS == nLevel );
141 :
142 172370 : if( (*ppNode)->bLeaf )
143 96658 : nLeafCount++;
144 : else
145 : {
146 75712 : (*ppNode)->pNext = pReduce[ nLevel ];
147 75712 : pReduce[ nLevel ] = *ppNode;
148 : }
149 : }
150 :
151 42071238 : if( (*ppNode)->bLeaf )
152 : {
153 7209175 : (*ppNode)->nCount++;
154 7209175 : (*ppNode)->nRed += pColor->GetRed();
155 7209175 : (*ppNode)->nGreen += pColor->GetGreen();
156 7209175 : (*ppNode)->nBlue += pColor->GetBlue();
157 : }
158 : else
159 : {
160 34862063 : const sal_uLong nShift = 7 - nLevel;
161 34862063 : const sal_uInt8 cMask = pImplMask[ nLevel ];
162 69724126 : const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
163 69724126 : ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
164 69724126 : ( ( pColor->GetBlue() & cMask ) >> nShift );
165 :
166 34862063 : nLevel++;
167 34862063 : ImplAdd( &(*ppNode)->pChild[ nIndex ] );
168 : }
169 42071238 : }
170 :
171 18120 : void Octree::ImplReduce()
172 : {
173 : sal_uLong i;
174 : PNODE pNode;
175 18120 : sal_uLong nRedSum = 0L;
176 18120 : sal_uLong nGreenSum = 0L;
177 18120 : sal_uLong nBlueSum = 0L;
178 18120 : sal_uLong nChildren = 0L;
179 :
180 18120 : for ( i = OCTREE_BITS - 1; i && !pReduce[i]; i-- ) {}
181 :
182 18120 : pNode = pReduce[ i ];
183 18120 : pReduce[ i ] = pNode->pNext;
184 :
185 163080 : for ( i = 0; i < 8; i++ )
186 : {
187 144960 : if ( pNode->pChild[ i ] )
188 : {
189 29350 : PNODE pChild = pNode->pChild[ i ];
190 :
191 29350 : nRedSum += pChild->nRed;
192 29350 : nGreenSum += pChild->nGreen;
193 29350 : nBlueSum += pChild->nBlue;
194 29350 : pNode->nCount += pChild->nCount;
195 :
196 29350 : pNodeCache->ImplReleaseNode( pNode->pChild[ i ] );
197 29350 : pNode->pChild[ i ] = NULL;
198 29350 : nChildren++;
199 : }
200 : }
201 :
202 18120 : pNode->bLeaf = true;
203 18120 : pNode->nRed = nRedSum;
204 18120 : pNode->nGreen = nGreenSum;
205 18120 : pNode->nBlue = nBlueSum;
206 18120 : nLeafCount -= --nChildren;
207 18120 : }
208 :
209 143020 : void Octree::CreatePalette( PNODE pNode )
210 : {
211 143020 : if( pNode->bLeaf )
212 : {
213 85428 : pNode->nPalIndex = nPalIndex;
214 256284 : aPal[ nPalIndex++ ] = BitmapColor( (sal_uInt8) ( (double) pNode->nRed / pNode->nCount ),
215 85428 : (sal_uInt8) ( (double) pNode->nGreen / pNode->nCount ),
216 170856 : (sal_uInt8) ( (double) pNode->nBlue / pNode->nCount ) );
217 : }
218 518328 : else for( sal_uLong i = 0UL; i < 8UL; i++ )
219 460736 : if( pNode->pChild[ i ] )
220 142461 : CreatePalette( pNode->pChild[ i ] );
221 :
222 143020 : }
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 559 : InverseColorMap::InverseColorMap( const BitmapPalette& rPal ) :
243 559 : nBits( 8 - OCTREE_BITS )
244 : {
245 559 : const sal_uLong nColorMax = 1 << OCTREE_BITS;
246 559 : const sal_uLong xsqr = 1 << ( nBits << 1 );
247 559 : const sal_uLong xsqr2 = xsqr << 1;
248 559 : const sal_uLong nColors = rPal.GetEntryCount();
249 559 : const long x = 1L << nBits;
250 559 : const long x2 = x >> 1L;
251 : sal_uLong r, g, b;
252 : long rxx, gxx, bxx;
253 :
254 559 : ImplCreateBuffers( nColorMax );
255 :
256 85987 : for( sal_uLong nIndex = 0; nIndex < nColors; nIndex++ )
257 : {
258 85428 : const BitmapColor& rColor = rPal[ (sal_uInt16) nIndex ];
259 85428 : const long cRed = rColor.GetRed();
260 85428 : const long cGreen = rColor.GetGreen();
261 85428 : const long cBlue = rColor.GetBlue();
262 :
263 85428 : long rdist = cRed - x2;
264 85428 : long gdist = cGreen - x2;
265 85428 : long bdist = cBlue - x2;
266 85428 : rdist = rdist*rdist + gdist*gdist + bdist*bdist;
267 :
268 85428 : const long crinc = ( xsqr - ( cRed << nBits ) ) << 1L;
269 85428 : const long cginc = ( xsqr - ( cGreen << nBits ) ) << 1L;
270 85428 : const long cbinc = ( xsqr - ( cBlue << nBits ) ) << 1L;
271 :
272 85428 : sal_uLong* cdp = reinterpret_cast<sal_uLong*>(pBuffer);
273 85428 : sal_uInt8* crgbp = pMap;
274 :
275 2819124 : for( r = 0, rxx = crinc; r < nColorMax; rdist += rxx, r++, rxx += xsqr2 )
276 : {
277 90211968 : for( g = 0, gdist = rdist, gxx = cginc; g < nColorMax; gdist += gxx, g++, gxx += xsqr2 )
278 : {
279 2886782976 : for( b = 0, bdist = gdist, bxx = cbinc; b < nColorMax; bdist += bxx, b++, cdp++, crgbp++, bxx += xsqr2 )
280 2799304704 : if ( !nIndex || ( (long) *cdp ) > bdist )
281 : {
282 353313369 : *cdp = bdist;
283 353313369 : *crgbp = (sal_uInt8) nIndex;
284 : }
285 : }
286 : }
287 : }
288 559 : }
289 :
290 559 : InverseColorMap::~InverseColorMap()
291 : {
292 559 : rtl_freeMemory( pBuffer );
293 559 : rtl_freeMemory( pMap );
294 559 : }
295 :
296 559 : void InverseColorMap::ImplCreateBuffers( const sal_uLong nMax )
297 : {
298 559 : const sal_uLong nCount = nMax * nMax * nMax;
299 559 : const sal_uLong nSize = nCount * sizeof( sal_uLong );
300 :
301 559 : pMap = static_cast<sal_uInt8*>(rtl_allocateMemory( nCount ));
302 559 : memset( pMap, 0x00, nCount );
303 :
304 559 : pBuffer = static_cast<sal_uInt8*>(rtl_allocateMemory( nSize ));
305 559 : memset( pBuffer, 0xff, nSize );
306 559 : }
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|