TIFFLoader.cpp 20 KB

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