TIFFLoader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (c) 2023, Lucas Chollet <lucas.chollet@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TIFFLoader.h"
  7. #include <AK/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/String.h>
  10. #include <LibCompress/LZWDecoder.h>
  11. #include <LibGfx/ImageFormats/TIFFMetadata.h>
  12. namespace Gfx {
  13. namespace TIFF {
  14. class TIFFLoadingContext {
  15. public:
  16. enum class State {
  17. NotDecoded = 0,
  18. Error,
  19. HeaderDecoded,
  20. FrameDecoded,
  21. };
  22. TIFFLoadingContext(NonnullOwnPtr<FixedMemoryStream> stream)
  23. : m_stream(move(stream))
  24. {
  25. }
  26. ErrorOr<void> decode_image_header()
  27. {
  28. TRY(read_image_file_header());
  29. TRY(read_next_image_file_directory());
  30. m_state = State::HeaderDecoded;
  31. return {};
  32. }
  33. ErrorOr<void> decode_frame()
  34. {
  35. auto maybe_error = decode_frame_impl();
  36. if (maybe_error.is_error()) {
  37. m_state = State::Error;
  38. return maybe_error.release_error();
  39. }
  40. return {};
  41. }
  42. IntSize size() const
  43. {
  44. return { *m_metadata.image_width(), *m_metadata.image_height() };
  45. }
  46. State state() const
  47. {
  48. return m_state;
  49. }
  50. RefPtr<Bitmap> bitmap() const
  51. {
  52. return m_bitmap;
  53. }
  54. private:
  55. enum class ByteOrder {
  56. LittleEndian,
  57. BigEndian,
  58. };
  59. template<typename ByteReader>
  60. ErrorOr<void> loop_over_pixels(ByteReader&& byte_reader, Function<ErrorOr<void>(u32)> initializer = {})
  61. {
  62. auto const strips_offset = *m_metadata.strip_offsets();
  63. auto const strip_byte_counts = *m_metadata.strip_byte_counts();
  64. for (u32 strip_index = 0; strip_index < strips_offset.size(); ++strip_index) {
  65. TRY(m_stream->seek(strips_offset[strip_index]));
  66. if (initializer)
  67. TRY(initializer(strip_byte_counts[strip_index]));
  68. for (u32 row = 0; row < *m_metadata.rows_per_strip(); row++) {
  69. auto const scanline = row + *m_metadata.rows_per_strip() * strip_index;
  70. if (scanline >= *m_metadata.image_height())
  71. break;
  72. Optional<Color> last_color {};
  73. for (u32 column = 0; column < *m_metadata.image_width(); ++column) {
  74. auto color = Color { TRY(byte_reader()), TRY(byte_reader()), TRY(byte_reader()) };
  75. if (m_metadata.predictor() == Predictor::HorizontalDifferencing && last_color.has_value()) {
  76. color.set_red(last_color->red() + color.red());
  77. color.set_green(last_color->green() + color.green());
  78. color.set_blue(last_color->blue() + color.blue());
  79. }
  80. last_color = color;
  81. m_bitmap->set_pixel(column, scanline, color);
  82. }
  83. }
  84. }
  85. return {};
  86. }
  87. ErrorOr<void> decode_frame_impl()
  88. {
  89. m_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, size()));
  90. switch (*m_metadata.compression()) {
  91. case Compression::NoCompression:
  92. TRY(loop_over_pixels([this]() { return read_value<u8>(); }));
  93. break;
  94. case Compression::LZW: {
  95. ByteBuffer decoded_bytes {};
  96. u32 read_head {};
  97. auto initializer = [&](u32 bytes) -> ErrorOr<void> {
  98. auto const encoded_bytes = TRY(m_stream->read_in_place<u8 const>(bytes));
  99. if (encoded_bytes.is_empty())
  100. return Error::from_string_literal("TIFFImageDecoderPlugin: Unable to read from empty LZW strip");
  101. // Note: AFAIK, there are two common ways to use LZW compression:
  102. // - With a LittleEndian stream and no Early-Change, this is used in the GIF format
  103. // - With a BigEndian stream and an EarlyChange of 1, this is used in the PDF format
  104. // The fun begins when they decided to change from the former to the latter when moving
  105. // from TIFF 5.0 to 6.0, and without including a way for files to be identified.
  106. // Fortunately, as the first byte of a LZW stream is a constant we can guess the endianess
  107. // and deduce the version from it. The first code is 0x100 (9-bits).
  108. if (encoded_bytes[0] == 0x00)
  109. decoded_bytes = TRY(Compress::LZWDecoder<LittleEndianInputBitStream>::decode_all(encoded_bytes, 8, 0));
  110. else
  111. decoded_bytes = TRY(Compress::LZWDecoder<BigEndianInputBitStream>::decode_all(encoded_bytes, 8, -1));
  112. read_head = 0;
  113. return {};
  114. };
  115. auto read_lzw_byte = [&]() -> ErrorOr<u8> {
  116. if (read_head < decoded_bytes.size())
  117. return decoded_bytes[read_head++];
  118. return Error::from_string_literal("TIFFImageDecoderPlugin: Reached end of LZW stream");
  119. };
  120. TRY(loop_over_pixels([read_lzw_byte = move(read_lzw_byte)]() { return read_lzw_byte(); }, move(initializer)));
  121. break;
  122. }
  123. case Compression::PackBits: {
  124. // Section 9: PackBits Compression
  125. Optional<i8> n;
  126. Optional<u8> saved_byte;
  127. auto read_packed_byte = [&]() -> ErrorOr<u8> {
  128. while (true) {
  129. if (!n.has_value())
  130. n = TRY(read_value<i8>());
  131. if (n.value() >= 0 && !saved_byte.has_value()) {
  132. n.value() = n.value() - 1;
  133. if (n.value() == -1)
  134. n.clear();
  135. return read_value<u8>();
  136. }
  137. if (n.value() == -128) {
  138. n.clear();
  139. continue;
  140. }
  141. if (!saved_byte.has_value())
  142. saved_byte = TRY(read_value<u8>());
  143. n.value() = n.value() + 1;
  144. auto const byte_backup = *saved_byte;
  145. if (n == 1) {
  146. saved_byte.clear();
  147. n.clear();
  148. }
  149. return byte_backup;
  150. }
  151. };
  152. TRY(loop_over_pixels(move(read_packed_byte)));
  153. break;
  154. }
  155. default:
  156. return Error::from_string_literal("This compression type is not supported yet :^)");
  157. }
  158. return {};
  159. }
  160. template<typename T>
  161. ErrorOr<T> read_value()
  162. {
  163. if (m_byte_order == ByteOrder::LittleEndian)
  164. return TRY(m_stream->read_value<LittleEndian<T>>());
  165. if (m_byte_order == ByteOrder::BigEndian)
  166. return TRY(m_stream->read_value<BigEndian<T>>());
  167. VERIFY_NOT_REACHED();
  168. }
  169. ErrorOr<void> read_next_idf_offset()
  170. {
  171. auto const next_block_position = TRY(read_value<u32>());
  172. if (next_block_position != 0)
  173. m_next_ifd = Optional<u32> { next_block_position };
  174. else
  175. m_next_ifd = OptionalNone {};
  176. dbgln_if(TIFF_DEBUG, "Setting image file directory pointer to {}", m_next_ifd);
  177. return {};
  178. }
  179. ErrorOr<void> read_image_file_header()
  180. {
  181. // Section 2: TIFF Structure - Image File Header
  182. auto const byte_order = TRY(m_stream->read_value<u16>());
  183. switch (byte_order) {
  184. case 0x4949:
  185. m_byte_order = ByteOrder::LittleEndian;
  186. break;
  187. case 0x4D4D:
  188. m_byte_order = ByteOrder::BigEndian;
  189. break;
  190. default:
  191. return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid byte order");
  192. }
  193. auto const magic_number = TRY(read_value<u16>());
  194. if (magic_number != 42)
  195. return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid magic number");
  196. TRY(read_next_idf_offset());
  197. return {};
  198. }
  199. ErrorOr<void> read_next_image_file_directory()
  200. {
  201. // Section 2: TIFF Structure - Image File Directory
  202. if (!m_next_ifd.has_value())
  203. return Error::from_string_literal("TIFFImageDecoderPlugin: Missing an Image File Directory");
  204. TRY(m_stream->seek(m_next_ifd.value()));
  205. auto const number_of_field = TRY(read_value<u16>());
  206. for (u16 i = 0; i < number_of_field; ++i)
  207. TRY(read_tag());
  208. TRY(read_next_idf_offset());
  209. return {};
  210. }
  211. ErrorOr<Type> read_type()
  212. {
  213. switch (TRY(read_value<u16>())) {
  214. case to_underlying(Type::Byte):
  215. return Type::Byte;
  216. case to_underlying(Type::ASCII):
  217. return Type::ASCII;
  218. case to_underlying(Type::UnsignedShort):
  219. return Type::UnsignedShort;
  220. case to_underlying(Type::UnsignedLong):
  221. return Type::UnsignedLong;
  222. case to_underlying(Type::UnsignedRational):
  223. return Type::UnsignedRational;
  224. case to_underlying(Type::Undefined):
  225. return Type::Undefined;
  226. case to_underlying(Type::SignedLong):
  227. return Type::SignedLong;
  228. case to_underlying(Type::SignedRational):
  229. return Type::SignedRational;
  230. case to_underlying(Type::UTF8):
  231. return Type::UTF8;
  232. default:
  233. return Error::from_string_literal("TIFFImageDecoderPlugin: Unknown type");
  234. }
  235. }
  236. static constexpr u8 size_of_type(Type type)
  237. {
  238. switch (type) {
  239. case Type::Byte:
  240. return 1;
  241. case Type::ASCII:
  242. return 1;
  243. case Type::UnsignedShort:
  244. return 2;
  245. case Type::UnsignedLong:
  246. return 4;
  247. case Type::UnsignedRational:
  248. return 8;
  249. case Type::Undefined:
  250. return 1;
  251. case Type::SignedLong:
  252. return 4;
  253. case Type::SignedRational:
  254. return 8;
  255. case Type::Float:
  256. return 4;
  257. case Type::Double:
  258. return 8;
  259. case Type::UTF8:
  260. return 1;
  261. default:
  262. VERIFY_NOT_REACHED();
  263. }
  264. }
  265. ErrorOr<Vector<Value, 1>> read_tiff_value(Type type, u32 count, u32 offset)
  266. {
  267. auto const old_offset = TRY(m_stream->tell());
  268. ScopeGuard reset_offset { [this, old_offset]() { MUST(m_stream->seek(old_offset)); } };
  269. TRY(m_stream->seek(offset));
  270. if (size_of_type(type) * count > m_stream->remaining())
  271. return Error::from_string_literal("TIFFImageDecoderPlugin: Tag size claims to be bigger that remaining bytes");
  272. auto const read_every_values = [this, count]<typename T>() -> ErrorOr<Vector<Value>> {
  273. Vector<Value, 1> result {};
  274. TRY(result.try_ensure_capacity(count));
  275. if constexpr (IsSpecializationOf<T, Rational>) {
  276. for (u32 i = 0; i < count; ++i)
  277. result.empend(T { TRY(read_value<typename T::Type>()), TRY(read_value<typename T::Type>()) });
  278. } else {
  279. for (u32 i = 0; i < count; ++i)
  280. result.empend(typename TypePromoter<T>::Type(TRY(read_value<T>())));
  281. }
  282. return result;
  283. };
  284. switch (type) {
  285. case Type::Byte:
  286. case Type::Undefined: {
  287. Vector<Value, 1> result;
  288. auto buffer = TRY(ByteBuffer::create_uninitialized(count));
  289. TRY(m_stream->read_until_filled(buffer));
  290. result.append(move(buffer));
  291. return result;
  292. }
  293. case Type::ASCII:
  294. case Type::UTF8: {
  295. Vector<Value, 1> result;
  296. auto string_data = TRY(ByteBuffer::create_uninitialized(count));
  297. TRY(m_stream->read_until_filled(string_data));
  298. result.empend(TRY(String::from_utf8(StringView { string_data.bytes() })));
  299. return result;
  300. }
  301. case Type::UnsignedShort:
  302. return read_every_values.template operator()<u16>();
  303. case Type::UnsignedLong:
  304. return read_every_values.template operator()<u32>();
  305. case Type::UnsignedRational:
  306. return read_every_values.template operator()<Rational<u32>>();
  307. case Type::SignedLong:
  308. return read_every_values.template operator()<i32>();
  309. ;
  310. case Type::SignedRational:
  311. return read_every_values.template operator()<Rational<i32>>();
  312. default:
  313. VERIFY_NOT_REACHED();
  314. }
  315. }
  316. ErrorOr<void> read_tag()
  317. {
  318. auto const tag = TRY(read_value<u16>());
  319. auto const type = TRY(read_type());
  320. auto const count = TRY(read_value<u32>());
  321. Checked<u32> checked_size = size_of_type(type);
  322. checked_size *= count;
  323. if (checked_size.has_overflow())
  324. return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid tag with too large data");
  325. auto tiff_value = TRY(([=, this]() -> ErrorOr<Vector<Value>> {
  326. if (checked_size.value() <= 4) {
  327. auto value = TRY(read_tiff_value(type, count, TRY(m_stream->tell())));
  328. TRY(m_stream->discard(4));
  329. return value;
  330. }
  331. auto const offset = TRY(read_value<u32>());
  332. return read_tiff_value(type, count, offset);
  333. }()));
  334. if constexpr (TIFF_DEBUG) {
  335. if (tiff_value.size() == 1) {
  336. tiff_value[0].visit(
  337. [&](ByteBuffer& value) {
  338. dbgln("Read tag({}), type({}): size {}", tag, to_underlying(type), value.size());
  339. },
  340. [&](auto const& value) {
  341. dbgln("Read tag({}), type({}): {}", tag, to_underlying(type), value);
  342. });
  343. } else {
  344. dbg("Read tag({}), type({}): [", tag, to_underlying(type));
  345. for (u32 i = 0; i < tiff_value.size(); ++i) {
  346. tiff_value[i].visit(
  347. [&](ByteBuffer&) {
  348. VERIFY_NOT_REACHED();
  349. },
  350. [&](auto const& value) {
  351. dbg("{}", value);
  352. });
  353. if (i != tiff_value.size() - 1)
  354. dbg(", ");
  355. }
  356. dbgln("]");
  357. }
  358. }
  359. TRY(handle_tag(m_metadata, tag, type, count, move(tiff_value)));
  360. return {};
  361. }
  362. NonnullOwnPtr<FixedMemoryStream> m_stream;
  363. State m_state {};
  364. RefPtr<Bitmap> m_bitmap {};
  365. ByteOrder m_byte_order {};
  366. Optional<u32> m_next_ifd {};
  367. Metadata m_metadata {};
  368. };
  369. }
  370. TIFFImageDecoderPlugin::TIFFImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  371. {
  372. m_context = make<TIFF::TIFFLoadingContext>(move(stream));
  373. }
  374. bool TIFFImageDecoderPlugin::sniff(ReadonlyBytes bytes)
  375. {
  376. if (bytes.size() < 4)
  377. return false;
  378. bool const valid_little_endian = bytes[0] == 0x49 && bytes[1] == 0x49 && bytes[2] == 0x2A && bytes[3] == 0x00;
  379. bool const valid_big_endian = bytes[0] == 0x4D && bytes[1] == 0x4D && bytes[2] == 0x00 && bytes[3] == 0x2A;
  380. return valid_little_endian || valid_big_endian;
  381. }
  382. IntSize TIFFImageDecoderPlugin::size()
  383. {
  384. return m_context->size();
  385. }
  386. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TIFFImageDecoderPlugin::create(ReadonlyBytes data)
  387. {
  388. auto stream = TRY(try_make<FixedMemoryStream>(data));
  389. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TIFFImageDecoderPlugin(move(stream))));
  390. TRY(plugin->m_context->decode_image_header());
  391. return plugin;
  392. }
  393. ErrorOr<ImageFrameDescriptor> TIFFImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  394. {
  395. if (index > 0)
  396. return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid frame index");
  397. if (m_context->state() == TIFF::TIFFLoadingContext::State::Error)
  398. return Error::from_string_literal("TIFFImageDecoderPlugin: Decoding failed");
  399. if (m_context->state() < TIFF::TIFFLoadingContext::State::FrameDecoded)
  400. TRY(m_context->decode_frame());
  401. return ImageFrameDescriptor { m_context->bitmap(), 0 };
  402. }
  403. }
  404. template<typename T>
  405. struct AK::Formatter<Gfx::TIFF::Rational<T>> : Formatter<FormatString> {
  406. ErrorOr<void> format(FormatBuilder& builder, Gfx::TIFF::Rational<T> value)
  407. {
  408. return Formatter<FormatString>::format(builder, "{} ({}/{})"sv, static_cast<double>(value.numerator) / value.denominator, value.numerator, value.denominator);
  409. }
  410. };