1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
24 #include <tools/solar.h>
25 #include <vcl/dllapi.h>
26 #include <vcl/salbtype.hxx>
27 #include <vcl/bitmap.hxx>
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
);
33 class VCL_DLLPUBLIC BitmapInfoAccess
35 friend class BitmapReadAccess
;
38 BitmapInfoAccess(Bitmap
& rBitmap
);
39 virtual ~BitmapInfoAccess();
41 bool operator!() const
43 return mpBuffer
== nullptr;
48 return mpBuffer
? mpBuffer
->mnWidth
: 0L;
53 return mpBuffer
? mpBuffer
->mnHeight
: 0L;
56 bool IsTopDown() const
58 assert(mpBuffer
&& "Access is not valid!");
60 return mpBuffer
&& (mpBuffer
->mnFormat
& ScanlineFormat::TopDown
);
63 bool IsBottomUp() const
68 ScanlineFormat
GetScanlineFormat() const
70 assert(mpBuffer
&& "Access is not valid!");
72 return mpBuffer
? RemoveScanline(mpBuffer
->mnFormat
) : ScanlineFormat::NONE
;
75 sal_uLong
GetScanlineSize() const
77 assert(mpBuffer
&& "Access is not valid!");
79 return mpBuffer
? mpBuffer
->mnScanlineSize
: 0UL;
82 sal_uInt16
GetBitCount() const
84 assert(mpBuffer
&& "Access is not valid!");
86 return mpBuffer
? mpBuffer
->mnBitCount
: 0;
89 BitmapColor
GetBestMatchingColor(const BitmapColor
& rBitmapColor
)
92 return BitmapColor((sal_uInt8
) GetBestPaletteIndex(rBitmapColor
));
97 bool HasPalette() const
99 assert(mpBuffer
&& "Access is not valid!");
101 return mpBuffer
&& !!mpBuffer
->maPalette
;
104 const BitmapPalette
& GetPalette() const
106 assert(mpBuffer
&& "Access is not valid!");
108 return mpBuffer
->maPalette
;
111 sal_uInt16
GetPaletteEntryCount() const
113 assert(HasPalette() && "Bitmap has no palette!");
115 return HasPalette() ? mpBuffer
->maPalette
.GetEntryCount() : 0;
118 const BitmapColor
& GetPaletteColor( sal_uInt16 nColor
) const
120 assert(mpBuffer
&& "Access is not valid!");
121 assert(HasPalette() && "Bitmap has no palette!");
123 return mpBuffer
->maPalette
[nColor
];
126 const BitmapColor
& GetBestPaletteColor(const BitmapColor
& rBitmapColor
) const
128 return GetPaletteColor(GetBestPaletteIndex(rBitmapColor
));
131 sal_uInt16
GetBestPaletteIndex(const BitmapColor
& rBitmapColor
) const;
133 ColorMask
& GetColorMask() const
135 assert(mpBuffer
&& "Access is not valid!");
137 return mpBuffer
->maColorMask
;
141 BitmapInfoAccess(const BitmapInfoAccess
&) = delete;
142 BitmapInfoAccess
& operator=(const BitmapInfoAccess
&) = delete;
146 BitmapBuffer
* mpBuffer
;
147 ColorMask maColorMask
;
148 BitmapAccessMode mnAccessMode
;
150 SAL_DLLPRIVATE
void ImplCreate(Bitmap
& rBitmap
);
151 SAL_DLLPRIVATE
void ImplDestroy();
154 BitmapInfoAccess(Bitmap
& rBitmap
, BitmapAccessMode nMode
);
158 class VCL_DLLPUBLIC BitmapReadAccess
: public BitmapInfoAccess
160 friend class BitmapWriteAccess
;
163 BitmapReadAccess(Bitmap
& rBitmap
);
164 virtual ~BitmapReadAccess() override
;
166 Scanline
GetBuffer() const
168 assert(mpBuffer
&& "Access is not valid!");
170 return mpBuffer
? mpBuffer
->mpBits
: nullptr;
173 Scanline
GetScanline(long nY
) const
175 assert(mpBuffer
&& "Access is not valid!");
176 assert(nY
< mpBuffer
->mnHeight
&& "y-coordinate out of range!");
178 if (mpBuffer
->mnFormat
& ScanlineFormat::TopDown
)
180 return mpBuffer
->mpBits
+ (nY
* mpBuffer
->mnScanlineSize
);
182 return mpBuffer
->mpBits
+ ((mpBuffer
->mnHeight
- 1 - nY
) * mpBuffer
->mnScanlineSize
);
185 BitmapColor
GetPixelFromData(const sal_uInt8
* pData
, long nX
) const
187 assert(pData
&& "Access is not valid!");
189 return mFncGetPixel( pData
, nX
, maColorMask
);
192 void SetPixelOnData(sal_uInt8
* pData
, long nX
, const BitmapColor
& rBitmapColor
)
194 assert(pData
&& "Access is not valid!");
196 mFncSetPixel(pData
, nX
, rBitmapColor
, maColorMask
);
199 BitmapColor
GetPixel(long nY
, long nX
) const
201 assert(mpBuffer
&& "Access is not valid!");
202 assert(nX
< mpBuffer
->mnWidth
&& "x-coordinate out of range!");
203 assert(nY
< mpBuffer
->mnHeight
&& "y-coordinate out of range!");
205 return mFncGetPixel(GetScanline(nY
), nX
, maColorMask
);
208 BitmapColor
GetColor(long nY
, long nX
) const
211 return mpBuffer
->maPalette
[GetPixelIndex(nY
, nX
)];
213 return GetPixel(nY
, nX
);
216 sal_uInt8
GetPixelIndex(long nY
, long nX
) const
218 return GetPixel(nY
, nX
).GetBlueOrIndex();
221 sal_uInt8
GetLuminance(long nY
, long nX
) const
223 return GetColor(nY
, nX
).GetLuminance();
226 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
227 BitmapColor
GetInterpolatedColorWithFallback( double fY
, double fX
, const BitmapColor
& rFallback
) const;
229 /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
230 inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
231 BitmapColor
GetColorWithFallback( double fY
, double fX
, const BitmapColor
& rFallback
) const;
234 BitmapReadAccess(const BitmapReadAccess
&) = delete;
235 BitmapReadAccess
& operator=(const BitmapReadAccess
&) = delete;
238 FncGetPixel mFncGetPixel
;
239 FncSetPixel mFncSetPixel
;
241 SAL_DLLPRIVATE
void ImplInitScanBuffer( Bitmap
& rBitmap
);
242 SAL_DLLPRIVATE
bool ImplSetAccessPointers( ScanlineFormat nFormat
);
246 SAL_DLLPRIVATE BitmapBuffer
* ImplGetBitmapBuffer() const
251 static BitmapColor
GetPixelForN1BitMsbPal(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
252 static BitmapColor
GetPixelForN1BitLsbPal(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
253 static BitmapColor
GetPixelForN4BitMsnPal(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
254 static BitmapColor
GetPixelForN4BitLsnPal(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
255 static BitmapColor
GetPixelForN8BitPal(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
256 static BitmapColor
GetPixelForN8BitTcMask(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
257 static BitmapColor
GetPixelForN16BitTcMsbMask(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
258 static BitmapColor
GetPixelForN16BitTcLsbMask(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
259 static BitmapColor
GetPixelForN24BitTcBgr(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
260 static BitmapColor
GetPixelForN24BitTcRgb(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
261 static BitmapColor
GetPixelForN32BitTcAbgr(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
262 static BitmapColor
GetPixelForN32BitTcArgb(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
263 static BitmapColor
GetPixelForN32BitTcBgra(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
264 static BitmapColor
GetPixelForN32BitTcRgba(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
265 static BitmapColor
GetPixelForN32BitTcMask(ConstScanline pScanline
, long nX
, const ColorMask
& rMask
);
267 static void SetPixelForN1BitMsbPal(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
268 static void SetPixelForN1BitLsbPal(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
269 static void SetPixelForN4BitMsnPal(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
270 static void SetPixelForN4BitLsnPal(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
271 static void SetPixelForN8BitPal(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
272 static void SetPixelForN8BitTcMask(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
273 static void SetPixelForN16BitTcMsbMask(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
274 static void SetPixelForN16BitTcLsbMask(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
275 static void SetPixelForN24BitTcBgr(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
276 static void SetPixelForN24BitTcRgb(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
277 static void SetPixelForN32BitTcAbgr(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
278 static void SetPixelForN32BitTcArgb(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
279 static void SetPixelForN32BitTcBgra(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
280 static void SetPixelForN32BitTcRgba(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
281 static void SetPixelForN32BitTcMask(Scanline pScanline
, long nX
, const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
284 BitmapReadAccess(Bitmap
& rBitmap
, BitmapAccessMode nMode
);
288 class VCL_DLLPUBLIC BitmapWriteAccess
: public BitmapReadAccess
291 BitmapWriteAccess(Bitmap
& rBitmap
);
292 virtual ~BitmapWriteAccess() override
;
294 void CopyScanline(long nY
, const BitmapReadAccess
& rReadAcc
);
295 void CopyScanline(long nY
,
296 ConstScanline aSrcScanline
,
297 ScanlineFormat nSrcScanlineFormat
,
298 sal_uLong nSrcScanlineSize
);
300 void CopyBuffer( const BitmapReadAccess
& rReadAcc
);
302 void SetPalette(const BitmapPalette
& rPalette
)
304 assert(mpBuffer
&& "Access is not valid!");
306 mpBuffer
->maPalette
= rPalette
;
309 void SetPaletteEntryCount(sal_uInt16 nCount
)
311 assert(mpBuffer
&& "Access is not valid!");
313 mpBuffer
->maPalette
.SetEntryCount(nCount
);
316 void SetPaletteColor(sal_uInt16 nColor
, const BitmapColor
& rBitmapColor
)
318 assert(mpBuffer
&& "Access is not valid!");
319 assert(HasPalette() && "Bitmap has no palette!");
321 mpBuffer
->maPalette
[nColor
] = rBitmapColor
;
324 void SetPixel(long nY
, long nX
, const BitmapColor
& rBitmapColor
)
326 assert(mpBuffer
&& "Access is not valid!");
327 assert(nX
< mpBuffer
->mnWidth
&& "x-coordinate out of range!");
328 assert(nY
< mpBuffer
->mnHeight
&& "y-coordinate out of range!");
330 mFncSetPixel(GetScanline(nY
), nX
, rBitmapColor
, maColorMask
);
333 void SetPixelIndex(long nY
, long nX
, sal_uInt8 cIndex
)
335 SetPixel(nY
, nX
, BitmapColor(cIndex
));
338 void SetLineColor(const Color
& rColor
);
341 void SetFillColor(const Color
& rColor
);
343 void Erase(const Color
& rColor
);
345 void DrawLine(const Point
& rStart
, const Point
& rEnd
);
347 void FillRect(const tools::Rectangle
& rRect
);
348 void DrawRect(const tools::Rectangle
& rRect
);
352 std::unique_ptr
<BitmapColor
> mpLineColor
;
353 std::unique_ptr
<BitmapColor
> mpFillColor
;
355 BitmapWriteAccess() = delete;
356 BitmapWriteAccess(const BitmapWriteAccess
&) = delete;
357 BitmapWriteAccess
& operator=(const BitmapWriteAccess
&) = delete;
360 #endif // INCLUDED_VCL_BMPACC_HXX
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */