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