image_loader_tga.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/os/os.h"
  34. #include "core/string/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. Vector<uint8_t> pixels;
  38. error = pixels.resize(p_pixel_size);
  39. if (error != OK) {
  40. return error;
  41. }
  42. uint8_t *pixels_w = pixels.ptrw();
  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[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[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. Vector<uint8_t> image_data;
  118. image_data.resize(width * height * sizeof(uint32_t));
  119. uint8_t *image_data_w = image_data.ptrw();
  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. p_image->initialize_data(width, height, false, Image::FORMAT_RGBA8, image_data);
  219. return OK;
  220. }
  221. Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  222. Vector<uint8_t> src_image;
  223. uint64_t src_image_len = f->get_length();
  224. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  225. ERR_FAIL_COND_V(src_image_len < (int64_t)sizeof(tga_header_s), ERR_FILE_CORRUPT);
  226. src_image.resize(src_image_len);
  227. Error err = OK;
  228. tga_header_s tga_header;
  229. tga_header.id_length = f->get_8();
  230. tga_header.color_map_type = f->get_8();
  231. tga_header.image_type = static_cast<tga_type_e>(f->get_8());
  232. tga_header.first_color_entry = f->get_16();
  233. tga_header.color_map_length = f->get_16();
  234. tga_header.color_map_depth = f->get_8();
  235. tga_header.x_origin = f->get_16();
  236. tga_header.y_origin = f->get_16();
  237. tga_header.image_width = f->get_16();
  238. tga_header.image_height = f->get_16();
  239. tga_header.pixel_depth = f->get_8();
  240. tga_header.image_descriptor = f->get_8();
  241. 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);
  242. bool has_color_map = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_INDEXED);
  243. bool is_monochrome = (tga_header.image_type == TGA_TYPE_RLE_MONOCHROME || tga_header.image_type == TGA_TYPE_MONOCHROME);
  244. if (tga_header.image_type == TGA_TYPE_NO_DATA) {
  245. err = FAILED;
  246. }
  247. uint64_t color_map_size;
  248. if (has_color_map) {
  249. if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) {
  250. err = FAILED;
  251. }
  252. color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
  253. } else {
  254. if (tga_header.color_map_type) {
  255. err = FAILED;
  256. }
  257. color_map_size = 0;
  258. }
  259. if ((src_image_len - f->get_position()) < (tga_header.id_length + color_map_size)) {
  260. err = FAILED; // TGA data appears to be truncated (fewer bytes than expected).
  261. }
  262. if (tga_header.image_width <= 0 || tga_header.image_height <= 0) {
  263. err = FAILED;
  264. }
  265. if (!(tga_header.pixel_depth == 8 || tga_header.pixel_depth == 16 || tga_header.pixel_depth == 24 || tga_header.pixel_depth == 32)) {
  266. err = FAILED;
  267. }
  268. if (err == OK) {
  269. f->seek(f->get_position() + tga_header.id_length);
  270. Vector<uint8_t> palette;
  271. if (has_color_map) {
  272. err = palette.resize(color_map_size);
  273. if (err == OK) {
  274. uint8_t *palette_w = palette.ptrw();
  275. f->get_buffer(&palette_w[0], color_map_size);
  276. } else {
  277. return OK;
  278. }
  279. }
  280. uint8_t *src_image_w = src_image.ptrw();
  281. f->get_buffer(&src_image_w[0], src_image_len - f->get_position());
  282. const uint8_t *src_image_r = src_image.ptr();
  283. const size_t pixel_size = tga_header.pixel_depth >> 3;
  284. size_t buffer_size = (tga_header.image_width * tga_header.image_height) * pixel_size;
  285. Vector<uint8_t> uncompressed_buffer;
  286. uncompressed_buffer.resize(buffer_size);
  287. uint8_t *uncompressed_buffer_w = uncompressed_buffer.ptrw();
  288. const uint8_t *uncompressed_buffer_r;
  289. const uint8_t *buffer = nullptr;
  290. if (is_encoded) {
  291. err = decode_tga_rle(src_image_r, pixel_size, uncompressed_buffer_w, buffer_size, src_image_len);
  292. if (err == OK) {
  293. uncompressed_buffer_r = uncompressed_buffer.ptr();
  294. buffer = uncompressed_buffer_r;
  295. }
  296. } else {
  297. buffer = src_image_r;
  298. buffer_size = src_image_len;
  299. };
  300. if (err == OK) {
  301. const uint8_t *palette_r = palette.ptr();
  302. err = convert_to_image(p_image, buffer, tga_header, palette_r, is_monochrome, buffer_size);
  303. }
  304. }
  305. return err;
  306. }
  307. void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const {
  308. p_extensions->push_back("tga");
  309. }
  310. static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
  311. Ref<FileAccessMemory> memfile;
  312. memfile.instantiate();
  313. Error open_memfile_error = memfile->open_custom(p_tga, p_size);
  314. ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
  315. Ref<Image> img;
  316. img.instantiate();
  317. Error load_error = ImageLoaderTGA().load_image(img, memfile, false, 1.0f);
  318. ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load TGA image.");
  319. return img;
  320. }
  321. ImageLoaderTGA::ImageLoaderTGA() {
  322. Image::_tga_mem_loader_func = _tga_mem_loader_func;
  323. }