WebPLoaderLossy.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <AK/MemoryStream.h>
  10. #include <AK/Vector.h>
  11. #include <LibGfx/ImageFormats/WebPLoaderLossy.h>
  12. // Lossy format: https://datatracker.ietf.org/doc/html/rfc6386
  13. namespace Gfx {
  14. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossy
  15. // https://datatracker.ietf.org/doc/html/rfc6386#section-19 "Annex A: Bitstream Syntax"
  16. ErrorOr<VP8Header> decode_webp_chunk_VP8_header(ReadonlyBytes vp8_data)
  17. {
  18. if (vp8_data.size() < 10)
  19. return Error::from_string_literal("WebPImageDecoderPlugin: 'VP8 ' chunk too small");
  20. // FIXME: Eventually, this should probably call into LibVideo/VP8,
  21. // and image decoders should move into LibImageDecoders which depends on both LibGfx and LibVideo.
  22. // (LibVideo depends on LibGfx, so LibGfx can't depend on LibVideo itself.)
  23. // https://datatracker.ietf.org/doc/html/rfc6386#section-4 "Overview of Compressed Data Format"
  24. // "The decoder is simply presented with a sequence of compressed frames [...]
  25. // The first frame presented to the decompressor is [...] a key frame. [...]
  26. // [E]very compressed frame has three or more pieces. It begins with an uncompressed data chunk comprising 10 bytes in the case of key frames"
  27. u8 const* data = vp8_data.data();
  28. // https://datatracker.ietf.org/doc/html/rfc6386#section-9.1 "Uncompressed Data Chunk"
  29. u32 frame_tag = data[0] | (data[1] << 8) | (data[2] << 16);
  30. bool is_key_frame = (frame_tag & 1) == 0; // https://www.rfc-editor.org/errata/eid5534
  31. u8 version = (frame_tag & 0xe) >> 1;
  32. bool show_frame = (frame_tag & 0x10) != 0;
  33. u32 size_of_first_partition = frame_tag >> 5;
  34. if (!is_key_frame)
  35. return Error::from_string_literal("WebPImageDecoderPlugin: 'VP8 ' chunk not a key frame");
  36. // FIXME: !show_frame does not make sense in a webp file either, probably?
  37. u32 start_code = data[3] | (data[4] << 8) | (data[5] << 16);
  38. if (start_code != 0x2a019d) // https://www.rfc-editor.org/errata/eid7370
  39. return Error::from_string_literal("WebPImageDecoderPlugin: 'VP8 ' chunk invalid start_code");
  40. // "The scaling specifications for each dimension are encoded as follows.
  41. // 0 | No upscaling (the most common case).
  42. // 1 | Upscale by 5/4.
  43. // 2 | Upscale by 5/3.
  44. // 3 | Upscale by 2."
  45. // This is a display-time operation and doesn't affect decoding."
  46. u16 width_and_horizontal_scale = data[6] | (data[7] << 8);
  47. u16 width = width_and_horizontal_scale & 0x3fff;
  48. u8 horizontal_scale = width_and_horizontal_scale >> 14;
  49. u16 heigth_and_vertical_scale = data[8] | (data[9] << 8);
  50. u16 height = heigth_and_vertical_scale & 0x3fff;
  51. u8 vertical_scale = heigth_and_vertical_scale >> 14;
  52. dbgln_if(WEBP_DEBUG, "version {}, show_frame {}, size_of_first_partition {}, width {}, horizontal_scale {}, height {}, vertical_scale {}",
  53. version, show_frame, size_of_first_partition, width, horizontal_scale, height, vertical_scale);
  54. return VP8Header { version, show_frame, size_of_first_partition, width, horizontal_scale, height, vertical_scale, vp8_data.slice(10) };
  55. }
  56. ErrorOr<NonnullRefPtr<Bitmap>> decode_webp_chunk_VP8_contents(VP8Header const& vp8_header, bool include_alpha_channel)
  57. {
  58. auto bitmap_format = include_alpha_channel ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
  59. // Uncomment this to test ALPH decoding for WebP-lossy-with-alpha images while lossy decoding isn't implemented yet.
  60. #if 0
  61. return Bitmap::create(bitmap_format, { vp8_header.width, vp8_header.height });
  62. #else
  63. // FIXME: Implement webp lossy decoding.
  64. (void)vp8_header;
  65. (void)bitmap_format;
  66. return Error::from_string_literal("WebPImageDecoderPlugin: decoding lossy webps not yet implemented");
  67. #endif
  68. }
  69. }