image_loader_bmp.cpp 12 KB

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