Commit graph

77 commits

Author SHA1 Message Date
Lucas CHOLLET
8ef5170bd5 LibGfx/WebP: Remove a typo
:redthakis:
2023-07-15 09:34:07 +02:00
Lucas CHOLLET
4adef05e6d LibGfx/WebP: Decode the first chunk in create()
And remove `initialize()`.

This is done as a part of #19893.
2023-07-15 09:34:07 +02:00
Lucas CHOLLET
e5b70837de LibGfx: Remove ImageDecoder::set_[non]volatile()
These methods are unused so let's remove them.
2023-07-08 01:45:46 +01:00
MacDue
e7cddda7e1 LibGfx: Allow passing an ideal size to image decoders
The ideal size is the size the user will display the image. Raster
formats should ignore this parameter, but vector formats can use
it to generate a bitmap of the ideal size.
2023-07-03 23:54:51 +02:00
Nico Weber
52d17afd7e WebP: Add test for vertical ALPH chunk filtering_method 2023-06-09 04:35:19 -07:00
Nico Weber
816674de36 WebP: Add test for horizontal ALPH chunk filtering_method 2023-06-09 04:35:19 -07:00
Nico Weber
4e92027513 WebP: Add missing spec quotes in a comment, tweak whitespace 2023-06-09 04:35:19 -07:00
Nico Weber
df79a2720b WebP/Lossy+Alpha: Implement filtering_method for ALPH chunk
The spec says "Decoders are not required to use this information in any
specified way" about this field, but that's probably a typo and belongs
in the previous section. At least, images in the wild look wrong
without this, for example
https://fjord.dropboxstatic.com/warp/conversion/dropbox/warp/en-us/dropbox/Integrations_4@2x.png?id=ce8269af-ef1a-460a-8199-65af3dd978a3&output_type=webp

Implementation-wise, this now copies both uncompressed and compressed
data to yet another buffer for processing the alpha, then does
filtering on that buffer, and then copies the filtered alpha data
into the final image. (The RGB data comes from a lossy webp.)
This is a bit wasteful and we could probably manage without that
local copy, but that'd make the code more convoluted, so this is
good enough for now, at least until we've added tests for this case.

No test, because I haven't yet found out how to create images in this
format.
2023-06-07 08:08:52 +02:00
Nico Weber
5793579d84 Revert "WebP: Add optional debugging code for dumping ALPH as standa..."
This reverts commit 7a4dd8ad79aff9e66e8ae31820b41582be0a3a6d.
Having it in git history is enough.
2023-06-07 08:08:52 +02:00
Nico Weber
fc3249a1ca WebP: Add optional debugging code for dumping ALPH as standalone file
We can quickly revert this again, but I thought it'd be nice to have
it in git history for future debugging in this area.
2023-06-07 08:08:52 +02:00
Nico Weber
bf9e730566 WebP: Log error if there is one
Else, WebP files with a broken header just return "Decoding failed"
without any more details. This way, there's some debug logging with
more details.

Maybe we'll want to remove this again since it might lead to duplicate
error messages for files that have their error not in the header.
We'll see how this feels. (But most files don't have any errors, so
it's probably fine.)
2023-06-07 08:08:52 +02:00
Nico Weber
c2ec97dd79 WebP: Remove nonsensical comment
It's up to callers of the ImageDecoderPlugin to honor loop_count().
The ImageDecoderPlugin doesn't have to look at it when decoding frames.

No behavior change.
2023-06-01 16:23:46 +02:00
Nico Weber
ae5d1d5a25 WebP: Let ALPH replace alpha channel instead of augmenting it
Pixels will leave the lossy decoder with alpha set to 255.
The old code would be a no-op in that case.

No observable behavior change yet, since there still is no
decoder for lossy webp.
2023-05-22 19:24:49 +02:00
Ben Wiederhake
da394abe04 LibGfx+Fuzz: Convert ImageDecoder::initialize to ErrorOr
This prevents callers from accidentally discarding the result of
initialize(), which was the root cause of this OSS Fuzz bug:

https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=55896&q=label%3AProj-serenity&sort=summary
2023-05-12 09:40:24 +01:00
Nico Weber
9e8b507fad LibGfx/WebP: Do not add alpha channel for animated images without alpha
In practice, it looks like e.g. the animaged webp file on
https://mathiasbynens.be/demo/animated-webp has the header flag set,
because 2 of the frames have alpha, but they're composited on top of
the final bitmap, but the final bitmap isn't transparent there. So
that image still gets a useless alpha channel. Oh well.
2023-05-09 17:46:37 +02:00
Nico Weber
bc207fd0a0 LibGfx/WebP: Move lossy decoder to its own file
Pure code move (except of removing `static` on the two public functions
in the new header), not behavior change.

There isn't a lot of lossy decoder yet, but it'll make implementing it
more convenient.

No behavior change.
2023-05-09 06:35:56 +02:00
Nico Weber
83da7ad875 LibGfx/WebP: Give VP8 decoding functions same interface as VP8L ones
Namely:
* Store compressed data in VP8Header
* Make the functions just take ReadonlyBytes instead of a Chunk

Having a function that takes a header and does decoding of the data
after the header isn't really necessary for VP8. For VP8L, it's needed
because the ALPH chunk stores VP8L data without the VP8L header.
But it's nice to make the functions consistent, and it's kind of a
nice structure.

