WebPLoader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Endian.h>
  8. #include <AK/Format.h>
  9. #include <LibGfx/WebPLoader.h>
  10. // Container: https://developers.google.com/speed/webp/docs/riff_container
  11. // Lossless format: https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
  12. // Lossy format: https://datatracker.ietf.org/doc/html/rfc6386
  13. namespace Gfx {
  14. namespace {
  15. struct FourCC {
  16. constexpr FourCC(char const* name)
  17. {
  18. cc[0] = name[0];
  19. cc[1] = name[1];
  20. cc[2] = name[2];
  21. cc[3] = name[3];
  22. }
  23. bool operator==(FourCC const&) const = default;
  24. bool operator!=(FourCC const&) const = default;
  25. char cc[4];
  26. };
  27. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  28. struct WebPFileHeader {
  29. FourCC riff;
  30. LittleEndian<u32> file_size;
  31. FourCC webp;
  32. };
  33. static_assert(AssertSize<WebPFileHeader, 12>());
  34. struct ChunkHeader {
  35. FourCC chunk_type;
  36. LittleEndian<u32> chunk_size;
  37. };
  38. static_assert(AssertSize<ChunkHeader, 8>());
  39. struct Chunk {
  40. FourCC type;
  41. ReadonlyBytes data;
  42. };
  43. }
  44. struct WebPLoadingContext {
  45. enum State {
  46. NotDecoded = 0,
  47. Error,
  48. HeaderDecoded,
  49. SizeDecoded,
  50. ChunksDecoded,
  51. BitmapDecoded,
  52. };
  53. State state { State::NotDecoded };
  54. ReadonlyBytes data;
  55. RefPtr<Gfx::Bitmap> bitmap;
  56. Optional<ReadonlyBytes> icc_data;
  57. template<size_t N>
  58. [[nodiscard]] class Error error(char const (&string_literal)[N])
  59. {
  60. state = WebPLoadingContext::State::Error;
  61. return Error::from_string_literal(string_literal);
  62. }
  63. };
  64. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  65. static ErrorOr<void> decode_webp_header(WebPLoadingContext& context)
  66. {
  67. if (context.state >= WebPLoadingContext::HeaderDecoded)
  68. return {};
  69. if (context.data.size() < sizeof(WebPFileHeader))
  70. return context.error("Missing WebP header");
  71. auto& header = *bit_cast<WebPFileHeader const*>(context.data.data());
  72. if (header.riff != FourCC("RIFF") || header.webp != FourCC("WEBP"))
  73. return context.error("Invalid WebP header");
  74. // "File Size: [...] The size of the file in bytes starting at offset 8. The maximum value of this field is 2^32 minus 10 bytes."
  75. u32 const maximum_webp_file_size = 0xffff'ffff - 9;
  76. if (header.file_size > maximum_webp_file_size)
  77. return context.error("WebP header file size over maximum");
  78. // "The file size in the header is the total size of the chunks that follow plus 4 bytes for the 'WEBP' FourCC.
  79. // The file SHOULD NOT contain any data after the data specified by File Size.
  80. // Readers MAY parse such files, ignoring the trailing data."
  81. if (context.data.size() - 8 < header.file_size)
  82. return context.error("WebP data too small for size in header");
  83. if (context.data.size() - 8 > header.file_size) {
  84. dbgln_if(WEBP_DEBUG, "WebP has {} bytes of data, but header needs only {}. Trimming.", context.data.size(), header.file_size + 8);
  85. context.data = context.data.trim(header.file_size + 8);
  86. }
  87. context.state = WebPLoadingContext::HeaderDecoded;
  88. return {};
  89. }
  90. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  91. static ErrorOr<Chunk> decode_webp_chunk_header(WebPLoadingContext& context, ReadonlyBytes chunks)
  92. {
  93. if (chunks.size() < sizeof(ChunkHeader))
  94. return context.error("Not enough data for WebP chunk header");
  95. auto const& header = *bit_cast<ChunkHeader const*>(chunks.data());
  96. dbgln_if(WEBP_DEBUG, "chunk {} size {}", header.chunk_type, header.chunk_size);
  97. if (chunks.size() < sizeof(ChunkHeader) + header.chunk_size)
  98. return context.error("Not enough data for WebP chunk");
  99. return Chunk { header.chunk_type, { chunks.data() + sizeof(ChunkHeader), header.chunk_size } };
  100. }
  101. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  102. static ErrorOr<Chunk> decode_webp_advance_chunk(WebPLoadingContext& context, ReadonlyBytes& chunks)
  103. {
  104. auto chunk = TRY(decode_webp_chunk_header(context, chunks));
  105. // "Chunk Size: 32 bits (uint32)
  106. // The size of the chunk in bytes, not including this field, the chunk identifier or padding.
  107. // Chunk Payload: Chunk Size bytes
  108. // The data payload. If Chunk Size is odd, a single padding byte -- that MUST be 0 to conform with RIFF -- is added."
  109. chunks = chunks.slice(sizeof(ChunkHeader) + chunk.data.size());
  110. if (chunk.data.size() % 2 != 0) {
  111. if (chunks.is_empty())
  112. return context.error("Missing data for padding byte");
  113. if (*chunks.data() != 0)
  114. return context.error("Padding byte is not 0");
  115. chunks = chunks.slice(1);
  116. }
  117. return chunk;
  118. }
  119. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossy
  120. static ErrorOr<void> decode_webp_simple_lossy(WebPLoadingContext& context, Chunk const& vp8_chunk)
  121. {
  122. // FIXME
  123. (void)context;
  124. (void)vp8_chunk;
  125. return {};
  126. }
  127. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
  128. static ErrorOr<void> decode_webp_simple_lossless(WebPLoadingContext& context, Chunk const& vp8l_chunk)
  129. {
  130. // FIXME
  131. (void)context;
  132. (void)vp8l_chunk;
  133. return {};
  134. }
  135. static ErrorOr<void> decode_webp_chunk_VP8X(WebPLoadingContext& context, Chunk const& vp8x_chunk)
  136. {
  137. VERIFY(vp8x_chunk.type == FourCC("VP8X"));
  138. // The VP8X chunk is documented at "Extended WebP file header:" at the end of
  139. // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
  140. if (vp8x_chunk.data.size() < 10)
  141. return context.error("WebPImageDecoderPlugin: VP8X chunk too small");
  142. u8 const* data = vp8x_chunk.data.data();
  143. // 1 byte flags
  144. // "Reserved (Rsv): 2 bits MUST be 0. Readers MUST ignore this field.
  145. // ICC profile (I): 1 bit Set if the file contains an ICC profile.
  146. // Alpha (L): 1 bit Set if any of the frames of the image contain transparency information ("alpha").
  147. // Exif metadata (E): 1 bit Set if the file contains Exif metadata.
  148. // XMP metadata (X): 1 bit Set if the file contains XMP metadata.
  149. // Animation (A): 1 bit Set if this is an animated image. Data in 'ANIM' and 'ANMF' chunks should be used to control the animation.
  150. // Reserved (R): 1 bit MUST be 0. Readers MUST ignore this field."
  151. u8 flags = data[0];
  152. bool has_icc = flags & 0x20;
  153. bool has_alpha = flags & 0x10;
  154. bool has_exif = flags & 0x8;
  155. bool has_xmp = flags & 0x4;
  156. bool has_animation = flags & 0x2;
  157. // 3 byte reserved
  158. // 3 byte width minus one
  159. u32 width = (data[4] | (data[5] << 8) | (data[6] << 16)) + 1;
  160. // 3 byte height minus one
  161. u32 height = (data[7] | (data[8] << 8) | (data[9] << 16)) + 1;
  162. dbgln_if(WEBP_DEBUG, "flags 0x{:x} --{}{}{}{}{}{}, width {}, height {}",
  163. flags,
  164. has_icc ? " icc" : "",
  165. has_alpha ? " alpha" : "",
  166. has_exif ? " exif" : "",
  167. has_xmp ? " xmp" : "",
  168. has_animation ? " anim" : "",
  169. (flags & 0x3e) == 0 ? " none" : "",
  170. width, height);
  171. return {};
  172. }
  173. // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
  174. static ErrorOr<void> decode_webp_extended(WebPLoadingContext& context, Chunk const& vp8x_chunk, ReadonlyBytes chunks)
  175. {
  176. TRY(decode_webp_chunk_VP8X(context, vp8x_chunk));
  177. // FIXME: This isn't quite to spec, which says
  178. // "All chunks SHOULD be placed in the same order as listed above.
  179. // If a chunk appears in the wrong place, the file is invalid, but readers MAY parse the file, ignoring the chunks that are out of order."
  180. while (!chunks.is_empty()) {
  181. auto chunk = TRY(decode_webp_advance_chunk(context, chunks));
  182. if (chunk.type == FourCC("ICCP"))
  183. context.icc_data = chunk.data;
  184. // FIXME: Probably want to make this and decode_webp_simple_lossy/lossless call the same function
  185. // instead of calling the _simple functions from the _extended function.
  186. if (chunk.type == FourCC("VP8 "))
  187. TRY(decode_webp_simple_lossy(context, chunk));
  188. if (chunk.type == FourCC("VP8X"))
  189. TRY(decode_webp_simple_lossless(context, chunk));
  190. }
  191. context.state = WebPLoadingContext::State::ChunksDecoded;
  192. return {};
  193. }
  194. static ErrorOr<void> decode_webp_chunks(WebPLoadingContext& context)
  195. {
  196. if (context.state >= WebPLoadingContext::State::ChunksDecoded)
  197. return {};
  198. if (context.state < WebPLoadingContext::HeaderDecoded)
  199. TRY(decode_webp_header(context));
  200. ReadonlyBytes chunks = context.data.slice(sizeof(WebPFileHeader));
  201. auto first_chunk = TRY(decode_webp_advance_chunk(context, chunks));
  202. if (first_chunk.type == FourCC("VP8 ")) {
  203. context.state = WebPLoadingContext::State::ChunksDecoded;
  204. return decode_webp_simple_lossy(context, first_chunk);
  205. }
  206. if (first_chunk.type == FourCC("VP8L")) {
  207. context.state = WebPLoadingContext::State::ChunksDecoded;
  208. return decode_webp_simple_lossless(context, first_chunk);
  209. }
  210. if (first_chunk.type == FourCC("VP8X"))
  211. return decode_webp_extended(context, first_chunk, chunks);
  212. return context.error("WebPImageDecoderPlugin: Invalid first chunk type");
  213. }
  214. WebPImageDecoderPlugin::WebPImageDecoderPlugin(ReadonlyBytes data, OwnPtr<WebPLoadingContext> context)
  215. : m_context(move(context))
  216. {
  217. m_context->data = data;
  218. }
  219. WebPImageDecoderPlugin::~WebPImageDecoderPlugin() = default;
  220. IntSize WebPImageDecoderPlugin::size()
  221. {
  222. if (m_context->state == WebPLoadingContext::State::Error)
  223. return {};
  224. if (m_context->state < WebPLoadingContext::State::SizeDecoded) {
  225. // FIXME
  226. }
  227. // FIXME
  228. return { 0, 0 };
  229. }
  230. void WebPImageDecoderPlugin::set_volatile()
  231. {
  232. if (m_context->bitmap)
  233. m_context->bitmap->set_volatile();
  234. }
  235. bool WebPImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  236. {
  237. if (!m_context->bitmap)
  238. return false;
  239. return m_context->bitmap->set_nonvolatile(was_purged);
  240. }
  241. bool WebPImageDecoderPlugin::initialize()
  242. {
  243. return !decode_webp_header(*m_context).is_error();
  244. }
  245. ErrorOr<bool> WebPImageDecoderPlugin::sniff(ReadonlyBytes data)
  246. {
  247. WebPLoadingContext context;
  248. context.data = data;
  249. TRY(decode_webp_header(context));
  250. return true;
  251. }
  252. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> WebPImageDecoderPlugin::create(ReadonlyBytes data)
  253. {
  254. auto context = TRY(try_make<WebPLoadingContext>());
  255. return adopt_nonnull_own_or_enomem(new (nothrow) WebPImageDecoderPlugin(data, move(context)));
  256. }
  257. bool WebPImageDecoderPlugin::is_animated()
  258. {
  259. // FIXME
  260. return false;
  261. }
  262. size_t WebPImageDecoderPlugin::loop_count()
  263. {
  264. // FIXME
  265. return 0;
  266. }
  267. size_t WebPImageDecoderPlugin::frame_count()
  268. {
  269. // FIXME
  270. return 1;
  271. }
  272. ErrorOr<ImageFrameDescriptor> WebPImageDecoderPlugin::frame(size_t index)
  273. {
  274. if (index >= frame_count())
  275. return Error::from_string_literal("WebPImageDecoderPlugin: Invalid frame index");
  276. return Error::from_string_literal("WebPImageDecoderPlugin: decoding not yet implemented");
  277. }
  278. ErrorOr<Optional<ReadonlyBytes>> WebPImageDecoderPlugin::icc_data()
  279. {
  280. TRY(decode_webp_chunks(*m_context));
  281. // FIXME: "If this chunk is not present, sRGB SHOULD be assumed."
  282. return m_context->icc_data;
  283. }
  284. }
  285. template<>
  286. struct AK::Formatter<Gfx::FourCC> : StandardFormatter {
  287. ErrorOr<void> format(FormatBuilder& builder, Gfx::FourCC const& four_cc)
  288. {
  289. TRY(builder.put_padding('\'', 1));
  290. TRY(builder.put_padding(four_cc.cc[0], 1));
  291. TRY(builder.put_padding(four_cc.cc[1], 1));
  292. TRY(builder.put_padding(four_cc.cc[2], 1));
  293. TRY(builder.put_padding(four_cc.cc[3], 1));
  294. TRY(builder.put_padding('\'', 1));
  295. return {};
  296. }
  297. };