TIFFLoader.cpp 18 KB

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