image_loader_tga.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*************************************************************************/
  2. /* image_loader_tga.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_tga.h"
  31. #include "core/error_macros.h"
  32. #include "core/io/file_access_memory.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. Error ImageLoaderTGA::decode_tga_rle(const uint8_t *p_compressed_buffer, size_t p_pixel_size, uint8_t *p_uncompressed_buffer, size_t p_output_size) {
  36. Error error;
  37. PoolVector<uint8_t> pixels;
  38. error = pixels.resize(p_pixel_size);
  39. if (error != OK)
  40. return error;
  41. PoolVector<uint8_t>::Write pixels_w = pixels.write();
  42. size_t compressed_pos = 0;
  43. size_t output_pos = 0;
  44. size_t c = 0;
  45. size_t count = 0;
  46. while (output_pos < p_output_size) {
  47. c = p_compressed_buffer[compressed_pos];
  48. compressed_pos += 1;
  49. count = (c & 0x7f) + 1;
  50. if (c & 0x80) {
  51. for (size_t i = 0; i < p_pixel_size; i++) {
  52. pixels_w.ptr()[i] = p_compressed_buffer[compressed_pos];
  53. compressed_pos += 1;
  54. }
  55. for (size_t i = 0; i < count; i++) {
  56. for (size_t j = 0; j < p_pixel_size; j++) {
  57. p_uncompressed_buffer[output_pos + j] = pixels_w.ptr()[j];
  58. }
  59. output_pos += p_pixel_size;
  60. }
  61. } else {
  62. count *= p_pixel_size;
  63. for (size_t i = 0; i < count; i++) {
  64. p_uncompressed_buffer[output_pos] = p_compressed_buffer[compressed_pos];
  65. compressed_pos += 1;
  66. output_pos += 1;
  67. }
  68. }
  69. }
  70. return OK;
  71. }
  72. Error ImageLoaderTGA::convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome) {
  73. #define TGA_PUT_PIXEL(r, g, b, a) \
  74. int image_data_ofs = ((y * width) + x); \
  75. image_data_w[image_data_ofs * 4 + 0] = r; \
  76. image_data_w[image_data_ofs * 4 + 1] = g; \
  77. image_data_w[image_data_ofs * 4 + 2] = b; \
  78. image_data_w[image_data_ofs * 4 + 3] = a;
  79. uint32_t width = p_header.image_width;
  80. uint32_t height = p_header.image_height;
  81. tga_origin_e origin = static_cast<tga_origin_e>((p_header.image_descriptor & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT);
  82. uint32_t x_start;
  83. int32_t x_step;
  84. uint32_t x_end;
  85. uint32_t y_start;
  86. int32_t y_step;
  87. uint32_t y_end;
  88. if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_TOP_RIGHT) {
  89. y_start = 0;
  90. y_step = 1;
  91. y_end = height;
  92. } else {
  93. y_start = height - 1;
  94. y_step = -1;
  95. y_end = -1;
  96. }
  97. if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_BOTTOM_LEFT) {
  98. x_start = 0;
  99. x_step = 1;
  100. x_end = width;
  101. } else {
  102. x_start = width - 1;
  103. x_step = -1;
  104. x_end = -1;
  105. }
  106. PoolVector<uint8_t> image_data;
  107. image_data.resize(width * height * sizeof(uint32_t));
  108. PoolVector<uint8_t>::Write image_data_w = image_data.write();
  109. size_t i = 0;
  110. uint32_t x = x_start;
  111. uint32_t y = y_start;
  112. if (p_header.pixel_depth == 8) {
  113. if (p_is_monochrome) {
  114. while (y != y_end) {
  115. while (x != x_end) {
  116. uint8_t shade = p_buffer[i];
  117. TGA_PUT_PIXEL(shade, shade, shade, 0xff)
  118. x += x_step;
  119. i += 1;
  120. }
  121. x = x_start;
  122. y += y_step;
  123. }
  124. } else {
  125. while (y != y_end) {
  126. while (x != x_end) {
  127. uint8_t index = p_buffer[i];
  128. uint8_t r = 0x00;
  129. uint8_t g = 0x00;
  130. uint8_t b = 0x00;
  131. uint8_t a = 0xff;
  132. if (p_header.color_map_depth == 24) {
  133. // Due to low-high byte order, the color table must be
  134. // read in the same order as image data (little endian)
  135. r = (p_palette[(index * 3) + 2]);
  136. g = (p_palette[(index * 3) + 1]);
  137. b = (p_palette[(index * 3) + 0]);
  138. } else {
  139. return ERR_INVALID_DATA;
  140. }
  141. TGA_PUT_PIXEL(r, g, b, a)
  142. x += x_step;
  143. i += 1;
  144. }
  145. x = x_start;
  146. y += y_step;
  147. }
  148. }
  149. } else if (p_header.pixel_depth == 24) {
  150. while (y != y_end) {
  151. while (x != x_end) {
  152. uint8_t r = p_buffer[i + 2];
  153. uint8_t g = p_buffer[i + 1];
  154. uint8_t b = p_buffer[i + 0];
  155. TGA_PUT_PIXEL(r, g, b, 0xff)
  156. x += x_step;
  157. i += 3;
  158. }
  159. x = x_start;
  160. y += y_step;
  161. }
  162. } else if (p_header.pixel_depth == 32) {
  163. while (y != y_end) {
  164. while (x != x_end) {
  165. uint8_t a = p_buffer[i + 3];
  166. uint8_t r = p_buffer[i + 2];
  167. uint8_t g = p_buffer[i + 1];
  168. uint8_t b = p_buffer[i + 0];
  169. TGA_PUT_PIXEL(r, g, b, a)
  170. x += x_step;
  171. i += 4;
  172. }
  173. x = x_start;
  174. y += y_step;
  175. }
  176. }
  177. image_data_w.release();
  178. p_image->create(width, height, 0, Image::FORMAT_RGBA8, image_data);
  179. return OK;
  180. }
  181. Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  182. PoolVector<uint8_t> src_image;
  183. int src_image_len = f->get_len();
  184. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  185. ERR_FAIL_COND_V(src_image_len < (int)sizeof(tga_header_s), ERR_FILE_CORRUPT);
  186. src_image.resize(src_image_len);
  187. Error err = OK;
  188. tga_header_s tga_header;
  189. tga_header.id_length = f->get_8();
  190. tga_header.color_map_type = f->get_8();
  191. tga_header.image_type = static_cast<tga_type_e>(f->get_8());
  192. tga_header.first_color_entry = f->get_16();
  193. tga_header.color_map_length = f->get_16();
  194. tga_header.color_map_depth = f->get_8();
  195. tga_header.x_origin = f->get_16();
  196. tga_header.y_origin = f->get_16();
  197. tga_header.image_width = f->get_16();
  198. tga_header.image_height = f->get_16();
  199. tga_header.pixel_depth = f->get_8();
  200. tga_header.image_descriptor = f->get_8();
  201. bool is_encoded = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_RLE_RGB || tga_header.image_type == TGA_TYPE_RLE_MONOCHROME);
  202. bool has_color_map = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_INDEXED);
  203. bool is_monochrome = (tga_header.image_type == TGA_TYPE_RLE_MONOCHROME || tga_header.image_type == TGA_TYPE_MONOCHROME);
  204. if (tga_header.image_type == TGA_TYPE_NO_DATA)
  205. err = FAILED;
  206. if (has_color_map) {
  207. if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) {
  208. err = FAILED;
  209. }
  210. } else {
  211. if (tga_header.color_map_type) {
  212. err = FAILED;
  213. }
  214. }
  215. if (tga_header.image_width <= 0 || tga_header.image_height <= 0)
  216. err = FAILED;
  217. if (!(tga_header.pixel_depth == 8 || tga_header.pixel_depth == 24 || tga_header.pixel_depth == 32)) {
  218. err = FAILED;
  219. }
  220. if (err == OK) {
  221. f->seek(f->get_position() + tga_header.id_length);
  222. PoolVector<uint8_t> palette;
  223. if (has_color_map) {
  224. size_t color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
  225. err = palette.resize(color_map_size);
  226. if (err == OK) {
  227. PoolVector<uint8_t>::Write palette_w = palette.write();
  228. f->get_buffer(&palette_w[0], color_map_size);
  229. } else {
  230. return OK;
  231. }
  232. }
  233. PoolVector<uint8_t>::Write src_image_w = src_image.write();
  234. f->get_buffer(&src_image_w[0], src_image_len - f->get_position());
  235. PoolVector<uint8_t>::Read src_image_r = src_image.read();
  236. const size_t pixel_size = tga_header.pixel_depth >> 3;
  237. const size_t buffer_size = (tga_header.image_width * tga_header.image_height) * pixel_size;
  238. PoolVector<uint8_t> uncompressed_buffer;
  239. uncompressed_buffer.resize(buffer_size);
  240. PoolVector<uint8_t>::Write uncompressed_buffer_w = uncompressed_buffer.write();
  241. PoolVector<uint8_t>::Read uncompressed_buffer_r;
  242. const uint8_t *buffer = NULL;
  243. if (is_encoded) {
  244. err = decode_tga_rle(src_image_r.ptr(), pixel_size, uncompressed_buffer_w.ptr(), buffer_size);
  245. if (err == OK) {
  246. uncompressed_buffer_r = uncompressed_buffer.read();
  247. buffer = uncompressed_buffer_r.ptr();
  248. }
  249. } else {
  250. buffer = src_image_r.ptr();
  251. };
  252. if (err == OK) {
  253. PoolVector<uint8_t>::Read palette_r = palette.read();
  254. err = convert_to_image(p_image, buffer, tga_header, palette_r.ptr(), is_monochrome);
  255. }
  256. }
  257. f->close();
  258. return err;
  259. }
  260. void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const {
  261. p_extensions->push_back("tga");
  262. }
  263. static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
  264. FileAccessMemory memfile;
  265. Error open_memfile_error = memfile.open_custom(p_tga, p_size);
  266. ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
  267. Ref<Image> img;
  268. img.instance();
  269. Error load_error = ImageLoaderTGA().load_image(img, &memfile, false, 1.0f);
  270. ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load TGA image.");
  271. return img;
  272. }
  273. ImageLoaderTGA::ImageLoaderTGA() {
  274. Image::_tga_mem_loader_func = _tga_mem_loader_func;
  275. }