plug-ins: load metadata via libheif and disable rotation dialog

This commit is contained in:
Daniel Novomeský 2023-11-06 15:03:19 +01:00
parent 6b86bb72f0
commit 183cb49b1e

View file

@ -103,6 +103,8 @@ static GimpValueArray * heif_av1_save (GimpProcedure *pro
#endif #endif
static GimpImage * load_image (GFile *file, static GimpImage * load_image (GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
gboolean interactive, gboolean interactive,
GimpPDBStatusType *status, GimpPDBStatusType *status,
GError **error); GError **error);
@ -442,7 +444,7 @@ heif_load (GimpProcedure *procedure,
heif_init (NULL); heif_init (NULL);
#endif #endif
image = load_image (file, interactive, &status, &error); image = load_image (file, metadata, flags, interactive, &status, &error);
#if LIBHEIF_HAVE_VERSION(1,13,0) #if LIBHEIF_HAVE_VERSION(1,13,0)
heif_deinit (); heif_deinit ();
@ -836,10 +838,12 @@ nclx_to_gimp_profile (const struct heif_color_profile_nclx *nclx)
#endif #endif
GimpImage * GimpImage *
load_image (GFile *file, load_image (GFile *file,
gboolean interactive, GimpMetadata *metadata,
GimpPDBStatusType *status, GimpMetadataLoadFlags *flags,
GError **error) gboolean interactive,
GimpPDBStatusType *status,
GError **error)
{ {
GInputStream *input; GInputStream *input;
goffset file_size; goffset file_size;
@ -1267,117 +1271,111 @@ load_image (GFile *file,
g_object_unref (buffer); g_object_unref (buffer);
{ if (metadata)
size_t exif_data_size = 0; {
uint8_t *exif_data = NULL; size_t exif_data_size = 0;
size_t xmp_data_size = 0; uint8_t *exif_data = NULL;
uint8_t *xmp_data = NULL; size_t xmp_data_size = 0;
gint n_metadata; uint8_t *xmp_data = NULL;
heif_item_id metadata_id; gint n_metadata;
heif_item_id metadata_id;
n_metadata = n_metadata = heif_image_handle_get_list_of_metadata_block_IDs (handle, "Exif",
heif_image_handle_get_list_of_metadata_block_IDs (handle, &metadata_id, 1);
"Exif", if (n_metadata > 0)
&metadata_id, 1); {
if (n_metadata > 0) exif_data_size = heif_image_handle_get_metadata_size (handle, metadata_id);
{
exif_data_size = heif_image_handle_get_metadata_size (handle,
metadata_id);
exif_data = g_alloca (exif_data_size);
err = heif_image_handle_get_metadata (handle, metadata_id, exif_data); exif_data = g_alloca (exif_data_size);
if (err.code != 0)
{
exif_data = NULL;
exif_data_size = 0;
}
}
n_metadata = err = heif_image_handle_get_metadata (handle, metadata_id, exif_data);
heif_image_handle_get_list_of_metadata_block_IDs (handle, if (err.code != 0)
"mime", {
&metadata_id, 1); exif_data = NULL;
if (n_metadata > 0) exif_data_size = 0;
{ }
if (g_strcmp0 ( }
heif_image_handle_get_metadata_content_type (handle, metadata_id),
"application/rdf+xml") == 0)
{
xmp_data_size = heif_image_handle_get_metadata_size (handle,
metadata_id);
xmp_data = g_alloca (xmp_data_size);
err = heif_image_handle_get_metadata (handle, metadata_id, xmp_data); n_metadata = heif_image_handle_get_list_of_metadata_block_IDs (handle, "mime",
if (err.code != 0) &metadata_id, 1);
{ if (n_metadata > 0)
xmp_data = NULL; {
xmp_data_size = 0; if (g_strcmp0 (heif_image_handle_get_metadata_content_type (handle, metadata_id), "application/rdf+xml")
} == 0)
} {
} xmp_data_size = heif_image_handle_get_metadata_size (handle, metadata_id);
if (exif_data || xmp_data) xmp_data = g_alloca (xmp_data_size);
{
GimpMetadata *metadata = gimp_metadata_new ();
GimpMetadataLoadFlags flags = GIMP_METADATA_LOAD_COMMENT | GIMP_METADATA_LOAD_RESOLUTION;
if (exif_data) err = heif_image_handle_get_metadata (handle, metadata_id, xmp_data);
{ if (err.code != 0)
const guint8 tiffHeaderBE[4] = { 'M', 'M', 0, 42 }; {
const guint8 tiffHeaderLE[4] = { 'I', 'I', 42, 0 }; xmp_data = NULL;
GExiv2Metadata *exif_metadata = GEXIV2_METADATA (metadata); xmp_data_size = 0;
const guint8 *tiffheader = exif_data; }
glong new_exif_size = exif_data_size; }
}
while (new_exif_size >= 4) /*Searching for TIFF Header*/ if (exif_data || xmp_data)
{ {
if (tiffheader[0] == tiffHeaderBE[0] && tiffheader[1] == tiffHeaderBE[1] && gexiv2_metadata_clear (GEXIV2_METADATA (metadata));
tiffheader[2] == tiffHeaderBE[2] && tiffheader[3] == tiffHeaderBE[3])
{
break;
}
if (tiffheader[0] == tiffHeaderLE[0] && tiffheader[1] == tiffHeaderLE[1] &&
tiffheader[2] == tiffHeaderLE[2] && tiffheader[3] == tiffHeaderLE[3])
{
break;
}
new_exif_size--;
tiffheader++;
}
if (new_exif_size > 4) /* TIFF header + some data found*/ if (exif_data)
{ {
if (! gexiv2_metadata_open_buf (exif_metadata, tiffheader, new_exif_size, error)) const guint8 tiffHeaderBE[4] = { 'M', 'M', 0, 42 };
{ const guint8 tiffHeaderLE[4] = { 'I', 'I', 42, 0 };
g_printerr ("%s: Failed to set EXIF metadata: %s\n", G_STRFUNC, (*error)->message); GExiv2Metadata *exif_metadata = GEXIV2_METADATA (metadata);
g_clear_error (error); const guint8 *tiffheader = exif_data;
} glong new_exif_size = exif_data_size;
}
else
{
g_printerr ("%s: EXIF metadata not set\n", G_STRFUNC);
}
}
if (xmp_data) while (new_exif_size >= 4) /*Searching for TIFF Header*/
{ {
if (!gimp_metadata_set_from_xmp (metadata, xmp_data, xmp_data_size, error)) if (tiffheader[0] == tiffHeaderBE[0] && tiffheader[1] == tiffHeaderBE[1] &&
{ tiffheader[2] == tiffHeaderBE[2] && tiffheader[3] == tiffHeaderBE[3])
g_printerr ("%s: Failed to set XMP metadata: %s\n", G_STRFUNC, (*error)->message); {
g_clear_error (error); break;
} }
} if (tiffheader[0] == tiffHeaderLE[0] && tiffheader[1] == tiffHeaderLE[1] &&
tiffheader[2] == tiffHeaderLE[2] && tiffheader[3] == tiffHeaderLE[3])
{
break;
}
new_exif_size--;
tiffheader++;
}
gexiv2_metadata_try_set_orientation (GEXIV2_METADATA (metadata), if (new_exif_size > 4) /* TIFF header + some data found*/
GEXIV2_ORIENTATION_NORMAL, NULL); {
gexiv2_metadata_try_set_metadata_pixel_width (GEXIV2_METADATA (metadata), if (! gexiv2_metadata_open_buf (exif_metadata, tiffheader, new_exif_size, error))
width, NULL); {
gexiv2_metadata_try_set_metadata_pixel_height (GEXIV2_METADATA (metadata), g_printerr ("%s: Failed to set EXIF metadata: %s\n", G_STRFUNC, (*error)->message);
height, NULL); g_clear_error (error);
gimp_image_metadata_load_finish (image, "image/heif", }
metadata, flags); }
} else
} {
g_printerr ("%s: EXIF metadata not set\n", G_STRFUNC);
}
}
if (xmp_data)
{
if (! gimp_metadata_set_from_xmp (metadata, xmp_data, xmp_data_size, error))
{
g_printerr ("%s: Failed to set XMP metadata: %s\n", G_STRFUNC, (*error)->message);
g_clear_error (error);
}
}
}
gexiv2_metadata_try_set_orientation (GEXIV2_METADATA (metadata),
GEXIV2_ORIENTATION_NORMAL, NULL);
gexiv2_metadata_try_set_metadata_pixel_width (GEXIV2_METADATA (metadata), width, NULL);
gexiv2_metadata_try_set_metadata_pixel_height (GEXIV2_METADATA (metadata),
height, NULL);
*flags = GIMP_METADATA_LOAD_COMMENT | GIMP_METADATA_LOAD_RESOLUTION;
}
if (profile) if (profile)
g_object_unref (profile); g_object_unref (profile);