TIFFLoader.cpp 22 KB

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