TIFFLoader.cpp 21 KB

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