2 * Copyright (C) 20019 - The Geeqie Team
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.
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.
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.
21 #include "image-load-heif.h"
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <glib-object.h>
28 #include <libheif/heif.h>
31 #include "image-load.h"
36 struct ImageLoaderHEIF : public ImageLoaderBackend
39 ~ImageLoaderHEIF() override;
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;
50 AreaUpdatedCb area_updated_cb;
58 void free_buffer(guchar *, gpointer data)
60 heif_image_release(static_cast<const struct heif_image*>(data));
63 gboolean ImageLoaderHEIF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
65 struct heif_context* ctx;
66 struct heif_image* img;
67 struct heif_error error_code;
68 struct heif_image_handle* handle;
75 ctx = heif_context_alloc();
77 error_code = heif_context_read_from_memory_without_copy(ctx, buf, count, nullptr);
80 log_printf("warning: heif reader error: %s\n", error_code.message);
81 heif_context_free(ctx);
85 page_total = heif_context_get_number_of_top_level_images(ctx);
87 std::vector<heif_item_id> IDs(page_total);
89 /* get list of all (top level) image IDs */
90 heif_context_get_list_of_top_level_image_IDs(ctx, IDs.data(), page_total);
92 error_code = heif_context_get_image_handle(ctx, IDs[page_num], &handle);
95 log_printf("warning: heif reader error: %s\n", error_code.message);
96 heif_context_free(ctx);
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);
104 log_printf("warning: heif reader error: %s\n", error_code.message);
105 heif_context_free(ctx);
109 pixels = heif_image_get_plane(img, heif_channel_interleaved, &stride);
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);
116 pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, img);
118 area_updated_cb(nullptr, 0, 0, width, height, data);
120 heif_context_free(ctx);
126 void ImageLoaderHEIF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
128 this->area_updated_cb = area_updated_cb;
133 GdkPixbuf *ImageLoaderHEIF::get_pixbuf()
138 gchar *ImageLoaderHEIF::get_format_name()
140 return g_strdup("heif");
143 gchar **ImageLoaderHEIF::get_format_mime_types()
145 static const gchar *mime[] = {"image/heic", nullptr};
146 return g_strdupv(const_cast<gchar **>(mime));
149 void ImageLoaderHEIF::set_page_num(gint page_num)
151 this->page_num = page_num;
154 gint ImageLoaderHEIF::get_page_total()
159 ImageLoaderHEIF::~ImageLoaderHEIF()
161 if (pixbuf) g_object_unref(pixbuf);
166 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_heif()
168 return std::make_unique<ImageLoaderHEIF>();
171 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */