image_loader_bmp.cpp 13 KB

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