Commit graph

45 commits

Author SHA1 Message Date
Nico Weber
fab6a3915e LibGfx/WebP: Implement uncompressed ALPH chunk reading
ALPH chunks are only used to give lossy webp frames an alpha channel,
and lossy decompression isn't implemented yet. So this can currently
never be hit in practice -- but for debugging and testing, I put in
some code behind `#if 0` for now that fake-decompresses a lossy webp
frame by returning an empty bitmap.

But this also doesn't implement compressed ALPH chunks yet, and I
couldn't find any lossy-webp-with-alpha files that use uncompressed
alpha channels. So the code here isn't really tested.
2023-05-07 07:08:05 +02:00
Nico Weber
e9f5c9ab9d Tests/LibGfx: More preparation for lossy and alpha handling
If someone comes along who wants to implement lossy webp decoding,
they now only need to implement decode_webp_chunk_VP8() and everything
might Just Work.

It also makes it possible to implement alpha chunk decoding before
implementing lossy decoding (by making decode_webp_chunk_VP8()
return an empty black bitmap for testing).
2023-05-07 07:08:05 +02:00
Nico Weber
23386ac73a LibGfx/WebP: Extract a decode_webp_image_data() function
That way, animated and non-animated webp files use the same code path
to decode images. That will make it easier to add handling for lossy
decompression and for alpha chunk handling.

No behavior change.
2023-05-07 07:08:05 +02:00
Nico Weber
36c8c1129b LibGfx/WebP: Use context.error() in animation frame decoding function
That also sets the error state on the context.
2023-05-06 21:17:18 +02:00
Nico Weber
ce45ad6112 LibGfx/WebP: Implement decoding of animation frame bitmaps
With this, lossless animated webp files work :^)

