WebPWriterLossless.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/WebPWriterLossless.h>
  14. namespace Gfx {
  15. static bool are_all_pixels_opaque(Bitmap const& bitmap)
  16. {
  17. for (ARGB32 pixel : bitmap) {
  18. if ((pixel >> 24) != 0xff)
  19. return false;
  20. }
  21. return true;
  22. }
  23. NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, bool all_pixels_are_opaque)
  24. {
  25. // This is currently the hot loop. Keep performance in mind when you change it.
  26. for (ARGB32 pixel : bitmap) {
  27. u8 a = pixel >> 24;
  28. u8 r = pixel >> 16;
  29. u8 g = pixel >> 8;
  30. u8 b = pixel;
  31. // We wrote a huffman table that gives every symbol 8 bits. That means we can write the image data
  32. // out uncompressed –- but we do need to reverse the bit order of the bytes.
  33. TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[g], 8u));
  34. TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[r], 8u));
  35. TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[b], 8u));
  36. // If all pixels are opaque, we wrote a one-element huffman table for alpha, which needs 0 bits per element.
  37. if (!all_pixels_are_opaque)
  38. TRY(bit_stream.write_bits(Compress::reverse8_lookup_table[a], 8u));
  39. }
  40. return {};
  41. }
  42. static ErrorOr<void> write_VP8L_image_data(Stream& stream, Bitmap const& bitmap)
  43. {
  44. LittleEndianOutputBitStream bit_stream { MaybeOwned<Stream>(stream) };
  45. // optional-transform = (%b1 transform optional-transform) / %b0
  46. TRY(bit_stream.write_bits(0u, 1u)); // No transform for now.
  47. // https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#5_image_data
  48. // spatially-coded-image = color-cache-info meta-prefix data
  49. // color-cache-info = %b0
  50. // color-cache-info =/ (%b1 4BIT) ; 1 followed by color cache size
  51. TRY(bit_stream.write_bits(0u, 1u)); // No color cache for now.
  52. // meta-prefix = %b0 / (%b1 entropy-image)
  53. TRY(bit_stream.write_bits(0u, 1u)); // No meta prefix for now.
  54. // data = prefix-codes lz77-coded-image
  55. // prefix-codes = prefix-code-group *prefix-codes
  56. // prefix-code-group =
  57. // 5prefix-code ; See "Interpretation of Meta Prefix Codes" to
  58. // ; understand what each of these five prefix
  59. // ; codes are for.
  60. // We're writing a single prefix-code-group.
  61. // "These codes are (in bitstream order):
  62. // Prefix code #1: Used for green channel, backward-reference length, and color cache.
  63. // Prefix code #2, #3, and #4: Used for red, blue, and alpha channels, respectively.
  64. // Prefix code #5: Used for backward-reference distance."
  65. // We use neither back-references not color cache entries yet.
  66. // We write prefix trees for 256 literals all of length 8, which means each byte is encoded as itself.
  67. // That doesn't give any compression, but is a valid bit stream.
  68. // We can make this smarter later on.
  69. size_t const color_cache_size = 0;
  70. constexpr Array alphabet_sizes = to_array<size_t>({ 256 + 24 + static_cast<size_t>(color_cache_size), 256, 256, 256, 40 }); // XXX Shared?
  71. // If you add support for color cache: At the moment, CanonicalCodes does not support writing more than 288 symbols.
  72. if (alphabet_sizes[0] > 288)
  73. return Error::from_string_literal("Invalid alphabet size");
  74. bool all_pixels_are_opaque = are_all_pixels_opaque(bitmap);
  75. int number_of_full_channels = all_pixels_are_opaque ? 3 : 4;
  76. for (int i = 0; i < number_of_full_channels; ++i) {
  77. TRY(bit_stream.write_bits(0u, 1u)); // Normal code length code.
  78. // Write code length codes.
  79. constexpr int kCodeLengthCodes = 19;
  80. Array<int, kCodeLengthCodes> kCodeLengthCodeOrder = { 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  81. int num_code_lengths = max(4u, find_index(kCodeLengthCodeOrder.begin(), kCodeLengthCodeOrder.end(), 8) + 1);
  82. // "int num_code_lengths = 4 + ReadBits(4);"
  83. TRY(bit_stream.write_bits(num_code_lengths - 4u, 4u));
  84. for (int i = 0; i < num_code_lengths - 1; ++i)
  85. TRY(bit_stream.write_bits(0u, 3u));
  86. TRY(bit_stream.write_bits(1u, 3u));
  87. // Write code lengths.
  88. if (alphabet_sizes[i] == 256) {
  89. TRY(bit_stream.write_bits(0u, 1u)); // max_symbol is alphabet_size
  90. } else {
  91. TRY(bit_stream.write_bits(1u, 1u)); // max_symbol is explicitly coded
  92. // "int length_nbits = 2 + 2 * ReadBits(3);
  93. // int max_symbol = 2 + ReadBits(length_nbits);"
  94. TRY(bit_stream.write_bits(3u, 3u)); // length_nbits = 2 + 2 * 3
  95. TRY(bit_stream.write_bits(254u, 8u)); // max_symbol = 2 + 254
  96. }
  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. }
  107. // For code #5, use a simple empty code, since we don't use this yet.
  108. TRY(bit_stream.write_bits(1u, 1u)); // Simple code length code.
  109. TRY(bit_stream.write_bits(0u, 1u)); // num_symbols - 1
  110. TRY(bit_stream.write_bits(0u, 1u)); // is_first_8bits
  111. TRY(bit_stream.write_bits(0u, 1u)); // symbol0
  112. // Image data.
  113. TRY(write_image_data(bit_stream, bitmap, all_pixels_are_opaque));
  114. // FIXME: Make ~LittleEndianOutputBitStream do this, or make it VERIFY() that it has happened at least.
  115. TRY(bit_stream.align_to_byte_boundary());
  116. TRY(bit_stream.flush_buffer_to_stream());
  117. return {};
  118. }
  119. ErrorOr<ByteBuffer> compress_VP8L_image_data(Bitmap const& bitmap)
  120. {
  121. AllocatingMemoryStream vp8l_data_stream;
  122. TRY(write_VP8L_image_data(vp8l_data_stream, bitmap));
  123. return vp8l_data_stream.read_until_eof();
  124. }
  125. }