Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/mm/Bitmap.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 25 янв. 2026 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_MM_BITMAP_H_
23 #define LSP_PLUG_IN_MM_BITMAP_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/common/status.h>
28 #include <lsp-plug.in/common/types.h>
29 #include <lsp-plug.in/io/Path.h>
30 #include <lsp-plug.in/io/IInStream.h>
31 #include <lsp-plug.in/mm/pixel.h>
32 #include <lsp-plug.in/mm/IColorMap.h>
33 #include <lsp-plug.in/runtime/LSPString.h>
34
35 namespace lsp
36 {
37 namespace mm
38 {
39 /**
40 * Bitmap class, simple raster bitmap storage
41 */
42 class Bitmap
43 {
44 private:
45 uint8_t *pData;
46 size_t nWidth;
47 size_t nHeight;
48 size_t nStride;
49 pixel_format_t enFormat;
50
51 private:
52 status_t do_load(io::IInStream *is, pixel_format_t format, IColorMap *map);
53 status_t load_xpm(io::IInStream *is, pixel_format_t format, IColorMap *map);
54 static inline size_t calc_bytes_per_row(pixel_format_t format, size_t cols) noexcept;
55 static inline size_t calc_stride(pixel_format_t format, size_t cols) noexcept;
56
57 public:
58 Bitmap() noexcept;
59 Bitmap(const Bitmap &);
60 Bitmap(const Bitmap *);
61 Bitmap(Bitmap && src) noexcept;
62 ~Bitmap();
63
64 Bitmap & operator = (const Bitmap & src);
65 Bitmap & operator = (const Bitmap * src);
66 Bitmap & operator = (Bitmap && src) noexcept;
67
68 void swap(Bitmap & src) noexcept;
69 void swap(Bitmap * src) noexcept;
70
71 public:
72 /**
73 * Get pixel format of the bitmap
74 * @return pixel format
75 */
76 inline pixel_format_t format() const noexcept { return enFormat; }
77
78 /**
79 * Get number of rows
80 * @return number of rows
81 */
82 inline size_t rows() const noexcept { return nHeight; }
83
84 /**
85 * Get bitmap width
86 * @return bitmap width in pixels
87 */
88 inline size_t width() const noexcept { return nWidth; }
89
90 /**
91 * Get number of columns
92 * @return number of columns
93 */
94 inline size_t columns() const noexcept { return nWidth; }
95
96 /**
97 * Get bitmap height
98 * @return bitmap height in pixels
99 */
100 inline size_t height() const noexcept { return nHeight; }
101
102 /**
103 * Get number of bytes per each row
104 * @return number of bytes per each row
105 */
106 inline size_t stride() const noexcept { return nStride; }
107
108 /**
109 * Get number of bytes per each row
110 * @return number of bytes per each row
111 */
112 inline size_t bytes_per_row() const noexcept { return nStride; }
113
114 /**
115 * Get row of the bitmap
116 * @param index number of row
117 * @return pointer to row or NULL
118 */
119 inline uint8_t *row(size_t index) noexcept { return ((pData != NULL) && (index < nHeight)) ? &pData[nStride * index] : NULL; }
120
121 /**
122 * Get row of the bitmap
123 * @param index number of row
124 * @return pointer to row or NULL
125 */
126 inline const uint8_t *row(size_t index) const noexcept { return ((pData != NULL) && (index < nHeight)) ? &pData[nStride * index] : NULL; }
127
128 /**
129 * Get the full bitmap data
130 * @return full bitmap data
131 */
132 inline uint8_t *data() noexcept { return pData; }
133
134 /**
135 * Get the full bitmap data
136 * @return full bitmap data
137 */
138 inline const uint8_t *data() const noexcept { return pData; }
139
140 /**
141 * Check that bitmap is empty
142 * @return true if bitmap is empty
143 */
144 inline bool is_empty() const noexcept { return pData == NULL; }
145
146 public:
147 /**
148 * Initialize bitmap
149 * @param format pixel format of bitmap
150 * @param width image width
151 * @param height image height
152 * @return status of operation
153 */
154 status_t init(pixel_format_t format, size_t width, size_t height);
155
156 /**
157 * Set bitmap data
158 * @param src source bitmap
159 * @return status of operation
160 */
161 status_t set(const Bitmap & src);
162
163 /**
164 * Set bitmap data
165 * @param src source bitmap
166 * @return status of operation
167 */
168 status_t set(const Bitmap * src);
169
170 /**
171 * Reset bitmap content
172 */
173 void reset();
174
175 /**
176 * Resize bitmap. The stored image does not scale, it becomes cropped if new size is smaller.
177 *
178 * @param width image width
179 * @param height image height
180 * @return status of operation
181 */
182 status_t resize(size_t width, size_t height);
183
184 /**
185 * Convert bitmap to desired pixel format
186 * @param format desired pixel format
187 * @return status of operation
188 */
189 status_t convert(pixel_format_t format);
190
191 /**
192 * Convert bitmap to desired pixel format and store to new bitmap
193 * @param dst destination bitmap to store result
194 * @param format desired pixel format
195 * @return status of operation
196 */
197 status_t convert_to(Bitmap & dst, pixel_format_t format) const;
198
199 /**
200 * Convert bitmap to desired pixel format and store to new bitmap
201 * @param dst destination bitmap to store result
202 * @param format desired pixel format
203 * @return status of operation
204 */
205 status_t convert_to(Bitmap *dst, pixel_format_t format) const;
206
207 /**
208 * Convert source bitmap to desired pixel format and store to this bitmap
209 * @param src source bitmap
210 * @param format desired pixel format
211 * @return status of operation
212 */
213 status_t convert_from(const Bitmap & src, pixel_format_t format);
214
215 /**
216 * Convert source bitmap to desired pixel format and store to this bitmap
217 * @param src source bitmap
218 * @param format desired pixel format
219 * @return status of operation
220 */
221 status_t convert_from(const Bitmap * src, pixel_format_t format);
222
223 /**
224 * Load bitmap from file
225 * @param path path to the file
226 * @param format desired pixel format (optional)
227 * @param map color map (optional)
228 * @return status of operation
229 */
230 status_t load(const char *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
231
232 /**
233 * Load bitmap from file
234 * @param path path to the file
235 * @param format desired pixel format (optional)
236 * @param map color map (optional)
237 * @return status of operation
238 */
239 status_t load(const LSPString *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
240
241 /**
242 * Load bitmap from file
243 * @param path path to the file
244 * @param format desired pixel format (optional)
245 * @param map color map (optional)
246 * @return status of operation
247 */
248 status_t load(const LSPString & path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
249
250 /**
251 * Load bitmap from file
252 * @param path path to the file
253 * @param format desired pixel format (optional)
254 * @param map color map (optional)
255 * @return status of operation
256 */
257 status_t load(const io::Path *path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
258
259 /**
260 * Load bitmap from memory chunk
261 * @param data memory chunk data
262 * @param size size of memory chunk
263 * @param format desired pixel format (optional)
264 * @param map color map (optional)
265 * @return status of operation
266 */
267 status_t load(const void *data, size_t size, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
268
269 /**
270 * Load bitmap from file
271 * @param path path to the file
272 * @param format desired pixel format (optional)
273 * @param map color map (optional)
274 * @return status of operation
275 */
276 status_t load(const io::Path & path, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
277
278 /**
279 * Load bitmap from input stream
280 * @param is input stream
281 * @param format desired pixel format (optional)
282 * @param map color map (optional)
283 * @return status of operation
284 */
285 status_t load(io::IInStream *is, pixel_format_t format = PIXFMT_DEFAULT, IColorMap *map = NULL);
286 };
287
288 } /* namespace mm */
289 } /* namespace lsp */
290
291 #endif /* LSP_PLUG_IN_MM_BITMAP_H_ */