|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Copyright (C) 2025 Hans Petter Jansson |
| 4 | + * |
| 5 | + * This file is part of Chafa, a program that shows pictures on text terminals. |
| 6 | + * |
| 7 | + * Chafa is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * Chafa is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with Chafa. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | + |
| 20 | +#include "config.h" |
| 21 | +#include <assert.h> |
| 22 | +#include <errno.h> |
| 23 | +#include <stdbool.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <string.h> |
| 27 | +#include <unistd.h> |
| 28 | +#include <sys/types.h> |
| 29 | +#include <sys/stat.h> |
| 30 | + |
| 31 | +#include <chafa.h> |
| 32 | +#include <libheif/heif.h> |
| 33 | +#include "chicle-heif-loader.h" |
| 34 | + |
| 35 | +#define BYTES_PER_PIXEL 4 |
| 36 | +#define IMAGE_BUFFER_SIZE_MAX (0xffffffffU >> 2) |
| 37 | + |
| 38 | +struct ChicleHeifLoader |
| 39 | +{ |
| 40 | + ChicleFileMapping *mapping; |
| 41 | + const guint8 *file_data; |
| 42 | + size_t file_data_len; |
| 43 | + gint width, height; |
| 44 | + gint stride; |
| 45 | + |
| 46 | + heif_context *ctx; |
| 47 | + heif_image_handle *handle; |
| 48 | + heif_image *image; |
| 49 | + const uint8_t *frame_data; |
| 50 | +}; |
| 51 | + |
| 52 | +static void |
| 53 | +free_heif_handles (ChicleHeifLoader *loader) |
| 54 | +{ |
| 55 | + if (loader->image) |
| 56 | + heif_image_release (loader->image); |
| 57 | + if (loader->handle) |
| 58 | + heif_image_handle_release (loader->handle); |
| 59 | + if (loader->ctx) |
| 60 | + heif_context_free (loader->ctx); |
| 61 | + |
| 62 | + loader->image = NULL; |
| 63 | + loader->handle = NULL; |
| 64 | + loader->ctx = NULL; |
| 65 | +} |
| 66 | + |
| 67 | +static ChicleHeifLoader * |
| 68 | +chicle_heif_loader_new (void) |
| 69 | +{ |
| 70 | + return g_new0 (ChicleHeifLoader, 1); |
| 71 | +} |
| 72 | + |
| 73 | +ChicleHeifLoader * |
| 74 | +chicle_heif_loader_new_from_mapping (ChicleFileMapping *mapping) |
| 75 | +{ |
| 76 | + ChicleHeifLoader *loader = NULL; |
| 77 | + gboolean success = FALSE; |
| 78 | + |
| 79 | + g_return_val_if_fail (mapping != NULL, NULL); |
| 80 | + |
| 81 | + /* Quick check for the ISOBMFF ftyp box to filter out files that are |
| 82 | + * something else entirely */ |
| 83 | + if (!chicle_file_mapping_has_magic (mapping, 4, "ftyp", 4)) |
| 84 | + goto out; |
| 85 | + |
| 86 | + loader = chicle_heif_loader_new (); |
| 87 | + loader->mapping = mapping; |
| 88 | + |
| 89 | + loader->file_data = chicle_file_mapping_get_data (loader->mapping, &loader->file_data_len); |
| 90 | + if (!loader->file_data) |
| 91 | + goto out; |
| 92 | + |
| 93 | + loader->ctx = heif_context_alloc (); |
| 94 | + if (!loader->ctx) |
| 95 | + goto out; |
| 96 | + |
| 97 | + if (heif_context_read_from_memory_without_copy (loader->ctx, |
| 98 | + loader->file_data, |
| 99 | + loader->file_data_len, |
| 100 | + NULL).code |
| 101 | + != heif_error_Ok) |
| 102 | + goto out; |
| 103 | + |
| 104 | + heif_context_get_primary_image_handle (loader->ctx, |
| 105 | + &loader->handle); |
| 106 | + if (!loader->handle) |
| 107 | + goto out; |
| 108 | + |
| 109 | + heif_decode_image (loader->handle, |
| 110 | + &loader->image, |
| 111 | + heif_colorspace_RGB, |
| 112 | + heif_chroma_interleaved_RGBA, |
| 113 | + NULL); |
| 114 | + if (!loader->image) |
| 115 | + goto out; |
| 116 | + |
| 117 | + loader->width = heif_image_get_primary_width (loader->image); |
| 118 | + loader->height = heif_image_get_primary_height (loader->image); |
| 119 | + |
| 120 | + if (loader->width < 1 || loader->width >= (1 << 28) |
| 121 | + || loader->height < 1 || loader->height >= (1 << 28)) |
| 122 | + goto out; |
| 123 | + |
| 124 | + if ((unsigned int) loader->width * loader->height * BYTES_PER_PIXEL > IMAGE_BUFFER_SIZE_MAX) |
| 125 | + goto out; |
| 126 | + |
| 127 | + loader->frame_data = heif_image_get_plane_readonly (loader->image, |
| 128 | + heif_channel_interleaved, |
| 129 | + &loader->stride); |
| 130 | + if (!loader->frame_data || loader->stride < 1) |
| 131 | + goto out; |
| 132 | + |
| 133 | + success = TRUE; |
| 134 | + |
| 135 | +out: |
| 136 | + if (!success) |
| 137 | + { |
| 138 | + if (loader) |
| 139 | + { |
| 140 | + free_heif_handles (loader); |
| 141 | + g_free (loader); |
| 142 | + loader = NULL; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return loader; |
| 147 | +} |
| 148 | + |
| 149 | +void |
| 150 | +chicle_heif_loader_destroy (ChicleHeifLoader *loader) |
| 151 | +{ |
| 152 | + free_heif_handles (loader); |
| 153 | + |
| 154 | + if (loader->mapping) |
| 155 | + chicle_file_mapping_destroy (loader->mapping); |
| 156 | + |
| 157 | + g_free (loader); |
| 158 | +} |
| 159 | + |
| 160 | +gboolean |
| 161 | +chicle_heif_loader_get_is_animation (ChicleHeifLoader *loader) |
| 162 | +{ |
| 163 | + g_return_val_if_fail (loader != NULL, 0); |
| 164 | + |
| 165 | + return FALSE; |
| 166 | +} |
| 167 | + |
| 168 | +gconstpointer |
| 169 | +chicle_heif_loader_get_frame_data (ChicleHeifLoader *loader, |
| 170 | + ChafaPixelType *pixel_type_out, |
| 171 | + gint *width_out, |
| 172 | + gint *height_out, |
| 173 | + gint *rowstride_out) |
| 174 | +{ |
| 175 | + g_return_val_if_fail (loader != NULL, NULL); |
| 176 | + |
| 177 | + if (pixel_type_out) |
| 178 | + { |
| 179 | + *pixel_type_out = heif_image_is_premultiplied_alpha (loader->image) |
| 180 | + ? CHAFA_PIXEL_RGBA8_PREMULTIPLIED : CHAFA_PIXEL_RGBA8_UNASSOCIATED; |
| 181 | + } |
| 182 | + if (width_out) |
| 183 | + *width_out = loader->width; |
| 184 | + if (height_out) |
| 185 | + *height_out = loader->height; |
| 186 | + if (rowstride_out) |
| 187 | + *rowstride_out = loader->stride; |
| 188 | + |
| 189 | + return loader->frame_data; |
| 190 | +} |
| 191 | + |
| 192 | +gint |
| 193 | +chicle_heif_loader_get_frame_delay (ChicleHeifLoader *loader) |
| 194 | +{ |
| 195 | + g_return_val_if_fail (loader != NULL, 0); |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +void |
| 201 | +chicle_heif_loader_goto_first_frame (ChicleHeifLoader *loader) |
| 202 | +{ |
| 203 | + g_return_if_fail (loader != NULL); |
| 204 | +} |
| 205 | + |
| 206 | +gboolean |
| 207 | +chicle_heif_loader_goto_next_frame (ChicleHeifLoader *loader) |
| 208 | +{ |
| 209 | + g_return_val_if_fail (loader != NULL, FALSE); |
| 210 | + |
| 211 | + return FALSE; |
| 212 | +} |
0 commit comments