Deduplicate cell_renderer_height_override()
[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 <vector>
24
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <glib-object.h>
27 #include <glib.h>
28 #include <libheif/heif.h>
29
30 #include "debug.h"
31 #include "image-load.h"
32
33 namespace
34 {
35
36 struct ImageLoaderHEIF : public ImageLoaderBackend
37 {
38 public:
39         ~ImageLoaderHEIF() override;
40
41         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
42         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
43         GdkPixbuf *get_pixbuf() override;
44         gchar *get_format_name() override;
45         gchar **get_format_mime_types() override;
46         void set_page_num(gint page_num) override;
47         gint get_page_total() override;
48
49 private:
50         AreaUpdatedCb area_updated_cb;
51         gpointer data;
52
53         GdkPixbuf *pixbuf;
54         gint page_num;
55         gint page_total;
56 };
57
58 void free_buffer(guchar *, gpointer data)
59 {
60         heif_image_release(static_cast<const struct heif_image*>(data));
61 }
62
63 gboolean ImageLoaderHEIF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
64 {
65         struct heif_context* ctx;
66         struct heif_image* img;
67         struct heif_error error_code;
68         struct heif_image_handle* handle;
69         guint8* pixels;
70         gint width;
71         gint height;
72         gint stride;
73         gboolean alpha;
74
75         ctx = heif_context_alloc();
76
77         error_code = heif_context_read_from_memory_without_copy(ctx, buf, count, nullptr);
78         if (error_code.code)
79                 {
80                 log_printf("warning: heif reader error: %s\n", error_code.message);
81                 heif_context_free(ctx);
82                 return FALSE;
83                 }
84
85         page_total = heif_context_get_number_of_top_level_images(ctx);
86
87         std::vector<heif_item_id> IDs(page_total);
88
89         /* get list of all (top level) image IDs */
90         heif_context_get_list_of_top_level_image_IDs(ctx, IDs.data(), page_total);
91
92         error_code = heif_context_get_image_handle(ctx, IDs[page_num], &handle);
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         // decode the image and convert colorspace to RGB, saved as 24bit interleaved
101         error_code = heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_24bit, nullptr);
102         if (error_code.code)
103                 {
104                 log_printf("warning: heif reader error: %s\n", error_code.message);
105                 heif_context_free(ctx);
106                 return FALSE;
107                 }
108
109         pixels = heif_image_get_plane(img, heif_channel_interleaved, &stride);
110
111         height = heif_image_get_height(img,heif_channel_interleaved);
112         width = heif_image_get_width(img,heif_channel_interleaved);
113         alpha = heif_image_handle_has_alpha_channel(handle);
114         heif_image_handle_release(handle);
115
116         pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, img);
117
118         area_updated_cb(nullptr, 0, 0, width, height, data);
119
120         heif_context_free(ctx);
121
122         chunk_size = count;
123         return TRUE;
124 }
125
126 void ImageLoaderHEIF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
127 {
128         this->area_updated_cb = area_updated_cb;
129         this->data = data;
130         page_num = 0;
131 }
132
133 GdkPixbuf *ImageLoaderHEIF::get_pixbuf()
134 {
135         return pixbuf;
136 }
137
138 gchar *ImageLoaderHEIF::get_format_name()
139 {
140         return g_strdup("heif");
141 }
142
143 gchar **ImageLoaderHEIF::get_format_mime_types()
144 {
145         static const gchar *mime[] = {"image/heic", nullptr};
146         return g_strdupv(const_cast<gchar **>(mime));
147 }
148
149 void ImageLoaderHEIF::set_page_num(gint page_num)
150 {
151         this->page_num = page_num;
152 }
153
154 gint ImageLoaderHEIF::get_page_total()
155 {
156         return page_total;
157 }
158
159 ImageLoaderHEIF::~ImageLoaderHEIF()
160 {
161         if (pixbuf) g_object_unref(pixbuf);
162 }
163
164 } // namespace
165
166 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_heif()
167 {
168         return std::make_unique<ImageLoaderHEIF>();
169 }
170
171 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */