Debian Codegraph @

modules/lsp-runtime-lib/src/main/mm/Bitmap.cpp

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 #include <lsp-plug.in/fmt/xpm/xpm.h>
23 #include <lsp-plug.in/io/InFileStream.h>
24 #include <lsp-plug.in/io/InMemoryStream.h>
25 #include <lsp-plug.in/io/InBufStream.h>
26 #include <lsp-plug.in/mm/Bitmap.h>
27
28 #include <private/mm/bitmap/xpm.h>
29
30 namespace lsp
31 {
32 namespace mm
33 {
34 static inline bool is_exact_copy(pixel_format_t a, pixel_format_t b)
35 {
36 if (a == b)
37 return true;
38 switch (a)
39 {
40 case PIXFMT_G1: return b == PIXFMT_A1;
41 case PIXFMT_G2: return b == PIXFMT_A2;
42 case PIXFMT_G4: return b == PIXFMT_A4;
43 case PIXFMT_G8: return b == PIXFMT_A8;
44 case PIXFMT_A1: return b == PIXFMT_G1;
45 case PIXFMT_A2: return b == PIXFMT_G2;
46 case PIXFMT_A4: return b == PIXFMT_G4;
47 case PIXFMT_A8: return b == PIXFMT_G8;
48 default:
49 break;
50 }
51 return false;
52 }
53
54 Bitmap::Bitmap() noexcept
55 {
56 pData = NULL;
57 nWidth = 0;
58 nHeight = 0;
59 nStride = 0;
60 enFormat = PIXFMT_RGBA8888;
61 }
62
63 Bitmap::Bitmap(const Bitmap & src)
64 {
65 pData = NULL;
66 nWidth = 0;
67 nHeight = 0;
68 nStride = 0;
69 enFormat = PIXFMT_RGBA8888;
70
71 set(src);
72 }
73
74 Bitmap::Bitmap(const Bitmap * src)
75 {
76 pData = NULL;
77 nWidth = 0;
78 nHeight = 0;
79 nStride = 0;
80 enFormat = PIXFMT_RGBA8888;
81
82 set(src);
83 }
84
85 Bitmap::Bitmap(Bitmap && src) noexcept
86 {
87 pData = lsp::exchange(src.pData, static_cast<uint8_t *>(NULL));
88 nWidth = lsp::exchange(src.nWidth, 0);
89 nHeight = lsp::exchange(src.nHeight, 0);
90 nStride = lsp::exchange(src.nStride, 0);
91 enFormat = lsp::exchange(src.enFormat, PIXFMT_RGBA8888);
92 }
93
94 Bitmap::~Bitmap()
95 {
96 if (pData != NULL)
97 {
98 free(pData);
99 pData = NULL;
100 }
101 }
102
103 inline size_t Bitmap::calc_bytes_per_row(pixel_format_t format, size_t cols) noexcept
104 {
105 const size_t bpp = device_bits_per_pixel(format);
106 return (bpp * cols + 7) >> 3;
107 }
108
109 inline size_t Bitmap::calc_stride(pixel_format_t format, size_t cols) noexcept
110 {
111 const size_t bpp = device_bits_per_pixel(format);
112 return (bpp <= 8) ? (bpp * cols + 7) >> 3 : ((bpp * cols + 31) & (~size_t(0x1f))) >> 3;
113 }
114
115 Bitmap & Bitmap::operator = (Bitmap && src) noexcept
116 {
117 pData = lsp::exchange(src.pData, static_cast<uint8_t *>(NULL));
118 nWidth = lsp::exchange(src.nWidth, 0);
119 nHeight = lsp::exchange(src.nHeight, 0);
120 nStride = lsp::exchange(src.nStride, 0);
121 enFormat = lsp::exchange(src.enFormat, PIXFMT_RGBA8888);
122
123 return *this;
124 }
125
126 Bitmap & Bitmap::operator = (const Bitmap & src)
127 {
128 set(src);
129 return *this;
130 }
131
132 Bitmap & Bitmap::operator = (const Bitmap * src)
133 {
134 set(src);
135 return *this;
136 }
137
138 void Bitmap::swap(Bitmap & src) noexcept
139 {
140 lsp::swap(pData, src.pData);
141 lsp::swap(nWidth, src.nWidth);
142 lsp::swap(nHeight, src.nHeight);
143 lsp::swap(nStride, src.nStride);
144 lsp::swap(enFormat, src.enFormat);
145 }
146
147 void Bitmap::swap(Bitmap * src) noexcept
148 {
149 lsp::swap(pData, src->pData);
150 lsp::swap(nWidth, src->nWidth);
151 lsp::swap(nHeight, src->nHeight);
152 lsp::swap(nStride, src->nStride);
153 lsp::swap(enFormat, src->enFormat);
154 }
155
156 void Bitmap::reset()
157 {
158 if (pData != NULL)
159 {
160 free(pData);
161 pData = NULL;
162 }
163 nWidth = 0;
164 nHeight = 0;
165 nStride = 0;
166 }
167
168 status_t Bitmap::init(pixel_format_t format, size_t width, size_t height)
169 {
170 if (format == PIXFMT_DEFAULT)
171 return STATUS_INVALID_VALUE;
172 if ((height <= 0) || (width <= 0))
173 {
174 reset();
175 enFormat = format;
176 return STATUS_OK;
177 }
178
179 const size_t stride = calc_stride(format, width);
180 const size_t to_alloc = stride * height;
181 uint8_t * data = static_cast<uint8_t *>(realloc(pData, to_alloc));
182 if (data == NULL)
183 return STATUS_NO_MEM;
184
185 // Cleanup data
186 bzero(data, to_alloc);
187
188 // Commit data
189 pData = data;
190 nWidth = width;
191 nHeight = height;
192 nStride = stride;
193 enFormat = format;
194
195 return STATUS_OK;
196 }
197
198 status_t Bitmap::set(const Bitmap & src)
199 {
200 if (src.pData == NULL)
201 {
202 reset();
203 enFormat = src.enFormat;
204 return STATUS_OK;
205 }
206
207 // Compute parameters of the new bitmap
208 uint8_t * data = static_cast<uint8_t *>(realloc(pData, src.nHeight * src.nStride));
209 if (data == NULL)
210 return STATUS_NO_MEM;
211 lsp_finally {
212 if (data != NULL)
213 free(data);
214 };
215
216 // Copy data
217 memcpy(data, src.pData, src.nHeight * src.nStride);
218
219 pData = data;
220 nWidth = src.nWidth;
221 nHeight = src.nHeight;
222 nStride = src.nStride;
223 enFormat = src.enFormat;
224
225 return STATUS_OK;
226 }
227
228 status_t Bitmap::set(const Bitmap * src)
229 {
230 if (src == NULL)
231 return STATUS_BAD_ARGUMENTS;
232 return set(*src);
233 }
234
235 status_t Bitmap::load(const char *path, pixel_format_t format, IColorMap *map)
236 {
237 if (path == NULL)
238 return STATUS_BAD_ARGUMENTS;
239
240 io::InFileStream ifs;
241 status_t res = ifs.open(path);
242 if (res != STATUS_OK)
243 return res;
244
245 res = update_status(res, do_load(&ifs, format, map));
246 return update_status(res, ifs.close());
247 }
248
249 status_t Bitmap::load(const LSPString *path, pixel_format_t format, IColorMap *map)
250 {
251 return (path != NULL) ? load(*path, format, map) : STATUS_BAD_ARGUMENTS;
252 }
253
254 status_t Bitmap::load(const LSPString & path, pixel_format_t format, IColorMap *map)
255 {
256 io::InFileStream ifs;
257 status_t res = ifs.open(&path);
258 if (res != STATUS_OK)
259 return res;
260
261 res = update_status(res, do_load(&ifs, format, map));
262 return update_status(res, ifs.close());
263 }
264
265 status_t Bitmap::load(const io::Path *path, pixel_format_t format, IColorMap *map)
266 {
267 return (path != NULL) ? load(*path, format, map) : STATUS_BAD_ARGUMENTS;
268 }
269
270 status_t Bitmap::load(const io::Path & path, pixel_format_t format, IColorMap *map)
271 {
272 io::InFileStream ifs;
273 status_t res = ifs.open(&path);
274 if (res != STATUS_OK)
275 return res;
276
277 res = update_status(res, do_load(&ifs, format, map));
278 return update_status(res, ifs.close());
279 }
280
281 status_t Bitmap::load(const void *data, size_t size, pixel_format_t format, IColorMap *map)
282 {
283 if (data == NULL)
284 return STATUS_BAD_ARGUMENTS;
285
286 io::InMemoryStream ims;
287 ims.wrap(data, size);
288
289 status_t res = do_load(&ims, format, map);
290 return update_status(res, ims.close());
291 }
292
293 status_t Bitmap::load(io::IInStream *is, pixel_format_t format, IColorMap *map)
294 {
295 io::InBufStream ibs;
296 status_t res = ibs.wrap(is, WRAP_NONE);
297 if (res != STATUS_OK)
298 return res;
299
300 res = update_status(res, do_load(&ibs, format, map));
301 return update_status(res, ibs.close());
302 }
303
304
305 status_t Bitmap::do_load(io::IInStream *is, pixel_format_t format, IColorMap *map)
306 {
307 status_t res = load_xpm(is, format, map);
308 if ((res == STATUS_OK) || (res != STATUS_UNSUPPORTED_FORMAT))
309 return res;
310
311 return STATUS_UNSUPPORTED_FORMAT;
312 }
313
314 status_t Bitmap::resize(size_t width, size_t height)
315 {
316 if ((nWidth == width) && (nHeight == height))
317 return STATUS_OK;
318
319 // Compute parameters of the new bitmap
320 const size_t stride = calc_stride(enFormat, width);
321 const size_t to_alloc = stride * height;
322 uint8_t * data = static_cast<uint8_t *>(malloc(to_alloc));
323 if (data == NULL)
324 return STATUS_NO_MEM;
325 lsp_finally {
326 if (data != NULL)
327 free(data);
328 };
329
330 // Preserve previous picture if possible
331 if ((pData != NULL) && (nWidth > 0) && (nHeight > 0))
332 {
333 const size_t to_copy = lsp_min(stride, nStride);
334 const size_t to_rows = lsp_min(height, nHeight);
335
336 for (size_t i=0; i<to_rows; ++i)
337 {
338 memcpy(&data[i*stride], &pData[i*nStride], to_copy);
339 bzero(&data[i*stride + to_copy], to_copy - nStride);
340 }
341 bzero(&data[to_rows * stride], (height - nHeight) * stride);
342 }
343 else
344 bzero(data, to_alloc);
345
346 lsp::swap(pData, data);
347 nWidth = width;
348 nHeight = height;
349 nStride = stride;
350
351 return STATUS_OK;
352 }
353
354 status_t Bitmap::convert_to(Bitmap & dst, pixel_format_t format) const
355 {
356 if (enFormat == format)
357 return STATUS_OK;
358 else if (format == PIXFMT_DEFAULT)
359 return STATUS_INVALID_VALUE;
360
361 // Get pixel conversion function
362 const pixel_conversion_t conversion = pixel_convert_function(format, enFormat);
363 if (conversion == NULL)
364 return STATUS_UNSUPPORTED_FORMAT;
365
366 // Check if we need to perform reallocation
367 const size_t stride = calc_stride(format, nWidth);
368 if (stride * dst.nHeight == nStride * nHeight)
369 {
370 // Perform conversion without reallocation
371 for (size_t r = 0; r < nHeight; ++r)
372 conversion(&dst.pData[r*nStride], &pData[r*nStride], nWidth);
373 }
374 else
375 {
376 // Need to re-allocate memory
377 const size_t bpr = calc_bytes_per_row(format, nWidth);
378 const size_t to_alloc = stride * nHeight;
379 uint8_t * data = static_cast<uint8_t *>(malloc(to_alloc));
380 if (data == NULL)
381 return STATUS_NO_MEM;
382 lsp_finally {
383 if (data != NULL)
384 free(data);
385 };
386
387 // Perform conversion
388 for (size_t r = 0; r < nHeight; ++r)
389 {
390 conversion(&data[r*stride], &pData[r*nStride], nWidth);
391 bzero(&data[r*stride + bpr], stride - bpr);
392 }
393
394 // Commit new data
395 lsp::swap(dst.pData, data);
396 }
397
398 dst.nStride = stride;
399 dst.nWidth = nWidth;
400 dst.nHeight = nHeight;
401 dst.enFormat = format;
402
403 return STATUS_OK;
404 }
405
406 status_t Bitmap::convert_to(Bitmap *dst, pixel_format_t format) const
407 {
408 return (dst != NULL) ? convert_to(*dst, format) : STATUS_BAD_ARGUMENTS;
409 }
410
411 status_t Bitmap::convert(pixel_format_t format)
412 {
413 if (enFormat == format)
414 return STATUS_OK;
415 else if (format == PIXFMT_DEFAULT)
416 return STATUS_INVALID_VALUE;
417
418 // Check that no changes are required to be made
419 if ((nWidth <= 0) || (nHeight <= 0) || (is_exact_copy(enFormat, format)))
420 {
421 enFormat = format;
422 return STATUS_OK;
423 }
424
425 // Get pixel conversion function
426 const pixel_conversion_t conversion = pixel_convert_function(format, enFormat);
427 if (conversion == NULL)
428 return STATUS_UNSUPPORTED_FORMAT;
429
430 // Check if we need to perform reallocation
431 const size_t stride = calc_stride(format, nWidth);
432 if (stride == nStride)
433 {
434 // Perform conversion without reallocation
435 for (size_t r = 0; r < nHeight; ++r)
436 conversion(&pData[r*nStride], &pData[r*nStride], nWidth);
437 }
438 else
439 {
440 // Need to re-allocate memory
441 const size_t bpr = calc_bytes_per_row(format, nWidth);
442 const size_t to_alloc = stride * nHeight;
443 uint8_t * data = static_cast<uint8_t *>(malloc(to_alloc));
444 if (data == NULL)
445 return STATUS_NO_MEM;
446 lsp_finally {
447 if (data != NULL)
448 free(data);
449 };
450
451 // Perform conversion
452 for (size_t r = 0; r < nHeight; ++r)
453 {
454 conversion(&data[r*stride], &pData[r*nStride], nWidth);
455 bzero(&data[r*stride + bpr], stride - bpr);
456 }
457
458 // Commit new data
459 lsp::swap(pData, data);
460 }
461
462 nStride = stride;
463 enFormat = format;
464
465 return STATUS_OK;
466 }
467
468 status_t Bitmap::convert_from(const Bitmap & src, pixel_format_t format)
469 {
470 return src.convert_to(*this, format);
471 }
472
473 status_t Bitmap::convert_from(const Bitmap * src, pixel_format_t format)
474 {
475 return (src != NULL) ? src->convert_to(*this, format) : STATUS_BAD_ARGUMENTS;
476 }
477
478 } /* namespace mm */
479 } /* namespace lsp */
480