TIFFLoader.cpp 17 KB

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