No behavior change.
2023-05-09 06:35:56 +02:00
Nico Weber
d15dc28833 LibGfx/WebP: Tell decode_webp_chunk_VP8() if it needs an alpha channel
decode_webp_chunk_VP8() itself will only ever decode RGB data from a
lossy webp stream, but a separate ALPH chunk could add alpha data
later on. Let the function know if that will happen, so that it can
return a bitmap with an alpha channel if appropriate.

Since lossy decoding isn't implemented yet, no behavior change. But it
makes it a bit easier to implement lossy decoding in the future.
2023-05-09 06:35:56 +02:00
Nico Weber
e13c319972 LibGfx/WebP: Remove pointless decode_webp_chunk_VP8L() function
The one caller checked the chunk type, so the VERIFY() for that did
nothing.

The VERIFY() for vp8l data only being in files that start with
VP8L or VP8X chunks wasn't completely useless, but also not very
useful.

Remove the now-unused context parameter of decode_webp_image_data().
2023-05-09 06:35:56 +02:00
Nico Weber
1ec5c8395c LibGfx/WebP: Remove context.error() now that it is a no-op
Also remove `context` from many function parameter lists where the
parameter is now no longer needed.
2023-05-09 06:35:56 +02:00
Nico Weber
ddc2cc886b LibGfx/WebP: Redo error handling
Most places used to call `context.error()` to report an error,
which would set the context's state to `Error` and then return an
`Error::from_string_literal()`.

This is somewhat elegant, but it doesn't work: Some functions this
code calls returns ErrorOr<>s that aren't created by `context.error()`,
and for these we wouldn't enter the error state.

Instead, manually check error-ness at the leaf entry functions of the
class:

1. Add a set_error() helper for functions returning bool
2. In the two functions returning ErrorOr<>, awkwardly check the error
   manually.  If this becomes a very common pattern, maybe we can add
   a `TRY_WITH_HANDLER(expr, error_lambda)` which would invoke a
   lambda on error. We could use that here to set the error code.

No real behavior change (except we enter the error state more often
when something goes wrong).
2023-05-09 06:35:56 +02:00
Nico Weber
bdba70b38f LibGfx/WebP: Change ImageData optional-ness
Instead of ImageData having an Optional<Chunk> for the image data,
have it have a Chunk, and give the context an Optional<ImageData>.

This allows sharing a single code path for checking that either the
main image or an animation frame has a main image data chunk, and
that an alpha chunk is only present with a lossy main image data
chunk.

No behavior change.
2023-05-09 06:35:56 +02:00
Nico Weber
119ccfe5da LibGfx/WebP: Minor cosmetical changes in WebPLoaderLossless.h
Add a comment, remove a parameter name that doesn't add anything,
and remove a weird newline in WebPLoader.cpp too.

No behavior change.
2023-05-09 06:35:56 +02:00
Nico Weber
65c7145e69 LibGfx/WebP: Move lossless decoder to its own file
Pure code move (except of removing `static` on the two public functions
in the new header), not behavior change.
2023-05-08 12:52:05 +02:00
Nico Weber
4f1c9a4ba2 LibGfx/WebP: Let decode_webp_chunk_VP8L_header() take ReadonlyBytes
Both callers already have the assert, and that way the function
doesn't have to know about Chunk.
2023-05-08 12:52:05 +02:00
Nico Weber
9d4da5af43 LibGfx/WebP: Store pointer to lossless data in VP8LHeader struct
This way, the size of the header in bytes is only known in
decode_webp_chunk_VP8L_header().
2023-05-08 12:52:05 +02:00
Nico Weber
5252f1cd60 LibGfx/WebP: Stop storing vp8_header and vp8l_header in context
They're not needed on the context.
2023-05-08 12:52:05 +02:00
Nico Weber
135b029250 LibGfx/WebP: Stop passing context to decode_webp_chunk_VP8L_contents()
It was used only for context.error(), and the calling code needs to
be changed to deal normal Errors anyways, since CanonicalCode can
produce them.
2023-05-08 12:52:05 +02:00
Nico Weber
97d085abea LibGfx/WebP: Stop passing context to decode_webp_chunk_VP8L_image()
It was used only for context.error(), and the calling code needs to
be changed to deal normal Errors anyways, since CanonicalCode can
produce them.
2023-05-08 12:52:05 +02:00
Nico Weber
356cadc350 LibGfx/WebP: Stop passing context to decode_webp_chunk_VP8L_prefix_code
It was used only for context.error(), and the calling code needs to
be changed to deal normal Errors anyways, since CanonicalCode can
produce them.
2023-05-08 12:52:05 +02:00
Nico Weber
2f1f62cb3b LibGfx/WebP: Stop passing context to decode_webp_chunk_VP8L_header()
It was used only for context.error(), and the calling code needs to
be changed to deal normal Errors anyways, since CanonicalCode can
produce them.
2023-05-08 12:52:05 +02:00
Nico Weber
0d2e6162db LibGfx/WebP: Implement compressed ALPH chunk reading
A compressed ALPH chunk is a lossless webp bitstream, but without
the 5 byte "header" that stores width, height, is-alpha-channel-used
(it never is for an ALPH chunk since the ALPH chunk gets the alpha
data out of the lossless webp's green channel), and version fields.

For that reason, this cuts decode_webp_chunk_VP8L() into the
header-reading part and the remaining part, so that the remaining
part can be called by the ALPH reading routine.

Lossy webp files with a (losslessly) compressed alpha channel can
be found in the wild. Since we can't decode lossy webp data yet,
change the `#if 0` in decode_webp_chunk_VP8() to `#if 1` to test this.
2023-05-07 07:08:05 +02:00
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