LCOV - code coverage report
Current view: top level - include/vcl - bmpacc.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 66 68 97.1 %
Date: 2015-06-13 12:38:46 Functions: 28 29 96.6 %
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 INCLUDED_VCL_BMPACC_HXX
      21             : #define INCLUDED_VCL_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             : // - Access functions -
      29             : typedef BitmapColor (*FncGetPixel)(ConstScanline pScanline, long nX, const ColorMask& rMask);
      30             : typedef void (*FncSetPixel)(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
      31             : 
      32             : // - BitmapInfoAccess -
      33             : class VCL_DLLPUBLIC BitmapInfoAccess
      34             : {
      35             :     friend class BitmapReadAccess;
      36             : 
      37             : public:
      38             :     BitmapInfoAccess(Bitmap& rBitmap);
      39             :     virtual ~BitmapInfoAccess();
      40             : 
      41             :     inline bool operator!() const;
      42             : 
      43             :     inline long Width() const;
      44             :     inline long Height() const;
      45             :     inline Point TopLeft() const;
      46             :     inline Point BottomRight() const;
      47             : 
      48             :     inline bool  IsTopDown() const;
      49             :     inline bool  IsBottomUp() const;
      50             : 
      51             :     inline sal_uLong GetScanlineFormat() const;
      52             :     inline sal_uLong GetScanlineSize() const;
      53             : 
      54             :     inline sal_uInt16 GetBitCount() const;
      55             :     inline BitmapColor GetBestMatchingColor(const BitmapColor& rBitmapColor);
      56             : 
      57             :     inline bool HasPalette() const;
      58             :     inline const BitmapPalette& GetPalette() const;
      59             :     inline sal_uInt16 GetPaletteEntryCount() const;
      60             :     inline const BitmapColor& GetPaletteColor(sal_uInt16 nColor) const;
      61             :     inline const BitmapColor& GetBestPaletteColor(const BitmapColor& rBitmapColor) const;
      62             :     sal_uInt16 GetBestPaletteIndex(const BitmapColor& rBitmapColor) const;
      63             : 
      64             :     inline bool                 HasColorMask() const;
      65             :     inline ColorMask&           GetColorMask() const;
      66             : 
      67             : private:
      68             :     BitmapInfoAccess()
      69             :     {}
      70             : 
      71             :     BitmapInfoAccess(const BitmapInfoAccess&)
      72             :     {}
      73             : 
      74             :     BitmapInfoAccess& operator=(const BitmapInfoAccess&)
      75             :     {
      76             :         return *this;
      77             :     }
      78             : 
      79             : protected:
      80             :     Bitmap maBitmap;
      81             :     BitmapBuffer* mpBuffer;
      82             :     ColorMask maColorMask;
      83             :     BitmapAccessMode mnAccessMode;
      84             : 
      85             :     SAL_DLLPRIVATE void ImplCreate(Bitmap& rBitmap);
      86             :     SAL_DLLPRIVATE void ImplDestroy();
      87             : 
      88             : protected:
      89             :     BitmapInfoAccess(Bitmap& rBitmap, BitmapAccessMode nMode);
      90             : };
      91             : 
      92             : // - BitmapReadAccess -
      93             : class VCL_DLLPUBLIC BitmapReadAccess : public BitmapInfoAccess
      94             : {
      95             :     friend class BitmapWriteAccess;
      96             : 
      97             : public:
      98             :     BitmapReadAccess(Bitmap& rBitmap);
      99             :     virtual ~BitmapReadAccess();
     100             : 
     101             :     inline Scanline GetBuffer() const;
     102             :     inline Scanline GetScanline( long nY ) const;
     103             : 
     104             :     inline BitmapColor GetPixelFromData( const sal_uInt8* pData, long nX ) const;
     105             :     inline void SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor );
     106             : 
     107             :     inline BitmapColor GetPixel( long nY, long nX ) const;
     108             :     inline BitmapColor GetColor( long nY, long nX ) const;
     109             :     inline sal_uInt8 GetPixelIndex( long nY, long nX ) const;
     110             :     inline sal_uInt8 GetLuminance( long nY, long nX ) const;
     111             : 
     112             :     /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
     113             :     BitmapColor GetInterpolatedColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
     114             : 
     115             :     /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
     116             :         inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
     117             :     BitmapColor GetColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
     118             : 
     119             : private:
     120             : 
     121             :     BitmapReadAccess()
     122             :     {}
     123             : 
     124             :     BitmapReadAccess(const BitmapReadAccess&)
     125             :         : BitmapInfoAccess()
     126             :     {}
     127             : 
     128             :     BitmapReadAccess& operator=(const BitmapReadAccess&)
     129             :     {
     130             :         return *this;
     131             :     }
     132             : 
     133             : protected:
     134             :     Scanline*   mpScanBuf;
     135             :     FncGetPixel mFncGetPixel;
     136             :     FncSetPixel mFncSetPixel;
     137             : 
     138             :     SAL_DLLPRIVATE void ImplInitScanBuffer( Bitmap& rBitmap );
     139             :     SAL_DLLPRIVATE void ImplClearScanBuffer();
     140             :     SAL_DLLPRIVATE bool ImplSetAccessPointers( sal_uLong nFormat );
     141             : 
     142             : public:
     143             : 
     144             :     SAL_DLLPRIVATE void ImplZeroInitUnusedBits();
     145      200481 :     SAL_DLLPRIVATE BitmapBuffer* ImplGetBitmapBuffer() const
     146             :     {
     147      200481 :         return mpBuffer;
     148             :     }
     149             : 
     150             :     static BitmapColor GetPixelFor_1BIT_MSB_PAL(ConstScanline pScanline, long nX, const ColorMask& rMask);
     151             :     static BitmapColor GetPixelFor_1BIT_LSB_PAL(ConstScanline pScanline, long nX, const ColorMask& rMask);
     152             :     static BitmapColor GetPixelFor_4BIT_MSN_PAL(ConstScanline pScanline, long nX, const ColorMask& rMask);
     153             :     static BitmapColor GetPixelFor_4BIT_LSN_PAL(ConstScanline pScanline, long nX, const ColorMask& rMask);
     154             :     static BitmapColor GetPixelFor_8BIT_PAL(ConstScanline pScanline, long nX, const ColorMask& rMask);
     155             :     static BitmapColor GetPixelFor_8BIT_TC_MASK(ConstScanline pScanline, long nX, const ColorMask& rMask);
     156             :     static BitmapColor GetPixelFor_16BIT_TC_MSB_MASK(ConstScanline pScanline, long nX, const ColorMask& rMask);
     157             :     static BitmapColor GetPixelFor_16BIT_TC_LSB_MASK(ConstScanline pScanline, long nX, const ColorMask& rMask);
     158             :     static BitmapColor GetPixelFor_24BIT_TC_BGR(ConstScanline pScanline, long nX, const ColorMask& rMask);
     159             :     static BitmapColor GetPixelFor_24BIT_TC_RGB(ConstScanline pScanline, long nX, const ColorMask& rMask);
     160             :     static BitmapColor GetPixelFor_24BIT_TC_MASK(ConstScanline pScanline, long nX, const ColorMask& rMask);
     161             :     static BitmapColor GetPixelFor_32BIT_TC_ABGR(ConstScanline pScanline, long nX, const ColorMask& rMask);
     162             :     static BitmapColor GetPixelFor_32BIT_TC_ARGB(ConstScanline pScanline, long nX, const ColorMask& rMask);
     163             :     static BitmapColor GetPixelFor_32BIT_TC_BGRA(ConstScanline pScanline, long nX, const ColorMask& rMask);
     164             :     static BitmapColor GetPixelFor_32BIT_TC_RGBA(ConstScanline pScanline, long nX, const ColorMask& rMask);
     165             :     static BitmapColor GetPixelFor_32BIT_TC_MASK(ConstScanline pScanline, long nX, const ColorMask& rMask);
     166             : 
     167             :     static void SetPixelFor_1BIT_MSB_PAL(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     168             :     static void SetPixelFor_1BIT_LSB_PAL(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     169             :     static void SetPixelFor_4BIT_MSN_PAL(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     170             :     static void SetPixelFor_4BIT_LSN_PAL(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     171             :     static void SetPixelFor_8BIT_PAL(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     172             :     static void SetPixelFor_8BIT_TC_MASK(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     173             :     static void SetPixelFor_16BIT_TC_MSB_MASK(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     174             :     static void SetPixelFor_16BIT_TC_LSB_MASK(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     175             :     static void SetPixelFor_24BIT_TC_BGR(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     176             :     static void SetPixelFor_24BIT_TC_RGB(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     177             :     static void SetPixelFor_24BIT_TC_MASK(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     178             :     static void SetPixelFor_32BIT_TC_ABGR(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     179             :     static void SetPixelFor_32BIT_TC_ARGB(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     180             :     static void SetPixelFor_32BIT_TC_BGRA(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     181             :     static void SetPixelFor_32BIT_TC_RGBA(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     182             :     static void SetPixelFor_32BIT_TC_MASK(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
     183             : 
     184             : protected:
     185             :     BitmapReadAccess(Bitmap& rBitmap, BitmapAccessMode nMode);
     186             : };
     187             : 
     188             : // - BitmapWriteAccess -
     189             : class VCL_DLLPUBLIC BitmapWriteAccess : public BitmapReadAccess
     190             : {
     191             : public:
     192             :     BitmapWriteAccess(Bitmap& rBitmap);
     193             :     virtual ~BitmapWriteAccess();
     194             : 
     195             :     void CopyScanline(long nY, const BitmapReadAccess& rReadAcc);
     196             :     void CopyScanline(long nY,
     197             :                       ConstScanline aSrcScanline,
     198             :                       sal_uLong nSrcScanlineFormat,
     199             :                       sal_uLong nSrcScanlineSize);
     200             : 
     201             :     void CopyBuffer( const BitmapReadAccess& rReadAcc );
     202             : 
     203             :     inline void SetPalette(const BitmapPalette& rPalette);
     204             :     inline void SetPaletteEntryCount(sal_uInt16 nCount);
     205             :     inline void SetPaletteColor(sal_uInt16 nColor, const BitmapColor& rBitmapColor);
     206             : 
     207             :     inline void SetPixel(long nY, long nX, const BitmapColor& rBitmapColor);
     208             :     inline void SetPixelIndex(long nY, long nX, sal_uInt8 cIndex);
     209             : 
     210             :     void SetLineColor(const Color& rColor);
     211             : 
     212             :     void SetFillColor();
     213             :     void SetFillColor(const Color& rColor);
     214             : 
     215             :     void Erase(const Color& rColor);
     216             : 
     217             :     void DrawLine(const Point& rStart, const Point& rEnd);
     218             : 
     219             :     void FillRect(const Rectangle& rRect);
     220             :     void DrawRect(const Rectangle& rRect);
     221             : 
     222             : private:
     223             : 
     224             :     std::unique_ptr<BitmapColor> mpLineColor;
     225             :     std::unique_ptr<BitmapColor> mpFillColor;
     226             : 
     227             :     BitmapWriteAccess()
     228             :     {}
     229             : 
     230             :     BitmapWriteAccess(const BitmapWriteAccess&)
     231             :         : BitmapReadAccess()
     232             :     {}
     233             : 
     234             :     BitmapWriteAccess& operator=(const BitmapWriteAccess&)
     235             :     {
     236             :         return *this;
     237             :     }
     238             : };
     239             : 
     240             : // - Inlines -
     241      888038 : inline bool BitmapInfoAccess::operator!() const
     242             : {
     243      888038 :     return mpBuffer == NULL;
     244             : }
     245             : 
     246      489130 : inline long BitmapInfoAccess::Width() const
     247             : {
     248      489130 :     return mpBuffer ? mpBuffer->mnWidth : 0L;
     249             : }
     250             : 
     251      315533 : inline long BitmapInfoAccess::Height() const
     252             : {
     253      315533 :     return mpBuffer ? mpBuffer->mnHeight : 0L;
     254             : }
     255             : 
     256             : inline Point BitmapInfoAccess::TopLeft() const
     257             : {
     258             :     return Point();
     259             : }
     260             : 
     261             : inline Point BitmapInfoAccess::BottomRight() const
     262             : {
     263             :     return Point(Width() - 1L, Height() - 1L);
     264             : }
     265             : 
     266         634 : inline bool BitmapInfoAccess::IsTopDown() const
     267             : {
     268             :     assert(mpBuffer && "Access is not valid!");
     269             : 
     270         634 :     return mpBuffer && (BMP_SCANLINE_ADJUSTMENT(mpBuffer->mnFormat) == BMP_FORMAT_TOP_DOWN);
     271             : }
     272             : 
     273         633 : inline bool BitmapInfoAccess::IsBottomUp() const
     274             : {
     275         633 :     return !IsTopDown();
     276             : }
     277             : 
     278     1249484 : inline sal_uLong BitmapInfoAccess::GetScanlineFormat() const
     279             : {
     280             :     assert(mpBuffer && "Access is not valid!");
     281             : 
     282     1249484 :     return mpBuffer ? BMP_SCANLINE_FORMAT(mpBuffer->mnFormat) : 0UL;
     283             : }
     284             : 
     285      820244 : inline sal_uLong BitmapInfoAccess::GetScanlineSize() const
     286             : {
     287             :     assert(mpBuffer && "Access is not valid!");
     288             : 
     289      820244 :     return mpBuffer ? mpBuffer->mnScanlineSize : 0UL;
     290             : }
     291             : 
     292       65234 : inline sal_uInt16  BitmapInfoAccess::GetBitCount() const
     293             : {
     294             :     assert(mpBuffer && "Access is not valid!");
     295             : 
     296       65234 :     return mpBuffer ? mpBuffer->mnBitCount : 0;
     297             : }
     298             : 
     299        5135 : inline BitmapColor BitmapInfoAccess::GetBestMatchingColor(const BitmapColor& rBitmapColor)
     300             : {
     301        5135 :     if (HasPalette())
     302        4613 :         return BitmapColor((sal_uInt8) GetBestPaletteIndex(rBitmapColor));
     303             :     else
     304         522 :         return rBitmapColor;
     305             : }
     306             : 
     307  1124265678 : inline bool BitmapInfoAccess::HasPalette() const
     308             : {
     309             :     assert(mpBuffer && "Access is not valid!");
     310             : 
     311  1124265678 :     return mpBuffer && !!mpBuffer->maPalette;
     312             : }
     313             : 
     314      181966 : inline const BitmapPalette& BitmapInfoAccess::GetPalette() const
     315             : {
     316             :     assert(mpBuffer && "Access is not valid!");
     317             : 
     318      181966 :     return mpBuffer->maPalette;
     319             : }
     320             : 
     321      186131 : inline sal_uInt16 BitmapInfoAccess::GetPaletteEntryCount() const
     322             : {
     323             :     assert(HasPalette() && "Bitmap has no palette!");
     324             : 
     325      186131 :     return HasPalette() ? mpBuffer->maPalette.GetEntryCount() : 0;
     326             : }
     327             : 
     328   365565258 : inline const BitmapColor& BitmapInfoAccess::GetPaletteColor( sal_uInt16 nColor ) const
     329             : {
     330             :     assert(mpBuffer && "Access is not valid!");
     331             :     assert(HasPalette() && "Bitmap has no palette!");
     332             : 
     333   365565258 :     return mpBuffer->maPalette[nColor];
     334             : }
     335             : 
     336           0 : inline const BitmapColor& BitmapInfoAccess::GetBestPaletteColor(const BitmapColor& rBitmapColor) const
     337             : {
     338           0 :     return GetPaletteColor(GetBestPaletteIndex(rBitmapColor));
     339             : }
     340             : 
     341             : inline bool BitmapInfoAccess::HasColorMask() const
     342             : {
     343             :     assert(mpBuffer && "Access is not valid!");
     344             : 
     345             :     const sal_uLong nFormat = BMP_SCANLINE_FORMAT(mpBuffer->mnFormat);
     346             : 
     347             :     return nFormat == BMP_FORMAT_8BIT_TC_MASK  ||
     348             :            nFormat == BMP_FORMAT_16BIT_TC_MSB_MASK ||
     349             :            nFormat == BMP_FORMAT_16BIT_TC_LSB_MASK ||
     350             :            nFormat == BMP_FORMAT_24BIT_TC_MASK ||
     351             :            nFormat == BMP_FORMAT_32BIT_TC_MASK;
     352             : }
     353             : 
     354        6315 : inline ColorMask& BitmapInfoAccess::GetColorMask() const
     355             : {
     356             :     assert(mpBuffer && "Access is not valid!");
     357             : 
     358        6315 :     return mpBuffer->maColorMask;
     359             : }
     360             : 
     361       27854 : inline Scanline BitmapReadAccess::GetBuffer() const
     362             : {
     363             :     assert(mpBuffer && "Access is not valid!");
     364             : 
     365       27854 :     return mpBuffer ? mpBuffer->mpBits : NULL;
     366             : }
     367             : 
     368     8602963 : inline Scanline BitmapReadAccess::GetScanline(long nY) const
     369             : {
     370             :     assert(mpBuffer && mpScanBuf && "Access is not valid!");
     371             :     assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
     372             : 
     373     8602963 :     return mpScanBuf[nY];
     374             : }
     375             : 
     376  1780188801 : inline BitmapColor BitmapReadAccess::GetPixel(long nY, long nX) const
     377             : {
     378             :     assert(mpBuffer && mpScanBuf && "Access is not valid!");
     379             :     assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
     380             :     assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
     381             : 
     382  1780188801 :     return mFncGetPixel(mpScanBuf[nY], nX, maColorMask );
     383             : }
     384             : 
     385   363213217 : inline sal_uInt8 BitmapReadAccess::GetPixelIndex(long nY, long nX) const
     386             : {
     387   363213217 :     return GetPixel(nY, nX).GetBlueOrIndex();
     388             : }
     389             : 
     390        4400 : inline BitmapColor BitmapReadAccess::GetPixelFromData(const sal_uInt8* pData, long nX) const
     391             : {
     392             :     assert(pData && "Access is not valid!");
     393             : 
     394        4400 :     return mFncGetPixel( pData, nX, maColorMask );
     395             : }
     396             : 
     397           8 : inline void BitmapReadAccess::SetPixelOnData(sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor)
     398             : {
     399             :     assert(pData && "Access is not valid!");
     400             : 
     401           8 :     mFncSetPixel(pData, nX, rBitmapColor, maColorMask);
     402           8 : }
     403             : 
     404   285474299 : inline BitmapColor BitmapReadAccess::GetColor(long nY, long nX) const
     405             : {
     406   285474299 :     if (HasPalette())
     407     3117066 :         return mpBuffer->maPalette[GetPixelIndex(nY, nX)];
     408             :     else
     409   282357233 :         return GetPixel(nY, nX);
     410             : }
     411             : 
     412     2120935 : inline sal_uInt8 BitmapReadAccess::GetLuminance(long nY, long nX) const
     413             : {
     414     2120935 :     return GetColor(nY, nX).GetLuminance();
     415             : }
     416             : 
     417         576 : inline void BitmapWriteAccess::SetPalette(const BitmapPalette& rPalette)
     418             : {
     419             :     assert(mpBuffer && "Access is not valid!");
     420             : 
     421         576 :     mpBuffer->maPalette = rPalette;
     422         576 : }
     423             : 
     424        2270 : inline void BitmapWriteAccess::SetPaletteEntryCount(sal_uInt16 nCount)
     425             : {
     426             :     assert(mpBuffer && "Access is not valid!");
     427             : 
     428        2270 :     mpBuffer->maPalette.SetEntryCount(nCount);
     429        2270 : }
     430             : 
     431      376544 : inline void BitmapWriteAccess::SetPaletteColor(sal_uInt16 nColor, const BitmapColor& rBitmapColor)
     432             : {
     433             :     assert(mpBuffer && "Access is not valid!");
     434             :     assert(HasPalette() && "Bitmap has no palette!");
     435             : 
     436      376544 :     mpBuffer->maPalette[nColor] = rBitmapColor;
     437      376544 : }
     438             : 
     439   877624518 : inline void BitmapWriteAccess::SetPixel(long nY, long nX, const BitmapColor& rBitmapColor)
     440             : {
     441             :     assert(mpBuffer && "Access is not valid!");
     442             :     assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
     443             :     assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
     444             : 
     445   877624518 :     mFncSetPixel(mpScanBuf[nY], nX, rBitmapColor, maColorMask);
     446   877624518 : }
     447             : 
     448   277199550 : inline void BitmapWriteAccess::SetPixelIndex(long nY, long nX, sal_uInt8 cIndex)
     449             : {
     450   277199550 :     SetPixel(nY, nX, BitmapColor(cIndex));
     451   277199550 : }
     452             : 
     453             : #endif // INCLUDED_VCL_BMPACC_HXX
     454             : 
     455             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11