54 struct heif_error err;
55 struct heif_image_handle* handle = NULL;
56 struct heif_image* heif_img = NULL;
58 struct heif_context* ctx = heif_context_alloc();
62 "Unable to allocate HEIF context\n");
66 err = heif_context_read_from_file(ctx, filename, NULL);
67 if(err.code != heif_error_Ok)
70 "Failed to read HEIF file [%s]\n",
75 case heif_error_Unsupported_filetype:
76 case heif_error_Unsupported_feature:
77 fprintf(stderr,
"[imageio_heif] Unsupported file: `%s'! Is your libheif compiled with HEVC support?\n", filename);
87 const int num_images = heif_context_get_number_of_top_level_images(ctx);
91 "No images found in HEIF file [%s]\n",
98 err = heif_context_get_primary_image_handle(ctx, &handle);
99 if(err.code != heif_error_Ok)
102 "Failed to read primary image from HEIF file [%s]\n",
109 err = heif_decode_image(handle, &heif_img, heif_colorspace_RGB, heif_chroma_interleaved_RRGGBB_LE, NULL);
110 if(err.code != heif_error_Ok)
113 "Failed to decode HEIF file [%s]\n",
120 const uint8_t* data = heif_image_get_plane_readonly(heif_img, heif_channel_interleaved, &rowbytes);
121 const size_t width = heif_image_handle_get_width(handle);
122 const size_t height = heif_image_handle_get_height(handle);
130 img->
dsc.
bpp = 4 *
sizeof(float);
133 img->
flags &= ~DT_IMAGE_RAW;
134 img->
flags &= ~DT_IMAGE_S_RAW;
137 const int original_values_bit_depth = heif_image_handle_get_luma_bits_per_pixel(handle);
139 "Bit depth: '%d' for HEIF image [%s]\n",
140 original_values_bit_depth,
143 if(original_values_bit_depth > 8)
146 img->
flags &= ~DT_IMAGE_LDR;
151 img->
flags &= ~DT_IMAGE_HDR;
166 "Failed to allocate mipmap buffer for HEIF image [%s]\n",
172 const int decoded_values_bit_depth = heif_image_get_bits_per_pixel_range(heif_img, heif_channel_interleaved);
174 float max_channel_f = (float)((1 << decoded_values_bit_depth) - 1);
176 const uint8_t *
const restrict in = (
const uint8_t *)data;
178 for(
size_t y = 0; y <
height; y++)
182 uint16_t *in_pixel = (uint16_t *)&in[(y * rowbytes) + (3 *
sizeof(uint16_t) *
x)];
183 float *out_pixel = &mipbuf[(size_t)4 * ((y *
width) +
x)];
186 out_pixel[0] = ((float)in_pixel[0]) * (1.0f / max_channel_f);
187 out_pixel[1] = ((float)in_pixel[1]) * (1.0f / max_channel_f);
188 out_pixel[2] = ((float)in_pixel[2]) * (1.0f / max_channel_f);
194 size_t icc_size = heif_image_handle_get_raw_color_profile_size(handle);
197 img->
profile = (uint8_t *)g_malloc0(icc_size);
198 heif_image_handle_get_raw_color_profile(handle, img->
profile);
207 heif_image_release(heif_img);
211 heif_image_handle_release(handle);
213 heif_context_free(ctx);
230 struct heif_image_handle* handle = NULL;
231 struct heif_error err;
232 struct heif_color_profile_nclx *profile_info_nclx = NULL;
234 uint8_t *icc_data = NULL;
236 struct heif_context* ctx = heif_context_alloc();
240 "Unable to allocate HEIF context\n");
244 err = heif_context_read_from_file(ctx, filename, NULL);
245 if(err.code != heif_error_Ok)
248 "Failed to read HEIF file [%s]\n",
254 const int num_images = heif_context_get_number_of_top_level_images(ctx);
258 "No images found in HEIF file [%s]\n",
264 err = heif_context_get_primary_image_handle(ctx, &handle);
265 if(err.code != heif_error_Ok)
268 "Failed to read primary image from HEIF file [%s]\n",
274 enum heif_color_profile_type profile_type = heif_image_handle_get_color_profile_type(handle);
276 switch (profile_type)
278 case heif_color_profile_type_nclx:
280 "Found NCLX color profile for HEIF file [%s]\n",
282 err = heif_image_handle_get_nclx_color_profile(handle, &profile_info_nclx);
283 if(err.code != heif_error_Ok)
286 "Failed to get NCLX color profile data from HEIF file [%s]\n",
295 if(profile_info_nclx->color_primaries == heif_color_primaries_ITU_R_BT_709_5)
297 gboolean over =
FALSE;
299 if(profile_info_nclx->transfer_characteristics == heif_transfer_characteristic_ITU_R_BT_470_6_System_M
300 && profile_info_nclx->matrix_coefficients == heif_matrix_coefficients_ITU_R_BT_709_5)
310 filename, profile_info_nclx->transfer_characteristics, profile_info_nclx->matrix_coefficients,
316 case heif_color_profile_type_rICC:
317 case heif_color_profile_type_prof:
318 icc_size = heif_image_handle_get_raw_color_profile_size(handle);
324 icc_data = (uint8_t *)g_malloc0(
sizeof(uint8_t) * icc_size);
325 err = heif_image_handle_get_raw_color_profile(handle, icc_data);
326 if(err.code != heif_error_Ok)
329 "Failed to read embedded ICC profile from HEIF image [%s]\n",
338 case heif_color_profile_type_not_present:
340 "No color profile for HEIF file [%s]\n",
346 "Unknown color profile data from HEIF file [%s]\n",
353 if(profile_info_nclx)
355 heif_nclx_color_profile_free(profile_info_nclx);
359 heif_image_handle_release(handle);
361 heif_context_free(ctx);