image_loader_tga.cpp 12 KB

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