LCOV - code coverage report
Current view: top level - vcl/source/gdi - octree.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 150 161 93.2 %
Date: 2014-11-03 Functions: 14 15 93.3 %
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             : #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         554 : ImpNodeCache::ImpNodeCache( const sal_uLong nInitSize ) :
      35         554 :             pActNode( NULL )
      36             : {
      37         554 :     const sal_uLong nSize = nInitSize + 4;
      38             : 
      39      143632 :     for( sal_uLong i = 0; i < nSize; i++ )
      40             :     {
      41      143078 :         OctreeNode* pNewNode = new NODE;
      42             : 
      43      143078 :         pNewNode->pNextInCache = pActNode;
      44      143078 :         pActNode = pNewNode;
      45             :     }
      46         554 : }
      47             : 
      48         554 : ImpNodeCache::~ImpNodeCache()
      49             : {
      50      187415 :     while( pActNode )
      51             :     {
      52      186307 :         OctreeNode* pNode = pActNode;
      53             : 
      54      186307 :         pActNode = pNode->pNextInCache;
      55      186307 :         delete pNode;
      56             :     }
      57         554 : }
      58             : 
      59             : // - Octree -
      60             : 
      61         554 : Octree::Octree( const BitmapReadAccess& rReadAcc, sal_uLong nColors ) :
      62             :             nMax        ( nColors ),
      63             :             nLeafCount  ( 0L ),
      64             :             pTree       ( NULL ),
      65         554 :             pAcc        ( &rReadAcc )
      66             : {
      67         554 :     pNodeCache = new ImpNodeCache( nColors );
      68         554 :     memset( pReduce, 0, ( OCTREE_BITS + 1 ) * sizeof( PNODE ) );
      69         554 :     ImplCreateOctree();
      70         554 : }
      71             : 
      72        1108 : Octree::~Octree()
      73             : {
      74         554 :     ImplDeleteOctree( &pTree );
      75         554 :     delete pNodeCache;
      76         554 : }
      77             : 
      78         554 : void Octree::ImplCreateOctree()
      79             : {
      80         554 :     if( !!*pAcc )
      81             :     {
      82         554 :         const long      nWidth = pAcc->Width();
      83         554 :         const long      nHeight = pAcc->Height();
      84             : 
      85         554 :         if( pAcc->HasPalette() )
      86             :         {
      87          34 :             for( long nY = 0; nY < nHeight; nY++ )
      88             :             {
      89         544 :                 for( long nX = 0; nX < nWidth; nX++ )
      90             :                 {
      91         512 :                     pColor = &(BitmapColor&) pAcc->GetPaletteColor( pAcc->GetPixelIndex( nY, nX ) );
      92         512 :                     nLevel = 0L;
      93         512 :                     ImplAdd( &pTree );
      94             : 
      95        1024 :                     while( nLeafCount > nMax )
      96           0 :                         ImplReduce();
      97             :                 }
      98             :             }
      99             :         }
     100             :         else
     101             :         {
     102         552 :             BitmapColor aColor;
     103             : 
     104         552 :             pColor = &aColor;
     105             : 
     106       54807 :             for( long nY = 0; nY < nHeight; nY++ )
     107             :             {
     108     8270107 :                 for( long nX = 0; nX < nWidth; nX++ )
     109             :                 {
     110     8215852 :                     aColor = pAcc->GetPixel( nY, nX );
     111     8215852 :                     nLevel = 0L;
     112     8215852 :                     ImplAdd( &pTree );
     113             : 
     114    16449131 :                     while( nLeafCount > nMax )
     115       17427 :                         ImplReduce();
     116             :                 }
     117         552 :             }
     118             :         }
     119             :     }
     120         554 : }
     121             : 
     122      143090 : void Octree::ImplDeleteOctree( PPNODE ppNode )
     123             : {
     124     1287810 :     for ( sal_uLong i = 0UL; i < 8UL; i++ )
     125             :     {
     126     1144720 :         if ( (*ppNode)->pChild[ i ] )
     127      142536 :             ImplDeleteOctree( &(*ppNode)->pChild[ i ] );
     128             :     }
     129             : 
     130      143090 :     pNodeCache->ImplReleaseNode( *ppNode );
     131      143090 :     *ppNode = NULL;
     132      143090 : }
     133             : 
     134    47429879 : void Octree::ImplAdd( PPNODE ppNode )
     135             : {
     136             :     // ggf. neuen Knoten erzeugen
     137    47429879 :     if( !*ppNode )
     138             :     {
     139      171165 :         *ppNode = pNodeCache->ImplGetFreeNode();
     140      171165 :         (*ppNode)->bLeaf = ( OCTREE_BITS == nLevel );
     141             : 
     142      171165 :         if( (*ppNode)->bLeaf )
     143       96347 :             nLeafCount++;
     144             :         else
     145             :         {
     146       74818 :             (*ppNode)->pNext = pReduce[ nLevel ];
     147       74818 :             pReduce[ nLevel ] = *ppNode;
     148             :         }
     149             :     }
     150             : 
     151    47429879 :     if( (*ppNode)->bLeaf )
     152             :     {
     153     8216364 :         (*ppNode)->nCount++;
     154     8216364 :         (*ppNode)->nRed += pColor->GetRed();
     155     8216364 :         (*ppNode)->nGreen += pColor->GetGreen();
     156     8216364 :         (*ppNode)->nBlue += pColor->GetBlue();
     157             :     }
     158             :     else
     159             :     {
     160    39213515 :         const sal_uLong nShift = 7 - nLevel;
     161    39213515 :         const sal_uInt8  cMask = pImplMask[ nLevel ];
     162    78427030 :         const sal_uLong nIndex = ( ( ( pColor->GetRed() & cMask ) >> nShift ) << 2 ) |
     163    78427030 :                              ( ( ( pColor->GetGreen() & cMask ) >> nShift ) << 1 ) |
     164    78427030 :                              ( ( pColor->GetBlue() & cMask ) >> nShift );
     165             : 
     166    39213515 :         nLevel++;
     167    39213515 :         ImplAdd( &(*ppNode)->pChild[ nIndex ] );
     168             :     }
     169    47429879 : }
     170             : 
     171       17427 : void Octree::ImplReduce()
     172             : {
     173             :     sal_uLong   i;
     174             :     PNODE   pNode;
     175       17427 :     sal_uLong   nRedSum = 0L;
     176       17427 :     sal_uLong   nGreenSum = 0L;
     177       17427 :     sal_uLong   nBlueSum = 0L;
     178       17427 :     sal_uLong   nChildren = 0L;
     179             : 
     180       17427 :     for ( i = OCTREE_BITS - 1; i && !pReduce[i]; i-- ) {}
     181             : 
     182       17427 :     pNode = pReduce[ i ];
     183       17427 :     pReduce[ i ] = pNode->pNext;
     184             : 
     185      156843 :     for ( i = 0; i < 8; i++ )
     186             :     {
     187      139416 :         if ( pNode->pChild[ i ] )
     188             :         {
     189       28075 :             PNODE pChild = pNode->pChild[ i ];
     190             : 
     191       28075 :             nRedSum += pChild->nRed;
     192       28075 :             nGreenSum += pChild->nGreen;
     193       28075 :             nBlueSum += pChild->nBlue;
     194       28075 :             pNode->nCount += pChild->nCount;
     195             : 
     196       28075 :             pNodeCache->ImplReleaseNode( pNode->pChild[ i ] );
     197       28075 :             pNode->pChild[ i ] = NULL;
     198       28075 :             nChildren++;
     199             :         }
     200             :     }
     201             : 
     202       17427 :     pNode->bLeaf = true;
     203       17427 :     pNode->nRed = nRedSum;
     204       17427 :     pNode->nGreen = nGreenSum;
     205       17427 :     pNode->nBlue = nBlueSum;
     206       17427 :     nLeafCount -= --nChildren;
     207       17427 : }
     208             : 
     209      143090 : void Octree::CreatePalette( PNODE pNode )
     210             : {
     211      143090 :     if( pNode->bLeaf )
     212             :     {
     213       85699 :         pNode->nPalIndex = nPalIndex;
     214      257097 :         aPal[ nPalIndex++ ] = BitmapColor( (sal_uInt8) ( (double) pNode->nRed / pNode->nCount ),
     215       85699 :                                            (sal_uInt8) ( (double) pNode->nGreen / pNode->nCount ),
     216      171398 :                                            (sal_uInt8) ( (double) pNode->nBlue / pNode->nCount ) );
     217             :     }
     218      516519 :     else for( sal_uLong i = 0UL; i < 8UL; i++ )
     219      459128 :         if( pNode->pChild[ i ] )
     220      142536 :             CreatePalette( pNode->pChild[ i ] );
     221             : 
     222      143090 : }
     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         554 : InverseColorMap::InverseColorMap( const BitmapPalette& rPal ) :
     243         554 :             nBits( 8 - OCTREE_BITS )
     244             : {
     245             :     sal_uLong*          cdp;
     246             :     sal_uInt8*           crgbp;
     247         554 :     const sal_uLong     nColorMax = 1 << OCTREE_BITS;
     248         554 :     const sal_uLong     xsqr = 1 << ( nBits << 1 );
     249         554 :     const sal_uLong     xsqr2 = xsqr << 1;
     250         554 :     const sal_uLong     nColors = rPal.GetEntryCount();
     251         554 :     const long      x = 1L << nBits;
     252         554 :     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         554 :     ImplCreateBuffers( nColorMax );
     259             : 
     260       86253 :     for( sal_uLong nIndex = 0; nIndex < nColors; nIndex++ )
     261             :     {
     262       85699 :         const BitmapColor&  rColor = rPal[ (sal_uInt16) nIndex ];
     263       85699 :         const long          cRed = rColor.GetRed();
     264       85699 :         const long          cGreen = rColor.GetGreen();
     265       85699 :         const long          cBlue = rColor.GetBlue();
     266             : 
     267       85699 :         rdist = cRed - x2;
     268       85699 :         gdist = cGreen - x2;
     269       85699 :         bdist = cBlue - x2;
     270       85699 :         rdist = rdist*rdist + gdist*gdist + bdist*bdist;
     271             : 
     272       85699 :         crinc = ( xsqr - ( cRed << nBits ) ) << 1L;
     273       85699 :         cginc = ( xsqr - ( cGreen << nBits ) ) << 1L;
     274       85699 :         cbinc = ( xsqr - ( cBlue << nBits ) ) << 1L;
     275             : 
     276       85699 :         cdp = (sal_uLong*) pBuffer;
     277       85699 :         crgbp = pMap;
     278             : 
     279     2828067 :         for( r = 0, rxx = crinc; r < nColorMax; rdist += rxx, r++, rxx += xsqr2 )
     280             :         {
     281    90498144 :             for( g = 0, gdist = rdist, gxx = cginc; g < nColorMax;  gdist += gxx, g++, gxx += xsqr2 )
     282             :             {
     283  2895940608 :                 for( b = 0, bdist = gdist, bxx = cbinc; b < nColorMax; bdist += bxx, b++, cdp++, crgbp++, bxx += xsqr2 )
     284  2808184832 :                     if ( !nIndex || ( (long) *cdp ) > bdist )
     285             :                     {
     286   389694770 :                         *cdp = bdist;
     287   389694770 :                         *crgbp = (sal_uInt8) nIndex;
     288             :                     }
     289             :             }
     290             :         }
     291             :     }
     292         554 : }
     293             : 
     294         554 : InverseColorMap::~InverseColorMap()
     295             : {
     296         554 :     rtl_freeMemory( pBuffer );
     297         554 :     rtl_freeMemory( pMap );
     298         554 : }
     299             : 
     300         554 : void InverseColorMap::ImplCreateBuffers( const sal_uLong nMax )
     301             : {
     302         554 :     const sal_uLong nCount = nMax * nMax * nMax;
     303         554 :     const sal_uLong nSize = nCount * sizeof( sal_uLong );
     304             : 
     305         554 :     pMap = (sal_uInt8*) rtl_allocateMemory( nCount );
     306         554 :     memset( pMap, 0x00, nCount );
     307             : 
     308         554 :     pBuffer = (sal_uInt8*) rtl_allocateMemory( nSize );
     309         554 :     memset( pBuffer, 0xff, nSize );
     310        1787 : }
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10