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/error_macros.h"
  32. #include "core/io/file_access_memory.h"
  33. #include "core/io/image.h"
  34. #include "core/os/os.h"
  35. #include "core/string/print_string.h"
  36. 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) {
  37. Error error;
  38. Vector<uint8_t> pixels;
  39. error = pixels.resize(p_pixel_size);
  40. if (error != OK) {
  41. return error;
  42. }
  43. uint8_t *pixels_w = pixels.ptrw();
  44. size_t compressed_pos = 0;
  45. size_t output_pos = 0;
  46. size_t c = 0;
  47. size_t count = 0;
  48. while (output_pos < p_output_size) {
  49. c = p_compressed_buffer[compressed_pos];
  50. compressed_pos += 1;
  51. count = (c & 0x7f) + 1;
  52. if (output_pos + count * p_pixel_size > p_output_size) {
  53. return ERR_PARSE_ERROR;
  54. }
  55. if (c & 0x80) {
  56. if (compressed_pos + p_pixel_size > p_input_size) {
  57. return ERR_PARSE_ERROR;
  58. }
  59. for (size_t i = 0; i < p_pixel_size; i++) {
  60. pixels_w[i] = p_compressed_buffer[compressed_pos];
  61. compressed_pos += 1;
  62. }
  63. for (size_t i = 0; i < count; i++) {
  64. for (size_t j = 0; j < p_pixel_size; j++) {
  65. p_uncompressed_buffer[output_pos + j] = pixels_w[j];
  66. }
  67. output_pos += p_pixel_size;
  68. }
  69. } else {
  70. if (compressed_pos + count * p_pixel_size > p_input_size) {
  71. return ERR_PARSE_ERROR;
  72. }
  73. count *= p_pixel_size;
  74. for (size_t i = 0; i < count; i++) {
  75. p_uncompressed_buffer[output_pos] = p_compressed_buffer[compressed_pos];
  76. compressed_pos += 1;
  77. output_pos += 1;
  78. }
  79. }
  80. }
  81. return OK;
  82. }
  83. 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) {
  84. #define TGA_PUT_PIXEL(r, g, b, a) \
  85. int image_data_ofs = ((y * width) + x); \
  86. image_data_w[image_data_ofs * 4 + 0] = r; \
  87. image_data_w[image_data_ofs * 4 + 1] = g; \
  88. image_data_w[image_data_ofs * 4 + 2] = b; \
  89. image_data_w[image_data_ofs * 4 + 3] = a;
  90. uint32_t width = p_header.image_width;
  91. uint32_t height = p_header.image_height;
  92. tga_origin_e origin = static_cast<tga_origin_e>((p_header.image_descriptor & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT);
  93. uint8_t alpha_bits = p_header.image_descriptor & TGA_IMAGE_DESCRIPTOR_ALPHA_MASK;
  94. uint32_t x_start;
  95. int32_t x_step;
  96. uint32_t x_end;
  97. uint32_t y_start;
  98. int32_t y_step;
  99. uint32_t y_end;
  100. if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_TOP_RIGHT) {
  101. y_start = 0;
  102. y_step = 1;
  103. y_end = height;
  104. } else {
  105. y_start = height - 1;
  106. y_step = -1;
  107. y_end = -1;
  108. }
  109. if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_BOTTOM_LEFT) {
  110. x_start = 0;
  111. x_step = 1;
  112. x_end = width;
  113. } else {
  114. x_start = width - 1;
  115. x_step = -1;
  116. x_end = -1;
  117. }
  118. Vector<uint8_t> image_data;
  119. image_data.resize(width * height * sizeof(uint32_t));
  120. uint8_t *image_data_w = image_data.ptrw();
  121. size_t i = 0;
  122. uint32_t x = x_start;
  123. uint32_t y = y_start;
  124. if (p_header.pixel_depth == 8) {
  125. if (p_is_monochrome) {
  126. while (y != y_end) {
  127. while (x != x_end) {
  128. if (i >= p_input_size) {
  129. return ERR_PARSE_ERROR;
  130. }
  131. uint8_t shade = p_buffer[i];
  132. TGA_PUT_PIXEL(shade, shade, shade, 0xff)
  133. x += x_step;
  134. i += 1;
  135. }
  136. x = x_start;
  137. y += y_step;
  138. }
  139. } else {
  140. while (y != y_end) {
  141. while (x != x_end) {
  142. if (i >= p_input_size) {
  143. return ERR_PARSE_ERROR;
  144. }
  145. uint8_t index = p_buffer[i];
  146. uint8_t r = 0x00;
  147. uint8_t g = 0x00;
  148. uint8_t b = 0x00;
  149. uint8_t a = 0xff;
  150. if (p_header.color_map_depth == 24) {
  151. // Due to low-high byte order, the color table must be
  152. // read in the same order as image data (little endian)
  153. r = (p_palette[(index * 3) + 2]);
  154. g = (p_palette[(index * 3) + 1]);
  155. b = (p_palette[(index * 3) + 0]);
  156. } else {
  157. return ERR_INVALID_DATA;
  158. }
  159. TGA_PUT_PIXEL(r, g, b, a)
  160. x += x_step;
  161. i += 1;
  162. }
  163. x = x_start;
  164. y += y_step;
  165. }
  166. }
  167. } else if (p_header.pixel_depth == 16) {
  168. while (y != y_end) {
  169. while (x != x_end) {
  170. if (i + 1 >= p_input_size) {
  171. return ERR_PARSE_ERROR;
  172. }
  173. // Always stored as RGBA5551
  174. uint8_t r = (p_buffer[i + 1] & 0x7c) << 1;
  175. uint8_t g = ((p_buffer[i + 1] & 0x03) << 6) | ((p_buffer[i + 0] & 0xe0) >> 2);
  176. uint8_t b = (p_buffer[i + 0] & 0x1f) << 3;
  177. uint8_t a = (p_buffer[i + 1] & 0x80) ? 0xff : 0;
  178. TGA_PUT_PIXEL(r, g, b, alpha_bits ? a : 0xff);
  179. x += x_step;
  180. i += 2;
  181. }
  182. x = x_start;
  183. y += y_step;
  184. }
  185. } else if (p_header.pixel_depth == 24) {
  186. while (y != y_end) {
  187. while (x != x_end) {
  188. if (i + 2 >= p_input_size) {
  189. return ERR_PARSE_ERROR;
  190. }
  191. uint8_t r = p_buffer[i + 2];
  192. uint8_t g = p_buffer[i + 1];
  193. uint8_t b = p_buffer[i + 0];
  194. TGA_PUT_PIXEL(r, g, b, 0xff)
  195. x += x_step;
  196. i += 3;
  197. }
  198. x = x_start;
  199. y += y_step;
  200. }
  201. } else if (p_header.pixel_depth == 32) {
  202. while (y != y_end) {
  203. while (x != x_end) {
  204. if (i + 3 >= p_input_size) {
  205. return ERR_PARSE_ERROR;
  206. }
  207. uint8_t a = p_buffer[i + 3];
  208. uint8_t r = p_buffer[i + 2];
  209. uint8_t g = p_buffer[i + 1];
  210. uint8_t b = p_buffer[i + 0];
  211. TGA_PUT_PIXEL(r, g, b, a)
  212. x += x_step;
  213. i += 4;
  214. }
  215. x = x_start;
  216. y += y_step;
  217. }
  218. }
  219. p_image->initialize_data(width, height, false, Image::FORMAT_RGBA8, image_data);
  220. return OK;
  221. }
  222. Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  223. Vector<uint8_t> src_image;
  224. uint64_t src_image_len = f->get_length();
  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. Vector<uint8_t> palette;
  272. if (has_color_map) {
  273. err = palette.resize(color_map_size);
  274. if (err == OK) {
  275. uint8_t *palette_w = palette.ptrw();
  276. f->get_buffer(&palette_w[0], color_map_size);
  277. } else {
  278. return OK;
  279. }
  280. }
  281. uint8_t *src_image_w = src_image.ptrw();
  282. f->get_buffer(&src_image_w[0], src_image_len - f->get_position());
  283. const uint8_t *src_image_r = src_image.ptr();
  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. Vector<uint8_t> uncompressed_buffer;
  287. uncompressed_buffer.resize(buffer_size);
  288. uint8_t *uncompressed_buffer_w = uncompressed_buffer.ptrw();
  289. const uint8_t *uncompressed_buffer_r;
  290. const uint8_t *buffer = nullptr;
  291. if (is_encoded) {
  292. err = decode_tga_rle(src_image_r, pixel_size, uncompressed_buffer_w, buffer_size, src_image_len);
  293. if (err == OK) {
  294. uncompressed_buffer_r = uncompressed_buffer.ptr();
  295. buffer = uncompressed_buffer_r;
  296. }
  297. } else {
  298. buffer = src_image_r;
  299. buffer_size = src_image_len;
  300. };
  301. if (err == OK) {
  302. const uint8_t *palette_r = palette.ptr();
  303. err = convert_to_image(p_image, buffer, tga_header, palette_r, is_monochrome, buffer_size);
  304. }
  305. }
  306. return err;
  307. }
  308. void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const {
  309. p_extensions->push_back("tga");
  310. }
  311. static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
  312. Ref<FileAccessMemory> memfile;
  313. memfile.instantiate();
  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.instantiate();
  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. }