LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/vcl - bmpacc.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 60 67 89.6 %
Date: 2013-07-09 Functions: 26 29 89.7 %
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             : #ifndef _SV_BMPACC_HXX
      21             : #define _SV_BMPACC_HXX
      22             : 
      23             : #include <tools/solar.h>
      24             : #include <vcl/dllapi.h>
      25             : #include <vcl/salbtype.hxx>
      26             : #include <vcl/bitmap.hxx>
      27             : 
      28             : // --------------------
      29             : // - Access defines -
      30             : // --------------------
      31             : 
      32             : #define DECL_FORMAT_GETPIXEL( Format ) \
      33             : static BitmapColor GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask );
      34             : 
      35             : #define DECL_FORMAT_SETPIXEL( Format ) \
      36             : static void SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
      37             : 
      38             : #define DECL_FORMAT( Format )   \
      39             : DECL_FORMAT_GETPIXEL( Format )  \
      40             : DECL_FORMAT_SETPIXEL( Format )
      41             : 
      42             : #define IMPL_FORMAT_GETPIXEL( Format ) \
      43             : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& rMask )
      44             : 
      45             : #define IMPL_FORMAT_GETPIXEL_NOMASK( Format ) \
      46             : BitmapColor BitmapReadAccess::GetPixelFor##Format( ConstScanline pScanline, long nX, const ColorMask& )
      47             : 
      48             : #define IMPL_FORMAT_SETPIXEL( Format ) \
      49             : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask )
      50             : 
      51             : #define IMPL_FORMAT_SETPIXEL_NOMASK( Format ) \
      52             : void BitmapReadAccess::SetPixelFor##Format( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& )
      53             : 
      54             : #define CASE_FORMAT( Format )           \
      55             : case( BMP_FORMAT##Format ):            \
      56             : {                                       \
      57             :     mFncGetPixel = GetPixelFor##Format;\
      58             :     mFncSetPixel = SetPixelFor##Format;\
      59             : }                                       \
      60             : break;
      61             : 
      62             : 
      63             : // --------------------
      64             : // - Access functions -
      65             : // --------------------
      66             : 
      67             : typedef BitmapColor (*FncGetPixel)( ConstScanline pScanline, long nX, const ColorMask& rMask );
      68             : typedef void (*FncSetPixel)( Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask );
      69             : 
      70             : // --------------------
      71             : // - BitmapReadAccess -
      72             : // --------------------
      73             : 
      74             : class VCL_DLLPUBLIC BitmapReadAccess
      75             : {
      76             :     friend class BitmapWriteAccess;
      77             : 
      78             : private:
      79             : 
      80             :                                 BitmapReadAccess() {}
      81             :                                 BitmapReadAccess( const BitmapReadAccess& ) {}
      82             :     BitmapReadAccess&           operator=( const BitmapReadAccess& ) { return *this; }
      83             : 
      84             : protected:
      85             :     Bitmap                      maBitmap;
      86             :     BitmapBuffer*               mpBuffer;
      87             :     Scanline*                   mpScanBuf;
      88             :     ColorMask                   maColorMask;
      89             :     FncGetPixel                 mFncGetPixel;
      90             :     FncSetPixel                 mFncSetPixel;
      91             :     sal_Bool                        mbModify;
      92             : 
      93             : 
      94             : SAL_DLLPRIVATE  void            ImplCreate( Bitmap& rBitmap );
      95             : SAL_DLLPRIVATE  void            ImplDestroy();
      96             : SAL_DLLPRIVATE  sal_Bool            ImplSetAccessPointers( sal_uLong nFormat );
      97             : 
      98             : public:
      99             : 
     100             : SAL_DLLPRIVATE  void            ImplZeroInitUnusedBits();
     101      167214 : SAL_DLLPRIVATE  BitmapBuffer*   ImplGetBitmapBuffer() const { return mpBuffer; }
     102             : 
     103             :                                 DECL_FORMAT( _1BIT_MSB_PAL )
     104             :                                 DECL_FORMAT( _1BIT_LSB_PAL )
     105             :                                 DECL_FORMAT( _4BIT_MSN_PAL )
     106             :                                 DECL_FORMAT( _4BIT_LSN_PAL )
     107             :                                 DECL_FORMAT( _8BIT_PAL )
     108             :                                 DECL_FORMAT( _8BIT_TC_MASK )
     109             :                                 DECL_FORMAT( _16BIT_TC_MSB_MASK )
     110             :                                 DECL_FORMAT( _16BIT_TC_LSB_MASK )
     111             :                                 DECL_FORMAT( _24BIT_TC_BGR )
     112             :                                 DECL_FORMAT( _24BIT_TC_RGB )
     113             :                                 DECL_FORMAT( _24BIT_TC_MASK )
     114             :                                 DECL_FORMAT( _32BIT_TC_ABGR )
     115             :                                 DECL_FORMAT( _32BIT_TC_ARGB )
     116             :                                 DECL_FORMAT( _32BIT_TC_BGRA )
     117             :                                 DECL_FORMAT( _32BIT_TC_RGBA )
     118             :                                 DECL_FORMAT( _32BIT_TC_MASK )
     119             : protected:
     120             :                                 BitmapReadAccess( Bitmap& rBitmap, sal_Bool bModify );
     121             : 
     122             : public:
     123             :                                 BitmapReadAccess( Bitmap& rBitmap );
     124             :     virtual                     ~BitmapReadAccess();
     125             : 
     126             :     inline sal_Bool                 operator!() const;
     127             : 
     128             :     inline long                 Width() const;
     129             :     inline long                 Height() const;
     130             :     inline Point                TopLeft() const;
     131             :     inline Point                BottomRight() const;
     132             : 
     133             :     inline sal_Bool                 IsTopDown() const;
     134             :     inline sal_Bool                 IsBottomUp() const;
     135             : 
     136             :     inline sal_uLong                GetScanlineFormat() const;
     137             :     inline sal_uLong                GetScanlineSize() const;
     138             : 
     139             :     inline sal_uInt16               GetBitCount() const;
     140             :     inline BitmapColor          GetBestMatchingColor( const BitmapColor& rBitmapColor );
     141             : 
     142             :     inline Scanline             GetBuffer() const;
     143             :     inline Scanline             GetScanline( long nY ) const;
     144             : 
     145             :     inline sal_Bool                 HasPalette() const;
     146             :     inline const BitmapPalette& GetPalette() const;
     147             :     inline sal_uInt16               GetPaletteEntryCount() const;
     148             :     inline const BitmapColor&   GetPaletteColor( sal_uInt16 nColor ) const;
     149             :     inline const BitmapColor&   GetBestPaletteColor( const BitmapColor& rBitmapColor ) const;
     150             :     sal_uInt16                      GetBestPaletteIndex( const BitmapColor& rBitmapColor ) const;
     151             : 
     152             :     inline sal_Bool                 HasColorMask() const;
     153             :     inline ColorMask&           GetColorMask() const;
     154             : 
     155             :     inline BitmapColor          GetPixelFromData( const sal_uInt8* pData, long nX ) const;
     156             :     inline void                 SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor );
     157             :     inline BitmapColor          GetPixel( long nY, long nX ) const;
     158             :     inline BitmapColor          GetColor( long nY, long nX ) const;
     159             :     inline sal_uInt8            GetPixelIndex( long nY, long nX ) const;
     160             :     inline sal_uInt8            GetLuminance( long nY, long nX ) const;
     161             : };
     162             : 
     163             : // ---------------------
     164             : // - BitmapWriteAccess -
     165             : // ---------------------
     166             : 
     167             : class VCL_DLLPUBLIC BitmapWriteAccess : public BitmapReadAccess
     168             : {
     169             : public:
     170             : 
     171             :                                 BitmapWriteAccess( Bitmap& rBitmap );
     172             :     virtual                     ~BitmapWriteAccess();
     173             : 
     174             :     void                        CopyScanline( long nY, const BitmapReadAccess& rReadAcc );
     175             :     void                        CopyScanline( long nY, ConstScanline aSrcScanline,
     176             :                                               sal_uLong nSrcScanlineFormat, sal_uLong nSrcScanlineSize );
     177             : 
     178             :     void                        CopyBuffer( const BitmapReadAccess& rReadAcc );
     179             : 
     180             :     inline void                 SetPalette( const BitmapPalette& rPalette );
     181             :     inline void                 SetPaletteEntryCount( sal_uInt16 nCount );
     182             :     inline void                 SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor );
     183             : 
     184             :     inline void                 SetPixel( long nY, long nX, const BitmapColor& rBitmapColor );
     185             :     inline void                 SetPixelIndex( long nY, long nX, sal_uInt8 cIndex );
     186             : 
     187             :     void                        SetLineColor( const Color& rColor );
     188             : 
     189             :     void                        SetFillColor( const Color& rColor );
     190             : 
     191             :     void                        Erase( const Color& rColor );
     192             : 
     193             :     void                        DrawLine( const Point& rStart, const Point& rEnd );
     194             : 
     195             :     void                        FillRect( const Rectangle& rRect );
     196             :     void                        DrawRect( const Rectangle& rRect );
     197             : 
     198             :     void                        FillPolygon( const Polygon& rPoly );
     199             :     void                        DrawPolygon( const Polygon& rPoly );
     200             : 
     201             :     void                        FillPolyPolygon( const PolyPolygon& rPoly );
     202             :     void                        DrawPolyPolygon( const PolyPolygon& rPolyPoly );
     203             : 
     204             : private:
     205             : 
     206             :     BitmapColor*                mpLineColor;
     207             :     BitmapColor*                mpFillColor;
     208             : 
     209             :                                 BitmapWriteAccess() {}
     210             :                                 BitmapWriteAccess( const BitmapWriteAccess& ) : BitmapReadAccess() {}
     211             :     BitmapWriteAccess&          operator=( const BitmapWriteAccess& ) { return *this; }
     212             : };
     213             : 
     214             : // -----------
     215             : // - Inlines -
     216             : // -----------
     217             : 
     218      582212 : inline sal_Bool BitmapReadAccess::operator!() const
     219             : {
     220      582212 :     return( mpBuffer == NULL );
     221             : }
     222             : 
     223             : // ------------------------------------------------------------------
     224             : 
     225     1197354 : inline long BitmapReadAccess::Width() const
     226             : {
     227     1197354 :     return( mpBuffer ? mpBuffer->mnWidth : 0L );
     228             : }
     229             : 
     230             : // ------------------------------------------------------------------
     231             : 
     232     1014975 : inline long BitmapReadAccess::Height() const
     233             : {
     234     1014975 :     return( mpBuffer ? mpBuffer->mnHeight : 0L );
     235             : }
     236             : 
     237             : // ------------------------------------------------------------------
     238             : 
     239             : inline Point BitmapReadAccess::TopLeft() const
     240             : {
     241             :     return Point();
     242             : }
     243             : 
     244             : // ------------------------------------------------------------------
     245             : 
     246             : inline Point BitmapReadAccess::BottomRight() const
     247             : {
     248             :     return Point( Width() - 1L, Height() - 1L );
     249             : }
     250             : 
     251             : // ------------------------------------------------------------------
     252             : 
     253          63 : inline sal_Bool BitmapReadAccess::IsTopDown() const
     254             : {
     255             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     256          63 :     return( mpBuffer ? sal::static_int_cast<sal_Bool>( BMP_SCANLINE_ADJUSTMENT( mpBuffer->mnFormat ) == BMP_FORMAT_TOP_DOWN ) : sal_False );
     257             : }
     258             : 
     259             : // ------------------------------------------------------------------
     260             : 
     261          62 : inline sal_Bool BitmapReadAccess::IsBottomUp() const
     262             : {
     263          62 :     return !IsTopDown();
     264             : }
     265             : 
     266             : // ------------------------------------------------------------------
     267             : 
     268       78402 : inline sal_uLong BitmapReadAccess::GetScanlineFormat() const
     269             : {
     270             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     271       78402 :     return( mpBuffer ? BMP_SCANLINE_FORMAT( mpBuffer->mnFormat ) : 0UL );
     272             : }
     273             : 
     274             : // ------------------------------------------------------------------
     275             : 
     276       69361 : inline sal_uLong BitmapReadAccess::GetScanlineSize() const
     277             : {
     278             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     279       69361 :     return( mpBuffer ? mpBuffer->mnScanlineSize : 0UL );
     280             : }
     281             : 
     282             : // ------------------------------------------------------------------
     283             : 
     284       23920 : inline sal_uInt16  BitmapReadAccess::GetBitCount() const
     285             : {
     286             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     287       23920 :     return( mpBuffer ? mpBuffer->mnBitCount : 0 );
     288             : }
     289             : 
     290             : // ------------------------------------------------------------------
     291             : 
     292        6679 : inline BitmapColor BitmapReadAccess::GetBestMatchingColor( const BitmapColor& rBitmapColor )
     293             : {
     294        6679 :     if( HasPalette() )
     295        6102 :         return BitmapColor( (sal_uInt8) GetBestPaletteIndex( rBitmapColor ) );
     296             :     else
     297         577 :         return rBitmapColor;
     298             : }
     299             : 
     300             : // ------------------------------------------------------------------
     301             : 
     302       10216 : inline Scanline BitmapReadAccess::GetBuffer() const
     303             : {
     304             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     305       10216 :     return( mpBuffer ? mpBuffer->mpBits : NULL );
     306             : }
     307             : 
     308             : // ------------------------------------------------------------------
     309             : 
     310    60376064 : inline Scanline BitmapReadAccess::GetScanline( long nY ) const
     311             : {
     312             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     313             :     DBG_ASSERT( nY < mpBuffer->mnHeight, "y-coordinate out of range!" );
     314    60376064 :     return( mpBuffer ? mpScanBuf[ nY ] : NULL );
     315             : }
     316             : 
     317             : // ------------------------------------------------------------------
     318             : 
     319   885626234 : inline sal_Bool BitmapReadAccess::HasPalette() const
     320             : {
     321             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     322   885626234 :     return( mpBuffer && !!mpBuffer->maPalette );
     323             : }
     324             : 
     325             : // ------------------------------------------------------------------
     326             : 
     327      166050 : inline const BitmapPalette& BitmapReadAccess::GetPalette() const
     328             : {
     329             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     330      166050 :     return mpBuffer->maPalette;
     331             : }
     332             : 
     333             : // ------------------------------------------------------------------
     334             : 
     335      122291 : inline sal_uInt16 BitmapReadAccess::GetPaletteEntryCount() const
     336             : {
     337             :     DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
     338      122291 :     return( HasPalette() ? mpBuffer->maPalette.GetEntryCount() : 0 );
     339             : }
     340             : 
     341             : // ------------------------------------------------------------------
     342             : 
     343   340137010 : inline const BitmapColor& BitmapReadAccess::GetPaletteColor( sal_uInt16 nColor ) const
     344             : {
     345             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     346             :     DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
     347   340137010 :     return mpBuffer->maPalette[ nColor ];
     348             : }
     349             : 
     350             : // ------------------------------------------------------------------
     351             : 
     352           0 : inline const BitmapColor& BitmapReadAccess::GetBestPaletteColor( const BitmapColor& rBitmapColor ) const
     353             : {
     354           0 :     return GetPaletteColor( GetBestPaletteIndex( rBitmapColor ) );
     355             : }
     356             : 
     357             : // ------------------------------------------------------------------
     358             : 
     359             : inline sal_Bool BitmapReadAccess::HasColorMask() const
     360             : {
     361             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     362             :     const sal_uLong nFormat = BMP_SCANLINE_FORMAT( mpBuffer->mnFormat );
     363             : 
     364             :     return( nFormat == BMP_FORMAT_8BIT_TC_MASK  ||
     365             :             nFormat == BMP_FORMAT_16BIT_TC_MSB_MASK ||
     366             :             nFormat == BMP_FORMAT_16BIT_TC_LSB_MASK ||
     367             :             nFormat == BMP_FORMAT_24BIT_TC_MASK ||
     368             :             nFormat == BMP_FORMAT_32BIT_TC_MASK );
     369             : }
     370             : 
     371             : // ------------------------------------------------------------------
     372             : 
     373         831 : inline ColorMask& BitmapReadAccess::GetColorMask() const
     374             : {
     375             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     376         831 :     return mpBuffer->maColorMask;
     377             : }
     378             : 
     379             : // ------------------------------------------------------------------
     380             : 
     381  1132213966 : inline BitmapColor BitmapReadAccess::GetPixel( long nY, long nX ) const
     382             : {
     383             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     384             :     DBG_ASSERT( nX < mpBuffer->mnWidth, "x-coordinate out of range!" );
     385             :     DBG_ASSERT( nY < mpBuffer->mnHeight, "y-coordinate out of range!" );
     386  1132213966 :     return mFncGetPixel( mpScanBuf[ nY ], nX, maColorMask );
     387             : }
     388             : 
     389   340360088 : inline sal_uInt8 BitmapReadAccess::GetPixelIndex( long nY, long nX ) const
     390             : {
     391   340360088 :     return GetPixel( nY, nX ).GetBlueOrIndex();
     392             : }
     393             : 
     394             : // ------------------------------------------------------------------
     395             : 
     396           0 : inline BitmapColor BitmapReadAccess::GetPixelFromData( const sal_uInt8* pData, long nX ) const
     397             : {
     398             :     DBG_ASSERT( pData, "Access is not valid!" );
     399           0 :     return mFncGetPixel( pData, nX, maColorMask );
     400             : }
     401             : 
     402             : // ------------------------------------------------------------------
     403             : 
     404           0 : inline void BitmapReadAccess::SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor )
     405             : {
     406             :     DBG_ASSERT( pData, "Access is not valid!" );
     407           0 :     mFncSetPixel( pData, nX, rBitmapColor, maColorMask );
     408           0 : }
     409             : 
     410             : // ------------------------------------------------------------------
     411             : 
     412     6290077 : inline BitmapColor BitmapReadAccess::GetColor( long nY, long nX ) const
     413             : {
     414     6290077 :     if( HasPalette() )
     415     2399261 :         return mpBuffer->maPalette[ GetPixelIndex( nY, nX ) ];
     416             :     else
     417     3890816 :         return GetPixel( nY, nX );
     418             : }
     419             : 
     420             : // ------------------------------------------------------------------
     421             : 
     422     1636794 : inline sal_uInt8 BitmapReadAccess::GetLuminance( long nY, long nX ) const
     423             : {
     424     1636794 :     return GetColor( nY, nX ).GetLuminance();
     425             : }
     426             : 
     427             : // ------------------------------------------------------------------
     428             : 
     429         473 : inline void BitmapWriteAccess::SetPalette( const BitmapPalette& rPalette )
     430             : {
     431             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     432         473 :     mpBuffer->maPalette = rPalette;
     433         473 : }
     434             : 
     435             : // ------------------------------------------------------------------
     436             : 
     437         821 : inline void BitmapWriteAccess::SetPaletteEntryCount( sal_uInt16 nCount )
     438             : {
     439             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     440         821 :     mpBuffer->maPalette.SetEntryCount( nCount );
     441         821 : }
     442             : 
     443             : // ------------------------------------------------------------------
     444             : 
     445      125927 : inline void BitmapWriteAccess::SetPaletteColor( sal_uInt16 nColor, const BitmapColor& rBitmapColor )
     446             : {
     447             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     448             :     DBG_ASSERT( HasPalette(), "Bitmap has no palette!" );
     449      125927 :     mpBuffer->maPalette[ nColor ] = rBitmapColor;
     450      125927 : }
     451             : 
     452             : // ------------------------------------------------------------------
     453             : 
     454   401675452 : inline void BitmapWriteAccess::SetPixel( long nY, long nX, const BitmapColor& rBitmapColor )
     455             : {
     456             :     DBG_ASSERT( mpBuffer, "Access is not valid!" );
     457             :     DBG_ASSERT( nX < mpBuffer->mnWidth, "x-coordinate out of range!" );
     458             :     DBG_ASSERT( nY < mpBuffer->mnHeight, "y-coordinate out of range!" );
     459   401675452 :     mFncSetPixel( mpScanBuf[ nY ], nX, rBitmapColor, maColorMask );
     460   401675452 : }
     461             : 
     462    50713940 : inline void BitmapWriteAccess::SetPixelIndex( long nY, long nX, sal_uInt8 cIndex )
     463             : {
     464    50713940 :     SetPixel( nY, nX, BitmapColor( cIndex ));
     465    50713940 : }
     466             : 
     467             : // ------------------------------------------------------------------
     468             : 
     469             : #endif // _SV_BMPACC_HXX
     470             : 
     471             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10