Commit graph

73 commits

Author SHA1 Message Date
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
Lucas CHOLLET
8da9ff24e4 Tests: Add tests for 12 bits JPEGs
In this commit, two tests are added, one with a `SOF1` image, the other
with a `SOF2`.
2023-05-09 07:00:15 +02:00
Nico Weber
ae7e26e095 Tests/LibGfx: Add some test coverage for animated webp decoding
Also add two FIXME comments for lossy decoding.
2023-05-07 07:08:05 +02:00
Nico Weber
9bd35fda56 ICC: Implement TRC inversion in from_pcs for parametric curves 2023-05-02 17:15:48 +01:00
Nico Weber
4169c94ebe ICC: Implement some of Profile::from_pcs()
This implements conversion from profile connection space to the
device-dependent color for matrix-based profiles.

It only does the inverse color transform but does not yet do the
inverse tone reproduction curve transform -- i.e. it doesn't
implement many cases (LUT transforms), and it does the one thing
it does implement incorrectly. But to vindicate the commit a bit,
it also does the incorrect thing very inefficiently.
2023-05-02 17:15:48 +01:00
Nico Weber
cd81b3e2fa TestICCProfile: Don't capture sRGB by value
No behavior change.
2023-04-30 05:57:20 +02:00
Nico Weber
923027b1df LibGfx: Add DeltaE() function 2023-04-30 05:57:20 +02:00
Nico Weber
adec1abf81 LibGfx: Move CIELAB to its own file 2023-04-30 05:57:20 +02:00
Nico Weber
f3dbfb85d9 ICC: Add Profile::to_lab()
This can be used to convert a profile-dependent color to the L*a*b*
color space.

