/src/libreoffice/include/vcl/BitmapInfoAccess.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 | | |
28 | | typedef BitmapColor (*FncGetPixel)(ConstScanline pScanline, tools::Long nX); |
29 | | typedef void (*FncSetPixel)(Scanline pScanline, tools::Long nX, const BitmapColor& rBitmapColor); |
30 | | |
31 | | class VCL_DLLPUBLIC BitmapInfoAccess |
32 | | { |
33 | | friend class BitmapReadAccess; |
34 | | |
35 | | public: |
36 | | BitmapInfoAccess(const Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info); |
37 | | BitmapInfoAccess(const AlphaMask& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Info); |
38 | | |
39 | | virtual ~BitmapInfoAccess(); |
40 | | |
41 | 841k | bool operator!() const { return mpBuffer == nullptr; } |
42 | 257k | explicit operator bool() const { return mpBuffer != nullptr; } |
43 | | |
44 | 12.0M | tools::Long Width() const { return mpBuffer ? mpBuffer->mnWidth : 0L; } |
45 | | |
46 | 295k | tools::Long Height() const { return mpBuffer ? mpBuffer->mnHeight : 0L; } |
47 | | |
48 | | bool IsTopDown() const |
49 | 34.8k | { |
50 | 34.8k | assert(mpBuffer && "Access is not valid!"); |
51 | | |
52 | 34.8k | return mpBuffer && mpBuffer->meDirection == ScanlineDirection::TopDown; |
53 | 34.8k | } |
54 | | |
55 | 34.8k | bool IsBottomUp() const { return !IsTopDown(); } |
56 | | |
57 | | ScanlineFormat GetScanlineFormat() const |
58 | 94.3M | { |
59 | 94.3M | assert(mpBuffer && "Access is not valid!"); |
60 | | |
61 | 94.3M | return mpBuffer ? mpBuffer->meFormat : ScanlineFormat::NONE; |
62 | 94.3M | } |
63 | | |
64 | | sal_uInt32 GetScanlineSize() const |
65 | 122M | { |
66 | 122M | assert(mpBuffer && "Access is not valid!"); |
67 | | |
68 | 122M | return mpBuffer ? mpBuffer->mnScanlineSize : 0; |
69 | 122M | } |
70 | | |
71 | | sal_uInt16 GetBitCount() const |
72 | 5.07k | { |
73 | 5.07k | assert(mpBuffer && "Access is not valid!"); |
74 | | |
75 | 5.07k | return mpBuffer ? mpBuffer->mnBitCount : 0; |
76 | 5.07k | } |
77 | | |
78 | | /// Returns the BitmapColor (i.e. palette index) that is either an exact match |
79 | | /// of the required color, or failing that, the entry that is the closest i.e. least error |
80 | | /// as measured by Color::GetColorError. |
81 | | BitmapColor GetBestMatchingColor(const BitmapColor& rBitmapColor) const |
82 | 55.9k | { |
83 | 55.9k | if (HasPalette()) |
84 | 54.8k | return BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rBitmapColor))); |
85 | 1.02k | else |
86 | 1.02k | return rBitmapColor; |
87 | 55.9k | } |
88 | | |
89 | | bool HasPalette() const |
90 | 80.5M | { |
91 | 80.5M | const BitmapBuffer* pBuffer = mpBuffer; |
92 | | |
93 | 80.5M | assert(pBuffer && "Access is not valid!"); |
94 | | |
95 | 80.5M | return pBuffer && !!pBuffer->maPalette; |
96 | 80.5M | } |
97 | | |
98 | | const BitmapPalette& GetPalette() const |
99 | 127k | { |
100 | 127k | const BitmapBuffer* pBuffer = mpBuffer; |
101 | | |
102 | 127k | assert(pBuffer && "Access is not valid!"); |
103 | | |
104 | 127k | return pBuffer->maPalette; |
105 | 127k | } |
106 | | |
107 | | sal_uInt16 GetPaletteEntryCount() const |
108 | 38.8k | { |
109 | 38.8k | const BitmapBuffer* pBuffer = mpBuffer; |
110 | | |
111 | 38.8k | assert(HasPalette() && "Bitmap has no palette!"); |
112 | | |
113 | 38.8k | return HasPalette() ? pBuffer->maPalette.GetEntryCount() : 0; |
114 | 38.8k | } |
115 | | |
116 | | const BitmapColor& GetPaletteColor(sal_uInt16 nColor) const |
117 | 2.17G | { |
118 | 2.17G | const BitmapBuffer* pBuffer = mpBuffer; |
119 | 2.17G | assert(pBuffer && "Access is not valid!"); |
120 | 2.17G | assert(HasPalette() && "Bitmap has no palette!"); |
121 | | |
122 | 2.17G | return pBuffer->maPalette[nColor]; |
123 | 2.17G | } |
124 | | |
125 | | /// Returns the BitmapColor (i.e. palette index) that is either an exact match |
126 | | /// of the required color, or failing that, the entry that is the closest i.e. least error |
127 | | /// as measured by Color::GetColorError. |
128 | | sal_uInt16 GetBestPaletteIndex(const BitmapColor& rBitmapColor) const; |
129 | | /// Returns the BitmapColor (i.e. palette index) that is an exact match |
130 | | /// of the required color. Returns SAL_MAX_UINT16 if nothing found. |
131 | | sal_uInt16 GetMatchingPaletteIndex(const BitmapColor& rBitmapColor) const; |
132 | | |
133 | | private: |
134 | | BitmapInfoAccess(const BitmapInfoAccess&) = delete; |
135 | | BitmapInfoAccess& operator=(const BitmapInfoAccess&) = delete; |
136 | | |
137 | | protected: |
138 | | Bitmap maBitmap; |
139 | | BitmapBuffer* mpBuffer; |
140 | | BitmapAccessMode mnAccessMode; |
141 | | }; |
142 | | |
143 | | class BitmapScopedInfoAccess |
144 | | { |
145 | | public: |
146 | | BitmapScopedInfoAccess(const Bitmap& rBitmap) |
147 | 38.0k | : moAccess(rBitmap) |
148 | 38.0k | { |
149 | 38.0k | } |
150 | | BitmapScopedInfoAccess(const AlphaMask& rBitmap) |
151 | | : moAccess(rBitmap) |
152 | 0 | { |
153 | 0 | } |
154 | 0 | BitmapScopedInfoAccess() {} |
155 | | |
156 | | BitmapScopedInfoAccess& operator=(const Bitmap& rBitmap) |
157 | 0 | { |
158 | 0 | moAccess.emplace(rBitmap); |
159 | 0 | return *this; |
160 | 0 | } |
161 | | |
162 | | BitmapScopedInfoAccess& operator=(const AlphaMask& rBitmap) |
163 | 0 | { |
164 | 0 | moAccess.emplace(rBitmap); |
165 | 0 | return *this; |
166 | 0 | } |
167 | | |
168 | 4 | bool operator!() const { return !moAccess.has_value() || !*moAccess; } |
169 | 38.0k | explicit operator bool() const { return moAccess && bool(*moAccess); } |
170 | | |
171 | 0 | void reset() { moAccess.reset(); } |
172 | | |
173 | 0 | BitmapInfoAccess* get() { return moAccess ? &*moAccess : nullptr; } |
174 | 0 | const BitmapInfoAccess* get() const { return moAccess ? &*moAccess : nullptr; } |
175 | | |
176 | 76.1k | BitmapInfoAccess* operator->() { return &*moAccess; } |
177 | 0 | const BitmapInfoAccess* operator->() const { return &*moAccess; } |
178 | | |
179 | 0 | BitmapInfoAccess& operator*() { return *moAccess; } |
180 | 0 | const BitmapInfoAccess& operator*() const { return *moAccess; } |
181 | | |
182 | | private: |
183 | | std::optional<BitmapInfoAccess> moAccess; |
184 | | }; |
185 | | |
186 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |