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