1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-2000 Peter Mattis and Spencer Kimball
*
* gimpimagemetadata.c
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <sys/time.h>
#include <gexiv2/gexiv2.h>
#include "gimp.h"
#include "gimpimagemetadata.h"
#include "libgimp-intl.h"
static gchar * gimp_image_metadata_interpret_comment (gchar *comment,
gchar *tag,
gboolean interpret_raw,
GimpMetadata *metadata);
static void gimp_image_metadata_rotate (GimpImage *image,
GExiv2Orientation orientation);
/* public functions */
static gchar *
gimp_image_metadata_interpret_comment (gchar *comment,
gchar *tag,
gboolean interpret_raw,
GimpMetadata *metadata)
{
/* Exiv2 can return unwanted text at the start of a comment
* taken from Exif.Photo.UserComment since 0.27.3.
* Let's remove that part and return NULL if there
* is nothing else left as comment. */
if (! comment)
return NULL;
if (g_str_has_prefix (comment, "charset=InvalidCharsetId "))
{
GBytes *bytes = NULL;
if (tag && interpret_raw)
{
/* The Exif metadata writer forgot to add the charset.
* Read the raw data and assume it's UTF-8. */
g_printerr ("Invalid charset for tag %s. Using raw data.\n", tag);
bytes = gexiv2_metadata_try_get_tag_raw (GEXIV2_METADATA (metadata),
tag,
NULL);
if (bytes)
{
gsize size, strsize;
const gchar *data;
gchar *raw_comment;
data = g_bytes_get_data (bytes, &size);
raw_comment = g_new (gchar, size + 1 );
strsize = g_strlcpy (raw_comment, data, size + 1);
g_bytes_unref (bytes);
g_free (comment);
if (raw_comment && strsize > 0 &&
g_utf8_validate (raw_comment, size, NULL))
{
comment = raw_comment;
}
else
{
g_free (raw_comment);
comment = NULL;
}
/* Fix the tag in our metadata too, that way we don't have to
* check for this in other places. */
gexiv2_metadata_try_set_tag_string (GEXIV2_METADATA (metadata),
tag,
comment,
NULL);
}
}
else
{
/* The broken metadata comment was propagated */
gchar *real_comment;
/* Skip "charset=InvalidCharsetId " (length 25) to find the real comment */
real_comment = g_strdup (comment + 25);
g_free (comment);
comment = real_comment;
g_printerr ("Broken metadata comment was propagated. "
"Removing 'charset=InvalidCharsetId ' from comment.\n");
if (tag)
{
/* Update the metadata tag */
gexiv2_metadata_try_set_tag_string (GEXIV2_METADATA (metadata),
tag,
comment,
NULL);
}
}
}
else if (g_str_has_prefix (comment, "charset=Ascii "))
{
gchar *real_comment;
/* Skip "charset=Ascii " (length 14) to find the real comment */
real_comment = g_strdup (comment + 14);
g_free (comment);
comment = real_comment;
}
if (comment[0] == '\0')
{
/* Removing an empty comment.*/
g_free (comment);
return NULL;
}
return comment;
}
/**
* gimp_image_metadata_load_thumbnail:
* @file: A #GFile image
* @error: Return location for error message
*
* Retrieves a thumbnail from metadata if present.
*
* Returns: (transfer none) (nullable): a #GimpImage of the @file thumbnail.
*
* Since: 2.10
*/
GimpImage *
gimp_image_metadata_load_thumbnail (GFile *file,
GError **error)
{
GimpMetadata *metadata;
GInputStream *input_stream;
GdkPixbuf *pixbuf;
guint8 *thumbnail_buffer;
gint thumbnail_size;
GimpImage *image = NULL;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
metadata = gimp_metadata_load_from_file (file, error);
if (! metadata)
return NULL;
if (! gexiv2_metadata_get_exif_thumbnail (GEXIV2_METADATA (metadata),
&thumbnail_buffer,
&thumbnail_size))
{
g_object_unref (metadata);
return NULL;
}
input_stream = g_memory_input_stream_new_from_data (thumbnail_buffer,
thumbnail_size,
(GDestroyNotify) g_free);
pixbuf = gdk_pixbuf_new_from_stream (input_stream, NULL, error);
g_object_unref (input_stream);
if (pixbuf)
{
GimpLayer *layer;
image = gimp_image_new (gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
GIMP_RGB);
gimp_image_undo_disable (image);
layer = gimp_layer_new_from_pixbuf (image, _("Background"),
pixbuf,
100.0,
gimp_image_get_default_new_layer_mode (image),
0.0, 0.0);
g_object_unref (pixbuf);
gimp_image_insert_layer (image, layer, NULL, 0);
gimp_image_metadata_rotate (image,
gexiv2_metadata_try_get_orientation (GEXIV2_METADATA (metadata), NULL));
}
g_object_unref (metadata);
return image;
}
/* Internal functions */
/**
* _gimp_image_metadata_load_finish:
* @image: The image
* @mime_type: The loaded file's mime-type
* @metadata: The metadata to set on the image
* @flags: Flags to specify what of the metadata to apply to the image
*
* Applies the @metadata previously loaded with
* gimp_metadata_load_from_file() to the image, taking into account
* the passed @flags.
*
* Since: 3.0
*/
void
_gimp_image_metadata_load_finish (GimpImage *image,
const gchar *mime_type,
GimpMetadata *metadata,
GimpMetadataLoadFlags flags)
{
g_return_if_fail (GIMP_IS_IMAGE (image));
g_return_if_fail (GEXIV2_IS_METADATA (metadata));
if (flags & GIMP_METADATA_LOAD_COMMENT)
{
GimpParasite *comment_parasite;
gchar *comment = NULL;
gchar *user_comment = NULL;
GError *error = NULL;
comment_parasite = gimp_image_get_parasite (image, "gimp-comment");
if (comment_parasite)
{
guint32 parasite_size;
comment = (gchar *) gimp_parasite_get_data (comment_parasite, ¶site_size);
comment = g_strndup (comment, parasite_size);
comment = gimp_image_metadata_interpret_comment (comment,
NULL,
FALSE,
metadata);
gimp_parasite_free (comment_parasite);
}
/* UserComment may need special processing, so always read it. */
user_comment = gexiv2_metadata_try_get_tag_interpreted_string (GEXIV2_METADATA (metadata),
"Exif.Photo.UserComment",
&error);
if (error)
{
/* XXX. Should this be rather a user-facing error? */
g_printerr ("%s: unreadable '%s' metadata tag: %s\n",
G_STRFUNC, "Exif.Photo.UserComment", error->message);
g_clear_error (&error);
}
else if (user_comment)
{
user_comment = gimp_image_metadata_interpret_comment (user_comment,
"Exif.Photo.UserComment",
TRUE,
metadata);
if (! comment)
comment = user_comment;
else
g_free (user_comment);
user_comment = NULL;
}
user_comment = gexiv2_metadata_try_get_tag_interpreted_string (GEXIV2_METADATA (metadata),
"Exif.Image.ImageDescription",
&error);
if (error)
{
g_printerr ("%s: unreadable '%s' metadata tag: %s\n",
G_STRFUNC, "Exif.Image.ImageDescription", error->message);
g_clear_error (&error);
}
else
{
user_comment = gimp_image_metadata_interpret_comment (user_comment,
"Exif.Image.ImageDescription",
FALSE,
metadata);
if (! comment)
comment = user_comment;
else
g_free (user_comment);
}
if (comment)
{
GimpParasite *parasite;
parasite = gimp_parasite_new ("gimp-comment",
GIMP_PARASITE_PERSISTENT,
strlen (comment) + 1,
comment);
g_free (comment);
gimp_image_attach_parasite (image, parasite);
gimp_parasite_free (parasite);
}
}
if (flags & GIMP_METADATA_LOAD_RESOLUTION)
{
gdouble xres;
gdouble yres;
GimpUnit *unit;
if (gimp_metadata_get_resolution (metadata, &xres, &yres, &unit))
{
gimp_image_set_resolution (image, xres, yres);
gimp_image_set_unit (image, unit);
}
}
if (! (flags & GIMP_METADATA_LOAD_ORIENTATION))
{
gexiv2_metadata_try_set_orientation (GEXIV2_METADATA (metadata),
GEXIV2_ORIENTATION_NORMAL,
NULL);
}
if (flags & GIMP_METADATA_LOAD_COLORSPACE)
{
GimpColorProfile *profile = gimp_image_get_color_profile (image);
/* only look for colorspace information from metadata if the
* image didn't contain an embedded color profile
*/
if (! profile)
{
GimpMetadataColorspace colorspace;
colorspace = gimp_metadata_get_colorspace (metadata);
switch (colorspace)
{
case GIMP_METADATA_COLORSPACE_UNSPECIFIED:
case GIMP_METADATA_COLORSPACE_UNCALIBRATED:
case GIMP_METADATA_COLORSPACE_SRGB:
/* use sRGB, a NULL profile will do the right thing */
break;
case GIMP_METADATA_COLORSPACE_ADOBERGB:
profile = gimp_color_profile_new_rgb_adobe ();
break;
}
if (profile)
gimp_image_set_color_profile (image, profile);
}
if (profile)
g_object_unref (profile);
}
gimp_image_set_metadata (image, metadata);
}
/* private functions */
static void
gimp_image_metadata_rotate (GimpImage *image,
GExiv2Orientation orientation)
{
switch (orientation)
{
case GEXIV2_ORIENTATION_UNSPECIFIED:
case GEXIV2_ORIENTATION_NORMAL: /* standard orientation, do nothing */
break;
case GEXIV2_ORIENTATION_HFLIP:
gimp_image_flip (image, GIMP_ORIENTATION_HORIZONTAL);
break;
case GEXIV2_ORIENTATION_ROT_180:
gimp_image_rotate (image, GIMP_ROTATE_DEGREES180);
break;
case GEXIV2_ORIENTATION_VFLIP:
gimp_image_flip (image, GIMP_ORIENTATION_VERTICAL);
break;
case GEXIV2_ORIENTATION_ROT_90_HFLIP: /* flipped diagonally around '\' */
gimp_image_rotate (image, GIMP_ROTATE_DEGREES90);
gimp_image_flip (image, GIMP_ORIENTATION_HORIZONTAL);
break;
case GEXIV2_ORIENTATION_ROT_90: /* 90 CW */
gimp_image_rotate (image, GIMP_ROTATE_DEGREES90);
break;
case GEXIV2_ORIENTATION_ROT_90_VFLIP: /* flipped diagonally around '/' */
gimp_image_rotate (image, GIMP_ROTATE_DEGREES90);
gimp_image_flip (image, GIMP_ORIENTATION_VERTICAL);
break;
case GEXIV2_ORIENTATION_ROT_270: /* 90 CCW */
gimp_image_rotate (image, GIMP_ROTATE_DEGREES270);
break;
default: /* shouldn't happen */
break;
}
}
|