Commit graph

106 commits

Author SHA1 Message Date
Lucas CHOLLET
844e374de1 LibGfx/JPEG: Fix faded 4-channels images 2023-05-08 19:28:51 +02:00
Lucas CHOLLET
e81baa0464 LibGfx/JPEG: Discard the correct number of bytes
This path has never been tested as it requires a non-standard APP
segment. We (un?)fortunately found one, and it exposed a silly bug.
2023-05-08 19:26:17 +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
Lucas CHOLLET
677386bfaa LibGfx/JPEG: Use a lookup table to retrieve huffman symbols
Instead of testing all possible code to find the good symbol, we use a
lookup table to directly find the expected symbol. This method is used
by most Huffman decoder (gzip or libjpeg-turbo).

In order to use the correct key when peeking a constant number of bits
from the stream, we generate duplicates in the table. As an example, for
the code 110, all entries with that pattern 110***** will be set to
110's symbol. So, when you read this code plus garbage from following
codes, you still find the correct symbol.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
9389177e5f LibGfx/JPEG: Make generate_huffman_codes be a method of HuffmanTable
And call it when reading the table definition instead of when starting
to decode the stream.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
8cba8ed25a LibGfx/JPEG: Rename HuffmanTableSpec => HuffmanTable 2023-05-07 09:08:56 +02:00
Lucas CHOLLET
011fe0d9ba LibGfx/JPEG: Ensure capacity of vector instead of blindly appending 2023-05-07 09:08:56 +02:00
Lucas CHOLLET
f4014f898d LibGfx/JPEG: Use peek_bits in next_symbol
This allows us to read all bits in a single shot instead of one by one.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
5ec2aa4dcc LibGfx/JPEG: Introduce peek_bits and use it in read_bits
While already providing a performance improvement by removing the loop
in `read_bits`, this method was introduced to optimize `next_symbol`.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
86ce9cc30f LibGfx/JPEG: Encapsulate operations on HuffmanStream
Advantages of encapsulation are really obvious here:
- Put related code together
- Prevent external functions to modify the object
- Abstract the implementation

No functional changes intended.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
66108d102e LibGfx/JPEG: Store previous DC values in i16
Forgotten child of cfaa5120.
2023-05-07 09:08:56 +02:00
Lucas CHOLLET
a08b91e63e LibGfx/JPEG: Rename HuffmanStreamState => HuffmanStream 2023-05-07 09:08:56 +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
f0f82c6391 LibGfx/PNG: Add support for animated PNG images 2023-05-05 15:20:44 +01: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
Lucas CHOLLET
ddbe65e2f8 LibGfx/JPEG: Factorize chunk size reading 2023-04-25 06:01:39 +02:00
Lucas CHOLLET
105a40f4f6 LibGfx/JPEG: Rename qtable_id => quantization_table_id 2023-04-25 06:01:39 +02:00
Lucas CHOLLET
dd5d2e3505 LibGfx/JPEG: Support up to 4 quantization tables
We used to limit this number to two and name them with their usual
usage. The specification is however broader, and you can find files that
use more tables as in the following link:

20050519/tests/A_2_1-BF-01.htm
2023-04-25 06:01:39 +02:00
Lucas CHOLLET
f46e3e6644 LibGfx/JPEG: Add myself to the copyright header 2023-04-25 06:01:39 +02:00
Lucas CHOLLET
0d3b62cbb7 LibGfx/JPEG: Use more explicit types and add a bunch of const 2023-04-25 06:01:39 +02:00
Nico Weber
f09aaf826f LibGfx: Decrease indent in ImageDecoder::try_create_for_raw_bytes() some
No behavior change.
2023-04-23 14:35:20 +02:00
Nico Weber
cf3835b29b LibGfx/JPEG: Make non-zero-terminated APPn starts non-fatal
Necessary but not sufficient for #18456.
2023-04-21 22:09:31 -04:00
Nico Weber
66bd7cdb28 LibGfx: Move two globals into the only functions that use them
No real behavior change. (The two globals are now both initialized
at first use instead of before main(), but that should have no
observable effect. The motivation is readability.)
2023-04-21 15:55:03 +02: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
Ben Wiederhake
560133a0c6 Everywhere: Remove unused DeprecatedString includes 2023-04-09 22:00:54 +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