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