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