TIFFLoader.cpp 21 KB

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