WebPWriterLossless.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/HashTable.h>
  11. #include <AK/MemoryStream.h>
  12. #include <AK/QuickSort.h>
  13. #include <LibCompress/DeflateTables.h>
  14. #include <LibCompress/Huffman.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/ImageFormats/WebPSharedLossless.h>
  17. #include <LibGfx/ImageFormats/WebPWriterLossless.h>
  18. namespace Gfx {
  19. namespace {
  20. struct IsOpaque {
  21. bool is_fully_opaque { false };
  22. bool is_opacity_known { false };
  23. void set_is_fully_opaque_if_not_yet_known(bool is_fully_opaque)
  24. {
  25. if (is_opacity_known)
  26. return;
  27. this->is_fully_opaque = is_fully_opaque;
  28. is_opacity_known = true;
  29. }
  30. };
  31. }
  32. NEVER_INLINE static ErrorOr<void> write_image_data(LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, PrefixCodeGroup const& prefix_code_group)
  33. {
  34. // This is currently the hot loop. Keep performance in mind when you change it.
  35. for (ARGB32 pixel : bitmap) {
  36. u8 a = pixel >> 24;
  37. u8 r = pixel >> 16;
  38. u8 g = pixel >> 8;
  39. u8 b = pixel;
  40. TRY(prefix_code_group[0].write_symbol(bit_stream, g));
  41. TRY(prefix_code_group[1].write_symbol(bit_stream, r));
  42. TRY(prefix_code_group[2].write_symbol(bit_stream, b));
  43. TRY(prefix_code_group[3].write_symbol(bit_stream, a));
  44. }
  45. return {};
  46. }
  47. struct CodeLengthSymbol {
  48. u8 symbol { 0 };
  49. u8 count { 0 }; // used for special symbols 16-18
  50. };
  51. // This is very similar to DeflateCompressor::encode_huffman_lengths().
  52. // But:
  53. // * size can be larger than 288 for green, is always 256 for r, b, a, and is always 40 for distance codes
  54. // * code 16 has different semantics, requires last_non_zero_symbol
  55. static size_t encode_huffman_lengths(ReadonlyBytes lengths, Array<CodeLengthSymbol, 256>& encoded_lengths)
  56. {
  57. size_t encoded_count = 0;
  58. size_t i = 0;
  59. u8 last_non_zero_symbol = 8; // "If code 16 is used before a non-zero value has been emitted, a value of 8 is repeated."
  60. while (i < lengths.size()) {
  61. if (lengths[i] == 0) {
  62. auto zero_count = 0;
  63. for (size_t j = i; j < min(lengths.size(), i + 138) && lengths[j] == 0; j++)
  64. zero_count++;
  65. if (zero_count < 3) { // below minimum repeated zero count
  66. encoded_lengths[encoded_count++].symbol = 0;
  67. i++;
  68. continue;
  69. }
  70. if (zero_count <= 10) {
  71. // "Code 17 emits a streak of zeros [3..10], i.e., 3 + ReadBits(3) times."
  72. encoded_lengths[encoded_count].symbol = 17;
  73. encoded_lengths[encoded_count++].count = zero_count;
  74. } else {
  75. // "Code 18 emits a streak of zeros of length [11..138], i.e., 11 + ReadBits(7) times."
  76. encoded_lengths[encoded_count].symbol = 18;
  77. encoded_lengths[encoded_count++].count = zero_count;
  78. }
  79. i += zero_count;
  80. continue;
  81. }
  82. VERIFY(lengths[i] != 0);
  83. last_non_zero_symbol = lengths[i];
  84. encoded_lengths[encoded_count++].symbol = lengths[i++];
  85. // "Code 16 repeats the previous non-zero value [3..6] times, i.e., 3 + ReadBits(2) times."
  86. // This is different from deflate.
  87. auto copy_count = 0;
  88. for (size_t j = i; j < min(lengths.size(), i + 6) && lengths[j] == last_non_zero_symbol; j++)
  89. copy_count++;
  90. if (copy_count >= 3) {
  91. encoded_lengths[encoded_count].symbol = 16;
  92. encoded_lengths[encoded_count++].count = copy_count;
  93. i += copy_count;
  94. continue;
  95. }
  96. }
  97. return encoded_count;
  98. }
  99. static ErrorOr<CanonicalCode> write_simple_code_lengths(LittleEndianOutputBitStream& bit_stream, ReadonlyBytes symbols)
  100. {
  101. VERIFY(symbols.size() <= 2);
  102. static constexpr Array<u8, 1> empty { 0 };
  103. if (symbols.size() == 0) {
  104. // "Another special case is when all prefix code lengths are zeros (an empty prefix code). [...]
  105. // empty prefix codes can be coded as those containing a single symbol 0."
  106. symbols = empty;
  107. }
  108. unsigned non_zero_symbol_count = symbols.size();
  109. TRY(bit_stream.write_bits(1u, 1u)); // Simple code length code.
  110. TRY(bit_stream.write_bits(non_zero_symbol_count - 1, 1u)); // num_symbols - 1
  111. if (symbols[0] <= 1) {
  112. TRY(bit_stream.write_bits(0u, 1u)); // is_first_8bits: no
  113. TRY(bit_stream.write_bits(symbols[0], 1u)); // symbol0
  114. } else {
  115. TRY(bit_stream.write_bits(1u, 1u)); // is_first_8bits: yes
  116. TRY(bit_stream.write_bits(symbols[0], 8u)); // symbol0
  117. }
  118. if (non_zero_symbol_count > 1)
  119. TRY(bit_stream.write_bits(symbols[1], 8u)); // symbol1
  120. Array<u8, 256> bits_per_symbol {};
  121. // "When coding a single leaf node [...], all but one code length are zeros, and the single leaf node value
  122. // is marked with the length of 1 -- even when no bits are consumed when that single leaf node tree is used."
  123. // CanonicalCode follows that convention too, even when describing simple code lengths.
  124. bits_per_symbol[symbols[0]] = 1;
  125. if (non_zero_symbol_count > 1)
  126. bits_per_symbol[symbols[1]] = 1;
  127. return MUST(CanonicalCode::from_bytes(bits_per_symbol));
  128. }
  129. static ErrorOr<CanonicalCode> write_normal_code_lengths(LittleEndianOutputBitStream& bit_stream, Array<u8, 256> const& bit_lengths, size_t alphabet_size)
  130. {
  131. // bit_lengths stores how many bits each symbol is encoded with.
  132. // Drop trailing zero lengths.
  133. // This will keep at least three symbols; else we would've called write_simple_code_lengths() instead.
  134. // This is similar to the loops in Deflate::encode_block_lengths().
  135. size_t code_count = bit_lengths.size();
  136. while (bit_lengths[code_count - 1] == 0) {
  137. code_count--;
  138. VERIFY(code_count > 2);
  139. }
  140. Array<CodeLengthSymbol, 256> encoded_lengths {};
  141. auto encoded_lengths_count = encode_huffman_lengths(bit_lengths.span().trim(code_count), encoded_lengths);
  142. // The code to compute code length code lengths is very similar to some of the code in DeflateCompressor::flush().
  143. // count code length frequencies
  144. Array<u16, 19> code_lengths_frequencies { 0 };
  145. for (size_t i = 0; i < encoded_lengths_count; i++) {
  146. VERIFY(code_lengths_frequencies[encoded_lengths[i].symbol] < UINT16_MAX);
  147. code_lengths_frequencies[encoded_lengths[i].symbol]++;
  148. }
  149. // generate optimal huffman code lengths code lengths
  150. Array<u8, 19> code_lengths_bit_lengths {};
  151. Compress::generate_huffman_lengths(code_lengths_bit_lengths, code_lengths_frequencies, 7); // deflate code length huffman can use up to 7 bits per symbol
  152. // calculate actual code length code lengths count (without trailing zeros)
  153. auto code_lengths_count = code_lengths_bit_lengths.size();
  154. while (code_lengths_bit_lengths[kCodeLengthCodeOrder[code_lengths_count - 1]] == 0)
  155. code_lengths_count--;
  156. TRY(bit_stream.write_bits(0u, 1u)); // Normal code length code.
  157. // This here isn't needed in Deflate because it always writes EndOfBlock. WebP does not have an EndOfBlock marker, so it needs this check.
  158. if (code_lengths_count < 4)
  159. code_lengths_count = 4;
  160. dbgln_if(WEBP_DEBUG, "writing code_lengths_count: {}", code_lengths_count);
  161. // WebP uses a different kCodeLengthCodeOrder than deflate. Other than that, the following is similar to a loop in Compress::write_dynamic_huffman().
  162. // "int num_code_lengths = 4 + ReadBits(4);"
  163. TRY(bit_stream.write_bits(code_lengths_count - 4u, 4u));
  164. for (size_t i = 0; i < code_lengths_count; i++) {
  165. TRY(bit_stream.write_bits(code_lengths_bit_lengths[kCodeLengthCodeOrder[i]], 3));
  166. }
  167. // Write code lengths. This is slightly different from deflate too -- deflate writes literal and distance lengths here,
  168. // while WebP writes one of these codes each for g, r, b, a, and distance.
  169. if (alphabet_size == encoded_lengths_count) {
  170. TRY(bit_stream.write_bits(0u, 1u)); // max_symbol is alphabet_size
  171. } else {
  172. dbgln_if(WEBP_DEBUG, "writing max_symbol: {}", encoded_lengths_count);
  173. TRY(bit_stream.write_bits(1u, 1u)); // max_symbol is explicitly coded
  174. // "int length_nbits = 2 + 2 * ReadBits(3);
  175. // int max_symbol = 2 + ReadBits(length_nbits);"
  176. // => length_nbits is at most 2 + 2*7 == 16
  177. unsigned needed_length_nbits = encoded_lengths_count > 2 ? floor(log2(encoded_lengths_count - 2) + 1) : 2;
  178. VERIFY(needed_length_nbits <= 16);
  179. needed_length_nbits = ceil_div(needed_length_nbits, 2) * 2;
  180. TRY(bit_stream.write_bits((needed_length_nbits - 2) / 2, 3u));
  181. TRY(bit_stream.write_bits(encoded_lengths_count - 2, needed_length_nbits));
  182. }
  183. // The rest is identical to write_dynamic_huffman() again. (Code 16 has different semantics, but that doesn't matter here.)
  184. auto code_lengths_code = MUST(CanonicalCode::from_bytes(code_lengths_bit_lengths));
  185. for (size_t i = 0; i < encoded_lengths_count; i++) {
  186. auto encoded_length = encoded_lengths[i];
  187. TRY(code_lengths_code.write_symbol(bit_stream, encoded_length.symbol));
  188. if (encoded_length.symbol == 16) {
  189. // "Code 16 repeats the previous non-zero value [3..6] times, i.e., 3 + ReadBits(2) times."
  190. TRY(bit_stream.write_bits<u8>(encoded_length.count - 3, 2));
  191. } else if (encoded_length.symbol == 17) {
  192. // "Code 17 emits a streak of zeros [3..10], i.e., 3 + ReadBits(3) times."
  193. TRY(bit_stream.write_bits<u8>(encoded_length.count - 3, 3));
  194. } else if (encoded_length.symbol == 18) {
  195. // "Code 18 emits a streak of zeros of length [11..138], i.e., 11 + ReadBits(7) times."
  196. TRY(bit_stream.write_bits<u8>(encoded_length.count - 11, 7));
  197. }
  198. }
  199. return CanonicalCode::from_bytes(bit_lengths.span().trim(code_count));
  200. }
  201. static ErrorOr<void> write_VP8L_coded_image(ImageKind image_kind, LittleEndianOutputBitStream& bit_stream, Bitmap const& bitmap, IsOpaque& is_fully_opaque)
  202. {
  203. // https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#5_image_data
  204. // spatially-coded-image = color-cache-info meta-prefix data
  205. // entropy-coded-image = color-cache-info data
  206. // color-cache-info = %b0
  207. // color-cache-info =/ (%b1 4BIT) ; 1 followed by color cache size
  208. TRY(bit_stream.write_bits(0u, 1u)); // No color cache for now.
  209. if (image_kind == ImageKind::SpatiallyCoded) {
  210. // meta-prefix = %b0 / (%b1 entropy-image)
  211. TRY(bit_stream.write_bits(0u, 1u)); // No meta prefix for now.
  212. }
  213. // data = prefix-codes lz77-coded-image
  214. // prefix-codes = prefix-code-group *prefix-codes
  215. // prefix-code-group =
  216. // 5prefix-code ; See "Interpretation of Meta Prefix Codes" to
  217. // ; understand what each of these five prefix
  218. // ; codes are for.
  219. // We're writing a single prefix-code-group.
  220. // "These codes are (in bitstream order):
  221. // Prefix code #1: Used for green channel, backward-reference length, and color cache.
  222. // Prefix code #2, #3, and #4: Used for red, blue, and alpha channels, respectively.
  223. // Prefix code #5: Used for backward-reference distance."
  224. // We use neither back-references not color cache entries yet.
  225. // We can make this smarter later on.
  226. size_t const color_cache_size = 0;
  227. constexpr Array alphabet_sizes = to_array<size_t>({ 256 + 24 + static_cast<size_t>(color_cache_size), 256, 256, 256, 40 });
  228. // If you add support for color cache: At the moment, CanonicalCodes does not support writing more than 288 symbols.
  229. if (alphabet_sizes[0] > 288)
  230. return Error::from_string_literal("Invalid alphabet size");
  231. // We do use huffman coding by writing a single prefix-code-group for the entire image.
  232. // FIXME: Consider using a meta-prefix image and using one prefix-code-group per tile.
  233. Array<Array<u16, 256>, 4> symbol_frequencies {};
  234. for (ARGB32 pixel : bitmap) {
  235. static constexpr auto saturating_increment = [](u16& value) {
  236. if (value < UINT16_MAX)
  237. value++;
  238. };
  239. saturating_increment(symbol_frequencies[0][(pixel >> 8) & 0xff]); // green
  240. saturating_increment(symbol_frequencies[1][(pixel >> 16) & 0xff]); // red
  241. saturating_increment(symbol_frequencies[2][pixel & 0xff]); // blue
  242. saturating_increment(symbol_frequencies[3][pixel >> 24]); // alpha
  243. }
  244. Array<Array<u8, 256>, 4> code_lengths {};
  245. for (int i = 0; i < 4; ++i) {
  246. // "Code [0..15] indicates literal code lengths." => the maximum bit length is 15.
  247. Compress::generate_huffman_lengths(code_lengths[i], symbol_frequencies[i], 15);
  248. }
  249. PrefixCodeGroup prefix_code_group;
  250. for (int i = 0; i < 4; ++i) {
  251. u8 symbols[2];
  252. unsigned non_zero_symbol_count = 0;
  253. for (int j = 0; j < 256; ++j) {
  254. if (code_lengths[i][j] != 0) {
  255. if (non_zero_symbol_count < 2)
  256. symbols[non_zero_symbol_count] = j;
  257. non_zero_symbol_count++;
  258. }
  259. }
  260. if (non_zero_symbol_count <= 2)
  261. prefix_code_group[i] = TRY(write_simple_code_lengths(bit_stream, { symbols, non_zero_symbol_count }));
  262. else
  263. prefix_code_group[i] = TRY(write_normal_code_lengths(bit_stream, code_lengths[i], alphabet_sizes[i]));
  264. if (i == 3)
  265. is_fully_opaque.set_is_fully_opaque_if_not_yet_known(non_zero_symbol_count == 1 && symbols[0] == 0xff);
  266. }
  267. // For code #5, use a simple empty code, since we don't use this yet.
  268. prefix_code_group[4] = TRY(write_simple_code_lengths(bit_stream, {}));
  269. // Image data.
  270. TRY(write_image_data(bit_stream, bitmap, prefix_code_group));
  271. return {};
  272. }
  273. static ARGB32 sub_argb32(ARGB32 a, ARGB32 b)
  274. {
  275. auto a_color = Color::from_argb(a);
  276. auto b_color = Color::from_argb(b);
  277. return Color(a_color.red() - b_color.red(),
  278. a_color.green() - b_color.green(),
  279. a_color.blue() - b_color.blue(),
  280. a_color.alpha() - b_color.alpha())
  281. .value();
  282. }
  283. static ErrorOr<NonnullRefPtr<Bitmap>> maybe_write_color_indexing_transform(LittleEndianOutputBitStream& bit_stream, NonnullRefPtr<Bitmap> bitmap, IsOpaque& is_fully_opaque)
  284. {
  285. // https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification#44_color_indexing_transform
  286. unsigned color_table_size = 0;
  287. HashTable<ARGB32> seen_colors;
  288. ARGB32 channels = 0;
  289. ARGB32 first_pixel = bitmap->get_pixel(0, 0).value();
  290. for (ARGB32 pixel : *bitmap) {
  291. auto result = seen_colors.set(pixel);
  292. if (result == HashSetResult::InsertedNewEntry) {
  293. ++color_table_size;
  294. channels |= pixel ^ first_pixel;
  295. if (color_table_size > 256)
  296. break;
  297. }
  298. }
  299. dbgln_if(WEBP_DEBUG, "WebP: Image has {}{} colors; all pixels or'd is {:#08x}", color_table_size > 256 ? ">= " : "", color_table_size, channels);
  300. // If the image has a single color, the huffman table can encode it in 0 bits and color indexing does not help.
  301. if (color_table_size <= 1 || color_table_size > 256)
  302. return bitmap;
  303. // If all colors use just a single channel, color indexing does not help either,
  304. // except if there are <= 16 colors and we can do pixel bundling.
  305. // FIXME: Once we support color cache, maybe that helps for single-channel pixels with fewer than 16 colors
  306. // and we don't need to write a color index then?
  307. if (color_table_size > 16) {
  308. int number_of_non_constant_channels = 0;
  309. for (int i = 0; i < 4; ++i) {
  310. if (channels & (0xff << (i * 8)))
  311. number_of_non_constant_channels++;
  312. }
  313. if (number_of_non_constant_channels <= 1)
  314. return bitmap;
  315. }
  316. dbgln_if(WEBP_DEBUG, "WebP: Writing color index transform");
  317. TRY(bit_stream.write_bits(1u, 1u)); // Transform present.
  318. TRY(bit_stream.write_bits(static_cast<unsigned>(COLOR_INDEXING_TRANSFORM), 2u));
  319. // "int color_table_size = ReadBits(8) + 1;"
  320. TRY(bit_stream.write_bits(color_table_size - 1, 8u));
  321. // Store color index to bit stream.
  322. Vector<ARGB32, 256> colors;
  323. for (ARGB32 color : seen_colors)
  324. colors.append(color);
  325. quick_sort(colors.begin(), colors.end());
  326. // "The color table is stored using the image storage format itself." [...]
  327. // "The color table is always subtraction-coded to reduce image entropy."
  328. auto color_index_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { static_cast<int>(color_table_size), 1 }));
  329. color_index_bitmap->set_pixel(0, 0, Color::from_argb(colors[0]));
  330. for (unsigned i = 1; i < color_table_size; ++i)
  331. color_index_bitmap->set_pixel(i, 0, Color::from_argb(sub_argb32(colors[i], colors[i - 1])));
  332. TRY(write_VP8L_coded_image(ImageKind::EntropyCoded, bit_stream, *color_index_bitmap, is_fully_opaque));
  333. // Return a new bitmap with the color indexing transform applied.
  334. HashMap<ARGB32, u8> color_index_map;
  335. for (unsigned i = 0; i < color_table_size; ++i)
  336. color_index_map.set(colors[i], i);
  337. // "When the color table is small (equal to or less than 16 colors), several pixels are bundled into a single pixel.
  338. // The pixel bundling packs several (2, 4, or 8) pixels into a single pixel, reducing the image width respectively."
  339. int width_bits;
  340. if (color_table_size <= 2)
  341. width_bits = 3;
  342. else if (color_table_size <= 4)
  343. width_bits = 2;
  344. else if (color_table_size <= 16)
  345. width_bits = 1;
  346. else
  347. width_bits = 0;
  348. int pixels_per_pixel = 1 << width_bits;
  349. int image_width = ceil_div(bitmap->width(), pixels_per_pixel);
  350. auto new_bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { image_width, bitmap->height() }));
  351. unsigned bits_per_pixel = 8 / pixels_per_pixel;
  352. for (int y = 0; y < bitmap->height(); ++y) {
  353. for (int x = 0, new_x = 0; x < bitmap->width(); x += pixels_per_pixel, ++new_x) {
  354. u8 indexes = 0;
  355. for (int i = 0; i < pixels_per_pixel && x + i < bitmap->width(); ++i) {
  356. auto pixel = bitmap->get_pixel(x + i, y);
  357. auto result = color_index_map.get(pixel.value());
  358. VERIFY(result.has_value());
  359. indexes |= result.value() << (i * bits_per_pixel);
  360. }
  361. new_bitmap->set_pixel(new_x, y, Color(0, indexes, 0, 0));
  362. }
  363. }
  364. return new_bitmap;
  365. }
  366. static ErrorOr<void> write_VP8L_image_data(Stream& stream, NonnullRefPtr<Bitmap> bitmap, VP8LEncoderOptions const& options, IsOpaque& is_fully_opaque)
  367. {
  368. LittleEndianOutputBitStream bit_stream { MaybeOwned<Stream>(stream) };
  369. // image-stream = optional-transform spatially-coded-image
  370. // optional-transform = (%b1 transform optional-transform) / %b0
  371. if (options.allowed_transforms & (1u << COLOR_INDEXING_TRANSFORM))
  372. bitmap = TRY(maybe_write_color_indexing_transform(bit_stream, bitmap, is_fully_opaque));
  373. TRY(bit_stream.write_bits(0u, 1u)); // No further transforms for now.
  374. TRY(write_VP8L_coded_image(ImageKind::SpatiallyCoded, bit_stream, *bitmap, is_fully_opaque));
  375. // FIXME: Make ~LittleEndianOutputBitStream do this, or make it VERIFY() that it has happened at least.
  376. TRY(bit_stream.align_to_byte_boundary());
  377. TRY(bit_stream.flush_buffer_to_stream());
  378. return {};
  379. }
  380. ErrorOr<ByteBuffer> compress_VP8L_image_data(Bitmap const& bitmap, VP8LEncoderOptions const& options, bool& is_fully_opaque)
  381. {
  382. AllocatingMemoryStream vp8l_data_stream;
  383. IsOpaque is_opaque_struct;
  384. TRY(write_VP8L_image_data(vp8l_data_stream, bitmap, options, is_opaque_struct));
  385. VERIFY(is_opaque_struct.is_opacity_known);
  386. is_fully_opaque = is_opaque_struct.is_fully_opaque;
  387. return vp8l_data_stream.read_until_eof();
  388. }
  389. }