Deduplicate cr3 image loader
[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 using ImageLoaderWEBP = struct _ImageLoaderWEBP;
35 struct _ImageLoaderWEBP {
36         ImageLoaderBackendCbAreaUpdated area_updated_cb;
37         ImageLoaderBackendCbSize size_cb;
38         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
39         gpointer data;
40         GdkPixbuf *pixbuf;
41         guint requested_width;
42         guint requested_height;
43         gboolean abort;
44 };
45
46 static void free_buffer(guchar *pixels, gpointer)
47 {
48         g_free(pixels);
49 }
50
51 static gboolean image_loader_webp_load(gpointer loader, const guchar *buf, gsize count, GError **)
52 {
53         auto *ld = (ImageLoaderWEBP *) loader;
54         guint8* data;
55         gint width;
56         gint height;
57         gboolean res_info;
58         WebPBitstreamFeatures features;
59         VP8StatusCode status_code;
60
61         res_info = WebPGetInfo(buf, count, &width, &height);
62         if (!res_info)
63                 {
64                 log_printf("warning: webp reader error\n");
65                 return FALSE;
66                 }
67
68         status_code = WebPGetFeatures(buf, count, &features);
69         if (status_code != VP8_STATUS_OK)
70                 {
71                 log_printf("warning: webp reader error\n");
72                 return FALSE;
73                 }
74
75         if (features.has_alpha)
76                 {
77                 data = WebPDecodeRGBA(buf, count, &width, &height);
78                 }
79         else
80                 {
81                 data = WebPDecodeRGB(buf, count, &width, &height);
82                 }
83
84         ld->pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, features.has_alpha, 8, width, height, width * (features.has_alpha ? 4 : 3), free_buffer, nullptr);
85
86         ld->area_updated_cb(loader, 0, 0, width, height, ld->data);
87
88         return TRUE;
89 }
90
91 static gpointer image_loader_webp_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
92 {
93         auto *loader = g_new0(ImageLoaderWEBP, 1);
94         loader->area_updated_cb = area_updated_cb;
95         loader->size_cb = size_cb;
96         loader->area_prepared_cb = area_prepared_cb;
97         loader->data = data;
98
99         return (gpointer) loader;
100 }
101
102 static void image_loader_webp_set_size(gpointer loader, int width, int height)
103 {
104         auto *ld = (ImageLoaderWEBP *) loader;
105         ld->requested_width = width;
106         ld->requested_height = height;
107 }
108
109 static GdkPixbuf* image_loader_webp_get_pixbuf(gpointer loader)
110 {
111         auto *ld = (ImageLoaderWEBP *) loader;
112         return ld->pixbuf;
113 }
114
115 static gchar* image_loader_webp_get_format_name(gpointer)
116 {
117         return g_strdup("webp");
118 }
119
120 static gchar** image_loader_webp_get_format_mime_types(gpointer)
121 {
122         static const gchar *mime[] = {"image/webp", nullptr};
123         return g_strdupv(const_cast<gchar **>(mime));
124 }
125
126 static gboolean image_loader_webp_close(gpointer, GError **)
127 {
128         return TRUE;
129 }
130
131 static void image_loader_webp_abort(gpointer loader)
132 {
133         auto *ld = (ImageLoaderWEBP *) loader;
134         ld->abort = TRUE;
135 }
136
137 static void image_loader_webp_free(gpointer loader)
138 {
139         auto *ld = (ImageLoaderWEBP *) loader;
140         if (ld->pixbuf) g_object_unref(ld->pixbuf);
141         g_free(ld);
142 }
143
144 void image_loader_backend_set_webp(ImageLoaderBackend *funcs)
145 {
146 DEBUG_0("        "     );
147         funcs->loader_new = image_loader_webp_new;
148         funcs->set_size = image_loader_webp_set_size;
149         funcs->load = image_loader_webp_load;
150         funcs->write = nullptr;
151         funcs->get_pixbuf = image_loader_webp_get_pixbuf;
152         funcs->close = image_loader_webp_close;
153         funcs->abort = image_loader_webp_abort;
154         funcs->free = image_loader_webp_free;
155         funcs->get_format_name = image_loader_webp_get_format_name;
156         funcs->get_format_mime_types = image_loader_webp_get_format_mime_types;
157 }
158
159 #endif
160 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */