Debian Codegraph @

modules/bmp/image_loader_bmp.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* image_loader_bmp.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "image_loader_bmp.h"
32
33 #include "core/io/file_access_memory.h"
34
35 static uint8_t get_mask_width(uint16_t mask) {
36 // Returns number of ones in the binary value of the parameter: mask.
37 // Uses a Simple pop_count.
38 uint8_t c = 0u;
39 for (; mask != 0u; mask &= mask - 1u) {
40 c++;
41 }
42 return c;
43 }
44
45 Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
46 const uint8_t *p_buffer,
47 const uint8_t *p_color_buffer,
48 const uint32_t color_table_size,
49 const bmp_header_s &p_header) {
50 Error err = OK;
51
52 if (p_buffer == nullptr) {
53 err = FAILED;
54 }
55
56 if (err == OK) {
57 size_t index = 0;
58 size_t width = (size_t)p_header.bmp_info_header.bmp_width;
59 size_t height = (size_t)p_header.bmp_info_header.bmp_height;
60 size_t bits_per_pixel = (size_t)p_header.bmp_info_header.bmp_bit_count;
61
62 // Image data (might be indexed)
63 Vector<uint8_t> data;
64 int data_len = 0;
65
66 if (bits_per_pixel <= 8) { // indexed
67 data_len = width * height;
68 } else { // color
69 data_len = width * height * 4;
70 }
71 ERR_FAIL_COND_V_MSG(data_len == 0, ERR_BUG, "Couldn't parse the BMP image data.");
72 err = data.resize(data_len);
73
74 uint8_t *data_w = data.ptrw();
75 uint8_t *write_buffer = data_w;
76
77 const uint32_t width_bytes = (width * bits_per_pixel + 7) / 8;
78 const uint32_t line_width = (width_bytes + 3) & ~3; // Padded to 4 bytes.
79
80 const uint8_t *line = p_buffer + (line_width * (height - 1));
81 const uint8_t *end_buffer = p_buffer + p_header.bmp_file_header.bmp_file_size - p_header.bmp_file_header.bmp_file_offset;
82 ERR_FAIL_COND_V(line + line_width > end_buffer, ERR_FILE_CORRUPT);
83
84 for (uint64_t i = 0; i < height; i++) {
85 const uint8_t *line_ptr = line;
86
87 for (unsigned int j = 0; j < width; j++) {
88 switch (bits_per_pixel) {
89 case 1: {
90 write_buffer[index] = (line[(j * bits_per_pixel) / 8] >> (8 - bits_per_pixel * (1 + j % 8))) & 0x01;
91
92 index++;
93 } break;
94 case 2: {
95 write_buffer[index] = (line[(j * bits_per_pixel) / 8] >> (8 - bits_per_pixel * (1 + j % 4))) & 0x03;
96
97 index++;
98 } break;
99 case 4: {
100 write_buffer[index] = (line[(j * bits_per_pixel) / 8] >> (8 - bits_per_pixel * (1 + j % 2))) & 0x0f;
101
102 index++;
103 } break;
104 case 8: {
105 uint8_t color_index = *line_ptr;
106
107 write_buffer[index] = color_index;
108
109 index += 1;
110 line_ptr += 1;
111 } break;
112 case 16: {
113 uint16_t rgb = (static_cast<uint16_t>(line_ptr[1]) << 8) | line_ptr[0];
114 // A1R5G5B5/X1R5G5B5 => uint16_t
115 // [A/X]1R5G2 | G3B5 => uint8_t | uint8_t
116 uint8_t ba = (rgb & p_header.bmp_bitfield.alpha_mask) >> p_header.bmp_bitfield.alpha_offset; // Alpha 0b 1000 ...
117 uint8_t b0 = (rgb & p_header.bmp_bitfield.red_mask) >> p_header.bmp_bitfield.red_offset; // Red 0b 0111 1100 ...
118 uint8_t b1 = (rgb & p_header.bmp_bitfield.green_mask) >> p_header.bmp_bitfield.green_offset; // Green 0b 0000 0011 1110 ...
119 uint8_t b2 = (rgb & p_header.bmp_bitfield.blue_mask); // >> p_header.bmp_bitfield.blue_offset; // Blue 0b ... 0001 1111
120
121 // Next we apply some color scaling going from a variable value space to a 256 value space.
122 // This may be simplified some but left as is for legibility.
123 // float scaled_value = unscaled_value * byte_max_value / color_channel_maximum_value + rounding_offset;
124 float f0 = b0 * 255.0f / static_cast<float>(p_header.bmp_bitfield.red_max) + 0.5f;
125 float f1 = b1 * 255.0f / static_cast<float>(p_header.bmp_bitfield.green_max) + 0.5f;
126 float f2 = b2 * 255.0f / static_cast<float>(p_header.bmp_bitfield.blue_max) + 0.5f;
127 write_buffer[index + 0] = static_cast<uint8_t>(f0); // R
128 write_buffer[index + 1] = static_cast<uint8_t>(f1); // G
129 write_buffer[index + 2] = static_cast<uint8_t>(f2); // B
130
131 if (p_header.bmp_bitfield.alpha_mask_width > 0) {
132 write_buffer[index + 3] = ba * 0xFF; // Alpha value(Always true or false so no scaling)
133 } else {
134 write_buffer[index + 3] = 0xFF; // No Alpha channel, Show everything.
135 }
136
137 index += 4;
138 line_ptr += 2;
139 } break;
140 case 24: {
141 write_buffer[index + 2] = line_ptr[0];
142 write_buffer[index + 1] = line_ptr[1];
143 write_buffer[index + 0] = line_ptr[2];
144 write_buffer[index + 3] = 0xff;
145
146 index += 4;
147 line_ptr += 3;
148 } break;
149 case 32: {
150 write_buffer[index + 2] = line_ptr[0];
151 write_buffer[index + 1] = line_ptr[1];
152 write_buffer[index + 0] = line_ptr[2];
153 write_buffer[index + 3] = line_ptr[3];
154
155 index += 4;
156 line_ptr += 4;
157 } break;
158 }
159 }
160 line -= line_width;
161 }
162
163 if (p_color_buffer == nullptr || color_table_size == 0) { // regular pixels
164
165 p_image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
166
167 } else { // data is in indexed format, extend it
168
169 // Palette data
170 Vector<uint8_t> palette_data;
171 palette_data.resize(color_table_size * 4);
172
173 uint8_t *palette_data_w = palette_data.ptrw();
174 uint8_t *pal = palette_data_w;
175
176 const uint8_t *cb = p_color_buffer;
177
178 for (unsigned int i = 0; i < color_table_size; ++i) {
179 pal[i * 4 + 0] = cb[2];
180 pal[i * 4 + 1] = cb[1];
181 pal[i * 4 + 2] = cb[0];
182 pal[i * 4 + 3] = 0xff;
183
184 cb += 4;
185 }
186 // Extend palette to image
187 Vector<uint8_t> extended_data;
188 extended_data.resize(data.size() * 4);
189
190 uint8_t *ex_w = extended_data.ptrw();
191 uint8_t *dest = ex_w;
192
193 const int num_pixels = width * height;
194
195 for (int i = 0; i < num_pixels; i++) {
196 dest[0] = pal[write_buffer[i] * 4 + 0];
197 dest[1] = pal[write_buffer[i] * 4 + 1];
198 dest[2] = pal[write_buffer[i] * 4 + 2];
199 dest[3] = pal[write_buffer[i] * 4 + 3];
200
201 dest += 4;
202 }
203 p_image->set_data(width, height, false, Image::FORMAT_RGBA8, extended_data);
204 }
205 }
206 return err;
207 }
208
209 Error ImageLoaderBMP::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
210 bmp_header_s bmp_header;
211 Error err = ERR_INVALID_DATA;
212
213 // A valid bmp file should always at least have a
214 // file header and a minimal info header
215 if (f->get_length() > BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_MIN_SIZE) {
216 // File Header
217 bmp_header.bmp_file_header.bmp_signature = f->get_16();
218 if (bmp_header.bmp_file_header.bmp_signature == BITMAP_SIGNATURE) {
219 bmp_header.bmp_file_header.bmp_file_size = f->get_32();
220 bmp_header.bmp_file_header.bmp_file_padding = f->get_32();
221 bmp_header.bmp_file_header.bmp_file_offset = f->get_32();
222
223 // Info Header
224 bmp_header.bmp_info_header.bmp_header_size = f->get_32();
225 ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_header_size < BITMAP_INFO_HEADER_MIN_SIZE, ERR_FILE_CORRUPT,
226 vformat("Couldn't parse the BMP info header. The file is likely corrupt: %s", f->get_path()));
227
228 bmp_header.bmp_info_header.bmp_width = f->get_32();
229 bmp_header.bmp_info_header.bmp_height = f->get_32();
230
231 bmp_header.bmp_info_header.bmp_planes = f->get_16();
232 ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_planes != 1, ERR_FILE_CORRUPT,
233 vformat("Couldn't parse the BMP planes. The file is likely corrupt: %s", f->get_path()));
234
235 bmp_header.bmp_info_header.bmp_bit_count = f->get_16();
236 bmp_header.bmp_info_header.bmp_compression = f->get_32();
237 bmp_header.bmp_info_header.bmp_size_image = f->get_32();
238 bmp_header.bmp_info_header.bmp_pixels_per_meter_x = f->get_32();
239 bmp_header.bmp_info_header.bmp_pixels_per_meter_y = f->get_32();
240 bmp_header.bmp_info_header.bmp_colors_used = f->get_32();
241 bmp_header.bmp_info_header.bmp_important_colors = f->get_32();
242
243 switch (bmp_header.bmp_info_header.bmp_compression) {
244 case BI_BITFIELDS: {
245 bmp_header.bmp_bitfield.red_mask = f->get_32();
246 bmp_header.bmp_bitfield.green_mask = f->get_32();
247 bmp_header.bmp_bitfield.blue_mask = f->get_32();
248 bmp_header.bmp_bitfield.alpha_mask = f->get_32();
249
250 bmp_header.bmp_bitfield.red_mask_width = get_mask_width(bmp_header.bmp_bitfield.red_mask);
251 bmp_header.bmp_bitfield.green_mask_width = get_mask_width(bmp_header.bmp_bitfield.green_mask);
252 bmp_header.bmp_bitfield.blue_mask_width = get_mask_width(bmp_header.bmp_bitfield.blue_mask);
253 bmp_header.bmp_bitfield.alpha_mask_width = get_mask_width(bmp_header.bmp_bitfield.alpha_mask);
254
255 bmp_header.bmp_bitfield.alpha_offset = bmp_header.bmp_bitfield.red_mask_width + bmp_header.bmp_bitfield.green_mask_width + bmp_header.bmp_bitfield.blue_mask_width;
256 bmp_header.bmp_bitfield.red_offset = bmp_header.bmp_bitfield.green_mask_width + bmp_header.bmp_bitfield.blue_mask_width;
257 bmp_header.bmp_bitfield.green_offset = bmp_header.bmp_bitfield.blue_mask_width;
258
259 bmp_header.bmp_bitfield.red_max = (1 << bmp_header.bmp_bitfield.red_mask_width) - 1;
260 bmp_header.bmp_bitfield.green_max = (1 << bmp_header.bmp_bitfield.green_mask_width) - 1;
261 bmp_header.bmp_bitfield.blue_max = (1 << bmp_header.bmp_bitfield.blue_mask_width) - 1;
262 } break;
263 case BI_RLE8:
264 case BI_RLE4:
265 case BI_CMYKRLE8:
266 case BI_CMYKRLE4: {
267 // Stop parsing.
268 ERR_FAIL_V_MSG(ERR_UNAVAILABLE,
269 vformat("RLE compressed BMP files are not yet supported: %s", f->get_path()));
270 } break;
271 }
272 // Don't rely on sizeof(bmp_file_header) as structure padding
273 // adds 2 bytes offset leading to misaligned color table reading
274 uint32_t ct_offset = BITMAP_FILE_HEADER_SIZE + bmp_header.bmp_info_header.bmp_header_size;
275 f->seek(ct_offset);
276
277 uint32_t color_table_size = 0;
278
279 // bmp_colors_used may report 0 despite having a color table
280 // for 4 and 1 bit images, so don't rely on this information
281 if (bmp_header.bmp_info_header.bmp_bit_count <= 8) {
282 // Support 256 colors max
283 color_table_size = 1 << bmp_header.bmp_info_header.bmp_bit_count;
284 ERR_FAIL_COND_V_MSG(color_table_size == 0, ERR_BUG,
285 vformat("Couldn't parse the BMP color table: %s", f->get_path()));
286 }
287
288 Vector<uint8_t> bmp_color_table;
289 // Color table is usually 4 bytes per color -> [B][G][R][0]
290 bmp_color_table.resize(color_table_size * 4);
291 uint8_t *bmp_color_table_w = bmp_color_table.ptrw();
292 f->get_buffer(bmp_color_table_w, color_table_size * 4);
293
294 f->seek(bmp_header.bmp_file_header.bmp_file_offset);
295
296 uint32_t bmp_buffer_size = (bmp_header.bmp_file_header.bmp_file_size - bmp_header.bmp_file_header.bmp_file_offset);
297
298 Vector<uint8_t> bmp_buffer;
299 err = bmp_buffer.resize(bmp_buffer_size);
300 if (err == OK) {
301 uint8_t *bmp_buffer_w = bmp_buffer.ptrw();
302 f->get_buffer(bmp_buffer_w, bmp_buffer_size);
303
304 const uint8_t *bmp_buffer_r = bmp_buffer.ptr();
305 const uint8_t *bmp_color_table_r = bmp_color_table.ptr();
306 err = convert_to_image(p_image, bmp_buffer_r,
307 bmp_color_table_r, color_table_size, bmp_header);
308 }
309 }
310 }
311 return err;
312 }
313
314 void ImageLoaderBMP::get_recognized_extensions(List<String> *p_extensions) const {
315 p_extensions->push_back("bmp");
316 }
317
318 static Ref<Image> _bmp_mem_loader_func(const uint8_t *p_bmp, int p_size) {
319 Ref<FileAccessMemory> memfile;
320 memfile.instantiate();
321 Error open_memfile_error = memfile->open_custom(p_bmp, p_size);
322 ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for BMP image buffer.");
323
324 Ref<Image> img;
325 img.instantiate();
326 Error load_error = ImageLoaderBMP().load_image(img, memfile, false, 1.0f);
327 ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load BMP image.");
328 return img;
329 }
330
331 ImageLoaderBMP::ImageLoaderBMP() {
332 Image::_bmp_mem_loader_func = _bmp_mem_loader_func;
333 }