2 * Copyright (C) 20019 - The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "image-load-webp.h"
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib-object.h>
29 #include <webp/decode.h>
32 #include "image-load.h"
37 struct ImageLoaderWEBP : public ImageLoaderBackend
40 ~ImageLoaderWEBP() override;
42 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
43 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
44 GdkPixbuf *get_pixbuf() override;
45 gchar *get_format_name() override;
46 gchar **get_format_mime_types() override;
49 AreaUpdatedCb area_updated_cb;
55 void free_buffer(guchar *pixels, gpointer)
60 gboolean ImageLoaderWEBP::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
66 WebPBitstreamFeatures features;
67 VP8StatusCode status_code;
69 res_info = WebPGetInfo(buf, count, &width, &height);
72 log_printf("warning: webp reader error\n");
76 status_code = WebPGetFeatures(buf, count, &features);
77 if (status_code != VP8_STATUS_OK)
79 log_printf("warning: webp reader error\n");
83 if (features.has_alpha)
85 pixels = WebPDecodeRGBA(buf, count, &width, &height);
89 pixels = WebPDecodeRGB(buf, count, &width, &height);
92 pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, features.has_alpha, 8, width, height, width * (features.has_alpha ? 4 : 3), free_buffer, nullptr);
94 area_updated_cb(nullptr, 0, 0, width, height, data);
100 void ImageLoaderWEBP::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
102 this->area_updated_cb = area_updated_cb;
106 GdkPixbuf *ImageLoaderWEBP::get_pixbuf()
111 gchar *ImageLoaderWEBP::get_format_name()
113 return g_strdup("webp");
116 gchar **ImageLoaderWEBP::get_format_mime_types()
118 static const gchar *mime[] = {"image/webp", nullptr};
119 return g_strdupv(const_cast<gchar **>(mime));
122 ImageLoaderWEBP::~ImageLoaderWEBP()
124 if (pixbuf) g_object_unref(pixbuf);
130 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_webp()
133 return std::make_unique<ImageLoaderWEBP>();
137 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */