Let image loader backend decide how to process image buffer
[geeqie.git] / src / image-load-heif.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-heif.h"
22
23 #include <config.h>
24
25 #if HAVE_HEIF
26 #include <vector>
27
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
30 #include <glib.h>
31 #include <libheif/heif.h>
32
33 #include "debug.h"
34 #include "image-load.h"
35
36 namespace
37 {
38
39 struct ImageLoaderHEIF {
40         ImageLoaderBackendCbAreaUpdated area_updated_cb;
41         ImageLoaderBackendCbSize size_cb;
42         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
43         gpointer data;
44         GdkPixbuf *pixbuf;
45         guint requested_width;
46         guint requested_height;
47         gboolean abort;
48         gint page_num;
49         gint page_total;
50 };
51
52 void free_buffer(guchar *, gpointer data)
53 {
54         heif_image_release(static_cast<const struct heif_image*>(data));
55 }
56
57 gboolean image_loader_heif_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **)
58 {
59         auto ld = static_cast<ImageLoaderHEIF *>(loader);
60         struct heif_context* ctx;
61         struct heif_image* img;
62         struct heif_error error_code;
63         struct heif_image_handle* handle;
64         guint8* data;
65         gint width;
66         gint height;
67         gint stride;
68         gboolean alpha;
69         gint page_total;
70
71         ctx = heif_context_alloc();
72
73         error_code = heif_context_read_from_memory_without_copy(ctx, buf, count, nullptr);
74         if (error_code.code)
75                 {
76                 log_printf("warning: heif reader error: %s\n", error_code.message);
77                 heif_context_free(ctx);
78                 return FALSE;
79                 }
80
81         page_total = heif_context_get_number_of_top_level_images(ctx);
82         ld->page_total = page_total;
83
84         std::vector<heif_item_id> IDs(page_total);
85
86         /* get list of all (top level) image IDs */
87         heif_context_get_list_of_top_level_image_IDs(ctx, IDs.data(), page_total);
88
89         error_code = heif_context_get_image_handle(ctx, IDs[ld->page_num], &handle);
90         if (error_code.code)
91                 {
92                 log_printf("warning:  heif reader error: %s\n", error_code.message);
93                 heif_context_free(ctx);
94                 return FALSE;
95                 }
96
97         // decode the image and convert colorspace to RGB, saved as 24bit interleaved
98         error_code = heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_24bit, nullptr);
99         if (error_code.code)
100                 {
101                 log_printf("warning: heif reader error: %s\n", error_code.message);
102                 heif_context_free(ctx);
103                 return FALSE;
104                 }
105
106         data = heif_image_get_plane(img, heif_channel_interleaved, &stride);
107
108         height = heif_image_get_height(img,heif_channel_interleaved);
109         width = heif_image_get_width(img,heif_channel_interleaved);
110         alpha = heif_image_handle_has_alpha_channel(handle);
111         heif_image_handle_release(handle);
112
113         ld->pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, img);
114
115         ld->area_updated_cb(loader, 0, 0, width, height, ld->data);
116
117         heif_context_free(ctx);
118
119         chunk_size = count;
120         return TRUE;
121 }
122
123 gpointer image_loader_heif_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
124 {
125         auto loader = g_new0(ImageLoaderHEIF, 1);
126         loader->area_updated_cb = area_updated_cb;
127         loader->size_cb = size_cb;
128         loader->area_prepared_cb = area_prepared_cb;
129         loader->data = data;
130         loader->page_num = 0;
131         return loader;
132 }
133
134 void image_loader_heif_set_size(gpointer loader, int width, int height)
135 {
136         auto ld = static_cast<ImageLoaderHEIF *>(loader);
137         ld->requested_width = width;
138         ld->requested_height = height;
139 }
140
141 GdkPixbuf* image_loader_heif_get_pixbuf(gpointer loader)
142 {
143         auto ld = static_cast<ImageLoaderHEIF *>(loader);
144         return ld->pixbuf;
145 }
146
147 gchar* image_loader_heif_get_format_name(gpointer)
148 {
149         return g_strdup("heif");
150 }
151
152 gchar** image_loader_heif_get_format_mime_types(gpointer)
153 {
154         static const gchar *mime[] = {"image/heic", nullptr};
155         return g_strdupv(const_cast<gchar **>(mime));
156 }
157
158 void image_loader_heif_set_page_num(gpointer loader, gint page_num)
159 {
160         auto ld = static_cast<ImageLoaderHEIF *>(loader);
161
162         ld->page_num = page_num;
163 }
164
165 gint image_loader_heif_get_page_total(gpointer loader)
166 {
167         auto ld = static_cast<ImageLoaderHEIF *>(loader);
168
169         return ld->page_total;
170 }
171
172 gboolean image_loader_heif_close(gpointer, GError **)
173 {
174         return TRUE;
175 }
176
177 void image_loader_heif_abort(gpointer loader)
178 {
179         auto ld = static_cast<ImageLoaderHEIF *>(loader);
180         ld->abort = TRUE;
181 }
182
183 void image_loader_heif_free(gpointer loader)
184 {
185         auto ld = static_cast<ImageLoaderHEIF *>(loader);
186         if (ld->pixbuf) g_object_unref(ld->pixbuf);
187         g_free(ld);
188 }
189
190 } // namespace
191
192 void image_loader_backend_set_heif(ImageLoaderBackend *funcs)
193 {
194         funcs->loader_new = image_loader_heif_new;
195         funcs->set_size = image_loader_heif_set_size;
196         funcs->write = image_loader_heif_write;
197         funcs->get_pixbuf = image_loader_heif_get_pixbuf;
198         funcs->close = image_loader_heif_close;
199         funcs->abort = image_loader_heif_abort;
200         funcs->free = image_loader_heif_free;
201         funcs->get_format_name = image_loader_heif_get_format_name;
202         funcs->get_format_mime_types = image_loader_heif_get_format_mime_types;
203         funcs->set_page_num = image_loader_heif_set_page_num;
204         funcs->get_page_total = image_loader_heif_get_page_total;
205 }
206
207 #endif
208 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */