WebPWriterLossless.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // Lossless format: https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
  7. #include <AK/BitStream.h>
  8. #include <AK/Debug.h>
  9. #include <AK/Endian.h>
  10. #include <AK/MemoryStream.h>
  11. #include <LibCompress/DeflateTables.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/ImageFormats/WebPSharedLossless.h>
  14. #include <LibGfx/ImageFormats/WebPWriterLossless.h>
  15. namespace Gfx {
  16. static bool are_all_pixels_opaque(Bitmap const& bitmap)
  17. {
  18. for (ARGB32 pixel : bitmap) {
  19. if ((pixel >> 24) != 0xff)
  20. return false;
  21. }
  22. return true;
  23. }
  24. NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, PrefixCodeGroup const& prefix_code_group)
  25. {
  26. // This is currently the hot loop. Keep performance in mind when you change it.
  27. for (ARGB32 pixel : bitmap) {
  28. u8 a = pixel >> 24;
  29. u8 r = pixel >> 16;
  30. u8 g = pixel >> 8;
  31. u8 b = pixel;
  32. TRY(prefix_code_group[0].write_symbol(bit_stream, g));
  33. TRY(prefix_code_group[1].write_symbol(bit_stream, r));
  34. TRY(prefix_code_group[2].write_symbol(bit_stream, b));
  35. TRY(prefix_code_group[3].write_symbol(bit_stream, a));
  36. }
  37. return {};
  38. }
  39. static ErrorOr<void> write_VP8L_image_data(Stream& stream, Bitmap const& bitmap)
  40. {
  41. LittleEndianOutputBitStream bit_stream { MaybeOwned<Stream>(stream) };
  42. // optional-transform = (%b1 transform optional-transform) / %b0
  43. TRY(bit_stream.write_bits(0u, 1u)); // No transform for now.
  44. // https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#5_image_data
  45. // spatially-coded-image = color-cache-info meta-prefix data
  46. // color-cache-info = %b0
  47. // color-cache-info =/ (%b1 4BIT) ; 1 followed by color cache size
  48. TRY(bit_stream.write_bits(0u, 1u)); // No color cache for now.
  49. // meta-prefix = %b0 / (%b1 entropy-image)
  50. TRY(bit_stream.write_bits(0u, 1u)); // No meta prefix for now.
  51. // data = prefix-codes lz77-coded-image
  52. // prefix-codes = prefix-code-group *prefix-codes
  53. // prefix-code-group =
  54. // 5prefix-code ; See "Interpretation of Meta Prefix Codes" to
  55. // ; understand what each of these five prefix
  56. // ; codes are for.
  57. // We're writing a single prefix-code-group.
  58. // "These codes are (in bitstream order):
  59. // Prefix code #1: Used for green channel, backward-reference length, and color cache.
  60. // Prefix code #2, #3, and #4: Used for red, blue, and alpha channels, respectively.
  61. // Prefix code #5: Used for backward-reference distance."
  62. // We use neither back-references not color cache entries yet.
  63. // We write prefix trees for 256 literals all of length 8, which means each byte is encoded as itself.
  64. // That doesn't give any compression, but is a valid bit stream.
  65. // We can make this smarter later on.
  66. size_t const color_cache_size = 0;
  67. constexpr Array alphabet_sizes = to_array<size_t>({ 256 + 24 + static_cast<size_t>(color_cache_size), 256, 256, 256, 40 }); // XXX Shared?
  68. // If you add support for color cache: At the moment, CanonicalCodes does not support writing more than 288 symbols.
  69. if (alphabet_sizes[0] > 288)
  70. return Error::from_string_literal("Invalid alphabet size");
  71. bool all_pixels_are_opaque = are_all_pixels_opaque(bitmap);
  72. PrefixCodeGroup prefix_code_group;
  73. int number_of_full_channels = all_pixels_are_opaque ? 3 : 4;
  74. for (int i = 0; i < number_of_full_channels; ++i) {
  75. TRY(bit_stream.write_bits(0u, 1u)); // Normal code length code.
  76. // Write code length codes.
  77. constexpr int kCodeLengthCodes = 19;
  78. Array<int, kCodeLengthCodes> kCodeLengthCodeOrder = { 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  79. int num_code_lengths = max(4u, find_index(kCodeLengthCodeOrder.begin(), kCodeLengthCodeOrder.end(), 8) + 1);
  80. // "int num_code_lengths = 4 + ReadBits(4);"
  81. TRY(bit_stream.write_bits(num_code_lengths - 4u, 4u));
  82. for (int i = 0; i < num_code_lengths - 1; ++i)
  83. TRY(bit_stream.write_bits(0u, 3u));
  84. TRY(bit_stream.write_bits(1u, 3u));
  85. // Write code lengths.
  86. if (alphabet_sizes[i] == 256) {
  87. TRY(bit_stream.write_bits(0u, 1u)); // max_symbol is alphabet_size
  88. } else {
  89. TRY(bit_stream.write_bits(1u, 1u)); // max_symbol is explicitly coded
  90. // "int length_nbits = 2 + 2 * ReadBits(3);
  91. // int max_symbol = 2 + ReadBits(length_nbits);"
  92. TRY(bit_stream.write_bits(3u, 3u)); // length_nbits = 2 + 2 * 3
  93. TRY(bit_stream.write_bits(254u, 8u)); // max_symbol = 2 + 254
  94. }
  95. auto bits_per_symbol = Array<u8, 256>::from_repeated_value(8);
  96. prefix_code_group[i] = TRY(CanonicalCode::from_bytes(bits_per_symbol));
  97. // The code length codes only contain a single entry for '8'. WebP streams with a single element store 0 bits per element.
  98. // (This is different from deflate, which needs 1 bit per element.)
  99. }
  100. if (all_pixels_are_opaque) {
  101. // Use a simple 1-element code.
  102. TRY(bit_stream.write_bits(1u, 1u)); // Simple code length code.
  103. TRY(bit_stream.write_bits(0u, 1u)); // num_symbols - 1
  104. TRY(bit_stream.write_bits(1u, 1u)); // is_first_8bits
  105. TRY(bit_stream.write_bits(255u, 8u)); // symbol0
  106. Array<u8, 256> bits_per_symbol {};
  107. // "When coding a single leaf node [...], all but one code length are zeros, and the single leaf node value
  108. // is marked with the length of 1 -- even when no bits are consumed when that single leaf node tree is used."
  109. // CanonicalCode follows that convention too, even when describing simple code lengths.
  110. bits_per_symbol[255] = 1;
  111. prefix_code_group[3] = TRY(CanonicalCode::from_bytes(bits_per_symbol));
  112. }
  113. // For code #5, use a simple empty code, since we don't use this yet.
  114. // "Note: Another special case is when all prefix code lengths are zeros (an empty prefix code). [...]
  115. // empty prefix codes can be coded as those containing a single symbol 0."
  116. TRY(bit_stream.write_bits(1u, 1u)); // Simple code length code.
  117. TRY(bit_stream.write_bits(0u, 1u)); // num_symbols - 1
  118. TRY(bit_stream.write_bits(0u, 1u)); // is_first_8bits
  119. TRY(bit_stream.write_bits(0u, 1u)); // symbol0
  120. Array<u8, 256> bits_per_symbol {};
  121. bits_per_symbol[0] = 1; // See comment in `if (all_pixels_are_opaque)` block above.
  122. prefix_code_group[4] = TRY(CanonicalCode::from_bytes(bits_per_symbol));
  123. // Image data.
  124. TRY(write_image_data(bit_stream, bitmap, prefix_code_group));
  125. // FIXME: Make ~LittleEndianOutputBitStream do this, or make it VERIFY() that it has happened at least.
  126. TRY(bit_stream.align_to_byte_boundary());
  127. TRY(bit_stream.flush_buffer_to_stream());
  128. return {};
  129. }
  130. ErrorOr<ByteBuffer> compress_VP8L_image_data(Bitmap const& bitmap)
  131. {
  132. AllocatingMemoryStream vp8l_data_stream;
  133. TRY(write_VP8L_image_data(vp8l_data_stream, bitmap));
  134. return vp8l_data_stream.read_until_eof();
  135. }
  136. }