image_loader_bmp.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "image_loader_bmp.h"
  31. Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
  32. const uint8_t *p_buffer,
  33. const uint8_t *p_color_buffer,
  34. const bmp_header_s &p_header) {
  35. Error err = OK;
  36. if (p_buffer == NULL)
  37. err = FAILED;
  38. if (err == OK) {
  39. size_t index = 0;
  40. size_t width = (size_t)p_header.bmp_info_header.bmp_width;
  41. size_t height = (size_t)p_header.bmp_info_header.bmp_height;
  42. size_t bits_per_pixel = (size_t)p_header.bmp_info_header.bmp_bit_count;
  43. if (p_header.bmp_info_header.bmp_compression != 0) {
  44. err = FAILED;
  45. }
  46. if (!(bits_per_pixel == 24 || bits_per_pixel == 32)) {
  47. err = FAILED;
  48. }
  49. if (err == OK) {
  50. uint32_t line_width = ((p_header.bmp_info_header.bmp_width *
  51. p_header.bmp_info_header.bmp_bit_count / 8) +
  52. 3) &
  53. ~3;
  54. PoolVector<uint8_t> image_data;
  55. err = image_data.resize(width * height * 4);
  56. PoolVector<uint8_t>::Write image_data_w = image_data.write();
  57. uint8_t *write_buffer = image_data_w.ptr();
  58. const uint8_t *line = p_buffer + (line_width * (height - 1));
  59. for (unsigned int i = 0; i < height; i++) {
  60. const uint8_t *line_ptr = line;
  61. for (unsigned int j = 0; j < width; j++) {
  62. switch (bits_per_pixel) {
  63. case 24: {
  64. uint32_t color = *((uint32_t *)line_ptr);
  65. write_buffer[index + 2] = color & 0xff;
  66. write_buffer[index + 1] = (color >> 8) & 0xff;
  67. write_buffer[index + 0] = (color >> 16) & 0xff;
  68. write_buffer[index + 3] = 0xff;
  69. index += 4;
  70. line_ptr += 3;
  71. } break;
  72. case 32: {
  73. uint32_t color = *((uint32_t *)line_ptr);
  74. write_buffer[index + 2] = color & 0xff;
  75. write_buffer[index + 1] = (color >> 8) & 0xff;
  76. write_buffer[index + 0] = (color >> 16) & 0xff;
  77. write_buffer[index + 3] = color >> 24;
  78. index += 4;
  79. line_ptr += 4;
  80. } break;
  81. }
  82. }
  83. line -= line_width;
  84. }
  85. p_image->create(width, height, 0, Image::FORMAT_RGBA8, image_data);
  86. }
  87. }
  88. return err;
  89. }
  90. Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,
  91. bool p_force_linear, float p_scale) {
  92. bmp_header_s bmp_header;
  93. Error err = ERR_INVALID_DATA;
  94. if (f->get_len() > sizeof(bmp_header)) {
  95. // File Header
  96. bmp_header.bmp_file_header.bmp_signature = f->get_16();
  97. if (bmp_header.bmp_file_header.bmp_signature == BITMAP_SIGNATURE) {
  98. bmp_header.bmp_file_header.bmp_file_size = f->get_32();
  99. bmp_header.bmp_file_header.bmp_file_padding = f->get_32();
  100. bmp_header.bmp_file_header.bmp_file_offset = f->get_32();
  101. // Info Header
  102. bmp_header.bmp_info_header.bmp_header_size = f->get_32();
  103. bmp_header.bmp_info_header.bmp_width = f->get_32();
  104. bmp_header.bmp_info_header.bmp_height = f->get_32();
  105. bmp_header.bmp_info_header.bmp_planes = f->get_16();
  106. bmp_header.bmp_info_header.bmp_bit_count = f->get_16();
  107. bmp_header.bmp_info_header.bmp_compression = f->get_32();
  108. bmp_header.bmp_info_header.bmp_size_image = f->get_32();
  109. bmp_header.bmp_info_header.bmp_pixels_per_meter_x = f->get_32();
  110. bmp_header.bmp_info_header.bmp_pixels_per_meter_y = f->get_32();
  111. bmp_header.bmp_info_header.bmp_colors_used = f->get_32();
  112. bmp_header.bmp_info_header.bmp_important_colors = f->get_32();
  113. bmp_header.bmp_info_header.bmp_red_mask = f->get_32();
  114. bmp_header.bmp_info_header.bmp_green_mask = f->get_32();
  115. bmp_header.bmp_info_header.bmp_blue_mask = f->get_32();
  116. bmp_header.bmp_info_header.bmp_alpha_mask = f->get_32();
  117. bmp_header.bmp_info_header.bmp_cs_type = f->get_32();
  118. for (int i = 0; i < 9; i++)
  119. bmp_header.bmp_info_header.bmp_endpoints[i] = f->get_32();
  120. bmp_header.bmp_info_header.bmp_gamma_red = f->get_32();
  121. bmp_header.bmp_info_header.bmp_gamma_green = f->get_32();
  122. bmp_header.bmp_info_header.bmp_gamma_blue = f->get_32();
  123. f->seek(sizeof(bmp_header.bmp_file_header) +
  124. bmp_header.bmp_info_header.bmp_header_size);
  125. uint32_t color_table_size = 0;
  126. if (bmp_header.bmp_info_header.bmp_bit_count == 1)
  127. color_table_size = 2;
  128. else if (bmp_header.bmp_info_header.bmp_bit_count == 4)
  129. color_table_size = 16;
  130. else if (bmp_header.bmp_info_header.bmp_bit_count == 8)
  131. color_table_size = 256;
  132. PoolVector<uint8_t> bmp_color_table;
  133. if (color_table_size > 0) {
  134. err = bmp_color_table.resize(color_table_size * 4);
  135. PoolVector<uint8_t>::Write bmp_color_table_w = bmp_color_table.write();
  136. f->get_buffer(bmp_color_table_w.ptr(),
  137. bmp_header.bmp_info_header.bmp_colors_used * 4);
  138. }
  139. f->seek(bmp_header.bmp_file_header.bmp_file_offset);
  140. uint32_t bmp_buffer_size = (bmp_header.bmp_file_header.bmp_file_size -
  141. bmp_header.bmp_file_header.bmp_file_offset);
  142. PoolVector<uint8_t> bmp_buffer;
  143. err = bmp_buffer.resize(bmp_buffer_size);
  144. if (err == OK) {
  145. PoolVector<uint8_t>::Write bmp_buffer_w = bmp_buffer.write();
  146. f->get_buffer(bmp_buffer_w.ptr(), bmp_buffer_size);
  147. PoolVector<uint8_t>::Read bmp_buffer_r = bmp_buffer.read();
  148. PoolVector<uint8_t>::Read bmp_color_table_r = bmp_color_table.read();
  149. err = convert_to_image(p_image, bmp_buffer_r.ptr(),
  150. bmp_color_table_r.ptr(), bmp_header);
  151. }
  152. f->close();
  153. }
  154. }
  155. return err;
  156. }
  157. void ImageLoaderBMP::get_recognized_extensions(
  158. List<String> *p_extensions) const {
  159. p_extensions->push_back("bmp");
  160. }
  161. ImageLoaderBMP::ImageLoaderBMP() {}