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