Fix #1240: Regression: Option to open new full-function window directly is missing
[geeqie.git] / src / image-load-webp.cc
1 /*
2  * Copyright (C) 20019 - The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "image-load-webp.h"
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <glib-object.h>
25 #include <glib.h>
26 #include <webp/decode.h>
27
28 #include "debug.h"
29 #include "image-load.h"
30
31 namespace
32 {
33
34 struct ImageLoaderWEBP : public ImageLoaderBackend
35 {
36 public:
37         ~ImageLoaderWEBP() override;
38
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;
44
45 private:
46         AreaUpdatedCb area_updated_cb;
47         gpointer data;
48
49         GdkPixbuf *pixbuf;
50 };
51
52 void free_buffer(guchar *pixels, gpointer)
53 {
54         g_free(pixels);
55 }
56
57 gboolean ImageLoaderWEBP::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
58 {
59         guint8* pixels;
60         gint width;
61         gint height;
62         gboolean res_info;
63         WebPBitstreamFeatures features;
64         VP8StatusCode status_code;
65
66         res_info = WebPGetInfo(buf, count, &width, &height);
67         if (!res_info)
68                 {
69                 log_printf("warning: webp reader error\n");
70                 return FALSE;
71                 }
72
73         status_code = WebPGetFeatures(buf, count, &features);
74         if (status_code != VP8_STATUS_OK)
75                 {
76                 log_printf("warning: webp reader error\n");
77                 return FALSE;
78                 }
79
80         if (features.has_alpha)
81                 {
82                 pixels = WebPDecodeRGBA(buf, count, &width, &height);
83                 }
84         else
85                 {
86                 pixels = WebPDecodeRGB(buf, count, &width, &height);
87                 }
88
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);
90
91         area_updated_cb(nullptr, 0, 0, width, height, data);
92
93         chunk_size = count;
94         return TRUE;
95 }
96
97 void ImageLoaderWEBP::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
98 {
99         this->area_updated_cb = area_updated_cb;
100         this->data = data;
101 }
102
103 GdkPixbuf *ImageLoaderWEBP::get_pixbuf()
104 {
105         return pixbuf;
106 }
107
108 gchar *ImageLoaderWEBP::get_format_name()
109 {
110         return g_strdup("webp");
111 }
112
113 gchar **ImageLoaderWEBP::get_format_mime_types()
114 {
115         static const gchar *mime[] = {"image/webp", nullptr};
116         return g_strdupv(const_cast<gchar **>(mime));
117 }
118
119 ImageLoaderWEBP::~ImageLoaderWEBP()
120 {
121         if (pixbuf) g_object_unref(pixbuf);
122 }
123
124 } // namespace
125
126
127 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_webp()
128 {
129         DEBUG_0("        "     );
130         return std::make_unique<ImageLoaderWEBP>();
131 }
132
133 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */