LCOV - code coverage report
Current view: top level - libreoffice/vcl/source/gdi - octree.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 160 0.0 %
Date: 2012-12-27 Functions: 0 13 0.0 %
Legend: Lines: hit not hit

          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             : 
      21             : #include <limits.h>
      22             : 
      23             : #include <vcl/bmpacc.hxx>
      24             : #include <vcl/octree.hxx>
      25             : 
      26             : #include <impoct.hxx>
      27             : 
      28             : // ---------
      29             : // - pMask -
      30             : // ---------
      31             : 
      32             : static sal_uInt8 pImplMask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
      33             : 
      34             : // -------------
      35             : // - NodeCache -
      36             : // -------------
      37             : 
      38           0 : ImpNodeCache::ImpNodeCache( const sal_uLong nInitSize ) :
      39           0 :             pActNode( NULL )
      40             : {
      41           0 :     const sal_uLong nSize = nInitSize + 4;
      42             : 
      43           0 :     for( sal_uLong i = 0; i < nSize; i++ )
      44             :     {
      45           0 :         OctreeNode* pNewNode = new NODE;
      46             : 
      47           0 :         pNewNode->pNextInCache = pActNode;
      48           0 :         pActNode = pNewNode;
      49             :     }
      50           0 : }
      51             : 
      52             : // ------------------------------------------------------------------------
      53             : 
      54           0 : ImpNodeCache::~ImpNodeCache()
      55             : {
      56           0 :     while( pActNode )
      57             :     {
      58           0 :         OctreeNode* pNode = pActNode;
      59             : 
      60           0 :         pActNode = pNode->pNextInCache;
      61           0 :         delete pNode;
      62             :     }
      63           0 : }
      64             : 
      65             : // ----------
      66             : // - Octree -
      67             : // ----------
      68             : 
      69           0 : Octree::Octree( const BitmapReadAccess& rReadAcc, sal_uLong nColors ) :
      70             :             nMax        ( nColors ),
      71             :             nLeafCount  ( 0L ),
      72             :             pTree       ( NULL ),
      73           0 :             pAcc        ( &rReadAcc )
      74             : {
      75           0 :     pNodeCache = new ImpNodeCache( nColors );
      76           0 :     memset( pReduce, 0, ( OCTREE_BITS + 1 ) * sizeof( PNODE ) );
      77           0 :     ImplCreateOctree();
      78           0 : }
      79             : 
      80             : // ------------------------------------------------------------------------
      81             : 
      82           0 : Octree::~Octree()
      83             : {
      84           0 :     ImplDeleteOctree( &pTree );
      85           0 :     delete pNodeCache;
      86           0 : }
      87             : 
      88             : // ------------------------------------------------------------------------
      89             : 
      90           0 : void Octree::ImplCreateOctree()
      91             : {
      92           0 :     if( !!*pAcc )
      93             :     {
      94           0 :         const long      nWidth = pAcc->Width();
      95           0 :         const long      nHeight = pAcc->Height();
      96             : 
      97           0 :         if( pAcc->HasPalette() )
      98             :         {
      99           0 :             for( long nY = 0; nY < nHeight; nY++ )
     100             :             {
     101           0 :                 for( long nX = 0; nX < nWidth; nX++ )
     102             :                 {
     103           0 :                     pColor = &(BitmapColor&) pAcc->GetPaletteColor( pAcc->GetPixel( nY, nX ) );
     104           0 :                     nLevel = 0L;
     105           0 :                     ImplAdd( &pTree );
     106             : 
     107           0 :                     while( nLeafCount > nMax )
     108           0 :                         ImplReduce();
     109             :                 }
     110             :             }
     111             :         }
     112             :         else
     113             :         {
     114           0 :             BitmapColor aColor;
     115             : 
     116           0 :             pColor = &aColor;
     117             : 
     118           0 :             for( long nY = 0; nY < nHeight; nY++ )
     119             :             {
     120           0 :                 for( long nX = 0; nX < nWidth; nX++ )
     121             :                 {
     122           0 :                     aColor = pAcc->GetPixel( nY, nX );
     123           0 :                     nLevel = 0L;
     124           0 :                     ImplAdd( &pTree );
     125             : 
     126           0 :                     while( nLeafCount > nMax )
     127           0 :                         ImplReduce();
     128             :                 }
     129           0 :             }
     130             :         }
     131             :     }
     132           0 : }
     133             : 
     134             : // ------------------------------------------------------------------------
     135             : 
     136           0 : void Octree::ImplDeleteOctree( PPNODE ppNode )
     137             : {
     138           0 :     for ( sal_uLong i = 0UL; i < 8UL; i++ )
     139             :     {
     140           0 :         if ( (*ppNode)->pChild[ i ] )
     141           0 :             ImplDeleteOctree( &(*ppNode)->pChild[ i ] );
     142             :     }
     143             : 
     144           0 :     pNodeCache->ImplReleaseNode( *ppNode );
     145           0 :     *ppNode = NULL;
     146           0 : }
     147             : 
     148             : // ------------------------------------------------------------------------
     149             : 
     150           0 : void Octree::ImplAdd( PPNODE ppNode )
     151             : {
     152             :     // ggf. neuen Knoten erzeugen
     153           0 :     if( !*ppNode )
     154             :     {
     155           0 :         *ppNode = pNodeCache->ImplGetFreeNode();
     156           0 :         (*ppNode)->bLeaf = ( OCTREE_BITS == nLevel );
     157             : 
     158           0 :         if( (*ppNode)->bLeaf )
     159           0 :             nLeafCount++;
     160             :         else
     161             :         {
     162           0 :             (*ppNode)->pNext = pReduce[ nLevel ];
     163           0 :             pReduce[ nLevel ] = *ppNode;
     164             :         }
     165             :     }
     166             : 
     167           0 :     if( (*ppNode)->bLeaf )
     168             :     {
     169           0 :         (*ppNode)->nCount++;
     170           0 :         (*ppNode)->nRed += pColor->GetRed();
     171           0 :         (*ppNode)->nGreen += pColor->GetGreen();
     172           0 :         (*ppNode)->nBlue += pColor->GetBlue();
     173             :     }
     174             :     else
     175             :     {
     176           0 :         const sal_uLong nShift = 7 - nLevel;
     177           0 :         const sal_uInt8  cMask = pImplMask[ nLevel ];
     178           0 :         const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
     179           0 :                              ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
     180           0 :                              ( ( pColor->GetBlue() & cMask ) >> nShift );
     181             : 
     182           0 :         nLevel++;
     183           0 :         ImplAdd( &(*ppNode)->pChild[ nIndex ] );
     184             :     }
     185           0 : }
     186             : 
     187             : // ------------------------------------------------------------------------
     188             : 
     189           0 : void Octree::ImplReduce()
     190             : {
     191             :     sal_uLong   i;
     192             :     PNODE   pNode;
     193           0 :     sal_uLong   nRedSum = 0L;
     194           0 :     sal_uLong   nGreenSum = 0L;
     195           0 :     sal_uLong   nBlueSum = 0L;
     196           0 :     sal_uLong   nChildren = 0L;
     197             : 
     198           0 :     for ( i = OCTREE_BITS - 1; i && !pReduce[i]; i-- ) {}
     199             : 
     200           0 :     pNode = pReduce[ i ];
     201           0 :     pReduce[ i ] = pNode->pNext;
     202             : 
     203           0 :     for ( i = 0; i < 8; i++ )
     204             :     {
     205           0 :         if ( pNode->pChild[ i ] )
     206             :         {
     207           0 :             PNODE pChild = pNode->pChild[ i ];
     208             : 
     209           0 :             nRedSum += pChild->nRed;
     210           0 :             nGreenSum += pChild->nGreen;
     211           0 :             nBlueSum += pChild->nBlue;
     212           0 :             pNode->nCount += pChild->nCount;
     213             : 
     214           0 :             pNodeCache->ImplReleaseNode( pNode->pChild[ i ] );
     215           0 :             pNode->pChild[ i ] = NULL;
     216           0 :             nChildren++;
     217             :         }
     218             :     }
     219             : 
     220           0 :     pNode->bLeaf = sal_True;
     221           0 :     pNode->nRed = nRedSum;
     222           0 :     pNode->nGreen = nGreenSum;
     223           0 :     pNode->nBlue = nBlueSum;
     224           0 :     nLeafCount -= --nChildren;
     225           0 : }
     226             : 
     227             : // ------------------------------------------------------------------------
     228             : 
     229           0 : void Octree::CreatePalette( PNODE pNode )
     230             : {
     231           0 :     if( pNode->bLeaf )
     232             :     {
     233           0 :         pNode->nPalIndex = nPalIndex;
     234           0 :         aPal[ nPalIndex++ ] = BitmapColor( (sal_uInt8) ( (double) pNode->nRed / pNode->nCount ),
     235             :                                            (sal_uInt8) ( (double) pNode->nGreen / pNode->nCount ),
     236           0 :                                            (sal_uInt8) ( (double) pNode->nBlue / pNode->nCount ) );
     237             :     }
     238           0 :     else for( sal_uLong i = 0UL; i < 8UL; i++ )
     239           0 :         if( pNode->pChild[ i ] )
     240           0 :             CreatePalette( pNode->pChild[ i ] );
     241             : 
     242           0 : }
     243             : 
     244             : // ------------------------------------------------------------------------
     245             : 
     246           0 : void Octree::GetPalIndex( PNODE pNode )
     247             : {
     248           0 :     if ( pNode->bLeaf )
     249           0 :         nPalIndex = pNode->nPalIndex;
     250             :     else
     251             :     {
     252           0 :         const sal_uLong nShift = 7 - nLevel;
     253           0 :         const sal_uInt8  cMask = pImplMask[ nLevel++ ];
     254           0 :         const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
     255           0 :                              ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
     256           0 :                              ( ( pColor->GetBlue() & cMask ) >> nShift );
     257             : 
     258           0 :         GetPalIndex( pNode->pChild[ nIndex ] );
     259             :     }
     260           0 : }
     261             : 
     262             : // -------------------
     263             : // - InverseColorMap -
     264             : // -------------------
     265             : 
     266           0 : InverseColorMap::InverseColorMap( const BitmapPalette& rPal ) :
     267           0 :             nBits( 8 - OCTREE_BITS )
     268             : {
     269             :     sal_uLong*          cdp;
     270             :     sal_uInt8*           crgbp;
     271           0 :     const sal_uLong     nColorMax = 1 << OCTREE_BITS;
     272           0 :     const sal_uLong     xsqr = 1 << ( nBits << 1 );
     273           0 :     const sal_uLong     xsqr2 = xsqr << 1;
     274           0 :     const sal_uLong     nColors = rPal.GetEntryCount();
     275           0 :     const long      x = 1L << nBits;
     276           0 :     const long      x2 = x >> 1L;
     277             :     sal_uLong           r, g, b;
     278             :     long            rxx, gxx, bxx;
     279             :     long            rdist, gdist, bdist;
     280             :     long            crinc, cginc, cbinc;
     281             : 
     282           0 :     ImplCreateBuffers( nColorMax );
     283             : 
     284           0 :     for( sal_uLong nIndex = 0; nIndex < nColors; nIndex++ )
     285             :     {
     286           0 :         const BitmapColor&  rColor = rPal[ (sal_uInt16) nIndex ];
     287           0 :         const sal_uInt8         cRed = rColor.GetRed();
     288           0 :         const sal_uInt8         cGreen = rColor.GetGreen();
     289           0 :         const sal_uInt8         cBlue = rColor.GetBlue();
     290             : 
     291           0 :         rdist = cRed - x2;
     292           0 :         gdist = cGreen - x2;
     293           0 :         bdist = cBlue - x2;
     294           0 :         rdist = rdist*rdist + gdist*gdist + bdist*bdist;
     295             : 
     296           0 :         crinc = ( xsqr - ( cRed << nBits ) ) << 1L;
     297           0 :         cginc = ( xsqr - ( cGreen << nBits ) ) << 1L;
     298           0 :         cbinc = ( xsqr - ( cBlue << nBits ) ) << 1L;
     299             : 
     300           0 :         cdp = (sal_uLong*) pBuffer;
     301           0 :         crgbp = pMap;
     302             : 
     303           0 :         for( r = 0, rxx = crinc; r < nColorMax; rdist += rxx, r++, rxx += xsqr2 )
     304             :         {
     305           0 :             for( g = 0, gdist = rdist, gxx = cginc; g < nColorMax;  gdist += gxx, g++, gxx += xsqr2 )
     306             :             {
     307           0 :                 for( b = 0, bdist = gdist, bxx = cbinc; b < nColorMax; bdist += bxx, b++, cdp++, crgbp++, bxx += xsqr2 )
     308           0 :                     if ( !nIndex || ( (long) *cdp ) > bdist )
     309             :                     {
     310           0 :                         *cdp = bdist;
     311           0 :                         *crgbp = (sal_uInt8) nIndex;
     312             :                     }
     313             :             }
     314             :         }
     315             :     }
     316           0 : }
     317             : 
     318             : // ------------------------------------------------------------------------
     319             : 
     320           0 : InverseColorMap::~InverseColorMap()
     321             : {
     322           0 :     rtl_freeMemory( pBuffer );
     323           0 :     rtl_freeMemory( pMap );
     324           0 : }
     325             : 
     326             : // ------------------------------------------------------------------------
     327             : 
     328           0 : void InverseColorMap::ImplCreateBuffers( const sal_uLong nMax )
     329             : {
     330           0 :     const sal_uLong nCount = nMax * nMax * nMax;
     331           0 :     const sal_uLong nSize = nCount * sizeof( sal_uLong );
     332             : 
     333           0 :     pMap = (sal_uInt8*) rtl_allocateMemory( nCount );
     334           0 :     memset( pMap, 0x00, nCount );
     335             : 
     336           0 :     pBuffer = (sal_uInt8*) rtl_allocateMemory( nSize );
     337           0 :     memset( pBuffer, 0xff, nSize );
     338           0 : }
     339             : 
     340             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10