/src/libreoffice/include/vcl/BitmapReadAccess.hxx
Line | Count | Source |
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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | #pragma once |
20 | | |
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> |
28 | | |
29 | | class SAL_DLLPUBLIC_RTTI BitmapReadAccess : public BitmapInfoAccess |
30 | | { |
31 | | friend class BitmapWriteAccess; |
32 | | |
33 | | public: |
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; |
39 | | |
40 | | Scanline GetBuffer() const |
41 | 9.96k | { |
42 | 9.96k | assert(mpBuffer && "Access is not valid!"); |
43 | | |
44 | 9.96k | return mpBuffer ? mpBuffer->mpBits : nullptr; |
45 | 9.96k | } |
46 | | |
47 | | Scanline GetScanline(tools::Long nY) const |
48 | 12.8G | { |
49 | 12.8G | assert(mpBuffer && "Access is not valid!"); |
50 | 12.8G | assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!"); |
51 | | |
52 | 12.8G | if (mpBuffer->meDirection == ScanlineDirection::TopDown) |
53 | 12.8G | { |
54 | 12.8G | return mpBuffer->mpBits + (nY * mpBuffer->mnScanlineSize); |
55 | 12.8G | } |
56 | 0 | return mpBuffer->mpBits + ((mpBuffer->mnHeight - 1 - nY) * mpBuffer->mnScanlineSize); |
57 | 12.8G | } |
58 | | |
59 | | BitmapColor GetPixelFromData(const sal_uInt8* pData, tools::Long nX) const |
60 | 4.53G | { |
61 | 4.53G | assert(pData && "Access is not valid!"); |
62 | | |
63 | 4.53G | return mFncGetPixel(pData, nX); |
64 | 4.53G | } |
65 | | |
66 | | sal_uInt8 GetIndexFromData(const sal_uInt8* pData, tools::Long nX) const |
67 | 1.34G | { |
68 | 1.34G | return GetPixelFromData(pData, nX).GetIndex(); |
69 | 1.34G | } |
70 | | |
71 | | void SetPixelOnData(sal_uInt8* pData, tools::Long nX, const BitmapColor& rBitmapColor) |
72 | 5.75G | { |
73 | 5.75G | assert(pData && "Access is not valid!"); |
74 | | |
75 | 5.75G | mFncSetPixel(pData, nX, rBitmapColor); |
76 | 5.75G | } |
77 | | |
78 | | BitmapColor GetPixel(tools::Long nY, tools::Long nX) const |
79 | 7.85M | { |
80 | 7.85M | assert(mpBuffer && "Access is not valid!"); |
81 | 7.85M | assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!"); |
82 | | |
83 | 7.85M | return GetPixelFromData(GetScanline(nY), nX); |
84 | 7.85M | } |
85 | | |
86 | 0 | BitmapColor GetPixel(const Point& point) const { return GetPixel(point.Y(), point.X()); } |
87 | | |
88 | | BitmapColor GetColorFromData(const sal_uInt8* pData, tools::Long nX) const |
89 | 27.6M | { |
90 | 27.6M | if (HasPalette()) |
91 | 26.2M | return GetPaletteColor(GetIndexFromData(pData, nX)); |
92 | 1.40M | else |
93 | 1.40M | return GetPixelFromData(pData, nX); |
94 | 27.6M | } |
95 | | |
96 | | BitmapColor GetColor(tools::Long nY, tools::Long nX) const |
97 | 27.6M | { |
98 | 27.6M | assert(mpBuffer && "Access is not valid!"); |
99 | 27.6M | assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!"); |
100 | 27.6M | return GetColorFromData(GetScanline(nY), nX); |
101 | 27.6M | } |
102 | | |
103 | 0 | BitmapColor GetColor(const Point& point) const { return GetColor(point.Y(), point.X()); } |
104 | | |
105 | | sal_uInt8 GetPixelIndex(tools::Long nY, tools::Long nX) const |
106 | 0 | { |
107 | 0 | return GetPixel(nY, nX).GetIndex(); |
108 | 0 | } |
109 | | |
110 | | sal_uInt8 GetPixelIndex(const Point& point) const |
111 | 0 | { |
112 | 0 | return GetPixelIndex(point.Y(), point.X()); |
113 | 0 | } |
114 | | |
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; |
118 | | |
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; |
122 | | |
123 | | private: |
124 | | BitmapReadAccess(const BitmapReadAccess&) = delete; |
125 | | BitmapReadAccess& operator=(const BitmapReadAccess&) = delete; |
126 | | |
127 | | protected: |
128 | | FncGetPixel mFncGetPixel; |
129 | | FncSetPixel mFncSetPixel; |
130 | | |
131 | | public: |
132 | 0 | BitmapBuffer* ImplGetBitmapBuffer() const { return mpBuffer; } |
133 | | |
134 | | static BitmapColor GetPixelForN8BitPal(ConstScanline pScanline, tools::Long nX); |
135 | | static BitmapColor GetPixelForN24BitTcBgr(ConstScanline pScanline, tools::Long nX); |
136 | | static BitmapColor GetPixelForN24BitTcRgb(ConstScanline pScanline, tools::Long nX); |
137 | | static BitmapColor GetPixelForN32BitTcAbgr(ConstScanline pScanline, tools::Long nX); |
138 | | static BitmapColor GetPixelForN32BitTcXbgr(ConstScanline pScanline, tools::Long nX); |
139 | | static BitmapColor GetPixelForN32BitTcArgb(ConstScanline pScanline, tools::Long nX); |
140 | | static BitmapColor GetPixelForN32BitTcXrgb(ConstScanline pScanline, tools::Long nX); |
141 | | static BitmapColor GetPixelForN32BitTcBgra(ConstScanline pScanline, tools::Long nX); |
142 | | static BitmapColor GetPixelForN32BitTcBgrx(ConstScanline pScanline, tools::Long nX); |
143 | | static BitmapColor GetPixelForN32BitTcRgba(ConstScanline pScanline, tools::Long nX); |
144 | | static BitmapColor GetPixelForN32BitTcRgbx(ConstScanline pScanline, tools::Long nX); |
145 | | |
146 | | static void SetPixelForN8BitPal(Scanline pScanline, tools::Long nX, |
147 | | const BitmapColor& rBitmapColor); |
148 | | static void SetPixelForN24BitTcBgr(Scanline pScanline, tools::Long nX, |
149 | | const BitmapColor& rBitmapColor); |
150 | | static void SetPixelForN24BitTcRgb(Scanline pScanline, tools::Long nX, |
151 | | const BitmapColor& rBitmapColor); |
152 | | static void SetPixelForN32BitTcAbgr(Scanline pScanline, tools::Long nX, |
153 | | const BitmapColor& rBitmapColor); |
154 | | static void SetPixelForN32BitTcXbgr(Scanline pScanline, tools::Long nX, |
155 | | const BitmapColor& rBitmapColor); |
156 | | static void SetPixelForN32BitTcArgb(Scanline pScanline, tools::Long nX, |
157 | | const BitmapColor& rBitmapColor); |
158 | | static void SetPixelForN32BitTcXrgb(Scanline pScanline, tools::Long nX, |
159 | | const BitmapColor& rBitmapColor); |
160 | | static void SetPixelForN32BitTcBgra(Scanline pScanline, tools::Long nX, |
161 | | const BitmapColor& rBitmapColor); |
162 | | static void SetPixelForN32BitTcBgrx(Scanline pScanline, tools::Long nX, |
163 | | const BitmapColor& rBitmapColor); |
164 | | static void SetPixelForN32BitTcRgba(Scanline pScanline, tools::Long nX, |
165 | | const BitmapColor& rBitmapColor); |
166 | | static void SetPixelForN32BitTcRgbx(Scanline pScanline, tools::Long nX, |
167 | | const BitmapColor& rBitmapColor); |
168 | | |
169 | | static FncGetPixel GetPixelFunction(ScanlineFormat nFormat); |
170 | | static FncSetPixel SetPixelFunction(ScanlineFormat nFormat); |
171 | | }; |
172 | | |
173 | | class BitmapScopedReadAccess |
174 | | { |
175 | | public: |
176 | | BitmapScopedReadAccess(const Bitmap& rBitmap) |
177 | 264k | : moAccess(rBitmap) |
178 | 264k | { |
179 | 264k | } |
180 | | BitmapScopedReadAccess(const AlphaMask& rBitmap) |
181 | 12 | : moAccess(rBitmap) |
182 | 12 | { |
183 | 12 | } |
184 | 0 | BitmapScopedReadAccess() {} |
185 | | |
186 | | BitmapScopedReadAccess& operator=(const Bitmap& rBitmap) |
187 | 0 | { |
188 | 0 | moAccess.emplace(rBitmap); |
189 | 0 | return *this; |
190 | 0 | } |
191 | | |
192 | | BitmapScopedReadAccess& operator=(const AlphaMask& rBitmap) |
193 | 0 | { |
194 | 0 | moAccess.emplace(rBitmap); |
195 | 0 | return *this; |
196 | 0 | } |
197 | | |
198 | 110k | bool operator!() const { return !moAccess.has_value() || !*moAccess; } |
199 | 15.9k | explicit operator bool() const { return moAccess && bool(*moAccess); } |
200 | | |
201 | 93.9k | void reset() { moAccess.reset(); } |
202 | | |
203 | 2.10k | BitmapReadAccess* get() { return moAccess ? &*moAccess : nullptr; } |
204 | 0 | const BitmapReadAccess* get() const { return moAccess ? &*moAccess : nullptr; } |
205 | | |
206 | 6.17G | BitmapReadAccess* operator->() { return &*moAccess; } |
207 | 0 | const BitmapReadAccess* operator->() const { return &*moAccess; } |
208 | | |
209 | 27.8M | BitmapReadAccess& operator*() { return *moAccess; } |
210 | 0 | const BitmapReadAccess& operator*() const { return *moAccess; } |
211 | | |
212 | | private: |
213 | | std::optional<BitmapReadAccess> moAccess; |
214 | | }; |
215 | | |
216 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |