Commit graph

52 commits

Author SHA1 Message Date
Asutosh Variar
229b64a4b7 Everywhere: Convert from_string_view -> from_string_literal where static 2024-09-11 10:59:04 +01:00
Andreas Kling
681a2ac14e LibGfx: Remove support for the various "portable" image formats
These formats are not supported by other browsers.
2024-06-17 21:57:35 +02:00
Andreas Kling
4b4254c3d0 LibGfx: Remove QOI image format support
This format is not supported by other browsers.
2024-06-17 21:57:35 +02:00
Andreas Kling
2a888ca626 LibGfx: Remove home-grown JPEG codec in favor of libjpeg-turbo 2024-06-17 17:59:54 +02:00
Nico Weber
8a21992030 image: Add a --webp-allowed-transforms switch
This is mainly useful for testing.
2024-05-31 22:39:25 +02:00
Lucas CHOLLET
cd486a7040 image: Support exporting GIF files 2024-05-14 12:33:53 -06:00
Nico Weber
d04f9cf3c8 LibGfx+image: Add scaffolding for writing webp files 2024-05-06 17:32:19 +02:00
Nico Weber
1ae174e380 image: Add a --crop option
You can now run

    image -o out.png Tests/LibGfx/test-inputs/bmp/bitmap.bmp \
        --crop 130,86,108,114

and end up with the nose part of that image in out.png.
2024-03-20 13:58:23 +01:00
Nico Weber
2e2cae26c6 LibGfx+Fallout: Make ImageDecoder return ErrorOr
...from try_create_for_raw_bytes().

If a plugin returns `true` from sniff but then fails when calling
its `create()` method, we now no longer swallow that error.

Allows `image` (and other places in the system) to print a more
actionable error if early image headers are invalid.

(We now no longer try to find another plugin that can also handle
the image.)

Fixes a regression from #20063 / #19893 -- before then, we didn't
do fallible work this early.
2024-03-07 11:20:06 -05:00
Nico Weber
c4780f4ee1 image: Add an --invert-cmyk option
This is useful for working with CMYK jpegs extracted from PDFs
by mutool. CMYK channels for jpegs in PDFs are inverted compared to
CMYK channels in standalone jpegs (!), and mutool doesn't compensate
for that.

You can now do:

    mutool extract  ~/Downloads/0000/0000711.pdf 461

Followed by:

    Build/lagom/bin/image -o out.png \
        --invert-cmyk \
        --assign-color-profile \
            Build/lagom/Root/res/icc/Adobe/CMYK/USWebCoatedSWOP.icc \
        --convert-to-color-profile serenity-sRGB.icc \
        cmyk.jpg

Doesn't exactly roll off the keyboard, but at least it's possible.

(We should probably add an implicit default CMYK color profile if
the input file doesn't have one, and assume conversion to sRGB when
saving to a format that can only store RGB.)
2024-02-04 21:34:53 +01:00
Nico Weber
fa3fead7a4 image: Move ppm_ascii down a bit
It belongs close to `quality` since it needed at output time.

No behavior change (except for `--help` output order).
2024-02-04 21:34:53 +01:00
Nico Weber
eeb0d6f52b image: Remove "do_" prefix on two function names
These were here due to the prefix-less name conflicting with a local
bool, but now that options are in a struct that's no longer a problem.

No behavior change.
2024-02-02 07:20:02 +01:00
Nico Weber
d575d4fb9b image: Extract option parsing to separate function
No behavior change.
2024-02-02 07:20:02 +01:00
Nico Weber
6dc5608d95 image: Allow saving CMYK jpegs
`image` can't transform from RGB to CMYK yet, so the only way to
do this is by having a CMYK input.

For example, this works now (and it does a full decode and re-encode):

    Build/lagom/bin/image -o out.jpg \
        Tests/LibGfx/test-inputs/jpg/buggie-cmyk.jpg

Using this to convert `.pam` files written by `mutool extract`
to jpegs is a somewhat convenient method of looking at these
.pam files.
2024-02-02 07:19:18 +01:00
Nico Weber
2298aac1d1 image: Only create output file for known extensions
Previously `image -o foo.asdf foo.png` would create a 0-byte foo.asdf
before complaining that it doesn't know how to write .asdf images.

(Lifetimes of temporaries are extended to the end of the full
expresion, so this is fine.)
2024-02-02 07:19:18 +01:00
Nico Weber
db4982bd29 LibGfx+image: Implement writing ICC profiles to jpeg files 2024-01-26 14:03:20 -05:00
Nico Weber
fac48fdda4 image: Allow converting CMYK inputs to RGB
This worked in PDFs for a while, but now `image` can also do
CMYK->RGB conversions that actually use color profiles :^)
2024-01-25 15:53:44 +01:00
Nico Weber
0acc370f7b image: Load images that contain CMYK data into a CMYKBitmap
Every single of images's functions then go ahead and error out on
CMYKBitmaps for now.

(Alternatively, we could make them the low-quality CMYK->RGB
conversion that previously happened implicitly, but let's go
with this for now.)

(With this structure, we could also do something smarter for
Vector images in the future.)

Here's how this looks:

    % Build/lagom/bin/image \
        -o out.png Tests/LibGfx/test-inputs/jpg/ycck-2111.jpg
    Runtime error: Can't save CMYK bitmaps yet, convert to RGB first
                   with --convert-to-color-profile

    % Build/lagom/bin/image \
        --convert-to-color-profile serenity-sRGB.icc \
        -o out.png Tests/LibGfx/test-inputs/jpg/ycck-2111.jpg
    Runtime error: No source color space embedded in image.
                   Pass one with --assign-color-profile.

    % Build/lagom/bin/image \
        --assign-color-profile path/to/CMYK/USWebCoatedSWOP.icc \
        --convert-to-color-profile serenity-sRGB.icc \
        -o out.png Tests/LibGfx/test-inputs/jpg/ycck-2111.jpg
    Runtime error: Psych, can't convert CMYK bitmaps yet either

As usual, serenity-sRGB.icc is from
`Build/lagom/bin/icc  -n sRGB --reencode-to serenity-sRGB.icc` and
the adobe cmyk profiles are available at
https://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html
which ultimately leads to:
https://download.adobe.com/pub/adobe/iccprofiles/win/AdobeICCProfilesCS4Win_end-user.zip
2024-01-25 15:53:44 +01:00
Nico Weber
2c40886851 image: Update an error string
We have been able to write .jpg files for a while now.
2024-01-25 15:53:44 +01:00
Nico Weber
8a56abf48b image: Move image writers up that don't return bytes
Makes the early returns a bit easier to see.

Also alphabetize them while touching these lines.

No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
96c71b7042 image: Move image loading code into a separate function
Haters will say this is overkill, but this function will do a bit more
once we allow loading CMYK data.

No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
653b614611 image: Move image saving code into helper function
No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
0681df7f3b image: Move profile conversion code into helper function
No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
48a0fb1e8e image: Introduce a struct "LoadedImage" to hold bitmap and icc data
This makes it easier to move the color space conversion code out
into a helper function, and will be used to pass around CMYK bitmaps.

No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
a339f297d1 image: Move image modification code to helper functions
No behavior change.
2024-01-25 15:53:44 +01:00
Nico Weber
da243ebb8b image: Use Error::from_string_view() for errors
...instead of warn() + manual return.

Before:

    % Build/lagom/bin/image -o out.asdf in.png
    can only write .bmp, .png, .ppm, and .qoi

Now:

    % Build/lagom/bin/image -o out.asdf in.png
    Runtime error: can only write .bmp, .png, .ppm, and .qoi

That doesn't look worse, and it's less code (and makes moving the
code into helper functions easier).
2024-01-25 15:53:44 +01:00
Nico Weber
7d796c83f1 image: Fix flag names and grammar in diagnostic texts 2024-01-11 08:00:27 +00:00
Andreas Kling
a396bb0c0b LibGfx: Remove indexed palette formats from Bitmap and Painter
Nobody was actually using these formats anymore, and this simplifies
and shrinks the code. :^)
2023-10-12 07:39:05 +02:00
kleines Filmröllchen
062e0db46c LibCore: Make MappedFile OwnPtr-based
Since it will become a stream in a little bit, it should behave like all
non-trivial stream classes, who are not primarily intended to have
shared ownership to make closing behavior more predictable. Across all
uses of MappedFile, there is only one use case of shared mapped files in
LibVideo, which now uses the thin SharedMappedFile wrapper.
2023-09-27 03:22:56 +02:00
Lucas CHOLLET
2e474e8c18 image: Add an option to tweak the quality of output JPEG 2023-07-04 00:01:06 +02:00
Lucas CHOLLET
b7c30f3096 image: Support JPEG as an output format 2023-06-22 21:13:04 +02:00
Nico Weber
0db8ac7465 image: Add a --move-alpha-to-rgb flag
I didn't put this as a method on Bitmap since it doesn't seem generally
useful.  Easy to move the impl over to Bitmap in the future if we want
to use it elsewhere.
2023-06-14 08:30:06 +02:00
Nico Weber
ca35b5d767 image: Add a --strip-alpha flag
It can be used to look at an input image without its alpha channel.
2023-06-14 08:30:06 +02:00
Nico Weber
5934c4ebfb image: Add --no-output flag to omit output writing
This is useful for timing just decoder performance.

(It'd be nice to add a `-t` flag that prints timings for all
the different phases, but for now just disabling writing is
sufficient for me.)
2023-05-24 16:01:09 +02:00
Nico Weber
32ad77b8c7 image: Remove no-longer-needed comment
The future is now, and Bitmap::load_from_file() can't do the things that
`image` now does.
2023-05-24 16:01:09 +02:00
Lucas CHOLLET
d4d3c3f262 LibGfx/PortableFormat+image: Make encode take a Stream
As we directly write to the stream, we don't need to store a copy of the
entire image in memory. However, writing to a stream is heavier on the
CPU than to a ByteBuffer. This commit unfortunately makes `add_pixels`
two times slower.
2023-05-09 11:18:46 +02:00
Lucas CHOLLET
2a91245a96 image: Initialize strip_color_profile with a default value 2023-05-09 11:18:46 +02:00
Nico Weber
40e839fd7e image: Add a --frame-index option 2023-05-06 21:17:18 +02:00
Nico Weber
926c0d8676 ICC+image: Add conversion between color spaces for images :^)
For now, only for color spaces that are supported by Profile::to_pcs()
and Profile::from_pcs(), which currently means that all matrix profiles
(but not LUT profiles) in the source color space work, and that
matrix profiles with parametric curves in the destination color
space work.

This adds Profile::convert_image(Bitmap, source_profile), and
adds a `--convert-to-color-profile file.icc` flag to `image`.

It only takes a file path, so to use it with the built-in
sRGB profile, you have to write it to a file first:

% Build/lagom/icc -n sRGB --reencode-to serenity-sRGB.icc

`image` by default writes the source image's color profile
to the output image, and most image viewers display images
looking at the profile.

For example, take `Seven_Coloured_Pencils_(rg-switch_sRGB).jpg`
from https://commons.wikimedia.org/wiki/User:Colin/BrowserTest.

It looks normal in image viewers because they apply the unusual
profile embedded in the profile. But if you run

% Build/lagom/image -o huh.png --strip-color-profile \
    'Seven_Coloured_Pencils_(rg-switch_sRGB).jpeg'

and then look at huh.png, you can see how the image's colors
look like when interpreted as sRGB (which is the color space
PNG data is in if the PNG doesn't store an embedded profile).

If you now run

% Build/lagom/image -o wow.png \
    --convert-to-color-profile serenity-sRGB.icc --strip-color-profile \
    'Seven_Coloured_Pencils_(rg-switch_sRGB).jpeg'

this will convert that image to sRGB, but then not write
the profile to the output image (verify with `Build/lagom/icc wow.png`).
It will look correct in image viewers, since they display PNGs without
an embedded color profile as sRGB.

(This works because 'Seven_Coloured_Pencils_(rg-switch_sRGB).jpeg'
contains a matrix profile, and Serenity's built-in sRGB profile
uses a matrix profile with a parametric curve.)
2023-05-03 15:05:13 +02:00
Lucas CHOLLET
496b7ffb2b LibGfx: Move all image loaders and writers to a subdirectory 2023-03-21 22:39:25 +01:00
Nico Weber
3a7257c9fe image: Don't just crash when the input file can't be decoded
If the input file didn't exist at all, the TRY(MappedFile::map())
gives us a (cryptic) error message, but if it existed but wasn't an
image file, we would crash. Now we print a message instead.
2023-03-16 10:18:44 -04:00
Nico Weber
0591aa1d96 image: Add --assign-color-profile= flag
This allows assigning a color profile from a .icc file to the output.
No pixel data conversion is taking place: the output will just contain
this profile, so it better matches the image data already.
2023-03-16 10:18:44 -04:00
Nico Weber
e05bb47134 image: Add --strip-color-profile flag
With this flag, no color profile is copied from the source image
to the destination image.
2023-03-16 10:18:44 -04:00
Nico Weber
871a528f4d image: Preserve ICC profiles in BMP output 2023-03-15 13:54:09 +00:00
Nico Weber
76d8e5d866 image: Make the ppm writing code a tiny bit shorter 2023-03-15 11:02:41 +00:00
Nico Weber
fbc70eca93 image: Preserve ICC profiles in PNG output
This probably does strange things for CMYK jpegs, since JPEGLoader
converts those from CMYK to RGB but the ICC profile is still an CMYK
profile. The Right Fix for that is probably for JPEGLoader to consume
the profile when it does CMYK->RGB conversion and then not hand out
the profile data. (Or we could add a CMYK bitmap type.)

But most of the time, this is a progression :^)
2023-03-15 11:02:41 +00:00
Tim Schumacher
ecd1862859 AK: Rename Stream::write_entire_buffer to Stream::write_until_depleted
No functional changes.
2023-03-13 15:16:20 +00:00
Lucas CHOLLET
516d2f4892 image: Add an argument to choose to write PPMs in binary or in ASCII 2023-03-13 15:15:41 +00:00
Lucas CHOLLET
13d1721852 image: Support writing to PPM files 2023-03-13 15:15:41 +00:00
Nico Weber
f1a3028ef1 LibGfx: Change BMPWriter API to be consistent with other image writers 2023-03-12 21:32:21 +01:00