(I'd like to use this to implement the DeltaE (CIE 2000) algorithm,
which is a metric for how similar two colors are perceived.
(And I'd like to use that to evaluate color conversion roundtrip
quality, once I've implemented full conversions.)
2023-04-30 00:46:11 +02:00
Nico Weber
227072a5af ICC: Rename XYZ and XYZNumber fields to uppercase
Given that XYZ and xyz are distinct things, let's use the correct
case for these member variables.

No behavior change.
2023-04-29 06:49:36 +02:00
Nico Weber
de7a413a9f ICC: Add a test for Profile::to_pcs 2023-04-29 06:49:36 +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
f2efb97578 Tests: Add webp lossless test with color index and < 16 colors
For the test files, I opened Base/res/icons/catdog/alert.png in Adobe
Photoshop 2023, used Image->Mode->Index Color...->
Palette: Local (Perceptive) to reduce the number of colors to 13, 8, and
3 with transparency, and 2 without transparency, then converted it back
to Image->Mode->RGB Color (else it can't be saved as webp), then
File->Save a Copy... to save a WebP (mode lossless) for every palette
size.
2023-04-08 19:24:13 +02:00
Nico Weber
6151a251f5 Tests: Add test for lossless webp file using a color indexing tranform
The image is https://quakewiki.org/wiki/File:Qpalette.png in lossless
webp format with a color indexing transform.

I've created Qpalette.webp by running

    examples/cwebp -z 0 ~/src/serenity/tmp.ppm -o Qpalette.webp

built at libwebp webmproject/libwebp@0825faa4c1 (without
png support, so I first ran

    Build/lagom/image ~/Downloads/Qpalette.png -o tmp.ppm

to convert it from png to a format my cwebp binary could read).

This file also happens to explicitly set max_symbol, so it serves
as a test for that code path as well.
2023-04-08 16:50:40 +02:00
Nico Weber
61b540e737 Tests: Add another test for webp lossless decoding 2023-04-07 20:49:39 +02:00
Nico Weber
09dd9c4fad Tests: Add test for webp lossless decoding 2023-04-05 13:24:00 +02:00
Lucas CHOLLET
2812ef246d Tests: Add benchmarks for JPEGLoader
I'm the author of the image.
2023-04-03 20:58:49 +01:00
Lucas CHOLLET
62290d57f7 Tests: Add a test for SOF2 JPEGs with successive approximations
This image was generated using `cjpeg` with the following scan file:

0 1 2: 0 0 0 2;
0: 1 63 0 1;
1: 1 63 0 1;
2: 1 63 0 1;
0 1 2: 0 0 2 1;
0: 1 63 1 0;
1: 1 63 1 0;
2: 1 63 1 0;
0 1 2: 0 0 1 0;
2023-04-03 17:06:21 +01:00
Lucas CHOLLET
3f9c5af553 LibGfx/JPEG: More support for scans with a single component
Introduced in 2c98eff, support for non-interleaved scans was not working
for frames with a number of MCU per line or column that is odd. Indeed,
the decoder assumed that they have scans that include a fabricated MCU
like scans with multiple components.

This patch makes the decoder handle images with a number of MCU per line
or column that is odd. To do so, as in the current decoder state we do
not know if components are interleaved at allocation time, we skip over
falsely-created macroblocks when filling them. As stated in 2c98eff,
this is probably not a good solution and a whole refactor will be
welcome.

It also comes with a test that open a square image with a side of 600px,
meaning 75 MCUs.
2023-03-25 21:31:21 +01:00
Lucas CHOLLET
496b7ffb2b LibGfx: Move all image loaders and writers to a subdirectory 2023-03-21 22:39:25 +01:00
Tim Ledbetter
d5c913082b Tests: Add tests to verify alpha values are premultiplied when scaling 2023-03-21 00:29:33 +01:00
Nico Weber
b3a0534e02 LibGfx: Add test for serialized bytes of built-in sRGB profile 2023-03-16 09:20:48 -04:00
Lucas CHOLLET
af58f012be Tests: Add a test for JPEGs with RGB components 2023-03-10 22:22:36 +01:00
Lucas CHOLLET
7ddf9e9177 Tests: Replace test image with my own creation 2023-03-04 23:39:41 +00:00
Lucas CHOLLET
68cd6f5614 Tests: Add a test for SOF2 images with only spectral selection
You can generate one by using `cjpeg` with the -scan argument.

This image has been generated with the following scan file:
0 1 2: 0 0 0 0;
0: 1 9 0 0;
2: 1 63 0 0 ;
1: 1 63 0 0 ;
0: 10 63 0 0;
2023-03-04 23:39:41 +00:00
Lucas CHOLLET
8c1a409263 Tests: Add a test for SOF0 images with several scans
This type of image isn't common, and you can probably only find one by
generating it yourself. It can be done using `cjpeg` with the -scan
argument.

This image has been generated with the following scan file:
0: 0 63 0 0;
1: 0 63 0 0;
2: 0 63 0 0;
2023-02-27 13:39:22 +01:00
Lucas CHOLLET
a40c7354c1 Tests: Rename "test_jpg" to "test_jpeg_sof0_one_scan" 2023-02-27 13:39:22 +01:00
MacDue
6cf8eeb7a4 LibGfx: Return bool not ErrorOr<bool> from ImageDecoderPlugin::sniff()
Nobody made use of the ErrorOr return value and it just added more
chance of confusion, since it was not clear if failing to sniff an
image should return an error or false. The answer was false, if you
returned Error you'd crash the ImageDecoder.
2023-02-26 19:43:17 +01:00
Nico Weber
fa34832297 LibGfx: Implement WebPImageDecoderPlugin::loop_count()
Turns out extended-lossless-animated.webp did have a loop count of 0.
So I opened it in Hex Fiend and changed the byte at position 42
(which is the first byte of the little-endian u16 storing the loop
count) to 0x2A, so that the test can compare the loop count to something
not 0.
2023-02-26 15:54:22 +01:00
Nico Weber
3c5450b8be LibGfx: Implement is_animated() and frame_count() for webp plugin 2023-02-26 15:54:22 +01:00
Nico Weber
d66f143fb7 Tests: Add webp size decoding tests for webp 2023-02-26 12:21:40 +01:00
Nico Weber
3cfcd2397b Tests: Add one more webp test file
This is "Lossless animated WebP (5 KB) 4" from
https://developers.google.com/speed/webp/faq
2023-02-26 12:21:40 +01:00
Nico Weber
1bb3abd9ab Tests: Add one more webp test file
This is Discord's reencoded version of my profile picture,
which is GitHub's auto-generated avatar for my github profile.
2023-02-26 12:21:40 +01:00
Nico Weber
889a02f9df Tests: Add one more webp test file
It's a simple lossless file from:
https://developers.google.com/speed/webp/gallery2#webp_links

> "baby tux for my user page"
> Image Author: Fizyplankton
> This file is in the public domain.
2023-02-26 12:21:40 +01:00
Nico Weber
0190be9788 Tests: Use MUST more in TestImageDecoder
No behavior change.
2023-02-26 01:15:10 +01:00
Timothy Flynn
34567bc145 LibGfx: Remove single-code point Font::glyph_or_emoji_width API
All callers are now aware of multi-code point emoji (and must remain so
going forward).
2023-02-24 20:28:23 +01:00
Nico Weber
14c0bae704 LibGfx+Tests: Add test for webp ICC loading and fix bug
I drew the two webp files in Photoshop and saved them using the
"Save a Copy..." dialog, with ICC profile and all other boxes checked.

(I also tried saving with all the boxes unchecked, but it still wrote an
extended webp instead of a basic file.)

The lossless file exposed a bug: I didn't handle chunk padding
correctly before this patch.
2023-02-24 20:13:52 +01:00
Nico Weber
2c8b3fed71 Tests: Add a test for Gfx::ICC::encode()
The test verifies that loading an icc file and serializing it
again produces exactly the same output as the input. That's not
always the case, but often. It requires the input file either
not having any padding or using null bytes as padding, it
requires the input file putting tag data in the order the
tag data is referenced in in the tag table, and it requires the
input file only using known tag types (which at the moment
means it only works for v4 profiles, but that part will change
in the future).

The new file p3-v4.icc was extracted from a jpeg taken by an
iPhone Mini.
2023-02-19 08:12:04 +00:00
Lucas CHOLLET
856d0202f2 LibGfx: Rename JPGLoader to JPEGLoader
The patch also contains modifications on several classes, functions or
files that are related to the `JPGLoader`.

Renaming include:
 - JPGLoader{.h, .cpp}
 - JPGImageDecoderPlugin
 - JPGLoadingContext
 - JPG_DEBUG
 - decode_jpg
 - FuzzJPGLoader.cpp
 - Few string literals or texts
2023-02-18 23:56:24 +01:00
Aliaksandr Kalenik
1f4106842d LibGfx: Pass font width to FontDatabase::get()
Width need to be passed to `FontDatabase::get()` to resolve font name
unambiguously.
2023-02-05 08:06:06 +00:00
Nico Weber
fb79fc0ba6 Tests: Add a basic ICC profile test
icc-v4.jpg is Meta/Websites/serenityos.org/happy/3rd/bgianf.jpg.
There are a whole bunch of jpgs with v4 color profiles and I just picked
one fairly arbitrarily. It looks like a fairly standard v4 matrix
profile that in this form is also present in many jpgs taken by mobile
phone cameras. It uses parametric curves.

icc-v2.png is based on ./Documentation/WebServer_localhost.jpg since
that is the only image in the repo with a v2 color profile. It also has
all kinds of interesting and somewhat exotic tags, such as an 'dscm' (an
Apple extension to have a description of type 'mluc', since normal
'desc' is required ot have type 'desc' in v2 files -- in v4, 'desc' has
type 'mluc') tag of type 'mluc' that actually contains data in several
languages and that exercises the non-BMP UTF-16BE decoder. It's however
still also a fairly standard v2 matrix profile, which uses 'curv'
instead of 'para' for its curves ('para' is v4-only).

I converted that jpeg file to png, and cropped most of the image
data to save on file size by running:

    sips -s format png --cropToHeightWidth 21 42 in.jpg --out out.png
2023-02-01 19:19:30 +01:00
Nico Weber
c8832807d6 LibGfx+Tests: Remove code unnecessary after 9e7c16d0a4 2023-02-01 08:56:56 -05:00
Nico Weber
bea3f3fc46 LibGfx: Move TestImageDecoder over to input file approach in 8cfabbcd93
Rather than reading files out of /res, put them in a subfolder of
Tests/LibGfx/ and pick the path based on AK_OS_SERENITY.

That way, the tests can also pass when run under lagom.

(I just `cp`d all the files that the test previously read from
random places into Tests/LibGfx/test-inputs.)
2023-02-01 08:56:56 -05:00
Nico Weber
b8b5e0f680 LibGfx: Move TestFontHandling over to input file approach in 8cfabbcd93
Rather than reading files out of /res, put them in a subfolder of
Tests/LibGfx/ and pick the path based on AK_OS_SERENITY.

That way, the tests can also pass when run under lagom.
2023-02-01 08:56:56 -05:00
Nico Weber
f4c1269d4c Tests: Modernize TestImageDecoder a bit
- Use MUST() instead of checking plugin_decoder_or_error.is_error()
- Use MappedFile::bytes()
- Don't use EXPECT_EQ when comparing to fixed bools

No intended behavior change.
2023-01-29 22:37:37 +00:00
Nico Weber
10ae570ac5 Tests: Make LibGfx tests not depend on LibGUI
As far as I can tell, that's not needed and never was.
2023-01-29 22:36:13 +00:00
Tim Schumacher
82a152b696 LibGfx: Remove try_ prefix from bitmap creation functions
Those don't have any non-try counterpart, so we might as well just omit
it.
2023-01-26 20:24:37 +00:00
Liav A
20033d06d8 Tests/LibGfx: Fix test_gif test case
We should expect the GIF image to be animated, therefore fix that
condition.
2023-01-20 17:05:09 +00:00
Liav A
da525ccc43 Tests/LibGfx: Fix test_not_ico test case
We should expect the sniffing method and the initialize method to fail
because this test case is testing that the ICO image decoder should not
decode random data within it.
2023-01-20 17:05:09 +00:00
Liav A
57e19a7e56 LibGfx: Re-structure the whole initialization pattern for image decoders
When trying to figure out the correct implementation, we now have a very
strong distinction on plugins that are well suited for sniffing, and
plugins that need a MIME type to be chosen.

Instead of having multiple calls to non-static virtual sniff methods for
each Image decoding plugin, we have 2 static methods for each
implementation:
1. The sniff method, which in contrast to the old method, gets a
    ReadonlyBytes parameter and ensures we can figure out the result
    with zero heap allocations for most implementations.
2. The create method, which just creates a new instance so we don't
    expose the constructor to everyone anymore.

In addition to that, we have a new virtual method called initialize,
which has a per-implementation initialization pattern to actually ensure
each implementation can construct a decoder object, and then have a
correct context being applied to it for the actual decoding.
2023-01-20 15:13:31 +00:00