Bug fix: Change all .desktop files to RDNS style
[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         else
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
108         heif_context_free(ctx);
109
110         return TRUE;
111 }
112
113 static gpointer image_loader_heif_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
114 {
115         auto loader = g_new0(ImageLoaderHEIF, 1);
116         loader->area_updated_cb = area_updated_cb;
117         loader->size_cb = size_cb;
118         loader->area_prepared_cb = area_prepared_cb;
119         loader->data = data;
120         loader->page_num = 0;
121         return loader;
122 }
123
124 static void image_loader_heif_set_size(gpointer loader, int width, int height)
125 {
126         auto ld = static_cast<ImageLoaderHEIF *>(loader);
127         ld->requested_width = width;
128         ld->requested_height = height;
129 }
130
131 static GdkPixbuf* image_loader_heif_get_pixbuf(gpointer loader)
132 {
133         auto ld = static_cast<ImageLoaderHEIF *>(loader);
134         return ld->pixbuf;
135 }
136
137 static gchar* image_loader_heif_get_format_name(gpointer)
138 {
139         return g_strdup("heif");
140 }
141
142 static gchar** image_loader_heif_get_format_mime_types(gpointer)
143 {
144         static const gchar *mime[] = {"image/heic", nullptr};
145         return g_strdupv(const_cast<gchar **>(mime));
146 }
147
148 static void image_loader_heif_set_page_num(gpointer loader, gint page_num)
149 {
150         auto ld = static_cast<ImageLoaderHEIF *>(loader);
151
152         ld->page_num = page_num;
153 }
154
155 static gint image_loader_heif_get_page_total(gpointer loader)
156 {
157         auto ld = static_cast<ImageLoaderHEIF *>(loader);
158
159         return ld->page_total;
160 }
161
162 static gboolean image_loader_heif_close(gpointer, GError **)
163 {
164         return TRUE;
165 }
166
167 static void image_loader_heif_abort(gpointer loader)
168 {
169         auto ld = static_cast<ImageLoaderHEIF *>(loader);
170         ld->abort = TRUE;
171 }
172
173 static void image_loader_heif_free(gpointer loader)
174 {
175         auto ld = static_cast<ImageLoaderHEIF *>(loader);
176         if (ld->pixbuf) g_object_unref(ld->pixbuf);
177         g_free(ld);
178 }
179
180 void image_loader_backend_set_heif(ImageLoaderBackend *funcs)
181 {
182         funcs->loader_new = image_loader_heif_new;
183         funcs->set_size = image_loader_heif_set_size;
184         funcs->load = image_loader_heif_load;
185         funcs->write = nullptr;
186         funcs->get_pixbuf = image_loader_heif_get_pixbuf;
187         funcs->close = image_loader_heif_close;
188         funcs->abort = image_loader_heif_abort;
189         funcs->free = image_loader_heif_free;
190         funcs->get_format_name = image_loader_heif_get_format_name;
191         funcs->get_format_mime_types = image_loader_heif_get_format_mime_types;
192         funcs->set_page_num = image_loader_heif_set_page_num;
193         funcs->get_page_total = image_loader_heif_get_page_total;
194 }
195
196 #endif
197 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */