image_loader_tga.cpp 9.5 KB

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