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"
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
31 #include <libheif/heif.h>
34 #include "image-load.h"
39 struct ImageLoaderHEIF : public ImageLoaderBackend
42 ~ImageLoaderHEIF() override;
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;
53 AreaUpdatedCb area_updated_cb;
61 void free_buffer(guchar *, gpointer data)
63 heif_image_release(static_cast<const struct heif_image*>(data));
66 gboolean ImageLoaderHEIF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
68 struct heif_context* ctx;
69 struct heif_image* img;
70 struct heif_error error_code;
71 struct heif_image_handle* handle;
78 ctx = heif_context_alloc();
80 error_code = heif_context_read_from_memory_without_copy(ctx, buf, count, nullptr);
83 log_printf("warning: heif reader error: %s\n", error_code.message);
84 heif_context_free(ctx);
88 page_total = heif_context_get_number_of_top_level_images(ctx);
90 std::vector<heif_item_id> IDs(page_total);
92 /* get list of all (top level) image IDs */
93 heif_context_get_list_of_top_level_image_IDs(ctx, IDs.data(), page_total);
95 error_code = heif_context_get_image_handle(ctx, IDs[page_num], &handle);
98 log_printf("warning: heif reader error: %s\n", error_code.message);
99 heif_context_free(ctx);
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);
107 log_printf("warning: heif reader error: %s\n", error_code.message);
108 heif_context_free(ctx);
112 pixels = heif_image_get_plane(img, heif_channel_interleaved, &stride);
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);
119 pixbuf = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, alpha, 8, width, height, stride, free_buffer, img);
121 area_updated_cb(nullptr, 0, 0, width, height, data);
123 heif_context_free(ctx);
129 void ImageLoaderHEIF::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
131 this->area_updated_cb = area_updated_cb;
136 GdkPixbuf *ImageLoaderHEIF::get_pixbuf()
141 gchar *ImageLoaderHEIF::get_format_name()
143 return g_strdup("heif");
146 gchar **ImageLoaderHEIF::get_format_mime_types()
148 static const gchar *mime[] = {"image/heic", nullptr};
149 return g_strdupv(const_cast<gchar **>(mime));
152 void ImageLoaderHEIF::set_page_num(gint page_num)
154 this->page_num = page_num;
157 gint ImageLoaderHEIF::get_page_total()
162 ImageLoaderHEIF::~ImageLoaderHEIF()
164 if (pixbuf) g_object_unref(pixbuf);
169 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_heif()
171 return std::make_unique<ImageLoaderHEIF>();
175 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */