mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
LibGfx+image: Add scaffolding for writing webp files
This commit is contained in:
parent
65d166e570
commit
d04f9cf3c8
Notes:
sideshowbarker
2024-07-18 01:43:16 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/d04f9cf3c8 Pull-request: https://github.com/SerenityOS/serenity/pull/24212 Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/trflynn89 ✅
5 changed files with 53 additions and 1 deletions
|
@ -96,6 +96,7 @@ shared_library("LibGfx") {
|
|||
"ImageFormats/WebPLoader.cpp",
|
||||
"ImageFormats/WebPLoaderLossless.cpp",
|
||||
"ImageFormats/WebPLoaderLossy.cpp",
|
||||
"ImageFormats/WebPWriter.cpp",
|
||||
"ImmutableBitmap.cpp",
|
||||
"Painter.cpp",
|
||||
"Palette.cpp",
|
||||
|
|
|
@ -69,6 +69,7 @@ set(SOURCES
|
|||
ImageFormats/WebPLoader.cpp
|
||||
ImageFormats/WebPLoaderLossless.cpp
|
||||
ImageFormats/WebPLoaderLossy.cpp
|
||||
ImageFormats/WebPWriter.cpp
|
||||
ImmutableBitmap.cpp
|
||||
Painter.cpp
|
||||
Palette.cpp
|
||||
|
|
16
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp
Normal file
16
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Nico Weber <thakis@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/ImageFormats/WebPWriter.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
ErrorOr<void> WebPWriter::encode(Stream&, Bitmap const&, Options const&)
|
||||
{
|
||||
return Error::from_string_literal("WebP encoding not yet implemented");
|
||||
}
|
||||
|
||||
}
|
29
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.h
Normal file
29
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Nico Weber <thakis@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
struct WebPEncoderOptions {
|
||||
Optional<ReadonlyBytes> icc_data;
|
||||
};
|
||||
|
||||
class WebPWriter {
|
||||
public:
|
||||
using Options = WebPEncoderOptions;
|
||||
|
||||
// Always lossless at the moment.
|
||||
static ErrorOr<void> encode(Stream&, Bitmap const&, Options const& = {});
|
||||
|
||||
private:
|
||||
WebPWriter() = delete;
|
||||
};
|
||||
|
||||
}
|
|
@ -14,6 +14,7 @@
|
|||
#include <LibGfx/ImageFormats/PNGWriter.h>
|
||||
#include <LibGfx/ImageFormats/PortableFormatWriter.h>
|
||||
#include <LibGfx/ImageFormats/QOIWriter.h>
|
||||
#include <LibGfx/ImageFormats/WebPWriter.h>
|
||||
|
||||
using AnyBitmap = Variant<RefPtr<Gfx::Bitmap>, RefPtr<Gfx::CMYKBitmap>>;
|
||||
struct LoadedImage {
|
||||
|
@ -176,6 +177,10 @@ static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, bool pp
|
|||
TRY(Gfx::PortableFormatWriter::encode(*TRY(stream()), *frame, { .format = format }));
|
||||
return {};
|
||||
}
|
||||
if (out_path.ends_with(".webp"sv, CaseSensitivity::CaseInsensitive)) {
|
||||
TRY(Gfx::WebPWriter::encode(*TRY(stream()), *frame, { .icc_data = image.icc_data }));
|
||||
return {};
|
||||
}
|
||||
|
||||
ByteBuffer bytes;
|
||||
if (out_path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) {
|
||||
|
@ -185,7 +190,7 @@ static ErrorOr<void> save_image(LoadedImage& image, StringView out_path, bool pp
|
|||
} else if (out_path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) {
|
||||
bytes = TRY(Gfx::QOIWriter::encode(*frame));
|
||||
} else {
|
||||
return Error::from_string_view("can only write .bmp, .jpg, .png, .ppm, and .qoi"sv);
|
||||
return Error::from_string_view("can only write .bmp, .jpg, .png, .ppm, .qoi, and .webp"sv);
|
||||
}
|
||||
TRY(TRY(stream())->write_until_depleted(bytes));
|
||||
|
||||
|
|
Loading…
Reference in a new issue