WebPLoader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. };
  58. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  59. static ErrorOr<void> decode_webp_header(WebPLoadingContext& context)
  60. {
  61. if (context.state >= WebPLoadingContext::HeaderDecoded)
  62. return {};
  63. if (context.data.size() < sizeof(WebPFileHeader)) {
  64. context.state = WebPLoadingContext::State::Error;
  65. return Error::from_string_literal("Missing WebP header");
  66. }
  67. auto& header = *bit_cast<WebPFileHeader const*>(context.data.data());
  68. if (header.riff != FourCC("RIFF") || header.webp != FourCC("WEBP")) {
  69. context.state = WebPLoadingContext::State::Error;
  70. return Error::from_string_literal("Invalid WebP header");
  71. }
  72. // "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."
  73. u32 const maximum_webp_file_size = 0xffff'ffff - 9;
  74. if (header.file_size > maximum_webp_file_size) {
  75. context.state = WebPLoadingContext::State::Error;
  76. return Error::from_string_literal("WebP header file size over maximum");
  77. }
  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. context.state = WebPLoadingContext::State::Error;
  83. return Error::from_string_literal("WebP data too small for size in header");
  84. }
  85. if (context.data.size() - 8 > header.file_size) {
  86. dbgln_if(WEBP_DEBUG, "WebP has {} bytes of data, but header needs only {}. Trimming.", context.data.size(), header.file_size + 8);
  87. context.data = context.data.trim(header.file_size + 8);
  88. }
  89. context.state = WebPLoadingContext::HeaderDecoded;
  90. return {};
  91. }
  92. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  93. static ErrorOr<Chunk> decode_webp_chunk_header(WebPLoadingContext& context, ReadonlyBytes chunks)
  94. {
  95. if (chunks.size() < sizeof(ChunkHeader)) {
  96. context.state = WebPLoadingContext::State::Error;
  97. return Error::from_string_literal("Not enough data for WebP chunk header");
  98. }
  99. auto const& header = *bit_cast<ChunkHeader const*>(chunks.data());
  100. dbgln_if(WEBP_DEBUG, "chunk {} size {}", header.chunk_type, header.chunk_size);
  101. if (chunks.size() < sizeof(ChunkHeader) + header.chunk_size) {
  102. context.state = WebPLoadingContext::State::Error;
  103. return Error::from_string_literal("Not enough data for WebP chunk");
  104. }
  105. return Chunk { header.chunk_type, { chunks.data() + sizeof(ChunkHeader), header.chunk_size } };
  106. }
  107. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  108. static ErrorOr<Chunk> decode_webp_advance_chunk(WebPLoadingContext& context, ReadonlyBytes& chunks)
  109. {
  110. auto chunk = TRY(decode_webp_chunk_header(context, chunks));
  111. // "Chunk Size: 32 bits (uint32)
  112. // The size of the chunk in bytes, not including this field, the chunk identifier or padding.
  113. // Chunk Payload: Chunk Size bytes
  114. // The data payload. If Chunk Size is odd, a single padding byte -- that MUST be 0 to conform with RIFF -- is added."
  115. chunks = chunks.slice(sizeof(ChunkHeader) + chunk.data.size());
  116. if (chunk.data.size() % 2 != 0) {
  117. if (chunks.is_empty()) {
  118. context.state = WebPLoadingContext::State::Error;
  119. return Error::from_string_literal("Missing data for padding byte");
  120. }
  121. if (*chunks.data() != 0) {
  122. context.state = WebPLoadingContext::State::Error;
  123. return Error::from_string_literal("Padding byte is not 0");
  124. }
  125. chunks = chunks.slice(1);
  126. }
  127. return chunk;
  128. }
  129. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossy
  130. static ErrorOr<void> decode_webp_simple_lossy(WebPLoadingContext& context, Chunk const& vp8_chunk)
  131. {
  132. // FIXME
  133. (void)context;
  134. (void)vp8_chunk;
  135. return {};
  136. }
  137. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
  138. static ErrorOr<void> decode_webp_simple_lossless(WebPLoadingContext& context, Chunk const& vp8l_chunk)
  139. {
  140. // FIXME
  141. (void)context;
  142. (void)vp8l_chunk;
  143. return {};
  144. }
  145. // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
  146. static ErrorOr<void> decode_webp_extended(WebPLoadingContext& context, Chunk const& vp8x_chunk, ReadonlyBytes chunks)
  147. {
  148. // FIXME: Do something with this.
  149. (void)vp8x_chunk;
  150. // FIXME: This isn't quite to spec, which says
  151. // "All chunks SHOULD be placed in the same order as listed above.
  152. // 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."
  153. while (!chunks.is_empty()) {
  154. auto chunk = TRY(decode_webp_advance_chunk(context, chunks));
  155. if (chunk.type == FourCC("ICCP"))
  156. context.icc_data = chunk.data;
  157. // FIXME: Probably want to make this and decode_webp_simple_lossy/lossless call the same function
  158. // instead of calling the _simple functions from the _extended function.
  159. if (chunk.type == FourCC("VP8 "))
  160. TRY(decode_webp_simple_lossy(context, chunk));
  161. if (chunk.type == FourCC("VP8X"))
  162. TRY(decode_webp_simple_lossless(context, chunk));
  163. }
  164. context.state = WebPLoadingContext::State::ChunksDecoded;
  165. return {};
  166. }
  167. static ErrorOr<void> decode_webp_chunks(WebPLoadingContext& context)
  168. {
  169. if (context.state >= WebPLoadingContext::State::ChunksDecoded)
  170. return {};
  171. if (context.state < WebPLoadingContext::HeaderDecoded)
  172. TRY(decode_webp_header(context));
  173. ReadonlyBytes chunks = context.data.slice(sizeof(WebPFileHeader));
  174. auto first_chunk = TRY(decode_webp_advance_chunk(context, chunks));
  175. if (first_chunk.type == FourCC("VP8 ")) {
  176. context.state = WebPLoadingContext::State::ChunksDecoded;
  177. return decode_webp_simple_lossy(context, first_chunk);
  178. }
  179. if (first_chunk.type == FourCC("VP8L")) {
  180. context.state = WebPLoadingContext::State::ChunksDecoded;
  181. return decode_webp_simple_lossless(context, first_chunk);
  182. }
  183. if (first_chunk.type == FourCC("VP8X"))
  184. return decode_webp_extended(context, first_chunk, chunks);
  185. return Error::from_string_literal("WebPImageDecoderPlugin: Invalid first chunk type");
  186. }
  187. WebPImageDecoderPlugin::WebPImageDecoderPlugin(ReadonlyBytes data, OwnPtr<WebPLoadingContext> context)
  188. : m_context(move(context))
  189. {
  190. m_context->data = data;
  191. }
  192. WebPImageDecoderPlugin::~WebPImageDecoderPlugin() = default;
  193. IntSize WebPImageDecoderPlugin::size()
  194. {
  195. if (m_context->state == WebPLoadingContext::State::Error)
  196. return {};
  197. if (m_context->state < WebPLoadingContext::State::SizeDecoded) {
  198. // FIXME
  199. }
  200. // FIXME
  201. return { 0, 0 };
  202. }
  203. void WebPImageDecoderPlugin::set_volatile()
  204. {
  205. if (m_context->bitmap)
  206. m_context->bitmap->set_volatile();
  207. }
  208. bool WebPImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  209. {
  210. if (!m_context->bitmap)
  211. return false;
  212. return m_context->bitmap->set_nonvolatile(was_purged);
  213. }
  214. bool WebPImageDecoderPlugin::initialize()
  215. {
  216. return !decode_webp_header(*m_context).is_error();
  217. }
  218. ErrorOr<bool> WebPImageDecoderPlugin::sniff(ReadonlyBytes data)
  219. {
  220. WebPLoadingContext context;
  221. context.data = data;
  222. TRY(decode_webp_header(context));
  223. return true;
  224. }
  225. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> WebPImageDecoderPlugin::create(ReadonlyBytes data)
  226. {
  227. auto context = TRY(try_make<WebPLoadingContext>());
  228. return adopt_nonnull_own_or_enomem(new (nothrow) WebPImageDecoderPlugin(data, move(context)));
  229. }
  230. bool WebPImageDecoderPlugin::is_animated()
  231. {
  232. // FIXME
  233. return false;
  234. }
  235. size_t WebPImageDecoderPlugin::loop_count()
  236. {
  237. // FIXME
  238. return 0;
  239. }
  240. size_t WebPImageDecoderPlugin::frame_count()
  241. {
  242. // FIXME
  243. return 1;
  244. }
  245. ErrorOr<ImageFrameDescriptor> WebPImageDecoderPlugin::frame(size_t index)
  246. {
  247. if (index >= frame_count())
  248. return Error::from_string_literal("WebPImageDecoderPlugin: Invalid frame index");
  249. return Error::from_string_literal("WebPImageDecoderPlugin: decoding not yet implemented");
  250. }
  251. ErrorOr<Optional<ReadonlyBytes>> WebPImageDecoderPlugin::icc_data()
  252. {
  253. TRY(decode_webp_chunks(*m_context));
  254. return m_context->icc_data;
  255. }
  256. }
  257. template<>
  258. struct AK::Formatter<Gfx::FourCC> : StandardFormatter {
  259. ErrorOr<void> format(FormatBuilder& builder, Gfx::FourCC const& four_cc)
  260. {
  261. TRY(builder.put_padding('\'', 1));
  262. TRY(builder.put_padding(four_cc.cc[0], 1));
  263. TRY(builder.put_padding(four_cc.cc[1], 1));
  264. TRY(builder.put_padding(four_cc.cc[2], 1));
  265. TRY(builder.put_padding(four_cc.cc[3], 1));
  266. TRY(builder.put_padding('\'', 1));
  267. return {};
  268. }
  269. };