TGALoader.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2022, Tom Needham <06needhamt@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Span.h>
  7. #include <AK/StdLibExtraDetails.h>
  8. #include <AK/String.h>
  9. #include <LibGfx/TGALoader.h>
  10. namespace Gfx {
  11. enum TGADataType : u8 {
  12. None = 0,
  13. UncompressedColorMapped = 1,
  14. UncompressedRGB = 2,
  15. UncompressedBlackAndWhite = 3,
  16. RunLengthEncodedColorMapped = 9,
  17. RunLengthEncodedRGB = 10,
  18. CompressedBlackAndWhite = 11,
  19. CompressedColorMapped = 32,
  20. CompressedColorMappedFourPass = 33
  21. };
  22. struct [[gnu::packed]] TGAHeader {
  23. u8 id_length;
  24. u8 color_map_type;
  25. TGADataType data_type_code;
  26. i16 color_map_origin;
  27. i16 color_map_length;
  28. u8 color_map_depth;
  29. i16 x_origin;
  30. i16 y_origin;
  31. u16 width;
  32. u16 height;
  33. u8 bits_per_pixel;
  34. u8 image_descriptor;
  35. };
  36. static_assert(sizeof(TGAHeader) == 18);
  37. union [[gnu::packed]] TGAPixel {
  38. struct TGAColor {
  39. u8 blue;
  40. u8 green;
  41. u8 red;
  42. u8 alpha;
  43. } components;
  44. u32 data;
  45. };
  46. struct TGAPixelPacket {
  47. bool raw;
  48. u8 pixels_count;
  49. };
  50. static_assert(AssertSize<TGAPixel, 4>());
  51. class TGAReader {
  52. public:
  53. TGAReader(ReadonlyBytes data)
  54. : m_data(move(data))
  55. {
  56. }
  57. ALWAYS_INLINE u8 read_u8()
  58. {
  59. u8 value = m_data[m_index];
  60. m_index++;
  61. return value;
  62. }
  63. ALWAYS_INLINE i8 read_i8()
  64. {
  65. return static_cast<i8>(read_u8());
  66. }
  67. ALWAYS_INLINE u16 read_u16()
  68. {
  69. return read_u8() | read_u8() << 8;
  70. }
  71. ALWAYS_INLINE i16 read_i16()
  72. {
  73. return read_i8() | read_i8() << 8;
  74. }
  75. ALWAYS_INLINE u32 read_u32()
  76. {
  77. return read_u16() | read_u16() << 16;
  78. }
  79. ALWAYS_INLINE i32 read_i32()
  80. {
  81. return read_i16() | read_i16() << 16;
  82. }
  83. ALWAYS_INLINE TGAPixelPacket read_packet_type()
  84. {
  85. auto pixel_packet_type = read_u8();
  86. auto pixel_packet = TGAPixelPacket();
  87. pixel_packet.raw = !(pixel_packet_type & 0x80);
  88. pixel_packet.pixels_count = (pixel_packet_type & 0x7f);
  89. // NOTE: Run-length-encoded/Raw pixel packets cannot encode zero pixels,
  90. // so value 0 stands for 1 pixel, 1 stands for 2, etc...
  91. pixel_packet.pixels_count++;
  92. return pixel_packet;
  93. }
  94. ALWAYS_INLINE TGAPixel read_pixel(u8 bits_per_pixel)
  95. {
  96. auto pixel = TGAPixel();
  97. switch (bits_per_pixel) {
  98. case 24:
  99. pixel.components.blue = read_u8();
  100. pixel.components.green = read_u8();
  101. pixel.components.red = read_u8();
  102. pixel.components.alpha = 0xFF;
  103. return pixel;
  104. case 32:
  105. pixel.components.blue = read_u8();
  106. pixel.components.green = read_u8();
  107. pixel.components.red = read_u8();
  108. pixel.components.alpha = read_u8();
  109. return pixel;
  110. default:
  111. VERIFY_NOT_REACHED();
  112. }
  113. }
  114. size_t index() const
  115. {
  116. return m_index;
  117. }
  118. ReadonlyBytes data() const
  119. {
  120. return m_data;
  121. }
  122. private:
  123. ReadonlyBytes m_data;
  124. size_t m_index { 0 };
  125. };
  126. struct TGALoadingContext {
  127. TGAHeader header;
  128. ReadonlyBytes bytes;
  129. size_t file_size;
  130. OwnPtr<TGAReader> reader = { nullptr };
  131. RefPtr<Gfx::Bitmap> bitmap;
  132. };
  133. TGAImageDecoderPlugin::TGAImageDecoderPlugin(u8 const* file_data, size_t file_size)
  134. {
  135. m_context = make<TGALoadingContext>();
  136. m_context->bytes = ReadonlyBytes(file_data, file_size);
  137. m_context->file_size = move(file_size);
  138. m_context->reader = make<TGAReader>(m_context->bytes);
  139. }
  140. TGAImageDecoderPlugin::~TGAImageDecoderPlugin() = default;
  141. IntSize TGAImageDecoderPlugin::size()
  142. {
  143. return IntSize { m_context->header.width, m_context->header.height };
  144. }
  145. void TGAImageDecoderPlugin::set_volatile()
  146. {
  147. if (m_context->bitmap)
  148. m_context->bitmap->set_volatile();
  149. }
  150. bool TGAImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  151. {
  152. if (!m_context->bitmap)
  153. return false;
  154. return m_context->bitmap->set_nonvolatile(was_purged);
  155. }
  156. bool TGAImageDecoderPlugin::decode_tga_header()
  157. {
  158. auto& reader = m_context->reader;
  159. m_context->header = TGAHeader();
  160. m_context->header.id_length = reader->read_u8();
  161. m_context->header.color_map_type = reader->read_u8();
  162. m_context->header.data_type_code = static_cast<TGADataType>(reader->read_u8());
  163. m_context->header.color_map_origin = reader->read_i16();
  164. m_context->header.color_map_length = reader->read_i16();
  165. m_context->header.color_map_depth = reader->read_u8();
  166. m_context->header.x_origin = reader->read_i16();
  167. m_context->header.y_origin = reader->read_i16();
  168. m_context->header.width = reader->read_u16();
  169. m_context->header.height = reader->read_u16();
  170. m_context->header.bits_per_pixel = reader->read_u8();
  171. m_context->header.image_descriptor = reader->read_u8();
  172. auto bytes_remaining = reader->data().size() - reader->index();
  173. if (m_context->header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < (m_context->header.width * m_context->header.height * (m_context->header.bits_per_pixel / 8)))
  174. return false;
  175. if (m_context->header.bits_per_pixel < 8 || m_context->header.bits_per_pixel > 32)
  176. return false;
  177. return true;
  178. }
  179. bool TGAImageDecoderPlugin::sniff()
  180. {
  181. return decode_tga_header();
  182. }
  183. bool TGAImageDecoderPlugin::is_animated()
  184. {
  185. return false;
  186. }
  187. size_t TGAImageDecoderPlugin::loop_count()
  188. {
  189. return 0;
  190. }
  191. size_t TGAImageDecoderPlugin::frame_count()
  192. {
  193. return 1;
  194. }
  195. ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index)
  196. {
  197. auto bits_per_pixel = m_context->header.bits_per_pixel;
  198. auto color_map = m_context->header.color_map_type;
  199. auto data_type = m_context->header.data_type_code;
  200. auto width = m_context->header.width;
  201. auto height = m_context->header.height;
  202. auto x_origin = m_context->header.x_origin;
  203. auto y_origin = m_context->header.y_origin;
  204. if (index != 0)
  205. return Error::from_string_literal("TGAImageDecoderPlugin: frame index must be 0");
  206. if (color_map > 1)
  207. return Error::from_string_literal("TGAImageDecoderPlugin: Invalid color map type");
  208. switch (bits_per_pixel) {
  209. case 24:
  210. m_context->bitmap = TRY(Bitmap::try_create(BitmapFormat::BGRx8888, { m_context->header.width, m_context->header.height }));
  211. break;
  212. case 32:
  213. m_context->bitmap = TRY(Bitmap::try_create(BitmapFormat::BGRA8888, { m_context->header.width, m_context->header.height }));
  214. break;
  215. default:
  216. // FIXME: Implement other TGA bit depths
  217. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle 24 and 32 bits per pixel");
  218. }
  219. // FIXME: Try to understand the Image origin (instead of X and Y origin coordinates)
  220. // based on the Image descriptor, Field 5.6, bits 4 and 5.
  221. // NOTE: If Y origin is set to a negative number, just assume the generating software
  222. // meant that we start with Y origin at the top height of the picture.
  223. // At least this is the observed behavior when generating some pictures in GIMP.
  224. if (y_origin < 0)
  225. y_origin = height;
  226. if (y_origin != 0 && y_origin != height)
  227. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle Y origin which is 0 or the entire height");
  228. if (x_origin != 0 && x_origin != width)
  229. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle X origin which is 0 or the entire width");
  230. switch (data_type) {
  231. case TGADataType::UncompressedRGB: {
  232. for (int row = 0; row < height; ++row) {
  233. for (int col = 0; col < width; ++col) {
  234. auto pixel = m_context->reader->read_pixel(bits_per_pixel);
  235. auto actual_row = row;
  236. if (y_origin < height)
  237. actual_row = height - 1 - row;
  238. auto actual_col = col;
  239. if (x_origin > width)
  240. actual_col = width - 1 - col;
  241. m_context->bitmap->scanline(actual_row)[actual_col] = pixel.data;
  242. }
  243. }
  244. break;
  245. }
  246. case TGADataType::RunLengthEncodedRGB: {
  247. size_t pixel_index = 0;
  248. size_t pixel_count = height * width;
  249. while (pixel_index < pixel_count) {
  250. auto packet_type = m_context->reader->read_packet_type();
  251. VERIFY(packet_type.pixels_count > 0);
  252. TGAPixel pixel = m_context->reader->read_pixel(bits_per_pixel);
  253. auto max_pixel_index = min(pixel_index + packet_type.pixels_count, pixel_count);
  254. for (size_t current_pixel_index = pixel_index; current_pixel_index < max_pixel_index; ++current_pixel_index) {
  255. int row = current_pixel_index / width;
  256. int col = current_pixel_index % width;
  257. auto actual_row = row;
  258. if (y_origin < height)
  259. actual_row = height - 1 - row;
  260. auto actual_col = col;
  261. if (x_origin > width)
  262. actual_col = width - 1 - col;
  263. m_context->bitmap->scanline(actual_row)[actual_col] = pixel.data;
  264. if (packet_type.raw && (current_pixel_index + 1) < max_pixel_index)
  265. pixel = m_context->reader->read_pixel(bits_per_pixel);
  266. }
  267. pixel_index += packet_type.pixels_count;
  268. }
  269. break;
  270. }
  271. default:
  272. // FIXME: Implement other TGA data types
  273. return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB or CompressedRGB data type");
  274. }
  275. VERIFY(m_context->bitmap);
  276. return ImageFrameDescriptor { m_context->bitmap, 0 };
  277. }
  278. }