1 /*************************************************************************/
2 /*  image_loader_bmp.cpp                                                 */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 
convert_to_image(Ref<Image> p_image,const uint8_t * p_buffer,const uint8_t * p_color_buffer,const uint32_t color_table_size,const bmp_header_s & p_header)33 Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
34 		const uint8_t *p_buffer,
35 		const uint8_t *p_color_buffer,
36 		const uint32_t color_table_size,
37 		const bmp_header_s &p_header) {
38 
39 	Error err = OK;
40 
41 	if (p_buffer == NULL)
42 		err = FAILED;
43 
44 	if (err == OK) {
45 		size_t index = 0;
46 		size_t width = (size_t)p_header.bmp_info_header.bmp_width;
47 		size_t height = (size_t)p_header.bmp_info_header.bmp_height;
48 		size_t bits_per_pixel = (size_t)p_header.bmp_info_header.bmp_bit_count;
49 
50 		// Check whether we can load it
51 
52 		if (bits_per_pixel == 1) {
53 			// Requires bit unpacking...
54 			ERR_FAIL_COND_V_MSG(width % 8 != 0, ERR_UNAVAILABLE,
55 					vformat("1-bpp BMP images must have a width that is a multiple of 8, but the imported BMP is %d pixels wide.", int(width)));
56 			ERR_FAIL_COND_V_MSG(height % 8 != 0, ERR_UNAVAILABLE,
57 					vformat("1-bpp BMP images must have a height that is a multiple of 8, but the imported BMP is %d pixels tall.", int(height)));
58 
59 		} else if (bits_per_pixel == 4) {
60 			// Requires bit unpacking...
61 			ERR_FAIL_COND_V_MSG(width % 2 != 0, ERR_UNAVAILABLE,
62 					vformat("4-bpp BMP images must have a width that is a multiple of 2, but the imported BMP is %d pixels wide.", int(width)));
63 			ERR_FAIL_COND_V_MSG(height % 2 != 0, ERR_UNAVAILABLE,
64 					vformat("4-bpp BMP images must have a height that is a multiple of 2, but the imported BMP is %d pixels tall.", int(height)));
65 
66 		} else if (bits_per_pixel == 16) {
67 			ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "16-bpp BMP images are not supported.");
68 		}
69 
70 		// Image data (might be indexed)
71 		PoolVector<uint8_t> data;
72 		int data_len = 0;
73 
74 		if (bits_per_pixel <= 8) { // indexed
75 			data_len = width * height;
76 		} else { // color
77 			data_len = width * height * 4;
78 		}
79 		ERR_FAIL_COND_V_MSG(data_len == 0, ERR_BUG, "Couldn't parse the BMP image data.");
80 		err = data.resize(data_len);
81 
82 		PoolVector<uint8_t>::Write data_w = data.write();
83 		uint8_t *write_buffer = data_w.ptr();
84 
85 		const uint32_t width_bytes = width * bits_per_pixel / 8;
86 		const uint32_t line_width = (width_bytes + 3) & ~3;
87 
88 		// The actual data traversal is determined by
89 		// the data width in case of 8/4/1 bit images
90 		const uint32_t w = bits_per_pixel >= 24 ? width : width_bytes;
91 		const uint8_t *line = p_buffer + (line_width * (height - 1));
92 
93 		for (uint64_t i = 0; i < height; i++) {
94 			const uint8_t *line_ptr = line;
95 
96 			for (unsigned int j = 0; j < w; j++) {
97 				switch (bits_per_pixel) {
98 					case 1: {
99 						uint8_t color_index = *line_ptr;
100 
101 						write_buffer[index + 0] = (color_index >> 7) & 1;
102 						write_buffer[index + 1] = (color_index >> 6) & 1;
103 						write_buffer[index + 2] = (color_index >> 5) & 1;
104 						write_buffer[index + 3] = (color_index >> 4) & 1;
105 						write_buffer[index + 4] = (color_index >> 3) & 1;
106 						write_buffer[index + 5] = (color_index >> 2) & 1;
107 						write_buffer[index + 6] = (color_index >> 1) & 1;
108 						write_buffer[index + 7] = (color_index >> 0) & 1;
109 
110 						index += 8;
111 						line_ptr += 1;
112 					} break;
113 					case 4: {
114 						uint8_t color_index = *line_ptr;
115 
116 						write_buffer[index + 0] = (color_index >> 4) & 0x0f;
117 						write_buffer[index + 1] = color_index & 0x0f;
118 
119 						index += 2;
120 						line_ptr += 1;
121 					} break;
122 					case 8: {
123 						uint8_t color_index = *line_ptr;
124 
125 						write_buffer[index] = color_index;
126 
127 						index += 1;
128 						line_ptr += 1;
129 					} break;
130 					case 24: {
131 						uint32_t color = *((uint32_t *)line_ptr);
132 
133 						write_buffer[index + 2] = color & 0xff;
134 						write_buffer[index + 1] = (color >> 8) & 0xff;
135 						write_buffer[index + 0] = (color >> 16) & 0xff;
136 						write_buffer[index + 3] = 0xff;
137 
138 						index += 4;
139 						line_ptr += 3;
140 					} break;
141 					case 32: {
142 						uint32_t color = *((uint32_t *)line_ptr);
143 
144 						write_buffer[index + 2] = color & 0xff;
145 						write_buffer[index + 1] = (color >> 8) & 0xff;
146 						write_buffer[index + 0] = (color >> 16) & 0xff;
147 						write_buffer[index + 3] = color >> 24;
148 
149 						index += 4;
150 						line_ptr += 4;
151 					} break;
152 				}
153 			}
154 			line -= line_width;
155 		}
156 
157 		if (p_color_buffer == NULL || color_table_size == 0) { // regular pixels
158 
159 			p_image->create(width, height, 0, Image::FORMAT_RGBA8, data);
160 
161 		} else { // data is in indexed format, extend it
162 
163 			// Palette data
164 			PoolVector<uint8_t> palette_data;
165 			palette_data.resize(color_table_size * 4);
166 
167 			PoolVector<uint8_t>::Write palette_data_w = palette_data.write();
168 			uint8_t *pal = palette_data_w.ptr();
169 
170 			const uint8_t *cb = p_color_buffer;
171 
172 			for (unsigned int i = 0; i < color_table_size; ++i) {
173 				uint32_t color = *((uint32_t *)cb);
174 
175 				pal[i * 4 + 0] = (color >> 16) & 0xff;
176 				pal[i * 4 + 1] = (color >> 8) & 0xff;
177 				pal[i * 4 + 2] = (color)&0xff;
178 				pal[i * 4 + 3] = 0xff;
179 
180 				cb += 4;
181 			}
182 			// Extend palette to image
183 			PoolVector<uint8_t> extended_data;
184 			extended_data.resize(data.size() * 4);
185 
186 			PoolVector<uint8_t>::Write ex_w = extended_data.write();
187 			uint8_t *dest = ex_w.ptr();
188 
189 			const int num_pixels = width * height;
190 
191 			for (int i = 0; i < num_pixels; i++) {
192 				dest[0] = pal[write_buffer[i] * 4 + 0];
193 				dest[1] = pal[write_buffer[i] * 4 + 1];
194 				dest[2] = pal[write_buffer[i] * 4 + 2];
195 				dest[3] = pal[write_buffer[i] * 4 + 3];
196 
197 				dest += 4;
198 			}
199 			p_image->create(width, height, 0, Image::FORMAT_RGBA8, extended_data);
200 		}
201 	}
202 	return err;
203 }
204 
load_image(Ref<Image> p_image,FileAccess * f,bool p_force_linear,float p_scale)205 Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,
206 		bool p_force_linear, float p_scale) {
207 
208 	bmp_header_s bmp_header;
209 	Error err = ERR_INVALID_DATA;
210 
211 	// A valid bmp file should always at least have a
212 	// file header and a minimal info header
213 	if (f->get_len() > BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_MIN_SIZE) {
214 		// File Header
215 		bmp_header.bmp_file_header.bmp_signature = f->get_16();
216 		if (bmp_header.bmp_file_header.bmp_signature == BITMAP_SIGNATURE) {
217 			bmp_header.bmp_file_header.bmp_file_size = f->get_32();
218 			bmp_header.bmp_file_header.bmp_file_padding = f->get_32();
219 			bmp_header.bmp_file_header.bmp_file_offset = f->get_32();
220 
221 			// Info Header
222 			bmp_header.bmp_info_header.bmp_header_size = f->get_32();
223 			ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_header_size < BITMAP_INFO_HEADER_MIN_SIZE, ERR_FILE_CORRUPT,
224 					vformat("Couldn't parse the BMP info header. The file is likely corrupt: %s", f->get_path()));
225 
226 			bmp_header.bmp_info_header.bmp_width = f->get_32();
227 			bmp_header.bmp_info_header.bmp_height = f->get_32();
228 
229 			bmp_header.bmp_info_header.bmp_planes = f->get_16();
230 			ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_planes != 1, ERR_FILE_CORRUPT,
231 					vformat("Couldn't parse the BMP planes. The file is likely corrupt: %s", f->get_path()));
232 
233 			bmp_header.bmp_info_header.bmp_bit_count = f->get_16();
234 			bmp_header.bmp_info_header.bmp_compression = f->get_32();
235 			bmp_header.bmp_info_header.bmp_size_image = f->get_32();
236 			bmp_header.bmp_info_header.bmp_pixels_per_meter_x = f->get_32();
237 			bmp_header.bmp_info_header.bmp_pixels_per_meter_y = f->get_32();
238 			bmp_header.bmp_info_header.bmp_colors_used = f->get_32();
239 			bmp_header.bmp_info_header.bmp_important_colors = f->get_32();
240 
241 			switch (bmp_header.bmp_info_header.bmp_compression) {
242 				case BI_RLE8:
243 				case BI_RLE4:
244 				case BI_CMYKRLE8:
245 				case BI_CMYKRLE4: {
246 					// Stop parsing.
247 					f->close();
248 					ERR_FAIL_V_MSG(ERR_UNAVAILABLE,
249 							vformat("Compressed BMP files are not supported: %s", f->get_path()));
250 				} break;
251 			}
252 			// Don't rely on sizeof(bmp_file_header) as structure padding
253 			// adds 2 bytes offset leading to misaligned color table reading
254 			uint32_t ct_offset = BITMAP_FILE_HEADER_SIZE +
255 								 bmp_header.bmp_info_header.bmp_header_size;
256 			f->seek(ct_offset);
257 
258 			uint32_t color_table_size = 0;
259 
260 			// bmp_colors_used may report 0 despite having a color table
261 			// for 4 and 1 bit images, so don't rely on this information
262 			if (bmp_header.bmp_info_header.bmp_bit_count <= 8) {
263 				// Support 256 colors max
264 				color_table_size = 1 << bmp_header.bmp_info_header.bmp_bit_count;
265 				ERR_FAIL_COND_V_MSG(color_table_size == 0, ERR_BUG,
266 						vformat("Couldn't parse the BMP color table: %s", f->get_path()));
267 			}
268 
269 			PoolVector<uint8_t> bmp_color_table;
270 			// Color table is usually 4 bytes per color -> [B][G][R][0]
271 			bmp_color_table.resize(color_table_size * 4);
272 			PoolVector<uint8_t>::Write bmp_color_table_w = bmp_color_table.write();
273 			f->get_buffer(bmp_color_table_w.ptr(), color_table_size * 4);
274 
275 			f->seek(bmp_header.bmp_file_header.bmp_file_offset);
276 
277 			uint32_t bmp_buffer_size = (bmp_header.bmp_file_header.bmp_file_size -
278 										bmp_header.bmp_file_header.bmp_file_offset);
279 
280 			PoolVector<uint8_t> bmp_buffer;
281 			err = bmp_buffer.resize(bmp_buffer_size);
282 			if (err == OK) {
283 				PoolVector<uint8_t>::Write bmp_buffer_w = bmp_buffer.write();
284 				f->get_buffer(bmp_buffer_w.ptr(), bmp_buffer_size);
285 
286 				PoolVector<uint8_t>::Read bmp_buffer_r = bmp_buffer.read();
287 				PoolVector<uint8_t>::Read bmp_color_table_r = bmp_color_table.read();
288 				err = convert_to_image(p_image, bmp_buffer_r.ptr(),
289 						bmp_color_table_r.ptr(), color_table_size, bmp_header);
290 			}
291 			f->close();
292 		}
293 	}
294 	return err;
295 }
296 
get_recognized_extensions(List<String> * p_extensions) const297 void ImageLoaderBMP::get_recognized_extensions(
298 		List<String> *p_extensions) const {
299 
300 	p_extensions->push_back("bmp");
301 }
302 
ImageLoaderBMP()303 ImageLoaderBMP::ImageLoaderBMP() {}
304