Avoid crash in printer dlg due to empty QImage
[LibreOffice.git] / include / vcl / bitmapaccess.hxx
blob91343e501ea79d7458b8db4b9d57f2ed8c6de7f1
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 <memory>
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;
37 public:
38 BitmapInfoAccess(Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info);
39 virtual ~BitmapInfoAccess();
41 bool operator!() const
43 return mpBuffer == nullptr;
46 long Width() const
48 return mpBuffer ? mpBuffer->mnWidth : 0L;
51 long Height() const
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
65 return !IsTopDown();
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)
91 if (HasPalette())
92 return BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rBitmapColor)));
93 else
94 return 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;
140 private:
141 BitmapInfoAccess(const BitmapInfoAccess&) = delete;
142 BitmapInfoAccess& operator=(const BitmapInfoAccess&) = delete;
144 protected:
145 Bitmap maBitmap;
146 BitmapBuffer* mpBuffer;
147 ColorMask maColorMask;
148 BitmapAccessMode mnAccessMode;
152 class VCL_DLLPUBLIC BitmapReadAccess : public BitmapInfoAccess
154 friend class BitmapWriteAccess;
156 public:
157 BitmapReadAccess(Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Read);
158 virtual ~BitmapReadAccess() override;
160 Scanline GetBuffer() const
162 assert(mpBuffer && "Access is not valid!");
164 return mpBuffer ? mpBuffer->mpBits : nullptr;
167 Scanline GetScanline(long nY) const
169 assert(mpBuffer && "Access is not valid!");
170 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
172 if (mpBuffer->mnFormat & ScanlineFormat::TopDown)
174 return mpBuffer->mpBits + (nY * mpBuffer->mnScanlineSize);
176 return mpBuffer->mpBits + ((mpBuffer->mnHeight - 1 - nY) * mpBuffer->mnScanlineSize);
179 BitmapColor GetPixelFromData(const sal_uInt8* pData, long nX) const
181 assert(pData && "Access is not valid!");
183 return mFncGetPixel( pData, nX, maColorMask );
186 sal_uInt8 GetIndexFromData(const sal_uInt8* pData, long nX) const
188 return GetPixelFromData( pData, nX ).GetIndex();
191 void SetPixelOnData(sal_uInt8* pData, long nX, const BitmapColor& rBitmapColor)
193 assert(pData && "Access is not valid!");
195 mFncSetPixel(pData, nX, rBitmapColor, maColorMask);
198 BitmapColor GetPixel(long nY, long nX) const
200 assert(mpBuffer && "Access is not valid!");
201 assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!");
202 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!");
204 return mFncGetPixel(GetScanline(nY), nX, maColorMask );
207 BitmapColor GetColor(long nY, long nX) const
209 if (HasPalette())
210 return mpBuffer->maPalette[GetPixelIndex(nY, nX)];
211 else
212 return GetPixel(nY, nX);
215 sal_uInt8 GetPixelIndex(long nY, long nX) const
217 return GetPixel(nY, nX).GetBlueOrIndex();
220 sal_uInt8 GetLuminance(long nY, long nX) const
222 return GetColor(nY, nX).GetLuminance();
225 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
226 BitmapColor GetInterpolatedColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
228 /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
229 inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
230 BitmapColor GetColorWithFallback( double fY, double fX, const BitmapColor& rFallback ) const;
232 private:
233 BitmapReadAccess(const BitmapReadAccess&) = delete;
234 BitmapReadAccess& operator=(const BitmapReadAccess&) = delete;
236 protected:
237 FncGetPixel mFncGetPixel;
238 FncSetPixel mFncSetPixel;
240 SAL_DLLPRIVATE bool ImplSetAccessPointers( ScanlineFormat nFormat );
242 public:
244 SAL_DLLPRIVATE BitmapBuffer* ImplGetBitmapBuffer() const
246 return mpBuffer;
249 static BitmapColor GetPixelForN1BitMsbPal(ConstScanline pScanline, long nX, const ColorMask& rMask);
250 static BitmapColor GetPixelForN1BitLsbPal(ConstScanline pScanline, long nX, const ColorMask& rMask);
251 static BitmapColor GetPixelForN4BitMsnPal(ConstScanline pScanline, long nX, const ColorMask& rMask);
252 static BitmapColor GetPixelForN4BitLsnPal(ConstScanline pScanline, long nX, const ColorMask& rMask);
253 static BitmapColor GetPixelForN8BitPal(ConstScanline pScanline, long nX, const ColorMask& rMask);
254 static BitmapColor GetPixelForN8BitTcMask(ConstScanline pScanline, long nX, const ColorMask& rMask);
255 static BitmapColor GetPixelForN16BitTcMsbMask(ConstScanline pScanline, long nX, const ColorMask& rMask);
256 static BitmapColor GetPixelForN16BitTcLsbMask(ConstScanline pScanline, long nX, const ColorMask& rMask);
257 static BitmapColor GetPixelForN24BitTcBgr(ConstScanline pScanline, long nX, const ColorMask& rMask);
258 static BitmapColor GetPixelForN24BitTcRgb(ConstScanline pScanline, long nX, const ColorMask& rMask);
259 static BitmapColor GetPixelForN32BitTcAbgr(ConstScanline pScanline, long nX, const ColorMask& rMask);
260 static BitmapColor GetPixelForN32BitTcArgb(ConstScanline pScanline, long nX, const ColorMask& rMask);
261 static BitmapColor GetPixelForN32BitTcBgra(ConstScanline pScanline, long nX, const ColorMask& rMask);
262 static BitmapColor GetPixelForN32BitTcRgba(ConstScanline pScanline, long nX, const ColorMask& rMask);
263 static BitmapColor GetPixelForN32BitTcMask(ConstScanline pScanline, long nX, const ColorMask& rMask);
265 static void SetPixelForN1BitMsbPal(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
266 static void SetPixelForN1BitLsbPal(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
267 static void SetPixelForN4BitMsnPal(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
268 static void SetPixelForN4BitLsnPal(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
269 static void SetPixelForN8BitPal(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
270 static void SetPixelForN8BitTcMask(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
271 static void SetPixelForN16BitTcMsbMask(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
272 static void SetPixelForN16BitTcLsbMask(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
273 static void SetPixelForN24BitTcBgr(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
274 static void SetPixelForN24BitTcRgb(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
275 static void SetPixelForN32BitTcAbgr(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
276 static void SetPixelForN32BitTcArgb(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
277 static void SetPixelForN32BitTcBgra(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
278 static void SetPixelForN32BitTcRgba(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
279 static void SetPixelForN32BitTcMask(Scanline pScanline, long nX, const BitmapColor& rBitmapColor, const ColorMask& rMask);
284 #endif // INCLUDED_VCL_BMPACC_HXX
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */