LibGfx+image: Add scaffolding for writing webp files

This commit is contained in:
Nico Weber 2024-05-03 15:05:09 -04:00 committed by Andreas Kling
parent 65d166e570
commit d04f9cf3c8
5 changed files with 53 additions and 1 deletions

View file

@ -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",

View file

@ -69,6 +69,7 @@ set(SOURCES
ImageFormats/WebPLoader.cpp
ImageFormats/WebPLoaderLossless.cpp
ImageFormats/WebPLoaderLossy.cpp
ImageFormats/WebPWriter.cpp
ImmutableBitmap.cpp
Painter.cpp
Palette.cpp

View 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");
}
}

View 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;
};
}

View file

@ -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));