TGALoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/ImageFormats/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. TGAReader(ReadonlyBytes data, size_t index)
  58. : m_data(move(data))
  59. , m_index(index)
  60. {
  61. }
  62. ALWAYS_INLINE u8 read_u8()
  63. {
  64. u8 value = m_data[m_index];
  65. m_index++;
  66. return value;
  67. }
  68. ALWAYS_INLINE i8 read_i8()
  69. {
  70. return static_cast<i8>(read_u8());
  71. }
  72. ALWAYS_INLINE u16 read_u16()
  73. {
  74. return read_u8() | read_u8() << 8;
  75. }
  76. ALWAYS_INLINE i16 read_i16()
  77. {
  78. return read_i8() | read_i8() << 8;
  79. }
  80. ALWAYS_INLINE u32 read_u32()
  81. {
  82. return read_u16() | read_u16() << 16;
  83. }
  84. ALWAYS_INLINE i32 read_i32()
  85. {
  86. return read_i16() | read_i16() << 16;
  87. }
  88. ALWAYS_INLINE TGAPixelPacket read_packet_type()
  89. {
  90. auto pixel_packet_type = read_u8();
  91. auto pixel_packet = TGAPixelPacket();
  92. pixel_packet.raw = !(pixel_packet_type & 0x80);
  93. pixel_packet.pixels_count = (pixel_packet_type & 0x7f);
  94. // NOTE: Run-length-encoded/Raw pixel packets cannot encode zero pixels,
  95. // so value 0 stands for 1 pixel, 1 stands for 2, etc...
  96. pixel_packet.pixels_count++;
  97. return pixel_packet;
  98. }
  99. ALWAYS_INLINE TGAPixel read_pixel(u8 bits_per_pixel)
  100. {
  101. auto pixel = TGAPixel();
  102. switch (bits_per_pixel) {
  103. case 24:
  104. pixel.components.blue = read_u8();
  105. pixel.components.green = read_u8();
  106. pixel.components.red = read_u8();
  107. pixel.components.alpha = 0xFF;
  108. return pixel;
  109. case 32:
  110. pixel.components.blue = read_u8();
  111. pixel.components.green = read_u8();
  112. pixel.components.red = read_u8();
  113. pixel.components.alpha = read_u8();
  114. return pixel;
  115. default:
  116. VERIFY_NOT_REACHED();
  117. }
  118. }
  119. size_t index() const
  120. {
  121. return m_index;
  122. }
  123. ReadonlyBytes data() const
  124. {
  125. return m_data;
  126. }
  127. private:
  128. ReadonlyBytes m_data;
  129. size_t m_index { 0 };
  130. };
  131. struct TGALoadingContext {
  132. TGAHeader header;
  133. OwnPtr<TGAReader> reader = { nullptr };
  134. RefPtr<Gfx::Bitmap> bitmap;
  135. };
  136. TGAImageDecoderPlugin::TGAImageDecoderPlugin(u8 const* file_data, size_t file_size)
  137. {
  138. m_context = make<TGALoadingContext>();
  139. m_context->reader = make<TGAReader>(ReadonlyBytes { file_data, file_size });
  140. }
  141. TGAImageDecoderPlugin::~TGAImageDecoderPlugin() = default;
  142. IntSize TGAImageDecoderPlugin::size()
  143. {
  144. return IntSize { m_context->header.width, m_context->header.height };
  145. }
  146. bool TGAImageDecoderPlugin::decode_tga_header()
  147. {
  148. auto& reader = m_context->reader;
  149. if (reader->data().size() < sizeof(TGAHeader))
  150. return false;
  151. m_context->header = TGAHeader();
  152. m_context->header.id_length = reader->read_u8();
  153. m_context->header.color_map_type = reader->read_u8();
  154. m_context->header.data_type_code = static_cast<TGADataType>(reader->read_u8());
  155. m_context->header.color_map_origin = reader->read_i16();
  156. m_context->header.color_map_length = reader->read_i16();
  157. m_context->header.color_map_depth = reader->read_u8();
  158. m_context->header.x_origin = reader->read_i16();
  159. m_context->header.y_origin = reader->read_i16();
  160. m_context->header.width = reader->read_u16();
  161. m_context->header.height = reader->read_u16();
  162. m_context->header.bits_per_pixel = reader->read_u8();
  163. m_context->header.image_descriptor = reader->read_u8();
  164. auto bytes_remaining = reader->data().size() - reader->index();
  165. // FIXME: Check for multiplication overflow!
  166. if (m_context->header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < static_cast<size_t>(m_context->header.width * m_context->header.height * (m_context->header.bits_per_pixel / 8)))
  167. return false;
  168. if (m_context->header.bits_per_pixel < 8 || m_context->header.bits_per_pixel > 32)
  169. return false;
  170. return true;
  171. }
  172. ErrorOr<void> TGAImageDecoderPlugin::initialize()
  173. {
  174. if (decode_tga_header())
  175. return {};
  176. return Error::from_string_literal("Bad TGA header");
  177. }
  178. ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)
  179. {
  180. if (data.size() < sizeof(TGAHeader))
  181. return false;
  182. TGAHeader const& header = *reinterpret_cast<TGAHeader const*>(data.data());
  183. // FIXME: Check for multiplication overflow!
  184. if (header.data_type_code == TGADataType::UncompressedRGB && data.size() < static_cast<size_t>(header.width * header.height * (header.bits_per_pixel / 8)))
  185. return false;
  186. if (header.bits_per_pixel < 8 || header.bits_per_pixel > 32)
  187. return false;
  188. return true;
  189. }
  190. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(ReadonlyBytes data)
  191. {
  192. return adopt_nonnull_own_or_enomem(new (nothrow) TGAImageDecoderPlugin(data.data(), data.size()));
  193. }
  194. bool TGAImageDecoderPlugin::is_animated()
  195. {
  196. return false;
  197. }
  198. size_t TGAImageDecoderPlugin::loop_count()
  199. {
  200. return 0;
  201. }
  202. size_t TGAImageDecoderPlugin::frame_count()
  203. {
  204. return 1;
  205. }
  206. size_t TGAImageDecoderPlugin::first_animated_frame_index()
  207. {
  208. return 0;
  209. }
  210. ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  211. {
  212. auto bits_per_pixel = m_context->header.bits_per_pixel;
  213. auto color_map = m_context->header.color_map_type;
  214. auto data_type = m_context->header.data_type_code;
  215. auto width = m_context->header.width;
  216. auto height = m_context->header.height;
  217. auto x_origin = m_context->header.x_origin;
  218. auto y_origin = m_context->header.y_origin;
  219. if (index != 0)
  220. return Error::from_string_literal("TGAImageDecoderPlugin: frame index must be 0");
  221. if (color_map > 1)
  222. return Error::from_string_literal("TGAImageDecoderPlugin: Invalid color map type");
  223. if (m_context->bitmap) {
  224. return ImageFrameDescriptor { m_context->bitmap, 0 };
  225. } else {
  226. // NOTE: Just to be on the safe side, if m_context->bitmap is nullptr, then
  227. // just re-construct the reader object. This will ensure that if the bitmap
  228. // was set as volatile and therefore it is gone, we can always re-generate it
  229. // with a new call to this method!
  230. VERIFY(m_context->reader);
  231. m_context->reader = make<TGAReader>(m_context->reader->data(), sizeof(TGAHeader));
  232. }
  233. RefPtr<Gfx::Bitmap> bitmap;
  234. switch (bits_per_pixel) {
  235. case 24:
  236. bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_context->header.width, m_context->header.height }));
  237. break;
  238. case 32:
  239. bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { m_context->header.width, m_context->header.height }));
  240. break;
  241. default:
  242. // FIXME: Implement other TGA bit depths
  243. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle 24 and 32 bits per pixel");
  244. }
  245. // FIXME: Try to understand the Image origin (instead of X and Y origin coordinates)
  246. // based on the Image descriptor, Field 5.6, bits 4 and 5.
  247. // NOTE: If Y origin is set to a negative number, just assume the generating software
  248. // meant that we start with Y origin at the top height of the picture.
  249. // At least this is the observed behavior when generating some pictures in GIMP.
  250. if (y_origin < 0)
  251. y_origin = height;
  252. if (y_origin != 0 && y_origin != height)
  253. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle Y origin which is 0 or the entire height");
  254. if (x_origin != 0 && x_origin != width)
  255. return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle X origin which is 0 or the entire width");
  256. switch (data_type) {
  257. case TGADataType::UncompressedRGB: {
  258. for (int row = 0; row < height; ++row) {
  259. for (int col = 0; col < width; ++col) {
  260. auto pixel = m_context->reader->read_pixel(bits_per_pixel);
  261. auto actual_row = row;
  262. if (y_origin < height)
  263. actual_row = height - 1 - row;
  264. auto actual_col = col;
  265. if (x_origin > width)
  266. actual_col = width - 1 - col;
  267. bitmap->scanline(actual_row)[actual_col] = pixel.data;
  268. }
  269. }
  270. break;
  271. }
  272. case TGADataType::RunLengthEncodedRGB: {
  273. size_t pixel_index = 0;
  274. size_t pixel_count = height * width;
  275. while (pixel_index < pixel_count) {
  276. auto packet_type = m_context->reader->read_packet_type();
  277. VERIFY(packet_type.pixels_count > 0);
  278. TGAPixel pixel = m_context->reader->read_pixel(bits_per_pixel);
  279. auto max_pixel_index = min(pixel_index + packet_type.pixels_count, pixel_count);
  280. for (size_t current_pixel_index = pixel_index; current_pixel_index < max_pixel_index; ++current_pixel_index) {
  281. int row = current_pixel_index / width;
  282. int col = current_pixel_index % width;
  283. auto actual_row = row;
  284. if (y_origin < height)
  285. actual_row = height - 1 - row;
  286. auto actual_col = col;
  287. if (x_origin > width)
  288. actual_col = width - 1 - col;
  289. bitmap->scanline(actual_row)[actual_col] = pixel.data;
  290. if (packet_type.raw && (current_pixel_index + 1) < max_pixel_index)
  291. pixel = m_context->reader->read_pixel(bits_per_pixel);
  292. }
  293. pixel_index += packet_type.pixels_count;
  294. }
  295. break;
  296. }
  297. default:
  298. // FIXME: Implement other TGA data types
  299. return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB or CompressedRGB data type");
  300. }
  301. m_context->bitmap = bitmap;
  302. return ImageFrameDescriptor { m_context->bitmap, 0 };
  303. }
  304. ErrorOr<Optional<ReadonlyBytes>> TGAImageDecoderPlugin::icc_data()
  305. {
  306. return OptionalNone {};
  307. }
  308. }