(Missing: Loop count handling is not yet implemented, and alpha blending
between frames isn't done in linear space.)
2023-05-06 21:17:18 +02:00
Nico Weber
23ce1f641c LibGfx/WebP: Check that animation frame dimensions are in bounds 2023-05-06 21:17:18 +02:00
Nico Weber
0b70ecb7e6 LibGfx/WebP: Make decode_webp_chunk_VP8L() return a Bitmap
...instead of setting it on the context. We'll need this to decode
animation frames.
2023-05-06 21:17:18 +02:00
Nico Weber
1334bac548 LibGfx/WebP: Extract an ImageData struct
We'll need the same struct for animation frames.
2023-05-06 21:17:18 +02:00
Nico Weber
03b04ed66a LibGfx/WebP: Check ICCP chunk precedes alpha and animation frame chunks 2023-05-06 21:17:18 +02:00
Nico Weber
5c002c13c9 LibGfx/WebP: Some steps towards supporting animated webp files
No observable behavior change, but when building with WEBP_DEBUG 1,
this now prints frame data.
2023-05-06 08:01:13 +02:00
Nico Weber
812763e5b9 LibGfx/WebP: Stop dropping ANIM chunk decoding errors
decode_webp_chunks() was already called 5 lines up, no need to do that
again.
2023-05-06 08:01:13 +02:00
Nico Weber
835d328a6c LibGfx/WebP: Check presence of ANMF chunks correctly
Previously, we looked at the wrong variable here.
2023-05-06 08:01:13 +02:00
Tom
e7921cfe14 LibGfx: Add first_animated_frame_index method to ImageDecoder
Some image formats such as APNG may not use the first frame for
animations.
2023-05-05 15:20:44 +01:00
Nico Weber
f56b897622 Everywhere: Fix a few typos
Some even user-visible!
2023-04-12 19:37:35 +02:00
Nico Weber
97dc2d1dd5 LibGfx/WebP: Don't assert when size in header is smaller than header
read_webp_first_chunk() sensibly assumes that if decode_webp_header()
succeeds, there are at least sizeof(WebPFileHeader) bytes available.

But if the file size in the header was less than the size of the header,
decode_webp_header() would truncate the data to less than that and
happily report success. Now it no longer does that.

Found by clusterfuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=57843&sort=-opened&can=1&q=proj%3Aserenity
2023-04-12 17:32:00 +02:00
Nico Weber
bed86fb578 LibGfx: Remove some of the noisier logging from webp lossless decoder
Prefix code decoding seems to work fairly well and produces a ton of
log output with `#define WEBP_DEBUG 1`, so remove the log lines.

(If needed it's always possible to just locally revert this commit.)

No behavior change, since WEBP_DEBUG isn't usually defined.
2023-04-09 16:49:38 +02:00
Nico Weber
95e35b7f5e LibGfx: Correctly decode webp lossless with small palette and odd width
WebP lossless files that use a color indexing transform with <= 16
colors use pixel bundling to pack 2, 4, or 8 pixels into a single pixel.

If the image's width doesn't happen to be an exact multiple of the
bundling factor, we need to:

1. Use ceil_div() instead of just dividing the width by the bundling
   factor

2. Remember the original width and use it instead of computing
   reduced width times bundling factor

This does these changes, and adds a simple test for it -- it at least
checks that the decoded images have the right size.

(I created these images myself in Photoshop, and used the same
technique as for Tests/LibGfx/test-inputs/catdog-alert-*.webp
to create images with a certain number of colors.)
2023-04-09 00:14:15 +02:00
Nico Weber
1dab480015 LibGfx: Implement color index pixel bundling in webp decoder
See the lengthy comment added in this commit for details.

With this, the webp lossless decoder is feature complete :^)

(...except for bug fixes and performance improvements, as always.)
2023-04-08 19:24:13 +02:00
Nico Weber
7309441b31 LibGfx: Enable webp lossless Transform to return new bitmap
...in addition to modifying in-place. This is needed for bitpacking
support for the color indexing transform (and it could also be used
to make the color indexing transform return an indexed bitmap, which
is something we could do if that's the last transform that's applied).

No behavior change.
2023-04-08 19:24:13 +02:00
Nico Weber
13f8bbb284 LibGfx: Add some more dbgln_if()s to webp decoder 2023-04-08 19:24:13 +02:00
Nico Weber
82182f4560 LibGfx: Give PrefixCodeGroup a deleted copy ctor
This makes the accidental copy fixed in 2125ccdc19 a compile error.

No behavior change.
2023-04-08 19:23:44 +02:00
Nico Weber
1fc56e56c3 LibGfx: Make webp lossless decoder 6 times as fast
Reduces the time to run

    Build/lagom/image ~/src/libwebp/webp_js/test_webp_wasm.webp -o tmp.png

from 0.5s to 0.25s.

Before, 60% of the time was spent decoding webp and 40% writing png.
Now, 16% of the time was spent decoding webp and 84% writing png.

That means png writing takes 0.2s, and webp decoding time went from
0.3s to 0.05s.

A template expression without explicit return type deduces its return
type as if for a function whose return type is declared auto. That
does deduce return-by-value, while `decltype(auto)` would deduce
return-by-reference.  Explictly saying `decltype(auto)` would work
too, but writing out the type is maybe easier to understand.

No behavior change other than being much faster.
2023-04-08 18:57:37 +02:00
Nico Weber
24967b0d29 LibGfx: Second attempt to handle max_symbol correctly in webp decoder
The previous attempt was in commit e5e9d3b877, where I thought
max_symbol describes how many code lengths should be read.

But it looks like it instead describes how many code length input
symbols should be read. (The two aren't the same since one code length
input symbol can produce several code lengths.)

I still agree with the commit description of e5e9d3b877 that the spec
isn't very clear on this :)

This time I've found a file that sets max_symbol and with this change
here, that file decodes correctly. (It's Qpalette.webp, which I'm about
to add as a test case.)
2023-04-08 16:50:40 +02:00
Nico Weber
a915d07293 LibGfx: Implement most of COLOR_INDEXING_TRANSFORM for webp decoder
Doesn't yet implement pixel packing for when the palette has fewer
than 16 colors.
2023-04-08 16:50:40 +02:00
Nico Weber
6f4fdd85b7 LibGfx: Extract free add_argb32() function in webp decoder 2023-04-08 16:50:40 +02:00
Nico Weber
50c9b51eca LibGfx: Make a webp error message more detailed
Now that lossless decoding mostly works, make it clear that only
lossy decoding isn't implemented yet.
2023-04-08 16:50:40 +02:00
Nico Weber
d354c1b007 LibGfx: Implement meta prefix code support in webp decoder 2023-04-07 20:49:39 +02:00
Nico Weber
55b2977d5d LibGfx: Implement COLOR_TRANSFORM for webp lossless decoder 2023-04-07 20:49:39 +02:00
Nico Weber
ebbe4dafa1 LibGfx: Implement PREDICTOR_TRANSFORM for webp lossless decoder
Very much not written for performance at this point.
2023-04-07 20:49:39 +02:00
Nico Weber
b1cde0d432 LibGfx: Add CanonicalCode wrapper to webp lossless decoder
WebP lossless differs from deflate in how it handles 1-element codes.
Deflate consumes one bit from the bitstream to produce the element,
while webp lossless consumes 0 bits. Add a wrapper class to handle
this case.
2023-04-07 20:49:39 +02:00
Nico Weber
8760376abe LibGfx: Implement SUBTRACT_GREEN_TRANSFORM for webp lossless decoder 2023-04-07 09:47:04 +02:00
Nico Weber
cdc77f7512 LibGfx: Add scaffolding for applying transforms to webp lossless decoder
Each of the four transforms will inherit from this class.
2023-04-07 09:47:04 +02:00
Nico Weber
b15d3b2329 LibGfx: Add more dbgln_if()s to webp decoder
They were useful while debugging the decoder. Keep them in for a bit.
2023-04-07 09:47:04 +02:00
Nico Weber
2fc682c033 LibGfx: In webp decoder, check that each transform is used only once 2023-04-07 09:47:04 +02:00
Nico Weber
ae1f7124ac LibGfx: Correctly handle more than one PrefixCodeGroup in webp decoder
The `static` here meant we always kept the alphabet sizes of the
first image we happened to load -- and a single webp lossless image
can store several helper images used during decoding.

Usually, the helper images wouldn't use a color cache but the main
image would, but the main image would then use the first entry from
the helper images due to the `static`, which led us to not decoding
the codes for the color cache symbols.
2023-04-07 09:47:04 +02:00
Nico Weber
73c291f5ae LibGfx: Pass in format and size to webp image decoding function 2023-04-06 00:16:52 +01:00
Nico Weber
48f88b3cdd LibGfx: Teach webp image reading function to read entropy coded images 2023-04-06 00:16:52 +01:00
Nico Weber
4bd7090bc5 LibGfx: Move webp image decoding function up a bit
Pure code move, no changes (except that this allows removing the
explicit prototype for this function, so it removes that).
2023-04-06 00:16:52 +01:00
Nico Weber
f21af311c2 LibGfx: Move webp bitmap decoding code into its own function 2023-04-06 00:16:52 +01:00
Nico Weber
e5e9d3b877 LibGfx: Implement hopefully correct max_symbol handling in webp decoder
The spec is at best misleading here, suggesting that max_symbol should
be set to "num_code_lengths" if it's not explicitly stored.

But num_code_lengths doesn't mean the num_code_lengths mentioned a few
lines further up in the spec, but alphabet_size!

(I had to cheat and look at libwebp instead of the spec for this: See
vp8l_dec.c, ReadHuffmanCode() which passes alphabet_size to
ReadHuffmanCodeLengths() as num_symbols, and ReadHuffmanCodeLengths()
then sets max_symbol to that.)

I haven't yet found a file that uses max_symbol, so this isn't actually
tested. But it's close to what's in libwebp, so maybe it works!
2023-04-06 00:16:52 +01:00
Nico Weber
e8f5e699fe LibGfx: Read transform type in webp lossless decoder
Doesn't do anything with it yet, so this only makes the
"not yet implemented" message a bit more detailed.
2023-04-06 00:16:52 +01:00
Nico Weber
8e6911c8f6 LibGfx: Remove some noisy dbgln_if()s in webp decoder
Pixel decoding mostly works, so there's no need to log all this data.
2023-04-06 00:16:52 +01:00
Nico Weber
c84968dafd LibGfx: Add some support for decoding lossless webp files
Missing:
* Transform support (used by virtually all lossless webp files)
* Meta prefix / entropy image support

Working:
* Decoding of regular image streams
* Color cache

This happens to be enough to be able to decode
Tests/LibGfx/test-inputs/extended-lossless.webp

The canonical prefix code is very similar to deflate's, enough so that
this can use Compress::CanonicalCode (and take advantage of all the
recent performance improvements there).
2023-04-05 13:24:00 +02:00
Nico Weber
830fd0d5b2 LibGfx: Read webp lossless header using LittleEndianInputBitStream
No behavior change. Covered by existing webp decoder tests :^)
2023-04-05 13:24:00 +02:00
Lucas CHOLLET
496b7ffb2b LibGfx: Move all image loaders and writers to a subdirectory 2023-03-21 22:39:25 +01:00
Renamed from Userland/Libraries/LibGfx/WebPLoader.cpp (Browse further)