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.
21 #include "image-load-webp.h"
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
26 #include <webp/decode.h>
29 #include "image-load.h"
34 struct ImageLoaderWEBP : public ImageLoaderBackend
37 ~ImageLoaderWEBP() override;
39 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
40 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
41 GdkPixbuf *get_pixbuf() override;
42 gchar *get_format_name() override;
43 gchar **get_format_mime_types() override;
46 AreaUpdatedCb area_updated_cb;
52 void free_buffer(guchar *pixels, gpointer)
57 gboolean ImageLoaderWEBP::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
63 WebPBitstreamFeatures features;
64 VP8StatusCode status_code;
66 res_info = WebPGetInfo(buf, count, &width, &height);
69 log_printf("warning: webp reader error\n");
73 status_code = WebPGetFeatures(buf, count, &features);
74 if (status_code != VP8_STATUS_OK)
76 log_printf("warning: webp reader error\n");
80 if (features.has_alpha)
82 pixels = WebPDecodeRGBA(buf, count, &width, &height);
86 pixels = WebPDecodeRGB(buf, count, &width, &height);
89 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);
91 area_updated_cb(nullptr, 0, 0, width, height, data);
97 void ImageLoaderWEBP::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
99 this->area_updated_cb = area_updated_cb;
103 GdkPixbuf *ImageLoaderWEBP::get_pixbuf()
108 gchar *ImageLoaderWEBP::get_format_name()
110 return g_strdup("webp");
113 gchar **ImageLoaderWEBP::get_format_mime_types()
115 static const gchar *mime[] = {"image/webp", nullptr};
116 return g_strdupv(const_cast<gchar **>(mime));
119 ImageLoaderWEBP::~ImageLoaderWEBP()
121 if (pixbuf) g_object_unref(pixbuf);
127 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_webp()
130 return std::make_unique<ImageLoaderWEBP>();
133 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */