image_loader_bmp.cpp 12 KB

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