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