Explorar el Código

LibGfx+image: Add scaffolding for writing webp files

Nico Weber hace 1 año
padre
commit
d04f9cf3c8

+ 1 - 0
Meta/gn/secondary/Userland/Libraries/LibGfx/BUILD.gn

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

+ 1 - 0
Userland/Libraries/LibGfx/CMakeLists.txt

@@ -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 - 0
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp

@@ -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 - 0
Userland/Libraries/LibGfx/ImageFormats/WebPWriter.h

@@ -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;
+};
+
+}

+ 6 - 1
Userland/Utilities/image.cpp

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