image_loader_bmp.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "image_loader_bmp.h"
  31. #include "core/io/file_access_memory.h"
  32. static uint8_t get_mask_width(uint16_t mask) {
  33. // Returns number of ones in the binary value of the parameter: mask.
  34. // Uses a Simple pop_count.
  35. uint8_t c = 0u;
  36. for (; mask != 0u; mask &= mask - 1u) {
  37. c++;
  38. }
  39. return c;
  40. }
  41. Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
  42. const uint8_t *p_buffer,
  43. const uint8_t *p_color_buffer,
  44. const uint32_t color_table_size,
  45. const bmp_header_s &p_header) {
  46. Error err = OK;
  47. if (p_buffer == nullptr) {
  48. err = FAILED;
  49. }
  50. if (err == OK) {
  51. size_t index = 0;
  52. size_t width = (size_t)p_header.bmp_info_header.bmp_width;
  53. size_t height = (size_t)p_header.bmp_info_header.bmp_height;
  54. size_t bits_per_pixel = (size_t)p_header.bmp_info_header.bmp_bit_count;
  55. // Check whether we can load it
  56. if (bits_per_pixel == 1) {
  57. // Requires bit unpacking...
  58. ERR_FAIL_COND_V_MSG(width % 8 != 0, ERR_UNAVAILABLE,
  59. 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)));
  60. ERR_FAIL_COND_V_MSG(height % 8 != 0, ERR_UNAVAILABLE,
  61. 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)));
  62. } else if (bits_per_pixel == 2) {
  63. // Requires bit unpacking...
  64. ERR_FAIL_COND_V_MSG(width % 4 != 0, ERR_UNAVAILABLE,
  65. vformat("2-bpp BMP images must have a width that is a multiple of 4, but the imported BMP is %d pixels wide.", int(width)));
  66. ERR_FAIL_COND_V_MSG(height % 4 != 0, ERR_UNAVAILABLE,
  67. vformat("2-bpp BMP images must have a height that is a multiple of 4, but the imported BMP is %d pixels tall.", int(height)));
  68. } else if (bits_per_pixel == 4) {
  69. // Requires bit unpacking...
  70. ERR_FAIL_COND_V_MSG(width % 2 != 0, ERR_UNAVAILABLE,
  71. 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)));
  72. ERR_FAIL_COND_V_MSG(height % 2 != 0, ERR_UNAVAILABLE,
  73. 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)));
  74. }
  75. // Image data (might be indexed)
  76. Vector<uint8_t> data;
  77. int data_len = 0;
  78. if (bits_per_pixel <= 8) { // indexed
  79. data_len = width * height;
  80. } else { // color
  81. data_len = width * height * 4;
  82. }
  83. ERR_FAIL_COND_V_MSG(data_len == 0, ERR_BUG, "Couldn't parse the BMP image data.");
  84. err = data.resize(data_len);
  85. uint8_t *data_w = data.ptrw();
  86. uint8_t *write_buffer = data_w;
  87. const uint32_t width_bytes = width * bits_per_pixel / 8;
  88. const uint32_t line_width = (width_bytes + 3) & ~3;
  89. // The actual data traversal is determined by
  90. // the data width in case of 8/4/2/1 bit images
  91. const uint32_t w = bits_per_pixel >= 16 ? width : width_bytes;
  92. const uint8_t *line = p_buffer + (line_width * (height - 1));
  93. const uint8_t *end_buffer = p_buffer + p_header.bmp_file_header.bmp_file_size - p_header.bmp_file_header.bmp_file_offset;
  94. for (uint64_t i = 0; i < height; i++) {
  95. const uint8_t *line_ptr = line;
  96. for (unsigned int j = 0; j < w; j++) {
  97. ERR_FAIL_COND_V(line_ptr >= end_buffer, ERR_FILE_CORRUPT);
  98. switch (bits_per_pixel) {
  99. case 1: {
  100. uint8_t color_index = *line_ptr;
  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. index += 8;
  110. line_ptr += 1;
  111. } break;
  112. case 2: {
  113. uint8_t color_index = *line_ptr;
  114. write_buffer[index + 0] = (color_index >> 6) & 3;
  115. write_buffer[index + 1] = (color_index >> 4) & 3;
  116. write_buffer[index + 2] = (color_index >> 2) & 3;
  117. write_buffer[index + 3] = color_index & 3;
  118. index += 4;
  119. line_ptr += 1;
  120. } break;
  121. case 4: {
  122. uint8_t color_index = *line_ptr;
  123. write_buffer[index + 0] = (color_index >> 4) & 0x0f;
  124. write_buffer[index + 1] = color_index & 0x0f;
  125. index += 2;
  126. line_ptr += 1;
  127. } break;
  128. case 8: {
  129. uint8_t color_index = *line_ptr;
  130. write_buffer[index] = color_index;
  131. index += 1;
  132. line_ptr += 1;
  133. } break;
  134. case 16: {
  135. uint16_t rgb = (static_cast<uint16_t>(line_ptr[1]) << 8) | line_ptr[0];
  136. // A1R5G5B5/X1R5G5B5 => uint16_t
  137. // [A/X]1R5G2 | G3B5 => uint8_t | uint8_t
  138. uint8_t ba = (rgb & p_header.bmp_bitfield.alpha_mask) >> p_header.bmp_bitfield.alpha_offset; // Alpha 0b 1000 ...
  139. uint8_t b0 = (rgb & p_header.bmp_bitfield.red_mask) >> p_header.bmp_bitfield.red_offset; // Red 0b 0111 1100 ...
  140. uint8_t b1 = (rgb & p_header.bmp_bitfield.green_mask) >> p_header.bmp_bitfield.green_offset; // Green 0b 0000 0011 1110 ...
  141. uint8_t b2 = (rgb & p_header.bmp_bitfield.blue_mask); // >> p_header.bmp_bitfield.blue_offset; // Blue 0b ... 0001 1111
  142. // Next we apply some color scaling going from a variable value space to a 256 value space.
  143. // This may be simplified some but left as is for legibility.
  144. // float scaled_value = unscaled_value * byte_max_value / color_channel_maximum_value + rounding_offset;
  145. float f0 = b0 * 255.0f / static_cast<float>(p_header.bmp_bitfield.red_max) + 0.5f;
  146. float f1 = b1 * 255.0f / static_cast<float>(p_header.bmp_bitfield.green_max) + 0.5f;
  147. float f2 = b2 * 255.0f / static_cast<float>(p_header.bmp_bitfield.blue_max) + 0.5f;
  148. write_buffer[index + 0] = static_cast<uint8_t>(f0); // R
  149. write_buffer[index + 1] = static_cast<uint8_t>(f1); // G
  150. write_buffer[index + 2] = static_cast<uint8_t>(f2); // B
  151. if (p_header.bmp_bitfield.alpha_mask_width > 0) {
  152. write_buffer[index + 3] = ba * 0xFF; // Alpha value(Always true or false so no scaling)
  153. } else {
  154. write_buffer[index + 3] = 0xFF; // No Alpha channel, Show everything.
  155. }
  156. index += 4;
  157. line_ptr += 2;
  158. } break;
  159. case 24: {
  160. write_buffer[index + 2] = line_ptr[0];
  161. write_buffer[index + 1] = line_ptr[1];
  162. write_buffer[index + 0] = line_ptr[2];
  163. write_buffer[index + 3] = 0xff;
  164. index += 4;
  165. line_ptr += 3;
  166. } break;
  167. case 32: {
  168. write_buffer[index + 2] = line_ptr[0];
  169. write_buffer[index + 1] = line_ptr[1];
  170. write_buffer[index + 0] = line_ptr[2];
  171. write_buffer[index + 3] = line_ptr[3];
  172. index += 4;
  173. line_ptr += 4;
  174. } break;
  175. }
  176. }
  177. line -= line_width;
  178. }
  179. if (p_color_buffer == nullptr || color_table_size == 0) { // regular pixels
  180. p_image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
  181. } else { // data is in indexed format, extend it
  182. // Palette data
  183. Vector<uint8_t> palette_data;
  184. palette_data.resize(color_table_size * 4);
  185. uint8_t *palette_data_w = palette_data.ptrw();
  186. uint8_t *pal = palette_data_w;
  187. const uint8_t *cb = p_color_buffer;
  188. for (unsigned int i = 0; i < color_table_size; ++i) {
  189. pal[i * 4 + 0] = cb[2];
  190. pal[i * 4 + 1] = cb[1];
  191. pal[i * 4 + 2] = cb[0];
  192. pal[i * 4 + 3] = 0xff;
  193. cb += 4;
  194. }
  195. // Extend palette to image
  196. Vector<uint8_t> extended_data;
  197. extended_data.resize(data.size() * 4);
  198. uint8_t *ex_w = extended_data.ptrw();
  199. uint8_t *dest = ex_w;
  200. const int num_pixels = width * height;
  201. for (int i = 0; i < num_pixels; i++) {
  202. dest[0] = pal[write_buffer[i] * 4 + 0];
  203. dest[1] = pal[write_buffer[i] * 4 + 1];
  204. dest[2] = pal[write_buffer[i] * 4 + 2];
  205. dest[3] = pal[write_buffer[i] * 4 + 3];
  206. dest += 4;
  207. }
  208. p_image->set_data(width, height, false, Image::FORMAT_RGBA8, extended_data);
  209. }
  210. }
  211. return err;
  212. }
  213. Error ImageLoaderBMP::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  214. bmp_header_s bmp_header;
  215. Error err = ERR_INVALID_DATA;
  216. // A valid bmp file should always at least have a
  217. // file header and a minimal info header
  218. if (f->get_length() > BITMAP_FILE_HEADER_SIZE + BITMAP_INFO_HEADER_MIN_SIZE) {
  219. // File Header
  220. bmp_header.bmp_file_header.bmp_signature = f->get_16();
  221. if (bmp_header.bmp_file_header.bmp_signature == BITMAP_SIGNATURE) {
  222. bmp_header.bmp_file_header.bmp_file_size = f->get_32();
  223. bmp_header.bmp_file_header.bmp_file_padding = f->get_32();
  224. bmp_header.bmp_file_header.bmp_file_offset = f->get_32();
  225. // Info Header
  226. bmp_header.bmp_info_header.bmp_header_size = f->get_32();
  227. ERR_FAIL_COND_V_MSG(bmp_header.bmp_info_header.bmp_header_size < BITMAP_INFO_HEADER_MIN_SIZE, ERR_FILE_CORRUPT,
  228. vformat("Couldn't parse the BMP info header. The file is likely corrupt: %s", f->get_path()));
  229. bmp_header.bmp_info_header.bmp_width = f->get_32();
  230. bmp_header.bmp_info_header.bmp_height = f->get_32();
  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. bmp_header.bmp_info_header.bmp_bit_count = f->get_16();
  235. bmp_header.bmp_info_header.bmp_compression = f->get_32();
  236. bmp_header.bmp_info_header.bmp_size_image = f->get_32();
  237. bmp_header.bmp_info_header.bmp_pixels_per_meter_x = f->get_32();
  238. bmp_header.bmp_info_header.bmp_pixels_per_meter_y = f->get_32();
  239. bmp_header.bmp_info_header.bmp_colors_used = f->get_32();
  240. bmp_header.bmp_info_header.bmp_important_colors = f->get_32();
  241. switch (bmp_header.bmp_info_header.bmp_compression) {
  242. case BI_BITFIELDS: {
  243. bmp_header.bmp_bitfield.red_mask = f->get_32();
  244. bmp_header.bmp_bitfield.green_mask = f->get_32();
  245. bmp_header.bmp_bitfield.blue_mask = f->get_32();
  246. bmp_header.bmp_bitfield.alpha_mask = f->get_32();
  247. bmp_header.bmp_bitfield.red_mask_width = get_mask_width(bmp_header.bmp_bitfield.red_mask);
  248. bmp_header.bmp_bitfield.green_mask_width = get_mask_width(bmp_header.bmp_bitfield.green_mask);
  249. bmp_header.bmp_bitfield.blue_mask_width = get_mask_width(bmp_header.bmp_bitfield.blue_mask);
  250. bmp_header.bmp_bitfield.alpha_mask_width = get_mask_width(bmp_header.bmp_bitfield.alpha_mask);
  251. 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;
  252. bmp_header.bmp_bitfield.red_offset = bmp_header.bmp_bitfield.green_mask_width + bmp_header.bmp_bitfield.blue_mask_width;
  253. bmp_header.bmp_bitfield.green_offset = bmp_header.bmp_bitfield.blue_mask_width;
  254. bmp_header.bmp_bitfield.red_max = (1 << bmp_header.bmp_bitfield.red_mask_width) - 1;
  255. bmp_header.bmp_bitfield.green_max = (1 << bmp_header.bmp_bitfield.green_mask_width) - 1;
  256. bmp_header.bmp_bitfield.blue_max = (1 << bmp_header.bmp_bitfield.blue_mask_width) - 1;
  257. } break;
  258. case BI_RLE8:
  259. case BI_RLE4:
  260. case BI_CMYKRLE8:
  261. case BI_CMYKRLE4: {
  262. // Stop parsing.
  263. ERR_FAIL_V_MSG(ERR_UNAVAILABLE,
  264. vformat("RLE compressed BMP files are not yet supported: %s", f->get_path()));
  265. } break;
  266. }
  267. // Don't rely on sizeof(bmp_file_header) as structure padding
  268. // adds 2 bytes offset leading to misaligned color table reading
  269. uint32_t ct_offset = BITMAP_FILE_HEADER_SIZE + bmp_header.bmp_info_header.bmp_header_size;
  270. f->seek(ct_offset);
  271. uint32_t color_table_size = 0;
  272. // bmp_colors_used may report 0 despite having a color table
  273. // for 4 and 1 bit images, so don't rely on this information
  274. if (bmp_header.bmp_info_header.bmp_bit_count <= 8) {
  275. // Support 256 colors max
  276. color_table_size = 1 << bmp_header.bmp_info_header.bmp_bit_count;
  277. ERR_FAIL_COND_V_MSG(color_table_size == 0, ERR_BUG,
  278. vformat("Couldn't parse the BMP color table: %s", f->get_path()));
  279. }
  280. Vector<uint8_t> bmp_color_table;
  281. // Color table is usually 4 bytes per color -> [B][G][R][0]
  282. bmp_color_table.resize(color_table_size * 4);
  283. uint8_t *bmp_color_table_w = bmp_color_table.ptrw();
  284. f->get_buffer(bmp_color_table_w, color_table_size * 4);
  285. f->seek(bmp_header.bmp_file_header.bmp_file_offset);
  286. uint32_t bmp_buffer_size = (bmp_header.bmp_file_header.bmp_file_size - bmp_header.bmp_file_header.bmp_file_offset);
  287. Vector<uint8_t> bmp_buffer;
  288. err = bmp_buffer.resize(bmp_buffer_size);
  289. if (err == OK) {
  290. uint8_t *bmp_buffer_w = bmp_buffer.ptrw();
  291. f->get_buffer(bmp_buffer_w, bmp_buffer_size);
  292. const uint8_t *bmp_buffer_r = bmp_buffer.ptr();
  293. const uint8_t *bmp_color_table_r = bmp_color_table.ptr();
  294. err = convert_to_image(p_image, bmp_buffer_r,
  295. bmp_color_table_r, color_table_size, bmp_header);
  296. }
  297. }
  298. }
  299. return err;
  300. }
  301. void ImageLoaderBMP::get_recognized_extensions(List<String> *p_extensions) const {
  302. p_extensions->push_back("bmp");
  303. }
  304. static Ref<Image> _bmp_mem_loader_func(const uint8_t *p_bmp, int p_size) {
  305. Ref<FileAccessMemory> memfile;
  306. memfile.instantiate();
  307. Error open_memfile_error = memfile->open_custom(p_bmp, p_size);
  308. ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for BMP image buffer.");
  309. Ref<Image> img;
  310. img.instantiate();
  311. Error load_error = ImageLoaderBMP().load_image(img, memfile, false, 1.0f);
  312. ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load BMP image.");
  313. return img;
  314. }
  315. ImageLoaderBMP::ImageLoaderBMP() {
  316. Image::_bmp_mem_loader_func = _bmp_mem_loader_func;
  317. }