LibreOffice Module vcl (master)  1
BitmapTools.hxx
Go to the documentation of this file.
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 
10 #ifndef INCLUDED_VCL_BITMAP_TOOLS_HXX
11 #define INCLUDED_VCL_BITMAP_TOOLS_HXX
12 
13 #include <config_cairo_canvas.h>
14 #include <vcl/bitmapex.hxx>
15 #include <vcl/ImageTree.hxx>
16 #include <vcl/BitmapColor.hxx>
17 #if ENABLE_CAIRO_CANVAS
18 #include <vcl/cairo.hxx>
19 #endif
21 #include <o3tl/safeint.hxx>
22 #include <array>
23 
24 class SvStream;
25 namespace basegfx { class B2DHomMatrix; }
26 namespace com { namespace sun { namespace star { namespace geometry { struct IntegerRectangle2D; } } } }
27 
28 namespace vcl {
29 namespace bitmap {
30 
31 typedef sal_uInt8 (*lookup_table)[256];
32 
35 
38 
43 {
44 friend BitmapEx VCL_DLLPUBLIC CreateFromData( RawBitmap&& rawBitmap );
45  std::unique_ptr<sal_uInt8[]> mpData;
46  Size const maSize;
48 public:
49  RawBitmap(Size const & rSize, sal_uInt8 nBitCount)
50  : maSize(rSize),
51  mnBitCount(nBitCount)
52  {
53  assert(nBitCount == 24 || nBitCount == 32);
54  sal_Int32 nRowSize, nDataSize;
55  if (o3tl::checked_multiply<sal_Int32>(rSize.getWidth(), nBitCount/8, nRowSize) ||
56  o3tl::checked_multiply<sal_Int32>(nRowSize, rSize.getHeight(), nDataSize))
57  {
58  throw std::bad_alloc();
59  }
60  mpData.reset(new sal_uInt8[nDataSize]);
61  }
62  void SetPixel(long nY, long nX, Color nColor)
63  {
64  long p = (nY * maSize.getWidth() + nX) * (mnBitCount/8);
65  mpData[ p++ ] = nColor.GetRed();
66  mpData[ p++ ] = nColor.GetGreen();
67  mpData[ p++ ] = nColor.GetBlue();
68  if (mnBitCount == 32)
69  mpData[ p ] = nColor.GetTransparency();
70  }
71  Color GetPixel(long nY, long nX) const
72  {
73  long p = (nY * maSize.getWidth() + nX) * mnBitCount/8;
74  if (mnBitCount == 24)
75  return Color( mpData[p], mpData[p+1], mpData[p+2]);
76  else
77  return Color( mpData[p+3], mpData[p], mpData[p+1], mpData[p+2]);
78  }
79  // so we don't accidentally leave any code in that uses palette color indexes
80  void SetPixel(long nY, long nX, BitmapColor nColor) = delete;
81  long Height() { return maSize.Height(); }
82  long Width() { return maSize.Width(); }
83  sal_uInt8 GetBitCount() const { return mnBitCount; }
84 };
85 
86 BitmapEx VCL_DLLPUBLIC loadFromName(const OUString& rFileName, const ImageLoadFlags eFlags = ImageLoadFlags::NONE);
87 
88 void loadFromSvg(SvStream& rStream, const OUString& sPath, BitmapEx& rBitmapEx, double fScaleFactor);
89 
98 BitmapEx VCL_DLLPUBLIC CreateFromData( sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_uInt16 nBitCount );
99 
100 BitmapEx VCL_DLLPUBLIC CreateFromData( RawBitmap && data );
101 
102 #if ENABLE_CAIRO_CANVAS
103 VCL_DLLPUBLIC BitmapEx* CreateFromCairoSurface(Size size, cairo_surface_t* pSurface);
104 #endif
105 
107  const ::basegfx::B2DHomMatrix& rTransform,
108  ::basegfx::B2DRectangle const & rDestRect,
109  ::basegfx::B2DHomMatrix const & rLocalTransform );
110 
111 VCL_DLLPUBLIC void DrawAlphaBitmapAndAlphaGradient(BitmapEx & rBitmapEx, bool bFixedTransparence, float fTransparence, AlphaMask & rNewMask);
112 
113 VCL_DLLPUBLIC void DrawAndClipBitmap(const Point& rPos, const Size& rSize, const BitmapEx& rBitmap, BitmapEx & aBmpEx, basegfx::B2DPolyPolygon const & rClipPath);
114 
115 VCL_DLLPUBLIC css::uno::Sequence< sal_Int8 > GetMaskDIB(BitmapEx const & aBmpEx);
116 
121 VCL_DLLPUBLIC void CanvasCairoExtractBitmapData( BitmapEx const & rBmpEx, Bitmap & rBitmap, unsigned char*& data, bool& bHasAlpha, long& rnWidth, long& rnHeight );
122 
123 VCL_DLLPUBLIC css::uno::Sequence< sal_Int8 > CanvasExtractBitmapData(BitmapEx const & rBitmapEx, const css::geometry::IntegerRectangle2D& rect);
124 
125 // helper to construct historical 8x8 bitmaps with two colors
126 
127 BitmapEx VCL_DLLPUBLIC createHistorical8x8FromArray(std::array<sal_uInt8,64> const & pArray, Color aColorPix, Color aColorBack);
128 bool VCL_DLLPUBLIC isHistorical8x8(const BitmapEx& rBitmapEx, Color& o_rBack, Color& o_rFront);
129 
130 VCL_DLLPUBLIC bool convertBitmap32To24Plus8(BitmapEx const & rInput, BitmapEx & rResult);
131 
132 }} // end vcl::bitmap
133 
134 #endif // INCLUDED_VCL_BITMAP_TOOLS_HXX
135 
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void loadFromSvg(SvStream &rStream, const OUString &sPath, BitmapEx &rBitmapEx, double fScalingFactor)
Definition: BitmapTools.cxx:61
sal_uInt8 GetBitCount() const
Definition: BitmapTools.hxx:83
ImageLoadFlags
Definition: ImageTree.hxx:31
sal_uInt8 GetRed() const
void DrawAlphaBitmapAndAlphaGradient(BitmapEx &rBitmapEx, bool bFixedTransparence, float fTransparence, AlphaMask &rNewMask)
long Height() const
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
sal_uInt8 GetTransparency() const
lookup_table get_unpremultiply_table()
sal_uInt8(* lookup_table)[256]
Definition: BitmapTools.hxx:31
void CanvasCairoExtractBitmapData(BitmapEx const &aBmpEx, Bitmap &aBitmap, unsigned char *&data, bool &bHasAlpha, long &rnWidth, long &rnHeight)
RawBitmap(Size const &rSize, sal_uInt8 nBitCount)
Definition: BitmapTools.hxx:49
void SetPixel(long nY, long nX, Color nColor)
Definition: BitmapTools.hxx:62
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_uInt16 nBitCount)
Copy block of image data into the bitmap.
void DrawAndClipBitmap(const Point &rPos, const Size &rSize, const BitmapEx &rBitmap, BitmapEx &aBmpEx, basegfx::B2DPolyPolygon const &rClipPath)
sal_uInt8 premultiply(sal_uInt8 c, sal_uInt8 a)
sal_uInt8 GetBlue() const
sal_uInt8 unpremultiply(sal_uInt8 c, sal_uInt8 a)
bool isHistorical8x8(const BitmapEx &rBitmapEx, Color &o_rBack, Color &o_rFront)
Color GetPixel(long nY, long nX) const
Definition: BitmapTools.hxx:71
css::uno::Sequence< sal_Int8 > GetMaskDIB(BitmapEx const &aBmpEx)
uno::Sequence< sal_Int8 > CanvasExtractBitmapData(BitmapEx const &rBitmapEx, const geometry::IntegerRectangle2D &rect)
Intended to be used to feed into CreateFromData to create a BitmapEx.
Definition: BitmapTools.hxx:42
BitmapEx CanvasTransformBitmap(const BitmapEx &rBitmap, const ::basegfx::B2DHomMatrix &rTransform,::basegfx::B2DRectangle const &rDestRect,::basegfx::B2DHomMatrix const &rLocalTransform)
sal_uInt8 GetGreen() const
BitmapEx createHistorical8x8FromArray(std::array< sal_uInt8, 64 > const &pArray, Color aColorPix, Color aColorBack)
lookup_table get_premultiply_table()
sal_uInt8 const mnBitCount
Definition: BitmapTools.hxx:47
std::unique_ptr< sal_uInt8[]> mpData
Definition: BitmapTools.hxx:45
unsigned char sal_uInt8
bool convertBitmap32To24Plus8(BitmapEx const &rInput, BitmapEx &rResult)
BitmapEx loadFromName(const OUString &rFileName, const ImageLoadFlags eFlags)
Definition: BitmapTools.cxx:50
long getHeight() const
struct _cairo_surface cairo_surface_t
Definition: svpgdi.hxx:70
long getWidth() const