Debian Codegraph @

modules/lsp-runtime-lib/include/private/mm/bitmap/xpm.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: 28 янв. 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 PRIVATE_MM_BITMAP_XPM_H_
23 #define PRIVATE_MM_BITMAP_XPM_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/lltl/hash_index.h>
28 #include <lsp-plug.in/fmt/xpm/xpm.h>
29 #include <lsp-plug.in/mm/Bitmap.h>
30
31 namespace lsp
32 {
33 namespace mm
34 {
35 typedef struct xpm_color_t
36 {
37 uint32_t value;
38 char id[];
39 } xpm_color_t;
40
41 typedef lltl::hash_index<const char, xpm_color_t> xpm_colors_t;
42
43 enum xpm_color_schema_t
44 {
45 XPM_MONO,
46 XPM_GRAY4,
47 XPM_GRAY,
48 XPM_RGB
49 };
50
51 static void free_xpm_colors(xpm_colors_t & colors)
52 {
53 for (lltl::iterator<xpm_color_t> it = colors.values(); it; ++it)
54 free(it.get());
55 colors.flush();
56 }
57
58 static void xpm_convert_color(lsp::Color & dst, const xpm::ColorItem * const * list, IColorMap *map)
59 {
60 for (size_t i=0; i<5; ++i)
61 {
62 const xpm::ColorItem *item = list[i];
63 if (!item->is_set())
64 continue;
65 if (item->is_none())
66 {
67 dst.set_rgba(0.0f, 0.0f, 0.0f, 1.0f);
68 return;
69 }
70 else if ((item->is_name()) && (map != NULL))
71 {
72 if (map->resolve(dst, item->name()))
73 return;
74 }
75 else if (item->get(dst))
76 return;
77 }
78
79 // Fully transparent color by default
80 dst.set_rgba(0.0f, 0.0f, 0.0f, 1.0f);
81 }
82
83 status_t Bitmap::load_xpm(io::IInStream *is, pixel_format_t format, IColorMap *map)
84 {
85 // Open parser
86 xpm::Parser *parser = NULL;
87 status_t res = xpm::wrap(&parser, is, WRAP_NONE);
88 if (res != STATUS_OK)
89 return res;
90 lsp_finally {
91 if (parser != NULL)
92 {
93 parser->close();
94 delete parser;
95 }
96 };
97
98 // Read header
99 xpm::header_t hdr;
100 if ((res = parser->read_header(&hdr)) != STATUS_OK)
101 return res;
102
103 // Skip empty bitmap
104 if ((hdr.width <= 0) || (hdr.height <= 0))
105 {
106 reset();
107 enFormat = format;
108 return STATUS_OK;
109 }
110
111 // Determine pixel format for loading
112 xpm_color_schema_t schema = XPM_RGB;
113 pixel_format_t img_pixfmt = format;
114 switch (format)
115 {
116 case mm::PIXFMT_A1:
117 img_pixfmt = mm::PIXFMT_A8;
118 schema = XPM_MONO;
119 break;
120 case mm::PIXFMT_G1:
121 img_pixfmt = mm::PIXFMT_G8;
122 schema = XPM_MONO;
123 break;
124 case mm::PIXFMT_A4:
125 img_pixfmt = mm::PIXFMT_A8;
126 schema = XPM_GRAY4;
127 break;
128 case mm::PIXFMT_G4:
129 img_pixfmt = mm::PIXFMT_G8;
130 schema = XPM_GRAY4;
131 break;
132 case mm::PIXFMT_A8:
133 img_pixfmt = mm::PIXFMT_A8;
134 schema = XPM_GRAY;
135 break;
136 case mm::PIXFMT_G8:
137 img_pixfmt = mm::PIXFMT_G8;
138 schema = XPM_GRAY;
139 break;
140 case mm::PIXFMT_RGB888:
141 case mm::PIXFMT_BGR888:
142 case mm::PIXFMT_RGBA8888:
143 case mm::PIXFMT_BGRA8888:
144 case mm::PIXFMT_PRGBA8888:
145 case mm::PIXFMT_PBGRA8888:
146 schema = XPM_RGB;
147 break;
148 default:
149 schema = XPM_RGB;
150 img_pixfmt = PIXFMT_BGRA8888;
151 break;
152 }
153
154 // Initialize color palette
155 xpm_colors_t palette;
156 const size_t szof_color = sizeof(xpm_color_t) + (hdr.chars_per_pixel + 1) * sizeof(char);
157 lsp_finally { free_xpm_colors(palette); };
158 xpm::Color xpm_color;
159 lsp::Color tmp_color;
160
161 const xpm::ColorItem * color_order[5];
162 switch (schema)
163 {
164 case XPM_MONO:
165 color_order[0] = & xpm_color.mono_visual();
166 color_order[1] = & xpm_color.gray4_visual();
167 color_order[2] = & xpm_color.gray_visual();
168 color_order[3] = & xpm_color.color_visual();
169 break;
170
171 case XPM_GRAY4:
172 color_order[0] = & xpm_color.gray4_visual();
173 color_order[1] = & xpm_color.mono_visual();
174 color_order[2] = & xpm_color.gray_visual();
175 color_order[3] = & xpm_color.color_visual();
176 break;
177
178 case XPM_GRAY:
179 color_order[0] = & xpm_color.gray_visual();
180 color_order[1] = & xpm_color.gray4_visual();
181 color_order[2] = & xpm_color.mono_visual();
182 color_order[3] = & xpm_color.color_visual();
183 break;
184
185 case XPM_RGB:
186 default:
187 color_order[0] = & xpm_color.color_visual();
188 color_order[1] = & xpm_color.gray_visual();
189 color_order[2] = & xpm_color.gray4_visual();
190 color_order[3] = & xpm_color.mono_visual();
191 break;
192 }
193 color_order[4] = & xpm_color.symbolic_visual();
194
195 for (size_t i=0; i<hdr.num_colors; ++i)
196 {
197 // Read color
198 if ((res = parser->read_color(&xpm_color)) != STATUS_OK)
199 return (res == STATUS_NOT_FOUND) ? STATUS_CORRUPTED_FILE : res;
200
201 // Convert color
202 xpm_convert_color(tmp_color, color_order, map);
203
204 // Register color
205 xpm_color_t *p_color = static_cast<xpm_color_t *>(malloc(szof_color));
206 if (p_color == NULL)
207 return STATUS_NO_MEM;
208 memcpy(p_color->id, xpm_color.code(), hdr.chars_per_pixel);
209 p_color->id[hdr.chars_per_pixel] = '\0';
210
211 if (!palette.create(p_color->id, p_color))
212 {
213 res = (palette.contains(p_color->id)) ? STATUS_CORRUPTED_FILE : STATUS_NO_MEM;
214 free(p_color);
215 return res;
216 }
217
218 switch (img_pixfmt)
219 {
220 case mm::PIXFMT_A1:
221 case mm::PIXFMT_A2:
222 case mm::PIXFMT_A4:
223 case mm::PIXFMT_A8:
224 p_color->value = uint8_t(tmp_color.dev_alpha() * 255.0f);
225 break;
226 case mm::PIXFMT_G1:
227 case mm::PIXFMT_G2:
228 case mm::PIXFMT_G4:
229 case mm::PIXFMT_G8:
230 p_color->value = uint8_t(tmp_color.hsl_lightness() * 255.0f);
231 break;
232 case mm::PIXFMT_RGB888:
233 case mm::PIXFMT_BGR888:
234 p_color->value = tmp_color.rgb24();
235 break;
236 case mm::PIXFMT_RGBA8888:
237 case mm::PIXFMT_BGRA8888:
238 p_color->value = tmp_color.dev_rgba32();
239 break;
240 case mm::PIXFMT_PRGBA8888:
241 case mm::PIXFMT_PBGRA8888:
242 p_color->value = tmp_color.prgba32();
243 break;
244 default:
245 p_color->value = tmp_color.dev_rgba32();
246 break;
247 }
248 }
249
250 // Initialize temporary bitmap for loading
251 Bitmap tmp;
252 if ((res = tmp.init(img_pixfmt, hdr.width, hdr.height)) != STATUS_OK)
253 return res;
254
255 // Do the read & conversion
256 {
257 const size_t row_size = hdr.width * hdr.chars_per_pixel;
258 char *buf = static_cast<char *>(malloc(row_size + hdr.chars_per_pixel + 1));
259 if (buf == NULL)
260 return STATUS_NO_MEM;
261 lsp_finally { free(buf); };
262
263 char *id = &buf[row_size];
264 id[hdr.chars_per_pixel] = '\0';
265
266 // read and convert line
267 for (size_t i=0; i<hdr.height; ++i)
268 {
269 // Read line
270 uint8_t *dst = tmp.row(i);
271 if ((res = parser->read_line(buf)) != STATUS_OK)
272 return (res == STATUS_NOT_FOUND) ? STATUS_CORRUPTED_FILE : res;
273
274 // Convert row data
275 for (size_t j=0; j<row_size; ++j)
276 {
277 // Collect the color identifier
278 const size_t off = j % hdr.chars_per_pixel;
279 id[off] = buf[j];
280 if (off != hdr.chars_per_pixel - 1)
281 continue;
282
283 // Fetch color from palette
284 const xpm_color_t *xcol = palette.get(id);
285 if (xcol == NULL)
286 return STATUS_CORRUPTED_FILE;
287 const uint32_t c = xcol->value;
288
289 // Emit color pixel to the output image
290 switch (img_pixfmt)
291 {
292 case mm::PIXFMT_A1:
293 case mm::PIXFMT_A2:
294 case mm::PIXFMT_A4:
295 case mm::PIXFMT_A8:
296 case mm::PIXFMT_G1:
297 case mm::PIXFMT_G2:
298 case mm::PIXFMT_G4:
299 case mm::PIXFMT_G8:
300 *(dst++) = uint8_t(c);
301 break;
302 case mm::PIXFMT_RGB888:
303 dst[0] = uint8_t(c >> 16);
304 dst[1] = uint8_t(c >> 8);
305 dst[2] = uint8_t(c);
306 dst += 3;
307 break;
308 case mm::PIXFMT_BGR888:
309 dst[0] = uint8_t(c);
310 dst[1] = uint8_t(c >> 8);
311 dst[2] = uint8_t(c >> 16);
312 dst += 3;
313 break;
314 case mm::PIXFMT_RGBA8888:
315 case mm::PIXFMT_PRGBA8888:
316 dst[0] = uint8_t(c >> 16);
317 dst[1] = uint8_t(c >> 8);
318 dst[2] = uint8_t(c);
319 dst[3] = uint8_t(c >> 24);
320 dst += 4;
321 break;
322 case mm::PIXFMT_BGRA8888:
323 case mm::PIXFMT_PBGRA8888:
324 default:
325 dst[0] = uint8_t(c);
326 dst[1] = uint8_t(c >> 8);
327 dst[2] = uint8_t(c >> 16);
328 dst[3] = uint8_t(c >> 24);
329 dst += 4;
330 break;
331 }
332 }
333 }
334 }
335
336 // Free color palette
337 free_xpm_colors(palette);
338
339 // Close parser
340 if ((res = parser->close()) != STATUS_OK)
341 return res;
342 delete parser;
343 parser = NULL;
344
345 // Convert bitmap if necessary
346 if (img_pixfmt != format)
347 {
348 if ((res = tmp.convert(format)) != STATUS_OK)
349 return res;
350 }
351
352 // Commit result
353 tmp.swap(this);
354
355 return STATUS_OK;
356 }
357
358 } /* namespace mm */
359 } /* namespace lsp */
360
361
362 #endif /* PRIVATE_MM_BITMAP_XPM_H_ */