Branch libreoffice-5-0-4
[LibreOffice.git] / include / vcl / bmpacc.hxx
blobebc00c75c4c4bd4433aed9104afc1001bf136eeb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_BMPACC_HXX
21 #define INCLUDED_VCL_BMPACC_HXX
23 #include <tools/solar.h>
24 #include <vcl/dllapi.h>
25 #include <vcl/salbtype.hxx>
26 #include <vcl/bitmap.hxx>
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);
32 // - BitmapInfoAccess -
33 class VCL_DLLPUBLIC BitmapInfoAccess
35 friend class BitmapReadAccess;
37 public:
38 BitmapInfoAccess(Bitmap& rBitmap);
39 virtual ~BitmapInfoAccess();
41 inline bool operator!() const;
43 inline long Width() const;
44 inline long Height() const;
45 inline Point TopLeft() const;
46 inline Point BottomRight() const;
48 inline bool IsTopDown() const;
49 inline bool IsBottomUp() const;
51 inline sal_uLong GetScanlineFormat() const;
52 inline sal_uLong GetScanlineSize() const;
54 inline sal_uInt16 GetBitCount() const;
55 inline BitmapColor GetBestMatchingColor(const BitmapColor& rBitmapColor);
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;
64 inline bool HasColorMask() const;
65 inline ColorMask& GetColorMask() const;
67 private:
68 BitmapInfoAccess()
71 BitmapInfoAccess(const BitmapInfoAccess&)
74 BitmapInfoAccess& operator=(const BitmapInfoAccess&)
76 return *this;
79 protected:
80 Bitmap maBitmap;
81 BitmapBuffer* mpBuffer;
82 ColorMask maColorMask;
83 BitmapAccessMode mnAccessMode;
85 SAL_DLLPRIVATE void ImplCreate(Bitmap& rBitmap);
86 SAL_DLLPRIVATE void ImplDestroy();
88 protected:
89 BitmapInfoAccess(Bitmap& rBitmap, BitmapAccessMode nMode);
92 // - BitmapReadAccess -
93 class VCL_DLLPUBLIC BitmapReadAccess : public BitmapInfoAccess
95 friend class BitmapWriteAccess;
97 public:
98 BitmapReadAccess(Bitmap& rBitmap);
99 virtual ~BitmapReadAccess();
101 inline Scanline GetBuffer() const;
102 inline Scanline GetScanline( long nY ) const;
104 inline BitmapColor GetPixelFromData( const sal_uInt8* pData, long nX ) const;
105 inline void SetPixelOnData( sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor );
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;
112 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
113 BitmapColor GetInterpolatedColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
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;
119 private:
121 BitmapReadAccess()
124 BitmapReadAccess(const BitmapReadAccess&)
125 : BitmapInfoAccess()
128 BitmapReadAccess& operator=(const BitmapReadAccess&)
130 return *this;
133 protected:
134 Scanline* mpScanBuf;
135 FncGetPixel mFncGetPixel;
136 FncSetPixel mFncSetPixel;
138 SAL_DLLPRIVATE void ImplInitScanBuffer( Bitmap& rBitmap );
139 SAL_DLLPRIVATE void ImplClearScanBuffer();
140 SAL_DLLPRIVATE bool ImplSetAccessPointers( sal_uLong nFormat );
142 public:
144 SAL_DLLPRIVATE void ImplZeroInitUnusedBits();
145 SAL_DLLPRIVATE BitmapBuffer* ImplGetBitmapBuffer() const
147 return mpBuffer;
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);
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);
184 protected:
185 BitmapReadAccess(Bitmap& rBitmap, BitmapAccessMode nMode);
188 // - BitmapWriteAccess -
189 class VCL_DLLPUBLIC BitmapWriteAccess : public BitmapReadAccess
191 public:
192 BitmapWriteAccess(Bitmap& rBitmap);
193 virtual ~BitmapWriteAccess();
195 void CopyScanline(long nY, const BitmapReadAccess& rReadAcc);
196 void CopyScanline(long nY,
197 ConstScanline aSrcScanline,
198 sal_uLong nSrcScanlineFormat,
199 sal_uLong nSrcScanlineSize);
201 void CopyBuffer( const BitmapReadAccess& rReadAcc );
203 inline void SetPalette(const BitmapPalette& rPalette);
204 inline void SetPaletteEntryCount(sal_uInt16 nCount);
205 inline void SetPaletteColor(sal_uInt16 nColor, const BitmapColor& rBitmapColor);
207 inline void SetPixel(long nY, long nX, const BitmapColor& rBitmapColor);
208 inline void SetPixelIndex(long nY, long nX, sal_uInt8 cIndex);
210 void SetLineColor(const Color& rColor);
212 void SetFillColor();
213 void SetFillColor(const Color& rColor);
215 void Erase(const Color& rColor);
217 void DrawLine(const Point& rStart, const Point& rEnd);
219 void FillRect(const Rectangle& rRect);
220 void DrawRect(const Rectangle& rRect);
222 private:
224 std::unique_ptr<BitmapColor> mpLineColor;
225 std::unique_ptr<BitmapColor> mpFillColor;
227 BitmapWriteAccess()
230 BitmapWriteAccess(const BitmapWriteAccess&)
231 : BitmapReadAccess()
234 BitmapWriteAccess& operator=(const BitmapWriteAccess&)
236 return *this;
240 // - Inlines -
241 inline bool BitmapInfoAccess::operator!() const
243 return mpBuffer == NULL;
246 inline long BitmapInfoAccess::Width() const
248 return mpBuffer ? mpBuffer->mnWidth : 0L;
251 inline long BitmapInfoAccess::Height() const
253 return mpBuffer ? mpBuffer->mnHeight : 0L;
256 inline Point BitmapInfoAccess::TopLeft() const
258 return Point();
261 inline Point BitmapInfoAccess::BottomRight() const
263 return Point(Width() - 1L, Height() - 1L);
266 inline bool BitmapInfoAccess::IsTopDown() const
268 assert(mpBuffer && "Access is not valid!");
270 return mpBuffer && (BMP_SCANLINE_ADJUSTMENT(mpBuffer->mnFormat) == BMP_FORMAT_TOP_DOWN);
273 inline bool BitmapInfoAccess::IsBottomUp() const
275 return !IsTopDown();
278 inline sal_uLong BitmapInfoAccess::GetScanlineFormat() const
280 assert(mpBuffer && "Access is not valid!");
282 return mpBuffer ? BMP_SCANLINE_FORMAT(mpBuffer->mnFormat) : 0UL;
285 inline sal_uLong BitmapInfoAccess::GetScanlineSize() const
287 assert(mpBuffer && "Access is not valid!");
289 return mpBuffer ? mpBuffer->mnScanlineSize : 0UL;
292 inline sal_uInt16 BitmapInfoAccess::GetBitCount() const
294 assert(mpBuffer && "Access is not valid!");
296 return mpBuffer ? mpBuffer->mnBitCount : 0;
299 inline BitmapColor BitmapInfoAccess::GetBestMatchingColor(const BitmapColor& rBitmapColor)
301 if (HasPalette())
302 return BitmapColor((sal_uInt8) GetBestPaletteIndex(rBitmapColor));
303 else
304 return rBitmapColor;
307 inline bool BitmapInfoAccess::HasPalette() const
309 assert(mpBuffer && "Access is not valid!");
311 return mpBuffer && !!mpBuffer->maPalette;
314 inline const BitmapPalette& BitmapInfoAccess::GetPalette() const
316 assert(mpBuffer && "Access is not valid!");
318 return mpBuffer->maPalette;
321 inline sal_uInt16 BitmapInfoAccess::GetPaletteEntryCount() const
323 assert(HasPalette() && "Bitmap has no palette!");
325 return HasPalette() ? mpBuffer->maPalette.GetEntryCount() : 0;
328 inline const BitmapColor& BitmapInfoAccess::GetPaletteColor( sal_uInt16 nColor ) const
330 assert(mpBuffer && "Access is not valid!");
331 assert(HasPalette() && "Bitmap has no palette!");
333 return mpBuffer->maPalette[nColor];
336 inline const BitmapColor& BitmapInfoAccess::GetBestPaletteColor(const BitmapColor& rBitmapColor) const
338 return GetPaletteColor(GetBestPaletteIndex(rBitmapColor));
341 inline bool BitmapInfoAccess::HasColorMask() const
343 assert(mpBuffer && "Access is not valid!");
345 const sal_uLong nFormat = BMP_SCANLINE_FORMAT(mpBuffer->mnFormat);
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;
354 inline ColorMask& BitmapInfoAccess::GetColorMask() const
356 assert(mpBuffer && "Access is not valid!");
358 return mpBuffer->maColorMask;
361 inline Scanline BitmapReadAccess::GetBuffer() const
363 assert(mpBuffer && "Access is not valid!");
365 return mpBuffer ? mpBuffer->mpBits : NULL;
368 inline Scanline BitmapReadAccess::GetScanline(long nY) const
370 assert(mpBuffer && mpScanBuf && "Access is not valid!");
371 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
373 return mpScanBuf[nY];
376 inline BitmapColor BitmapReadAccess::GetPixel(long nY, long nX) const
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!");
382 return mFncGetPixel(mpScanBuf[nY], nX, maColorMask );
385 inline sal_uInt8 BitmapReadAccess::GetPixelIndex(long nY, long nX) const
387 return GetPixel(nY, nX).GetBlueOrIndex();
390 inline BitmapColor BitmapReadAccess::GetPixelFromData(const sal_uInt8* pData, long nX) const
392 assert(pData && "Access is not valid!");
394 return mFncGetPixel( pData, nX, maColorMask );
397 inline void BitmapReadAccess::SetPixelOnData(sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor)
399 assert(pData && "Access is not valid!");
401 mFncSetPixel(pData, nX, rBitmapColor, maColorMask);
404 inline BitmapColor BitmapReadAccess::GetColor(long nY, long nX) const
406 if (HasPalette())
407 return mpBuffer->maPalette[GetPixelIndex(nY, nX)];
408 else
409 return GetPixel(nY, nX);
412 inline sal_uInt8 BitmapReadAccess::GetLuminance(long nY, long nX) const
414 return GetColor(nY, nX).GetLuminance();
417 inline void BitmapWriteAccess::SetPalette(const BitmapPalette& rPalette)
419 assert(mpBuffer && "Access is not valid!");
421 mpBuffer->maPalette = rPalette;
424 inline void BitmapWriteAccess::SetPaletteEntryCount(sal_uInt16 nCount)
426 assert(mpBuffer && "Access is not valid!");
428 mpBuffer->maPalette.SetEntryCount(nCount);
431 inline void BitmapWriteAccess::SetPaletteColor(sal_uInt16 nColor, const BitmapColor& rBitmapColor)
433 assert(mpBuffer && "Access is not valid!");
434 assert(HasPalette() && "Bitmap has no palette!");
436 mpBuffer->maPalette[nColor] = rBitmapColor;
439 inline void BitmapWriteAccess::SetPixel(long nY, long nX, const BitmapColor& rBitmapColor)
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!");
445 mFncSetPixel(mpScanBuf[nY], nX, rBitmapColor, maColorMask);
448 inline void BitmapWriteAccess::SetPixelIndex(long nY, long nX, sal_uInt8 cIndex)
450 SetPixel(nY, nX, BitmapColor(cIndex));
453 #endif // INCLUDED_VCL_BMPACC_HXX
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */