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 .
21 #include <vcl/dllapi.h>
22 #include <vcl/bitmap.hxx>
23 #include <vcl/Scanline.hxx>
24 #include <vcl/BitmapBuffer.hxx>
25 #include <vcl/BitmapColor.hxx>
26 #include <vcl/BitmapAccessMode.hxx>
27 #include <vcl/BitmapInfoAccess.hxx>
29 class SAL_DLLPUBLIC_RTTI BitmapReadAccess
: public BitmapInfoAccess
31 friend class BitmapWriteAccess
;
34 VCL_DLLPUBLIC
BitmapReadAccess(const Bitmap
& rBitmap
,
35 BitmapAccessMode nMode
= BitmapAccessMode::Read
);
36 VCL_DLLPUBLIC
BitmapReadAccess(const AlphaMask
& rBitmap
,
37 BitmapAccessMode nMode
= BitmapAccessMode::Read
);
38 VCL_DLLPUBLIC
virtual ~BitmapReadAccess() override
;
40 Scanline
GetBuffer() const
42 assert(mpBuffer
&& "Access is not valid!");
44 return mpBuffer
? mpBuffer
->mpBits
: nullptr;
47 Scanline
GetScanline(tools::Long nY
) const
49 assert(mpBuffer
&& "Access is not valid!");
50 assert(nY
< mpBuffer
->mnHeight
&& "y-coordinate out of range!");
52 if (mpBuffer
->meDirection
== ScanlineDirection::TopDown
)
54 return mpBuffer
->mpBits
+ (nY
* mpBuffer
->mnScanlineSize
);
56 return mpBuffer
->mpBits
+ ((mpBuffer
->mnHeight
- 1 - nY
) * mpBuffer
->mnScanlineSize
);
59 BitmapColor
GetPixelFromData(const sal_uInt8
* pData
, tools::Long nX
) const
61 assert(pData
&& "Access is not valid!");
63 return mFncGetPixel(pData
, nX
, maColorMask
);
66 sal_uInt8
GetIndexFromData(const sal_uInt8
* pData
, tools::Long nX
) const
68 return GetPixelFromData(pData
, nX
).GetIndex();
71 void SetPixelOnData(sal_uInt8
* pData
, tools::Long nX
, const BitmapColor
& rBitmapColor
)
73 assert(pData
&& "Access is not valid!");
75 mFncSetPixel(pData
, nX
, rBitmapColor
, maColorMask
);
78 BitmapColor
GetPixel(tools::Long nY
, tools::Long nX
) const
80 assert(mpBuffer
&& "Access is not valid!");
81 assert(nX
< mpBuffer
->mnWidth
&& "x-coordinate out of range!");
83 return GetPixelFromData(GetScanline(nY
), nX
);
86 BitmapColor
GetPixel(const Point
& point
) const { return GetPixel(point
.Y(), point
.X()); }
88 BitmapColor
GetColorFromData(sal_uInt8
* pData
, tools::Long nX
) const
91 return GetPaletteColor(GetIndexFromData(pData
, nX
));
93 return GetPixelFromData(pData
, nX
);
96 BitmapColor
GetColor(tools::Long nY
, tools::Long nX
) const
98 assert(mpBuffer
&& "Access is not valid!");
99 assert(nX
< mpBuffer
->mnWidth
&& "x-coordinate out of range!");
100 return GetColorFromData(GetScanline(nY
), nX
);
103 BitmapColor
GetColor(const Point
& point
) const { return GetColor(point
.Y(), point
.X()); }
105 sal_uInt8
GetPixelIndex(tools::Long nY
, tools::Long nX
) const
107 return GetPixel(nY
, nX
).GetIndex();
110 sal_uInt8
GetPixelIndex(const Point
& point
) const
112 return GetPixelIndex(point
.Y(), point
.X());
115 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */
116 BitmapColor
GetInterpolatedColorWithFallback(double fY
, double fX
,
117 const BitmapColor
& rFallback
) const;
119 /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct
120 inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */
121 BitmapColor
GetColorWithFallback(double fY
, double fX
, const BitmapColor
& rFallback
) const;
124 BitmapReadAccess(const BitmapReadAccess
&) = delete;
125 BitmapReadAccess
& operator=(const BitmapReadAccess
&) = delete;
128 FncGetPixel mFncGetPixel
;
129 FncSetPixel mFncSetPixel
;
132 BitmapBuffer
* ImplGetBitmapBuffer() const { return mpBuffer
; }
134 static BitmapColor
GetPixelForN1BitMsbPal(ConstScanline pScanline
, tools::Long nX
,
135 const ColorMask
& rMask
);
136 static BitmapColor
GetPixelForN8BitPal(ConstScanline pScanline
, tools::Long nX
,
137 const ColorMask
& rMask
);
138 static BitmapColor
GetPixelForN24BitTcBgr(ConstScanline pScanline
, tools::Long nX
,
139 const ColorMask
& rMask
);
140 static BitmapColor
GetPixelForN24BitTcRgb(ConstScanline pScanline
, tools::Long nX
,
141 const ColorMask
& rMask
);
142 static BitmapColor
GetPixelForN32BitTcAbgr(ConstScanline pScanline
, tools::Long nX
,
143 const ColorMask
& rMask
);
144 static BitmapColor
GetPixelForN32BitTcXbgr(ConstScanline pScanline
, tools::Long nX
,
145 const ColorMask
& rMask
);
146 static BitmapColor
GetPixelForN32BitTcArgb(ConstScanline pScanline
, tools::Long nX
,
147 const ColorMask
& rMask
);
148 static BitmapColor
GetPixelForN32BitTcXrgb(ConstScanline pScanline
, tools::Long nX
,
149 const ColorMask
& rMask
);
150 static BitmapColor
GetPixelForN32BitTcBgra(ConstScanline pScanline
, tools::Long nX
,
151 const ColorMask
& rMask
);
152 static BitmapColor
GetPixelForN32BitTcBgrx(ConstScanline pScanline
, tools::Long nX
,
153 const ColorMask
& rMask
);
154 static BitmapColor
GetPixelForN32BitTcRgba(ConstScanline pScanline
, tools::Long nX
,
155 const ColorMask
& rMask
);
156 static BitmapColor
GetPixelForN32BitTcRgbx(ConstScanline pScanline
, tools::Long nX
,
157 const ColorMask
& rMask
);
158 static BitmapColor
GetPixelForN32BitTcMask(ConstScanline pScanline
, tools::Long nX
,
159 const ColorMask
& rMask
);
161 static void SetPixelForN1BitMsbPal(Scanline pScanline
, tools::Long nX
,
162 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
163 static void SetPixelForN8BitPal(Scanline pScanline
, tools::Long nX
,
164 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
165 static void SetPixelForN24BitTcBgr(Scanline pScanline
, tools::Long nX
,
166 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
167 static void SetPixelForN24BitTcRgb(Scanline pScanline
, tools::Long nX
,
168 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
169 static void SetPixelForN32BitTcAbgr(Scanline pScanline
, tools::Long nX
,
170 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
171 static void SetPixelForN32BitTcXbgr(Scanline pScanline
, tools::Long nX
,
172 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
173 static void SetPixelForN32BitTcArgb(Scanline pScanline
, tools::Long nX
,
174 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
175 static void SetPixelForN32BitTcXrgb(Scanline pScanline
, tools::Long nX
,
176 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
177 static void SetPixelForN32BitTcBgra(Scanline pScanline
, tools::Long nX
,
178 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
179 static void SetPixelForN32BitTcBgrx(Scanline pScanline
, tools::Long nX
,
180 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
181 static void SetPixelForN32BitTcRgba(Scanline pScanline
, tools::Long nX
,
182 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
183 static void SetPixelForN32BitTcRgbx(Scanline pScanline
, tools::Long nX
,
184 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
185 static void SetPixelForN32BitTcMask(Scanline pScanline
, tools::Long nX
,
186 const BitmapColor
& rBitmapColor
, const ColorMask
& rMask
);
188 static FncGetPixel
GetPixelFunction(ScanlineFormat nFormat
);
189 static FncSetPixel
SetPixelFunction(ScanlineFormat nFormat
);
192 class BitmapScopedReadAccess
195 BitmapScopedReadAccess(const Bitmap
& rBitmap
)
199 BitmapScopedReadAccess(const AlphaMask
& rBitmap
)
203 BitmapScopedReadAccess() {}
205 BitmapScopedReadAccess
& operator=(const Bitmap
& rBitmap
)
207 moAccess
.emplace(rBitmap
);
211 BitmapScopedReadAccess
& operator=(const AlphaMask
& rBitmap
)
213 moAccess
.emplace(rBitmap
);
217 bool operator!() const { return !moAccess
.has_value() || !*moAccess
; }
218 explicit operator bool() const { return moAccess
&& bool(*moAccess
); }
220 void reset() { moAccess
.reset(); }
222 BitmapReadAccess
* get() { return moAccess
? &*moAccess
: nullptr; }
223 const BitmapReadAccess
* get() const { return moAccess
? &*moAccess
: nullptr; }
225 BitmapReadAccess
* operator->() { return &*moAccess
; }
226 const BitmapReadAccess
* operator->() const { return &*moAccess
; }
228 BitmapReadAccess
& operator*() { return *moAccess
; }
229 const BitmapReadAccess
& operator*() const { return *moAccess
; }
232 std::optional
<BitmapReadAccess
> moAccess
;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */