Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
xmp_sidecar.cc
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2009-2014, 2016 johannes hanika.
4 Copyright (C) 2010-2012 Henrik Andersson.
5 Copyright (C) 2010, 2012-2015 Pascal de Bruijn.
6 Copyright (C) 2010-2018 Tobias Ellinghaus.
7 Copyright (C) 2011-2012, 2018 Edouard Gomez.
8 Copyright (C) 2011 Jochen Schroeder.
9 Copyright (C) 2011 Johanes Schneider.
10 Copyright (C) 2011-2012 Jérémy Rosen.
11 Copyright (C) 2011 Kanstantsin Shautsou.
12 Copyright (C) 2011 Robert Bieber.
13 Copyright (C) 2011, 2013 Simon Spannagel.
14 Copyright (C) 2011 Uli Scholler.
15 Copyright (C) 2012-2013, 2020 Aldric Renaudin.
16 Copyright (C) 2012 Alexander Clausen.
17 Copyright (C) 2012 James C. McPherson.
18 Copyright (C) 2012-2013 José Carlos García Sogo.
19 Copyright (C) 2012 Richard Wonka.
20 Copyright (C) 2012 Sergey Pavlov.
21 Copyright (C) 2012-2014, 2016 Ulrich Pegelow.
22 Copyright (C) 2012 Wolfgang Kühnel.
23 Copyright (C) 2013 Dennis Gnad.
24 Copyright (C) 2013 Jacek Naglak.
25 Copyright (C) 2013-2015, 2018-2022 Pascal Obry.
26 Copyright (C) 2013-2017 Roman Lebedev.
27 Copyright (C) 2014-2015 Jan Niklas Fingerle.
28 Copyright (C) 2014 Michael Neumann.
29 Copyright (C) 2014-2016 Pedro Côrte-Real.
30 Copyright (C) 2015 Jake Probst.
31 Copyright (C) 2015, 2018 Marcel Müller.
32 Copyright (C) 2015 Tom Vijlbrief.
33 Copyright (C) 2015 Torsten Bronger.
34 Copyright (C) 2016 Matthieu Volat.
35 Copyright (C) 2017, 2020 David-Tillmann Schaefer.
36 Copyright (C) 2017, 2019, 2021 luzpaz.
37 Copyright (C) 2018-2019 Andreas Schneider.
38 Copyright (C) 2018-2019 Edgardo Hoszowski.
39 Copyright (C) 2018 Fabian Wenzel.
40 Copyright (C) 2019 codingdave.
41 Copyright (C) 2019-2021 Hanno Schwalm.
42 Copyright (C) 2019-2022 Philippe Weyland.
43 Copyright (C) 2020 Chris Elston.
44 Copyright (C) 2020-2021 Hubert Kowalski.
45 Copyright (C) 2020 JP Verrue.
46 Copyright (C) 2020 Matt Maguire.
47 Copyright (C) 2020-2022 Miloš Komarčević.
48 Copyright (C) 2020-2021 Ralf Brown.
49 Copyright (C) 2021, 2023, 2025-2026 Aurélien PIERRE.
50 Copyright (C) 2021 Daniel Vogelbacher.
51 Copyright (C) 2021 Victor Forsiuk.
52 Copyright (C) 2022 gi-man.
53 Copyright (C) 2022 Martin Bařinka.
54 Copyright (C) 2022 paolodepetrillo.
55 Copyright (C) 2023, 2025 Alynx Zhou.
56 Copyright (C) 2023 Ricky Moon.
57 Copyright (C) 2025-2026 Guillaume Stutin.
58
59 darktable is free software: you can redistribute it and/or modify
60 it under the terms of the GNU General Public License as published by
61 the Free Software Foundation, either version 3 of the License, or
62 (at your option) any later version.
63
64 darktable is distributed in the hope that it will be useful,
65 but WITHOUT ANY WARRANTY; without even the implied warranty of
66 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 GNU General Public License for more details.
68
69 You should have received a copy of the GNU General Public License
70 along with darktable. If not, see <http://www.gnu.org/licenses/>.
71 */
72
73#define __STDC_FORMAT_MACROS
74
84
85#ifdef HAVE_CONFIG_H
86#include "config.h"
87#endif
88
89#include <errno.h>
90#include "common/paths.h" // DT_PATH_MAX
91#include <glib.h>
92#include <glib/gstdio.h>
93#include <time.h>
94
95#include <cmath>
96#include <cstdlib>
97#include <cstring>
98#include <fstream>
99#include <iostream>
100#include <sstream>
101#include <string>
102
103#include <exiv2/exiv2.hpp>
104
105#include <pugixml.hpp>
106
107#include "common/xmp_sidecar.h"
108#include "metadata/exif.h"
110
111#include "caches/image_cache.h"
112#include "common/conf.h"
113#include "common/datetime.h"
114#include "common/deprecations.h"
115#include "history/history.h"
116#include "common/utility.h"
117#include "common/variables.h"
119#include "database/database.h"
123#include "develop/blend.h"
124#include "develop/iop_order.h"
125#include "develop/masks.h"
126#include "imageio/imageio_core.h"
127#include "metadata/metadata.h"
128#include "metadata/notify.h"
129#include "metadata/tags.h"
130#include "system/macros.h"
131#include "system/mem_alloc.h"
132
133using namespace std;
134
135#define DT_XMP_EXIF_VERSION 5
136
137#if EXIV2_TEST_VERSION(0,28,0)
138#define AnyError Error
139#define toLong toInt64
140#endif
141
142static const char *_exif_get_exiv2_tag_type(const char *tagname)
143{
144 if(IS_NULL_PTR(tagname)) return NULL;
145
146 const size_t tagname_len = strlen(tagname);
147
148 // the list lives in metadata/exif.cc and is built on first use; it was a file-static
149 // shared with this code when the two were one translation unit
150 for(const GList *tag = dt_exif_get_exiv2_taglist(); tag; tag = g_list_next(tag))
151 {
152 const char *t = (const char *)tag->data;
153 if(IS_NULL_PTR(t)) continue; // was tested AFTER t had already been dereferenced
154
155 // each entry is "<tagname>,<type>". The prefix comparison having succeeded is what
156 // makes t[tagname_len] in bounds: it is either the ',' or the terminating NUL.
157 if(strncmp(t, tagname, tagname_len) == 0 && t[tagname_len] == ',')
158 return t + tagname_len + 1;
159 }
160 return NULL;
161}
162
163static void read_xmp_timestamps(Exiv2::XmpData &xmpData, dt_image_t *img, const int xmp_version);
164
165// this array should contain all XmpBag and XmpSeq keys used by dt
166const char *dt_xmp_keys[]
167 = { "Xmp.dc.subject", "Xmp.lr.hierarchicalSubject", "Xmp.darktable.colorlabels", "Xmp.darktable.history",
168 "Xmp.darktable.history_modversion", "Xmp.darktable.history_enabled", "Xmp.darktable.history_end",
169 "Xmp.darktable.iop_order_version", "Xmp.darktable.iop_order_list",
170 "Xmp.darktable.history_operation", "Xmp.darktable.history_params", "Xmp.darktable.blendop_params",
171 "Xmp.darktable.blendop_version", "Xmp.darktable.multi_priority", "Xmp.darktable.multi_name",
172 "Xmp.darktable.iop_order",
173 "Xmp.darktable.xmp_version", "Xmp.darktable.raw_params", "Xmp.darktable.auto_presets_applied",
174 "Xmp.darktable.mask_id", "Xmp.darktable.mask_type", "Xmp.darktable.mask_name",
175 "Xmp.darktable.masks_history", "Xmp.darktable.mask_num", "Xmp.darktable.mask_points",
176 "Xmp.darktable.mask_version", "Xmp.darktable.mask", "Xmp.darktable.mask_nb", "Xmp.darktable.mask_src",
177 "Xmp.darktable.history_basic_hash", "Xmp.darktable.history_auto_hash", "Xmp.darktable.history_current_hash",
178 "Xmp.darktable.import_timestamp", "Xmp.darktable.change_timestamp",
179 "Xmp.darktable.export_timestamp", "Xmp.darktable.print_timestamp",
180 "Xmp.acdsee.notes", "Xmp.darktable.version_name",
181 "Xmp.dc.creator", "Xmp.dc.publisher", "Xmp.dc.title", "Xmp.dc.description", "Xmp.dc.rights",
182 "Xmp.xmpMM.DerivedFrom" };
183
184static const guint dt_xmp_keys_n = G_N_ELEMENTS(dt_xmp_keys); // the number of XmpBag XmpSeq keys that dt uses
185
195static bool _read_xmp_packet(const char *filename, std::string &packet)
196{
197 size_t length = 0;
198 errno = 0;
199 char *content = dt_read_file(filename, &length);
200
201 if(IS_NULL_PTR(content))
202 {
203 fprintf(stderr, "cannot read xmp file '%s': '%s'\n", filename, strerror(errno));
204 return false;
205 }
206
207 packet.assign(content, length);
208 dt_free(content);
209 return true;
210}
211
212// function to remove known dt keys and subtrees from xmpdata, so not to append them twice
213// this should work because dt first reads all known keys
214static void dt_remove_known_keys(Exiv2::XmpData &xmp)
215{
216 xmp.sortByKey();
217 for(unsigned int i = 0; i < dt_xmp_keys_n; i++)
218 {
219 Exiv2::XmpData::iterator pos = xmp.findKey(Exiv2::XmpKey(dt_xmp_keys[i]));
220
221 while(pos != xmp.end())
222 {
223 std::string key = pos->key();
224 const char *ckey = key.c_str();
225 size_t len = key.size();
226 // stop iterating once the key no longer matches what we are trying to delete. this assumes sorted input
227 if(!(g_str_has_prefix(ckey, dt_xmp_keys[i]) && (ckey[len] == '[' || ckey[len] == '\0')))
228 break;
229 pos = xmp.erase(pos);
230 }
231 }
232}
233
234static void dt_remove_xmp_keys(Exiv2::XmpData &xmp, const char *keys[], unsigned int n_keys)
235{
236 for(unsigned int i = 0; i < n_keys; i++)
237 {
238 try
239 {
240 Exiv2::XmpData::iterator pos;
241 while((pos = xmp.findKey(Exiv2::XmpKey(keys[i]))) != xmp.end())
242 xmp.erase(pos);
243 }
244 catch(const std::exception &e)
245 {
246 // the only exception we may get is "invalid" tag, which is not
247 // important enough to either stop the function, or even display
248 // a message (it's probably the tag is not implemented in the
249 // exiv2 version used)
250 }
251 }
252}
253
254static void dt_remove_exif_geotag(Exiv2::ExifData &exifData)
255{
256 static const char *keys[] =
257 {
258 "Exif.GPSInfo.GPSLatitude",
259 "Exif.GPSInfo.GPSLongitude",
260 "Exif.GPSInfo.GPSAltitude",
261 "Exif.GPSInfo.GPSLatitudeRef",
262 "Exif.GPSInfo.GPSLongitudeRef",
263 "Exif.GPSInfo.GPSAltitudeRef",
264 "Exif.GPSInfo.GPSVersionID"
265 };
266 static const guint n_keys = G_N_ELEMENTS(keys);
267 dt_remove_exif_keys(exifData, keys, n_keys);
268}
269
270int dt_exif_read_blob(uint8_t **buf, const char *path, const int32_t imgid, const int sRGB, const int out_width,
271 const int out_height, const int dng_mode)
272{
273 *buf = NULL;
274 try
275 {
276 std::unique_ptr<Exiv2::Image> image(Exiv2::ImageFactory::open(WIDEN(path)));
277 if(!image.get()) return 1;
278 image->readMetadata();
279 Exiv2::ExifData &exifData = image->exifData();
280
281 // get rid of thumbnails
282 Exiv2::ExifThumb(exifData).erase();
283 Exiv2::ExifData::const_iterator pos;
284
285 {
286 static const char *keys[] = {
287 "Exif.Image.ImageWidth",
288 "Exif.Image.ImageLength",
289 "Exif.Image.BitsPerSample",
290 "Exif.Image.Compression",
291 "Exif.Image.PhotometricInterpretation",
292 "Exif.Image.FillOrder",
293 "Exif.Image.SamplesPerPixel",
294 "Exif.Image.StripOffsets",
295 "Exif.Image.RowsPerStrip",
296 "Exif.Image.StripByteCounts",
297 "Exif.Image.TileWidth",
298 "Exif.Image.TileLength",
299 "Exif.Image.TileOffsets",
300 "Exif.Image.TileByteCounts",
301 "Exif.Image.PlanarConfiguration"
302 };
303 static const guint n_keys = G_N_ELEMENTS(keys);
304 dt_remove_exif_keys(exifData, keys, n_keys);
305 }
306
307 /* Many tags should be removed in all cases as they are simply wrong also for dng files */
308
309 // remove subimage* trees, related to thumbnails or HDR usually; also UserCrop
310 for(Exiv2::ExifData::iterator i = exifData.begin(); i != exifData.end();)
311 {
312 static const std::string needle = "Exif.SubImage";
313 if(i->key().compare(0, needle.length(), needle) == 0)
314 i = exifData.erase(i);
315 else
316 ++i;
317 }
318
319 {
320 static const char *keys[] = {
321 // Canon color space info
322 "Exif.Canon.ColorSpace",
323 "Exif.Canon.ColorData",
324
325 // Nikon thumbnail data
326 "Exif.Nikon3.Preview",
327 "Exif.NikonPreview.JPEGInterchangeFormat",
328
329 // TIFF/EP & Exif stuff irrelevant after developing
330 "Exif.Image.CFARepeatPatternDim",
331 "Exif.Image.CFAPattern",
332 "Exif.Image.InterColorProfile",
333 "Exif.Image.SpectralSensitivity",
334 "Exif.Image.OECF",
335 "Exif.Image.SpatialFrequencyResponse",
336 "Exif.Image.Noise",
337 "Exif.Image.SensingMethod",
338 "Exif.Image.TIFFEPStandardID",
339 "Exif.Photo.SpectralSensitivity",
340 "Exif.Photo.OECF",
341 "Exif.Photo.SpatialFrequencyResponse",
342 "Exif.Photo.SensingMethod",
343 "Exif.Photo.CFAPattern",
344
345 // DNG stuff that is irrelevant or misleading
346 "Exif.Image.DNGVersion",
347 "Exif.Image.DNGBackwardVersion",
348 "Exif.Image.DNGPrivateData",
349 "Exif.Image.DefaultBlackRender",
350 "Exif.Image.DefaultCropOrigin",
351 "Exif.Image.DefaultCropSize",
352 "Exif.Image.RawDataUniqueID",
353 "Exif.Image.OriginalRawFileName",
354 "Exif.Image.OriginalRawFileData",
355 "Exif.Image.ActiveArea",
356 "Exif.Image.MaskedAreas",
357 "Exif.Image.AsShotICCProfile",
358 "Exif.Image.OpcodeList1",
359 "Exif.Image.OpcodeList2",
360 "Exif.Image.OpcodeList3",
361 "Exif.Image.AsShotNeutral",
362 "Exif.Image.AsShotWhiteXY",
363 "Exif.Image.BaselineExposure",
364 "Exif.Image.BaselineNoise",
365 "Exif.Image.BaselineSharpness",
366 "Exif.Image.LinearResponseLimit",
367 "Exif.Image.ShadowScale",
368 "Exif.Image.PreviewApplicationName",
369 "Exif.Image.PreviewApplicationVersion",
370 "Exif.Image.PreviewSettingsDigest",
371 "Exif.Image.PreviewColorSpace",
372 "Exif.Image.PreviewDateTime",
373 "Exif.Image.NoiseProfile",
374 "Exif.Image.NewRawImageDigest",
375 "Exif.Image.RawImageDigest",
376
377 "Exif.Photo.MakerNote",
378
379 // Pentax thumbnail data
380 "Exif.Pentax.PreviewResolution",
381 "Exif.Pentax.PreviewLength",
382 "Exif.Pentax.PreviewOffset",
383 "Exif.PentaxDng.PreviewResolution",
384 "Exif.PentaxDng.PreviewLength",
385 "Exif.PentaxDng.PreviewOffset",
386 // Pentax color info
387 "Exif.PentaxDng.ColorInfo",
388
389 // Minolta thumbnail data
390 "Exif.Minolta.Thumbnail",
391 "Exif.Minolta.ThumbnailOffset",
392 "Exif.Minolta.ThumbnailLength",
393
394 // Sony thumbnail data
395 "Exif.SonyMinolta.ThumbnailOffset",
396 "Exif.SonyMinolta.ThumbnailLength",
397
398 // Olympus thumbnail data
399 "Exif.Olympus.Thumbnail",
400 "Exif.Olympus.ThumbnailOffset",
401 "Exif.Olympus.ThumbnailLength",
402
403 "Exif.Image.BaselineExposureOffset",
404
405 // Samsung makernote cleanup, the entries below have no
406 // relevance for exported images
407 "Exif.Samsung2.SensorAreas",
408 "Exif.Samsung2.ColorSpace",
409 "Exif.Samsung2.EncryptionKey",
410 "Exif.Samsung2.WB_RGGBLevelsUncorrected",
411 "Exif.Samsung2.WB_RGGBLevelsAuto",
412 "Exif.Samsung2.WB_RGGBLevelsIlluminator1",
413 "Exif.Samsung2.WB_RGGBLevelsIlluminator2",
414 "Exif.Samsung2.WB_RGGBLevelsBlack",
415 "Exif.Samsung2.ColorMatrix",
416 "Exif.Samsung2.ColorMatrixSRGB",
417 "Exif.Samsung2.ColorMatrixAdobeRGB",
418 "Exif.Samsung2.ToneCurve1",
419 "Exif.Samsung2.ToneCurve2",
420 "Exif.Samsung2.ToneCurve3",
421 "Exif.Samsung2.ToneCurve4"
422 };
423 static const guint n_keys = G_N_ELEMENTS(keys);
424 dt_remove_exif_keys(exifData, keys, n_keys);
425 }
426
427 static const char *dngkeys[] = {
428 // Embedded color profile info
429 "Exif.Image.ColorMatrix1",
430 "Exif.Image.ColorMatrix2",
431 "Exif.Image.CameraCalibration1",
432 "Exif.Image.CameraCalibration2",
433 "Exif.Image.ReductionMatrix1",
434 "Exif.Image.ReductionMatrix2",
435 "Exif.Image.AnalogBalance",
436 "Exif.Image.CalibrationIlluminant1",
437 "Exif.Image.CalibrationIlluminant2",
438 "Exif.Image.CameraCalibrationSignature",
439 "Exif.Image.ProfileCalibrationSignature",
440 "Exif.Image.ExtraCameraProfiles",
441 "Exif.Image.AsShotProfileName",
442 "Exif.Image.ProfileName",
443 "Exif.Image.ProfileHueSatMapDims",
444 "Exif.Image.ProfileHueSatMapData1",
445 "Exif.Image.ProfileHueSatMapData2",
446 "Exif.Image.ProfileToneCurve",
447 "Exif.Image.ProfileEmbedPolicy",
448 "Exif.Image.ProfileCopyright",
449 "Exif.Image.ForwardMatrix1",
450 "Exif.Image.ForwardMatrix2",
451 "Exif.Image.ProfileLookTableDims",
452 "Exif.Image.ProfileLookTableData",
453 "Exif.Image.ProfileLookTableEncoding",
454 "Exif.Image.ProfileHueSatMapEncoding"
455 };
456 static const guint n_dngkeys = G_N_ELEMENTS(dngkeys);
457 dt_remove_exif_keys(exifData, dngkeys, n_dngkeys);
458
459 /* Write appropriate color space tag if using sRGB output */
460 if(sRGB)
461 exifData["Exif.Photo.ColorSpace"] = uint16_t(1); /* sRGB */
462 else
463 exifData["Exif.Photo.ColorSpace"] = uint16_t(0xFFFF); /* Uncalibrated */
464
465 // we don't write the orientation here for dng as it is set in dt_imageio_dng_write_tiff_header
466 // or might be defined in this blob.
467 if(!dng_mode) exifData["Exif.Image.Orientation"] = uint16_t(1);
468
469 /* Replace RAW dimension with output dimensions (for example after crop/scale, or orientation for dng
470 * mode) */
471 if(out_width > 0) exifData["Exif.Photo.PixelXDimension"] = (uint32_t)out_width;
472 if(out_height > 0) exifData["Exif.Photo.PixelYDimension"] = (uint32_t)out_height;
473
474 const int resolution = dt_conf_get_int("metadata/resolution");
475 exifData["Exif.Image.XResolution"] = Exiv2::Rational(resolution, 1);
476 exifData["Exif.Image.YResolution"] = Exiv2::Rational(resolution, 1);
477 exifData["Exif.Image.ResolutionUnit"] = uint16_t(2); /* inches */
478
479 exifData["Exif.Image.Software"] = darktable_package_string;
480
481 // TODO: find a nice place for the missing metadata (tags, publisher, colorlabels?). Additionally find out
482 // how to embed XMP data.
483 // And shall we add a description of the history stack to Exif.Image.ImageHistory?
484 if(imgid >= 0)
485 {
486 /* Delete metadata taken from the original file if it's fields we manage in dt, too */
487 static const char * keys[] = {
488 "Exif.Image.Artist",
489 "Exif.Image.ImageDescription",
490 "Exif.Photo.UserComment",
491 "Exif.Image.Copyright",
492 "Exif.Image.Rating",
493 "Exif.Image.RatingPercent",
494 "Exif.Photo.SubSecTimeOriginal",
495 "Exif.GPSInfo.GPSVersionID",
496 "Exif.GPSInfo.GPSLongitudeRef",
497 "Exif.GPSInfo.GPSLatitudeRef",
498 "Exif.GPSInfo.GPSLongitude",
499 "Exif.GPSInfo.GPSLatitude",
500 "Exif.GPSInfo.GPSAltitudeRef",
501 "Exif.GPSInfo.GPSAltitude"
502 };
503 static const guint n_keys = G_N_ELEMENTS(keys);
504 dt_remove_exif_keys(exifData, keys, n_keys);
505
506 GList *res = dt_metadata_get(imgid, "Xmp.dc.creator", NULL);
507 if(!IS_NULL_PTR(res))
508 {
509 exifData["Exif.Image.Artist"] = (char *)res->data;
510 g_list_free_full(res, dt_free_gpointer);
511 res = NULL;
512 }
513
514 res = dt_metadata_get(imgid, "Xmp.dc.description", NULL);
515 if(!IS_NULL_PTR(res))
516 {
517 char *desc = (char *)res->data;
518 if(g_str_is_ascii(desc))
519 exifData["Exif.Image.ImageDescription"] = desc;
520 else
521 exifData["Exif.Photo.UserComment"] = desc;
522 g_list_free_full(res, dt_free_gpointer);
523 res = NULL;
524 }
525#if EXIV2_TEST_VERSION(0,27,4)
526 else
527 // mandatory tag for TIFF/EP and recommended for Exif, empty is ok (unknown)
528 // but correctly written only by exiv2 >= 0.27.4
529 exifData["Exif.Image.ImageDescription"] = "";
530#endif
531
532 res = dt_metadata_get(imgid, "Xmp.dc.rights", NULL);
533 if(!IS_NULL_PTR(res))
534 {
535 exifData["Exif.Image.Copyright"] = (char *)res->data;
536 g_list_free_full(res, dt_free_gpointer);
537 res = NULL;
538 }
539#if EXIV2_TEST_VERSION(0,27,4)
540 else
541 // mandatory tag for TIFF/EP and optional for Exif, empty is ok (unknown)
542 // but correctly written only by exiv2 >= 0.27.4
543 exifData["Exif.Image.Copyright"] = "";
544#endif
545
546 res = dt_metadata_get(imgid, "Xmp.xmp.Rating", NULL);
547 if(!IS_NULL_PTR(res))
548 {
549 const int rating = GPOINTER_TO_INT(res->data) + 1;
550 exifData["Exif.Image.Rating"] = rating;
551 g_list_free(res);
552 res = NULL;
553 }
554
555 // GPS data
556 dt_remove_exif_geotag(exifData);
557 const dt_image_t *cimg = dt_image_cache_get(imgid, 'r');
558 if(IS_NULL_PTR(cimg))
559 {
560 std::cerr << "[exiv2 dt_exif_read_blob] " << path << ": could not load image " << imgid
561 << ", no Exif data written" << std::endl;
562 return 0;
563 }
564 if(!isnan(cimg->geoloc.longitude) && !isnan(cimg->geoloc.latitude))
565 {
566 exifData["Exif.GPSInfo.GPSVersionID"] = "02 02 00 00";
567 exifData["Exif.GPSInfo.GPSLongitudeRef"] = (cimg->geoloc.longitude < 0) ? "W" : "E";
568 exifData["Exif.GPSInfo.GPSLatitudeRef"] = (cimg->geoloc.latitude < 0) ? "S" : "N";
569
570 long long_deg = (int)floor(fabs(cimg->geoloc.longitude));
571 long lat_deg = (int)floor(fabs(cimg->geoloc.latitude));
572 long long_min = (int)floor((fabs(cimg->geoloc.longitude) - floor(fabs(cimg->geoloc.longitude))) * 60000000);
573 long lat_min = (int)floor((fabs(cimg->geoloc.latitude) - floor(fabs(cimg->geoloc.latitude))) * 60000000);
574 gchar *long_str = g_strdup_printf("%ld/1 %ld/1000000 0/1", long_deg, long_min);
575 gchar *lat_str = g_strdup_printf("%ld/1 %ld/1000000 0/1", lat_deg, lat_min);
576 exifData["Exif.GPSInfo.GPSLongitude"] = long_str;
577 exifData["Exif.GPSInfo.GPSLatitude"] = lat_str;
578 dt_free(long_str);
579 dt_free(lat_str);
580 }
581 if(!isnan(cimg->geoloc.elevation))
582 {
583 exifData["Exif.GPSInfo.GPSVersionID"] = "02 02 00 00";
584 exifData["Exif.GPSInfo.GPSAltitudeRef"] = (cimg->geoloc.elevation < 0) ? "1" : "0";
585
586 long ele_dm = (int)floor(fabs(10.0 * cimg->geoloc.elevation));
587 gchar *ele_str = g_strdup_printf("%ld/10", ele_dm);
588 exifData["Exif.GPSInfo.GPSAltitude"] = ele_str;
589 dt_free(ele_str);
590 }
591
592 // According to the Exif specs DateTime is to be set to the last modification time while
593 // DateTimeOriginal is to be kept.
594 // For us "keeping" it means to write out what we have in DB to support people adding a time offset in
595 // the geotagging module.
596 gchar new_datetime[DT_DATETIME_EXIF_LENGTH];
597 dt_datetime_now_to_exif(new_datetime);
598 exifData["Exif.Image.DateTime"] = new_datetime;
599 gchar datetime[DT_DATETIME_LENGTH];
600 dt_datetime_img_to_exif(datetime, sizeof(datetime), cimg);
601 datetime[DT_DATETIME_EXIF_LENGTH - 1] = '\0';
602 exifData["Exif.Image.DateTimeOriginal"] = datetime;
603 exifData["Exif.Photo.DateTimeOriginal"] = datetime;
604 if(g_strcmp0(&datetime[DT_DATETIME_EXIF_LENGTH], "000"))
605 exifData["Exif.Photo.SubSecTimeOriginal"] = &datetime[DT_DATETIME_EXIF_LENGTH];
606
608 }
609
610 Exiv2::Blob blob;
611 Exiv2::ExifParser::encode(blob, Exiv2::bigEndian, exifData);
612 const size_t length = blob.size();
613 *buf = (uint8_t *)malloc(length);
614 if (IS_NULL_PTR(*buf))
615 {
616 return 0;
617 }
618 memcpy(*buf, &(blob[0]), length);
619 return length;
620 }
621 catch(const std::exception &e)
622 {
623 // std::cerr.rdbuf(savecerr);
624 std::string s(e.what());
625 std::cerr << "[exiv2 dt_exif_read_blob] " << path << ": " << s << std::endl;
626 dt_free(*buf);
627 return 0;
628 }
629}
630
631typedef struct history_entry_t
632{
634 gboolean enabled;
636 unsigned char *params;
639 // int multi_name_hand_edited;
642 unsigned char *blendop_params;
644 int num;
645 double iop_order; // kept for compatibility with xmp version < 4
646
647 // sanity checking
650
651// used for a hash table that maps mask_id to the mask data
667
668static void print_history_entry(history_entry_t *entry) __attribute__((unused));
670{
671 if(IS_NULL_PTR(entry) || IS_NULL_PTR(entry->operation))
672 {
673 std::cout << "malformed entry" << std::endl;
674 return;
675 }
676
677 std::cout << entry->operation << std::endl;
678 std::cout << " modversion :" << entry->modversion << std::endl;
679 std::cout << " enabled :" << entry->enabled << std::endl;
680 std::cout << " params :" << (entry->params ? "<found>" : "<missing>") << std::endl;
681 std::cout << " multi_name :" << (entry->multi_name ? entry->multi_name : "<missing>") << std::endl;
682 std::cout << " multi_priority :" << entry->multi_priority << std::endl;
683 std::cout << " iop_order :" << entry->iop_order << std::endl;
684 std::cout << " blendop_version :" << entry->blendop_version << std::endl;
685 std::cout << " blendop_params :" << (entry->blendop_params ? "<found>" : "<missing>") << std::endl;
686 std::cout << std::endl;
687}
688
689static void free_history_entry(gpointer data)
690{
692 dt_free(entry->operation);
693 dt_free(entry->multi_name);
694 dt_free(entry->params);
695 dt_free(entry->blendop_params);
696 dt_free(entry);
697}
698
699// we have to use pugixml as the old format could contain empty rdf:li elements in the multi_name array
700// which causes problems when accessing it with libexiv2 :(
701// superold is a flag indicating that data is wrapped in <rdf:Bag> instead of <rdf:Seq>.
702static GList *read_history_v1(const std::string &xmpPacket, const char *filename, const int superold)
703{
704 GList *history_entries = NULL;
705
706 pugi::xml_document doc;
707#if defined(PUGIXML_VERSION) && PUGIXML_VERSION >= 150
708 pugi::xml_parse_result result = doc.load_string(xmpPacket.c_str());
709#else
710 pugi::xml_parse_result result = doc.load(xmpPacket.c_str());
711#endif
712
713 if(!result)
714 {
715 std::cerr << "XML '" << filename << "' parsed with errors" << std::endl;
716 std::cerr << "Error description: " << result.description() << std::endl;
717 std::cerr << "Error offset: " << result.offset << std::endl;
718 return NULL;
719 }
720
721 // get the old elements
722 // select_single_node() is deprecated and just kept for old versions shipped in some distributions
723#if defined(PUGIXML_VERSION) && PUGIXML_VERSION >= 150
724 pugi::xpath_node modversion = superold ?
725 doc.select_node("//darktable:history_modversion/rdf:Bag"):
726 doc.select_node("//darktable:history_modversion/rdf:Seq");
727 pugi::xpath_node enabled = superold ?
728 doc.select_node("//darktable:history_enabled/rdf:Bag"):
729 doc.select_node("//darktable:history_enabled/rdf:Seq");
730 pugi::xpath_node operation = superold ?
731 doc.select_node("//darktable:history_operation/rdf:Bag"):
732 doc.select_node("//darktable:history_operation/rdf:Seq");
733 pugi::xpath_node params = superold ?
734 doc.select_node("//darktable:history_params/rdf:Bag"):
735 doc.select_node("//darktable:history_params/rdf:Seq");
736 pugi::xpath_node blendop_params = superold ?
737 doc.select_node("//darktable:blendop_params/rdf:Bag"):
738 doc.select_node("//darktable:blendop_params/rdf:Seq");
739 pugi::xpath_node blendop_version = superold ?
740 doc.select_node("//darktable:blendop_version/rdf:Bag"):
741 doc.select_node("//darktable:blendop_version/rdf:Seq");
742 pugi::xpath_node multi_priority = superold ?
743 doc.select_node("//darktable:multi_priority/rdf:Bag"):
744 doc.select_node("//darktable:multi_priority/rdf:Seq");
745 pugi::xpath_node multi_name = superold ?
746 doc.select_node("//darktable:multi_name/rdf:Bag"):
747 doc.select_node("//darktable:multi_name/rdf:Seq");
748#else
749 pugi::xpath_node modversion = superold ?
750 doc.select_single_node("//darktable:history_modversion/rdf:Bag"):
751 doc.select_single_node("//darktable:history_modversion/rdf:Seq");
752 pugi::xpath_node enabled = superold ?
753 doc.select_single_node("//darktable:history_enabled/rdf:Bag"):
754 doc.select_single_node("//darktable:history_enabled/rdf:Seq");
755 pugi::xpath_node operation = superold ?
756 doc.select_single_node("//darktable:history_operation/rdf:Bag"):
757 doc.select_single_node("//darktable:history_operation/rdf:Seq");
758 pugi::xpath_node params = superold ?
759 doc.select_single_node("//darktable:history_params/rdf:Bag"):
760 doc.select_single_node("//darktable:history_params/rdf:Seq");
761 pugi::xpath_node blendop_params = superold ?
762 doc.select_single_node("//darktable:blendop_params/rdf:Bag"):
763 doc.select_single_node("//darktable:blendop_params/rdf:Seq");
764 pugi::xpath_node blendop_version = superold ?
765 doc.select_single_node("//darktable:blendop_version/rdf:Bag"):
766 doc.select_single_node("//darktable:blendop_version/rdf:Seq");
767 pugi::xpath_node multi_priority = superold ?
768 doc.select_single_node("//darktable:multi_priority/rdf:Bag"):
769 doc.select_single_node("//darktable:multi_priority/rdf:Seq");
770 pugi::xpath_node multi_name = superold ?
771 doc.select_single_node("//darktable:multi_name/rdf:Bag"):
772 doc.select_single_node("//darktable:multi_name/rdf:Seq");
773#endif
774
775 // fill the list of history entries. we are iterating over history_operation as we know that it's there.
776 // the other iters are taken care of manually.
777 auto modversion_iter = modversion.node().children().begin();
778 auto enabled_iter = enabled.node().children().begin();
779 auto params_iter = params.node().children().begin();
780 auto blendop_params_iter = blendop_params.node().children().begin();
781 auto blendop_version_iter = blendop_version.node().children().begin();
782 auto multi_priority_iter = multi_priority.node().children().begin();
783 auto multi_name_iter = multi_name.node().children().begin();
784
785 for(pugi::xml_node operation_iter: operation.node().children())
786 {
787 history_entry_t *current_entry = (history_entry_t *)calloc(1, sizeof(history_entry_t));
788 current_entry->blendop_version = 1; // default version in case it's not specified
789 history_entries = g_list_append(history_entries, current_entry);
790
791 current_entry->operation = g_strdup(operation_iter.child_value());
792
793 current_entry->enabled = g_strcmp0(enabled_iter->child_value(), "0") != 0;
794
795 current_entry->modversion = atoi(modversion_iter->child_value());
796
797 const char *params_text = params_iter->child_value();
798 current_entry->params = dt_exif_xmp_decode(params_text, strlen(params_text),
799 &current_entry->params_len);
800
801 if(multi_name && multi_name_iter != multi_name.node().children().end())
802 {
803 current_entry->multi_name = g_strdup(multi_name_iter->child_value());
804 multi_name_iter++;
805 }
806
807 if(multi_priority && multi_priority_iter != multi_priority.node().children().end())
808 {
809 current_entry->multi_priority = atoi(multi_priority_iter->child_value());
810 multi_priority_iter++;
811 }
812
813 if(blendop_version && blendop_version_iter != blendop_version.node().children().end())
814 {
815 current_entry->blendop_version = atoi(blendop_version_iter->child_value());
816 blendop_version_iter++;
817 }
818
819 if(blendop_params && blendop_params_iter != blendop_params.node().children().end())
820 {
821 const char *blendop_text = blendop_params_iter->child_value();
822 current_entry->blendop_params = dt_exif_xmp_decode(blendop_text, strlen(blendop_text),
823 &current_entry->blendop_params_len);
824 blendop_params_iter++;
825 }
826
827 current_entry->iop_order = -1.0;
828
829 modversion_iter++;
830 enabled_iter++;
831 params_iter++;
832 }
833
834 return history_entries;
835}
836
837static GList *read_history_v2(Exiv2::XmpData &xmpData, const char *filename)
838{
839 GList *history_entries = NULL;
840 history_entry_t *current_entry = NULL;
841
842 for(auto history = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.history")); history != xmpData.end(); history++)
843 {
844 // TODO: support human readable params via introspection with something like this:
845 // XmpText: Xmp.darktable.history[1]/darktable:settings[1]/darktable:name = width
846 // XmpText: Xmp.darktable.history[1]/darktable:settings[1]/darktable:value = 23
847
848 std::string key_item = history->key();
849 char *key = g_strdup(key_item.c_str());
850 char *key_iter = key;
851 if(g_str_has_prefix(key, "Xmp.darktable.history["))
852 {
853 key_iter += strlen("Xmp.darktable.history[");
854 errno = 0;
855 unsigned int n = strtol(key_iter, &key_iter, 10);
856 if(errno)
857 {
858 std::cerr << "error reading history from '" << key << "' (" << filename << ")" << std::endl;
859 g_list_free_full(history_entries, free_history_entry);
860 history_entries = NULL;
861 dt_free(key);
862 return NULL;
863 }
864
865 // skip everything that isn't part of the actual array
866 if(*(key_iter++) != ']')
867 {
868 std::cerr << "error reading history from '"
869 << key << "' (" << filename << ")" << std::endl;
870 g_list_free_full(history_entries, free_history_entry);
871 history_entries = NULL;
872 dt_free(key);
873 return NULL;
874 }
875 if(*(key_iter++) != '/') goto skip;
876 if(*key_iter == '?') key_iter++;
877
878 // make sure we are filling in the details of the correct entry
879 unsigned int length = g_list_length(history_entries);
880 if(n > length)
881 {
882 current_entry = (history_entry_t *)calloc(1, sizeof(history_entry_t));
883 current_entry->blendop_version = 1; // default version in case it's not specified
884 current_entry->iop_order = -1.0;
885 history_entries = g_list_append(history_entries, current_entry);
886 }
887 else if(n < length)
888 {
889 // AFAICT this can't happen with regular exiv2 parsed XMP data, but better safe than sorry.
890 // it can happen though when constructing things in a unusual order and then passing it to us without
891 // serializing it in between
892 current_entry = (history_entry_t *)g_list_nth_data(history_entries, n - 1); // XMP starts counting at 1!
893 }
894
895 // go on reading things into current_entry
896 if(g_str_has_prefix(key_iter, "darktable:operation"))
897 {
898 current_entry->have_operation = TRUE;
899 std::string value_item = history->toString();
900 current_entry->operation = g_strdup(value_item.c_str());
901 }
902 else if(g_str_has_prefix(key_iter, "darktable:num"))
903 {
904 current_entry->num = history->toLong();
905 }
906 else if(g_str_has_prefix(key_iter, "darktable:enabled"))
907 {
908 current_entry->enabled = history->toLong() == 1;
909 }
910 else if(g_str_has_prefix(key_iter, "darktable:modversion"))
911 {
912 current_entry->have_modversion = TRUE;
913 current_entry->modversion = history->toLong();
914 }
915 else if(g_str_has_prefix(key_iter, "darktable:params"))
916 {
917 current_entry->have_params = TRUE;
918 std::string value_item = history->toString();
919 current_entry->params = dt_exif_xmp_decode(value_item.c_str(),
920 history->size(),
921 &current_entry->params_len);
922 }
923 /*
924 else if(g_str_has_prefix(key_iter, "darktable:multi_name_hand_edited"))
925 {
926 current_entry->multi_name_hand_edited = history->toLong() == 1;
927 }
928 */
929 else if(g_str_has_prefix(key_iter, "darktable:multi_name"))
930 {
931 std::string value_item = history->toString();
932 current_entry->multi_name = g_strdup(value_item.c_str());
933 }
934 else if(g_str_has_prefix(key_iter, "darktable:multi_priority"))
935 {
936 current_entry->multi_priority = history->toLong();
937 }
938 else if(g_str_has_prefix(key_iter, "darktable:iop_order"))
939 {
940 // we ensure reading the iop_order as a high precision float
941 std::string value_item = history->toString();
942 string str = g_strdup(value_item.c_str());
943 static const std::locale& c_locale = std::locale("C");
944 std::istringstream istring(str);
945 istring.imbue(c_locale);
946 istring >> current_entry->iop_order;
947 }
948 else if(g_str_has_prefix(key_iter, "darktable:blendop_version"))
949 {
950 current_entry->blendop_version = history->toLong();
951 }
952 else if(g_str_has_prefix(key_iter, "darktable:blendop_params"))
953 {
954 std::string value_item = history->toString();
955 current_entry->blendop_params =
956 dt_exif_xmp_decode(value_item.c_str(),
957 history->size(),
958 &current_entry->blendop_params_len);
959 }
960 }
961skip:
962 dt_free(key);
963 }
964
965 // a final sanity check
966 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
967 {
968 history_entry_t *entry = (history_entry_t *)iter->data;
969 if(!(entry->have_operation && entry->have_params && entry->have_modversion))
970 {
971 std::cerr << "[exif] error: reading history from '" << filename << "' failed due to missing tags" << std::endl;
972 g_list_free_full(history_entries, free_history_entry);
973 history_entries = NULL;
974 break;
975 }
976 }
977
978 return history_entries;
979}
980
981void free_mask_entry(gpointer data)
982{
983 mask_entry_t *entry = (mask_entry_t *)data;
984 dt_free(entry->mask_name);
985 dt_free(entry->mask_points);
986 dt_free(entry->mask_src);
987 dt_free(entry);
988}
989
990static GHashTable *read_masks(Exiv2::XmpData &xmpData, const char *filename, const int version)
991{
992 GHashTable *mask_entries = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, free_mask_entry);
993
994 // TODO: turn that into something like Xmp.darktable.history!
995 Exiv2::XmpData::iterator mask;
996 Exiv2::XmpData::iterator mask_name;
997 Exiv2::XmpData::iterator mask_type;
998 Exiv2::XmpData::iterator mask_version;
999 Exiv2::XmpData::iterator mask_id;
1000 Exiv2::XmpData::iterator mask_nb;
1001 Exiv2::XmpData::iterator mask_src;
1002 if((mask = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask"))) != xmpData.end()
1003 && (mask_src = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_src"))) != xmpData.end()
1004 && (mask_name = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_name"))) != xmpData.end()
1005 && (mask_type = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_type"))) != xmpData.end()
1006 && (mask_version = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_version"))) != xmpData.end()
1007 && (mask_id = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_id"))) != xmpData.end()
1008 && (mask_nb = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.mask_nb"))) != xmpData.end())
1009 {
1010 // fixes API change happened after exiv2 v0.27.2.1
1011 const size_t cnt = (size_t)mask->count();
1012 const size_t mask_src_cnt = (size_t)mask_src->count();
1013 const size_t mask_name_cnt = (size_t)mask_name->count();
1014 const size_t mask_type_cnt = (size_t)mask_type->count();
1015 const size_t mask_version_cnt = (size_t)mask_version->count();
1016 const size_t mask_id_cnt = (size_t)mask_id->count();
1017 const size_t mask_nb_cnt = (size_t)mask_nb->count();
1018 if(cnt == mask_src_cnt && cnt == mask_name_cnt && cnt == mask_type_cnt
1019 && cnt == mask_version_cnt && cnt == mask_id_cnt && cnt == mask_nb_cnt)
1020 {
1021 for(size_t i = 0; i < cnt; i++)
1022 {
1023 mask_entry_t *entry = (mask_entry_t *)calloc(1, sizeof(mask_entry_t));
1024
1025 entry->version = version;
1026 entry->mask_id = mask_id->toLong(i);
1027 entry->mask_type = mask_type->toLong(i);
1028 std::string mask_name_str = mask_name->toString(i);
1029 if(mask_name_str.c_str() != NULL)
1030 entry->mask_name = g_strdup(mask_name_str.c_str());
1031 else
1032 entry->mask_name = g_strdup("form");
1033
1034 entry->mask_version = mask_version->toLong(i);
1035
1036 std::string mask_str = mask->toString(i);
1037 const char *mask_c = mask_str.c_str();
1038 const size_t mask_c_len = mask_str.size();
1039 entry->mask_points = dt_exif_xmp_decode(mask_c, mask_c_len, &entry->mask_points_len);
1040
1041 entry->mask_nb = mask_nb->toLong(i);
1042
1043 std::string mask_src_str = mask_src->toString(i);
1044 const char *mask_src_c = mask_src_str.c_str();
1045 const size_t mask_src_c_len = mask_src_str.size();
1046 entry->mask_src = dt_exif_xmp_decode(mask_src_c, mask_src_c_len, &entry->mask_src_len);
1047
1048 g_hash_table_insert(mask_entries, &entry->mask_id, (gpointer)entry);
1049 }
1050 }
1051 }
1052
1053 return mask_entries;
1054}
1055
1056static GList *read_masks_v3(Exiv2::XmpData &xmpData, const char *filename, const int version)
1057{
1058 GList *history_entries = NULL;
1059 mask_entry_t *current_entry = NULL;
1060
1061 for(auto history = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.masks_history")); history != xmpData.end(); history++)
1062 {
1063 // TODO: support human readable params via introspection with something like this:
1064 // XmpText: Xmp.darktable.history[1]/darktable:settings[1]/darktable:name = width
1065 // XmpText: Xmp.darktable.history[1]/darktable:settings[1]/darktable:value = 23
1066
1067 std::string key_item = history->key();
1068 char *key = g_strdup(key_item.c_str());
1069 char *key_iter = key;
1070 if(g_str_has_prefix(key, "Xmp.darktable.masks_history["))
1071 {
1072 key_iter += strlen("Xmp.darktable.masks_history[");
1073 errno = 0;
1074 unsigned int n = strtol(key_iter, &key_iter, 10);
1075 if(errno)
1076 {
1077 std::cerr << "error reading masks history from '" << key << "' (" << filename << ")" << std::endl;
1078 g_list_free_full(history_entries, free_mask_entry);
1079 history_entries = NULL;
1080 dt_free(key);
1081 return NULL;
1082 }
1083
1084 // skip everything that isn't part of the actual array
1085 if(*(key_iter++) != ']')
1086 {
1087 std::cerr << "error reading masks history from '" << key << "' (" << filename << ")" << std::endl;
1088 g_list_free_full(history_entries, free_mask_entry);
1089 history_entries = NULL;
1090 dt_free(key);
1091 return NULL;
1092 }
1093 if(*(key_iter++) != '/') goto skip;
1094 if(*key_iter == '?') key_iter++;
1095
1096 // make sure we are filling in the details of the correct entry
1097 unsigned int length = g_list_length(history_entries);
1098 if(n > length)
1099 {
1100 current_entry = (mask_entry_t *)calloc(1, sizeof(mask_entry_t));
1101 current_entry->version = version;
1102 history_entries = g_list_append(history_entries, current_entry);
1103 }
1104 else if(n < length)
1105 {
1106 // AFAICT this can't happen with regular exiv2 parsed XMP data, but better safe than sorry.
1107 // it can happen though when constructing things in a unusual order and then passing it to us without
1108 // serializing it in between
1109 current_entry = (mask_entry_t *)g_list_nth_data(history_entries, n - 1); // XMP starts counting at 1!
1110 }
1111
1112 // go on reading things into current_entry
1113 if(g_str_has_prefix(key_iter, "darktable:mask_num"))
1114 {
1115 current_entry->mask_num = history->toLong();
1116 }
1117 else if(g_str_has_prefix(key_iter, "darktable:mask_id"))
1118 {
1119 current_entry->mask_id = history->toLong();
1120 }
1121 else if(g_str_has_prefix(key_iter, "darktable:mask_type"))
1122 {
1123 current_entry->mask_type = history->toLong();
1124 }
1125 else if(g_str_has_prefix(key_iter, "darktable:mask_name"))
1126 {
1127 std::string value_item = history->toString();
1128 current_entry->mask_name = g_strdup(value_item.c_str());
1129 }
1130 else if(g_str_has_prefix(key_iter, "darktable:mask_version"))
1131 {
1132 current_entry->mask_version = history->toLong();
1133 }
1134 else if(g_str_has_prefix(key_iter, "darktable:mask_points"))
1135 {
1136 std::string value_item = history->toString();
1137 current_entry->mask_points = dt_exif_xmp_decode(value_item.c_str(),
1138 history->size(),
1139 &current_entry->mask_points_len);
1140 }
1141 else if(g_str_has_prefix(key_iter, "darktable:mask_nb"))
1142 {
1143 current_entry->mask_nb = history->toLong();
1144 }
1145 else if(g_str_has_prefix(key_iter, "darktable:mask_src"))
1146 {
1147 std::string value_item = history->toString();
1148 current_entry->mask_src = dt_exif_xmp_decode(value_item.c_str(),
1149 history->size(),
1150 &current_entry->mask_src_len);
1151 }
1152
1153 }
1154skip:
1155 dt_free(key);
1156 }
1157
1158 return history_entries;
1159}
1160
1161static void add_mask_entry_to_db(int32_t imgid, mask_entry_t *entry)
1162{
1163 // add the mask entry only once
1164 if(entry->already_added)
1165 return;
1166
1167 const int mask_num = 0;
1168
1169 const int mask_num_used = (entry->version < 3) ? mask_num : entry->mask_num;
1170 const gboolean inserted =
1171 dt_history_repository_write_mask_item(imgid, mask_num_used, entry->mask_id, entry->mask_type,
1172 entry->mask_name, entry->mask_version, entry->mask_points,
1173 entry->mask_points_len, entry->mask_nb, entry->mask_src,
1174 entry->mask_src_len);
1175
1176 // Mark entry to true only after confirmation the sql insert was successful.
1177 //
1178 // This used to compare sqlite3_step()'s result against SQLITE_OK. An INSERT that runs to
1179 // completion returns SQLITE_DONE, so the test was never true, the flag was never set, and the
1180 // guard at the top of this function never fired. For a legacy sidecar (xmp_version < 3) every
1181 // non-clone mask is reached TWICE -- once by the hash-table pass over every entry, and again
1182 // by the per-history-item recursion through its group -- and once more per extra module
1183 // sharing that group. Each of those wrote another row into main.masks_history.
1184 if(inserted)
1185 {
1186 entry->already_added = TRUE;
1187 }
1188}
1189
1190static void add_non_clone_mask_entries_to_db(gpointer key, gpointer value, gpointer user_data)
1191{
1192 int32_t imgid = *(int *)user_data;
1193 mask_entry_t *entry = (mask_entry_t *)value;
1194 if(!(entry->mask_type & (DT_MASKS_CLONE | DT_MASKS_NON_CLONE))) add_mask_entry_to_db(imgid, entry);
1195}
1196
1197static void add_mask_entries_to_db(int32_t imgid, GHashTable *mask_entries, int mask_id)
1198{
1199 if(mask_id <= 0) return;
1200
1201 // look for mask_id in the hash table
1202 mask_entry_t *entry = (mask_entry_t *)g_hash_table_lookup(mask_entries, &mask_id);
1203
1204 if(IS_NULL_PTR(entry)) return;
1205
1206 // if it's a group: recurse into the children first
1207 if(entry->mask_type & DT_MASKS_GROUP)
1208 {
1210 if((int)(entry->mask_nb * sizeof(dt_masks_form_group_t)) != entry->mask_points_len)
1211 {
1212 fprintf(stderr, "[masks] error loading masks from xmp file, bad binary blob size.\n");
1213 return;
1214 }
1215 for(int i = 0; i < entry->mask_nb; i++)
1216 add_mask_entries_to_db(imgid, mask_entries, group[i].formid);
1217 }
1218
1219 add_mask_entry_to_db(imgid, entry);
1220}
1221
1222// get MAX multi_priority
1223int _get_max_multi_priority(GList *history, const char *operation)
1224{
1225 int max_prio = 0;
1226
1227 for(GList *iter = history; iter; iter = g_list_next(iter))
1228 {
1229 history_entry_t *entry = (history_entry_t *)iter->data;
1230
1231 if(!strcmp(entry->operation, operation))
1232 max_prio = MAX(max_prio, entry->multi_priority);
1233 }
1234
1235 return max_prio;
1236}
1237
1238// need a write lock on *img (non-const) to write stars (and soon color labels).
1239int dt_exif_xmp_read(dt_image_t *img, const char *filename, const int history_only)
1240{
1241 // Neither argument was checked, and this is a public entry point: common/image.c passes
1242 // a dt_image_cache_get() result straight in, which is nullable everywhere else in the
1243 // tree. Non-zero is this function's existing "did not read it" answer, and every caller
1244 // already tests for it.
1245 if(IS_NULL_PTR(img) || IS_NULL_PTR(filename)) return 1;
1246
1247 // exclude pfm to avoid stupid errors on the console.
1248 // The length is checked BEFORE the pointer is formed: `filename + strlen(filename) - 4'
1249 // on a name shorter than four characters computes a pointer before the start of the
1250 // array, which is undefined behaviour -- only one-past-the-end is legal. The `c >=
1251 // filename' test that used to follow cannot rescue that; the pointer is already invalid.
1252 const size_t filename_len = strlen(filename);
1253 if(filename_len >= 4 && !strcmp(filename + filename_len - 4, ".pfm")) return 1;
1254 try
1255 {
1256 // read xmp sidecar
1257 std::unique_ptr<Exiv2::Image> image(Exiv2::ImageFactory::open(WIDEN(filename)));
1258 if(!image.get()) return 1;
1259 image->readMetadata();
1260 Exiv2::XmpData &xmpData = image->xmpData();
1261
1262 Exiv2::XmpData::iterator pos;
1263
1264 int xmp_version = 0;
1265 GList *iop_order_list = NULL;
1266 dt_iop_order_t iop_order_version = DT_IOP_ORDER_LEGACY;
1267
1268 int num_masks = 0;
1269 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.xmp_version"))) != xmpData.end())
1270 xmp_version = pos->toLong();
1271
1272 if(!history_only)
1273 {
1274 // otherwise we ignore title, description, ... from non-dt xmp files :(
1275 const size_t ns_pos = image->xmpPacket().find("xmlns:darktable=\"http://darktable.sf.net/\"");
1276 const bool is_a_dt_xmp = (ns_pos != std::string::npos);
1277 dt_exif_decode_xmp_data(img, xmpData, is_a_dt_xmp ? xmp_version : -1, false);
1278 }
1279
1280
1281 // convert legacy flip bits (will not be written anymore, convert to flip history item here):
1282 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.raw_params"))) != xmpData.end())
1283 {
1284 union {
1285 int32_t in;
1287 } raw_params;
1288 raw_params.in = pos->toLong();
1289 const int32_t user_flip = raw_params.out.user_flip;
1290 img->legacy_flip.user_flip = user_flip;
1291 img->legacy_flip.legacy = 0;
1292 }
1293
1294 int32_t preset_applied = 0;
1295
1296 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.auto_presets_applied"))) != xmpData.end())
1297 {
1298 preset_applied = pos->toLong();
1299
1300 // in any case, this is no legacy image.
1302 }
1303 else if(xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.xmp_version")) == xmpData.end())
1304 {
1305 // if there is no darktable xmp_version in the XMP, this XMP must have been generated by another
1306 // program; since this is the first time darktable sees it, there can't be legacy presets
1308 }
1309 else
1310 {
1311 // so we are legacy (thus have to clear the no-legacy flag)
1313 }
1314 // when we are reading the xmp data it doesn't make sense to flag the image as removed
1315 img->flags &= ~DT_IMAGE_REMOVE;
1316
1317 if(xmp_version == 4 || xmp_version == 5)
1318 {
1319 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.iop_order_version"))) != xmpData.end())
1320 {
1321 iop_order_version = (dt_iop_order_t)pos->toLong();
1322 }
1323
1324 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.iop_order_list"))) != xmpData.end())
1325 {
1326 iop_order_list = dt_ioppr_deserialize_text_iop_order_list(pos->toString().c_str());
1327 // insert modules created after this edit's order was serialized, so
1328 // their history entries land in the right pipeline position
1329 if(iop_order_list) iop_order_list = dt_ioppr_insert_missing_modules(iop_order_list);
1330 }
1331 else
1332 iop_order_list = dt_ioppr_get_iop_order_list_version(iop_order_version);
1333 }
1334 else if(xmp_version == 3)
1335 {
1336 iop_order_version = DT_IOP_ORDER_LEGACY;
1337
1338 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.iop_order_version"))) != xmpData.end())
1339 {
1340 // All iop-order version before 3 are legacy one. Starting with version 3 we have the first
1341 // attempts to propose the final v3 iop-order.
1342 iop_order_version = pos->toLong() < 3 ? DT_IOP_ORDER_LEGACY : DT_IOP_ORDER_ANSEL_RAW;
1343 iop_order_list = dt_ioppr_get_iop_order_list_version(iop_order_version);
1344 }
1345 else
1347 }
1348 else
1349 {
1350 iop_order_version = DT_IOP_ORDER_LEGACY;
1352 }
1353
1354 // masks
1355 GHashTable *mask_entries = NULL;
1356 GList *mask_entries_v3 = NULL;
1357
1358 // clean all old masks for this image
1360
1361 // read the masks from the file first so we can add them to the db while reading history entries
1362 if(xmp_version < 3)
1363 mask_entries = read_masks(xmpData, filename, xmp_version);
1364 else
1365 mask_entries_v3 = read_masks_v3(xmpData, filename, xmp_version);
1366
1367 // now add all masks that are not used for cloning. keeping them might be useful.
1368 // TODO: make this configurable? or remove it altogether?
1370
1371 if(xmp_version < 3)
1372 {
1373 g_hash_table_foreach(mask_entries, add_non_clone_mask_entries_to_db, &img->id);
1374 }
1375 else
1376 {
1377 for(GList *m_entries = g_list_first(mask_entries_v3); m_entries; m_entries = g_list_next(m_entries))
1378 {
1379 mask_entry_t *mask_entry = (mask_entry_t *)m_entries->data;
1380
1381 add_mask_entry_to_db(img->id, mask_entry);
1382 }
1383 }
1384
1386
1387 // history
1388 int num = 0;
1389 gboolean all_ok = TRUE;
1390 GList *history_entries = NULL;
1391
1392 if(xmp_version < 2)
1393 {
1394 std::string &xmpPacket = image->xmpPacket();
1395 history_entries = read_history_v1(xmpPacket, filename, 0);
1396 if(!history_entries) // didn't work? try super old version with rdf:Bag
1397 history_entries = read_history_v1(xmpPacket, filename, 1);
1398 }
1399 else if(xmp_version == 2 || xmp_version == 3 || xmp_version == 4 || xmp_version == 5 )
1400 history_entries = read_history_v2(xmpData, filename);
1401 else
1402 {
1403 std::cerr << "error: Xmp schema version " << xmp_version << " in " << filename << " not supported" << std::endl;
1404 g_hash_table_destroy(mask_entries);
1405 return 1;
1406 }
1407
1409
1411 {
1412 fprintf(stderr, "[exif] error deleting history for image %d\n", img->id);
1413 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1414 all_ok = FALSE;
1415 goto end;
1416 }
1417
1418 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
1419 {
1420 history_entry_t *entry = (history_entry_t *)iter->data;
1421 const int db_num = (xmp_version < 3) ? num : entry->num;
1422
1423 if(entry->blendop_params)
1424 {
1425 if(xmp_version < 3)
1426 {
1427 // check what mask entries belong to this iop and add them to the db
1428 const dt_develop_blend_params_t *blendop_params = (dt_develop_blend_params_t *)entry->blendop_params;
1429 add_mask_entries_to_db(img->id, mask_entries, blendop_params->mask_id);
1430 }
1431 }
1432
1433 if(!dt_history_repository_write_item(img->id, db_num, entry->operation,
1434 entry->params, entry->params_len,
1435 entry->modversion, entry->enabled != 0,
1436 entry->blendop_params, entry->blendop_params_len,
1437 entry->blendop_version, entry->multi_priority,
1438 entry->multi_name ? entry->multi_name : ""))
1439 {
1440 fprintf(stderr, "[exif] error adding history entry for image %d\n", img->id);
1441 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1442 all_ok = FALSE;
1443 goto end;
1444 }
1445
1446 num++;
1447 }
1448
1449 // we now need to create and store the proper iop-order taking into account all multi-instances
1450 // for previous xmp versions.
1451
1452 if(xmp_version < 4)
1453 {
1454 // in this version we had iop-order, use it
1455
1456 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
1457 {
1458 history_entry_t *entry = (history_entry_t *)iter->data;
1459
1461 memcpy(e->operation, entry->operation, sizeof(e->operation));
1462 e->instance = entry->multi_priority;
1463
1464 if(xmp_version < 3)
1465 {
1466 // prior to v3 there was no iop-order, all multi instances where grouped, use the multièpriority
1467 // to restore the order.
1468 GList *base_order = dt_ioppr_get_iop_order_link(iop_order_list, entry->operation, -1);
1469
1470 if(base_order)
1471 e->o.iop_order_f = ((dt_iop_order_entry_t *)(base_order->data))->o.iop_order_f
1472 - entry->multi_priority / 100.0f;
1473 else if(!dt_deprecated(entry->operation))
1474 {
1475 fprintf(stderr,
1476 "[exif] cannot get iop-order for module '%s', XMP may be corrupted\n",
1477 entry->operation);
1478 g_list_free_full(iop_order_list, dt_free_gpointer);
1479 iop_order_list = NULL;
1480 g_list_free_full(history_entries, free_history_entry);
1481 history_entries = NULL;
1482 g_list_free_full(mask_entries_v3, free_mask_entry);
1483 mask_entries_v3 = NULL;
1484 if(mask_entries) g_hash_table_destroy(mask_entries);
1485 dt_free(e);
1486 return 1;
1487 }
1488 }
1489 else
1490 {
1491 // otherwise use the iop_order for the entry
1492 e->o.iop_order_f = entry->iop_order; // legacy iop-order is used to insert item at the right location
1493 }
1494
1495 // remove a current entry from the iop-order list if found as it will be replaced, possibly with another iop-order
1496 // with a new item in the history.
1497
1498 GList *link = dt_ioppr_get_iop_order_link(iop_order_list, e->operation, e->instance);
1499 if(link) iop_order_list = g_list_delete_link(iop_order_list, link);
1500
1501 iop_order_list = g_list_append(iop_order_list, e);
1502 }
1503
1504 // and finally reorder the full list based on the iop-order
1505
1506 iop_order_list = g_list_sort(iop_order_list, dt_sort_iop_list_by_order_f);
1507 }
1508
1509 // if masks have been read, create a mask manager entry in history
1510 if(xmp_version < 3)
1511 {
1513
1514 if(num_masks > 0)
1515 {
1516 // make room for mask_manager entry
1518 {
1519 fprintf(stderr, "[exif] error shifting history nums for image %d\n", img->id);
1520 all_ok = FALSE;
1521 goto end;
1522 }
1523 if(!dt_history_repository_write_item(img->id, 0, "mask_manager", NULL, 0, 1, 0, NULL, 0, 0, 0, ""))
1524 {
1525 fprintf(stderr, "[exif] error adding mask history entry for image %d\n", img->id);
1526 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1527 all_ok = FALSE;
1528 goto end;
1529 }
1530
1531 num++;
1532 }
1533 }
1534
1535 // we shouldn't change history_end when no history was read!
1536 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.history_end"))) != xmpData.end() && num > 0)
1537 {
1538 int history_end = MIN(pos->toLong(), num);
1539 if(num_masks > 0) history_end++;
1540 if((history_end < 1) && preset_applied) preset_applied = -1;
1541 if(!dt_history_repository_set_end(img->id, history_end))
1542 {
1543 fprintf(stderr, "[exif] error writing history_end for image %d\n", img->id);
1544 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1545 all_ok = FALSE;
1546 goto end;
1547 }
1548 }
1549 else
1550 {
1551 if(preset_applied) preset_applied = -1;
1552 const int32_t history_end = dt_history_repository_get_next_num(img->id);
1553 if(!dt_history_repository_set_end(img->id, history_end))
1554 {
1555 fprintf(stderr, "[exif] error writing history_end for image %d\n", img->id);
1556 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1557 all_ok = FALSE;
1558 goto end;
1559 }
1560 }
1561 if(!dt_ioppr_write_iop_order_list(iop_order_list, img->id))
1562 {
1563 fprintf(stderr, "[exif] error writing iop_list for image %d\n", img->id);
1564 fprintf(stderr, "[exif] %s\n", dt_database_get_last_error());
1565 all_ok = FALSE;
1566 goto end;
1567 }
1568
1569 end:
1570
1571 read_xmp_timestamps(xmpData, img, xmp_version);
1572
1573 // set or clear bit in image struct. ONLY set if the Xmp.darktable.auto_presets_applied was 1
1574 // AND there was a history in xmp
1575 if(preset_applied > 0)
1576 {
1578 }
1579 else
1580 {
1581 // not found for old or buggy xmp where it was found but history was 0
1583
1584 if(preset_applied < 0)
1585 {
1586 fprintf(stderr,"[exif] dt_exif_xmp_read for %s, id %i found auto_presets_applied but there was no history\n",filename,img->id);
1587 }
1588 }
1589
1590 g_list_free_full(iop_order_list, dt_free_gpointer);
1591 iop_order_list = NULL;
1592 g_list_free_full(history_entries, free_history_entry);
1593 history_entries = NULL;
1594 g_list_free_full(mask_entries_v3, free_mask_entry);
1595 mask_entries_v3 = NULL;
1596 if(mask_entries) g_hash_table_destroy(mask_entries);
1597
1598 if(all_ok)
1599 {
1601
1602 // history_hash (current only)
1603 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.history_current_hash"))) != xmpData.end())
1604 {
1605 int hash_len = 0;
1606 // one temporary, measured once: toString() called twice returns two distinct
1607 // strings, so the length came from a different object than the pointer did --
1608 // same contents, so it worked, but nothing said it had to
1609 const std::string hash_str = pos->toString();
1610 unsigned char *decoded = dt_exif_xmp_decode(hash_str.c_str(), hash_str.size(), &hash_len);
1611 if(decoded && hash_len == (int)sizeof(uint64_t))
1612 {
1613 uint64_t be_hash = 0;
1614 memcpy(&be_hash, decoded, sizeof(be_hash));
1615 img->history_hash = GUINT64_FROM_BE(be_hash);
1616 }
1617 dt_free(decoded);
1618 }
1619 }
1620 else
1621 {
1622 std::cerr << "[exif] error reading history from '" << filename << "'" << std::endl;
1624 return 1;
1625 }
1626
1627 }
1628 catch(const std::exception &e)
1629 {
1630 // actually nobody's interested in that if the file doesn't exist:
1631 // std::string s(e.what());
1632 // std::cerr << "[exiv2] " << filename << ": " << s << std::endl;
1633 return 1;
1634 }
1635 return 0;
1636}
1637
1638// add history metadata to XmpData
1639typedef struct _xmp_mask_ctx_t
1640{
1641 Exiv2::XmpData *xmpData;
1642 int num;
1644
1645static void _xmp_append_mask(void *user_data, const int mask_num, const int mask_id,
1646 const int mask_type, const char *mask_name, const int mask_version,
1647 const void *points, const int points_len, const int mask_nb,
1648 const void *source, const int source_len)
1649{
1650 _xmp_mask_ctx_t *ctx = (_xmp_mask_ctx_t *)user_data;
1651 Exiv2::XmpData &xmpData = *ctx->xmpData;
1652 char key[1024];
1653 const int num = ctx->num;
1654
1655 char *mask_d = dt_exif_xmp_encode((const unsigned char *)points, points_len, NULL);
1656 char *mask_src = dt_exif_xmp_encode((const unsigned char *)source, source_len, NULL);
1657
1658 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_num", num);
1659 xmpData[key] = mask_num;
1660 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_id", num);
1661 xmpData[key] = mask_id;
1662 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_type", num);
1663 xmpData[key] = mask_type;
1664 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_name", num);
1665 xmpData[key] = mask_name;
1666 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_version", num);
1667 xmpData[key] = mask_version;
1668 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_points", num);
1669 xmpData[key] = mask_d;
1670 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_nb", num);
1671 xmpData[key] = mask_nb;
1672 snprintf(key, sizeof(key), "Xmp.darktable.masks_history[%d]/darktable:mask_src", num);
1673 xmpData[key] = mask_src;
1674
1675 dt_free(mask_d);
1676 dt_free(mask_src);
1677
1678 ctx->num++;
1679}
1680
1682{
1683 Exiv2::XmpData *xmpData;
1684 int num;
1686
1687static void _xmp_append_history(void *user_data, const int32_t row_imgid, const int hist_num,
1688 const int modversion, const char *operation,
1689 const void *op_params, const int params_len,
1690 const gboolean enabled, const void *blendop_params_blob,
1691 const int blendop_params_len, const int blendop_version,
1692 const int multi_priority, const char *multi_name,
1693 const char *preset_name)
1694{
1695 _xmp_history_ctx_t *ctx = (_xmp_history_ctx_t *)user_data;
1696 Exiv2::XmpData &xmpData = *ctx->xmpData;
1697 char key[1024];
1698 const int num = ctx->num;
1699
1700 if(IS_NULL_PTR(operation)) return; // no op is fatal.
1701
1702 char *params = dt_exif_xmp_encode((const unsigned char *)op_params, params_len, NULL);
1703
1704 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:num", num);
1705 xmpData[key] = hist_num;
1706 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:operation", num);
1707 xmpData[key] = operation;
1708 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:enabled", num);
1709 xmpData[key] = enabled;
1710 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:modversion", num);
1711 xmpData[key] = modversion;
1712 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:params", num);
1713 xmpData[key] = params;
1714 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:multi_name", num);
1715 xmpData[key] = multi_name ? multi_name : "";
1716 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:multi_priority", num);
1717 xmpData[key] = multi_priority;
1718
1719 if(blendop_params_blob)
1720 {
1721 // this shouldn't fail in general, but reading is robust enough to allow it,
1722 // and flipping images from LT will result in this being left out
1723 char *blendop_params = dt_exif_xmp_encode((const unsigned char *)blendop_params_blob, blendop_params_len, NULL);
1724 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:blendop_version", num);
1725 xmpData[key] = blendop_version;
1726 snprintf(key, sizeof(key), "Xmp.darktable.history[%d]/darktable:blendop_params", num);
1727 xmpData[key] = blendop_params;
1728 dt_free(blendop_params);
1729 }
1730
1731 dt_free(params);
1732
1733 ctx->num++;
1734}
1735
1736static void dt_set_xmp_dt_history(Exiv2::XmpData &xmpData, const int32_t imgid, int history_end)
1737{
1738 // masks history:
1739 int num = 1;
1740
1741 // create an array:
1742 Exiv2::XmpTextValue tvm("");
1743 tvm.setXmpArrayType(Exiv2::XmpValue::xaSeq);
1744 xmpData.add(Exiv2::XmpKey("Xmp.darktable.masks_history"), &tvm);
1745 _xmp_mask_ctx_t mask_ctx = { &xmpData, num };
1747 num = mask_ctx.num;
1748
1749 // history stack:
1750 num = 1;
1751
1752 // create an array:
1753 Exiv2::XmpTextValue tv("");
1754 tv.setXmpArrayType(Exiv2::XmpValue::xaSeq);
1755 xmpData.add(Exiv2::XmpKey("Xmp.darktable.history"), &tv);
1756 _xmp_history_ctx_t hist_ctx = { &xmpData, num };
1758 num = hist_ctx.num;
1759 if(history_end == -1) history_end = num - 1;
1760 else history_end = MIN(history_end, num - 1); // safeguard for some old buggy libraries
1761 xmpData["Xmp.darktable.history_end"] = history_end;
1762}
1763
1764// add timestamps to XmpData.
1765static void set_xmp_timestamps(Exiv2::XmpData &xmpData, const int32_t imgid)
1766{
1767 static const char *keys[] =
1768 {
1769 "Xmp.darktable.import_timestamp",
1770 "Xmp.darktable.change_timestamp",
1771 "Xmp.darktable.export_timestamp",
1772 "Xmp.darktable.print_timestamp"
1773 };
1774 static const guint n_keys = G_N_ELEMENTS(keys);
1775 dt_remove_xmp_keys(xmpData, keys, n_keys);
1776
1779 {
1780 if(ts.has_import) xmpData["Xmp.darktable.import_timestamp"] = ts.import_timestamp;
1781 if(ts.has_change) xmpData["Xmp.darktable.change_timestamp"] = ts.change_timestamp;
1782 if(ts.has_export) xmpData["Xmp.darktable.export_timestamp"] = ts.export_timestamp;
1783 if(ts.has_print) xmpData["Xmp.darktable.print_timestamp"] = ts.print_timestamp;
1784 }
1785}
1786
1787GTimeSpan _convert_unix_to_gtimespan(const time_t unix)
1788{
1789 GDateTime *gdt = g_date_time_new_from_unix_utc(unix);
1790 if(gdt)
1791 {
1792 GTimeSpan gts = dt_datetime_gdatetime_to_gtimespan(gdt);
1793 g_date_time_unref(gdt);
1794 return gts;
1795 }
1796 return 0;
1797}
1798
1799// read timestamps from XmpData
1800void read_xmp_timestamps(Exiv2::XmpData &xmpData, dt_image_t *img, const int xmp_version)
1801{
1802 Exiv2::XmpData::iterator pos;
1803
1804 // Do not read for import_ts. It must be updated at each import.
1805 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.change_timestamp"))) != xmpData.end())
1806 {
1807 if(xmp_version > 5)
1808 img->change_timestamp = pos->toLong();
1809 else if(pos->toLong() >= 1)
1810 img->change_timestamp = _convert_unix_to_gtimespan(pos->toLong());
1811 }
1812 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.export_timestamp"))) != xmpData.end())
1813 {
1814 if(xmp_version > 5)
1815 img->export_timestamp = pos->toLong();
1816 else if(pos->toLong() >= 1)
1817 img->export_timestamp = _convert_unix_to_gtimespan(pos->toLong());
1818 }
1819 if((pos = xmpData.findKey(Exiv2::XmpKey("Xmp.darktable.print_timestamp"))) != xmpData.end())
1820 {
1821 if(xmp_version > 5)
1822 img->print_timestamp = pos->toLong();
1823 else if(pos->toLong() >= 1)
1824 img->print_timestamp = _convert_unix_to_gtimespan(pos->toLong());
1825 }
1826}
1827
1828static void dt_remove_xmp_exif_geotag(Exiv2::XmpData &xmpData)
1829{
1830 static const char *keys[] =
1831 {
1832 "Xmp.exif.GPSVersionID",
1833 "Xmp.exif.GPSLongitude",
1834 "Xmp.exif.GPSLatitude",
1835 "Xmp.exif.GPSAltitudeRef",
1836 "Xmp.exif.GPSAltitude"
1837 };
1838 static const guint n_keys = G_N_ELEMENTS(keys);
1839 dt_remove_xmp_keys(xmpData, keys, n_keys);
1840}
1841
1842static void dt_set_xmp_exif_geotag(Exiv2::XmpData &xmpData, double longitude, double latitude, double altitude)
1843{
1845 if(!isnan(longitude) && !isnan(latitude))
1846 {
1847 char long_dir = 'E', lat_dir = 'N';
1848 if(longitude < 0) long_dir = 'W';
1849 if(latitude < 0) lat_dir = 'S';
1850
1851 longitude = fabs(longitude);
1852 latitude = fabs(latitude);
1853
1854 int long_deg = (int)floor(longitude);
1855 int lat_deg = (int)floor(latitude);
1856 double long_min = (longitude - (double)long_deg) * 60.0;
1857 double lat_min = (latitude - (double)lat_deg) * 60.0;
1858
1859 char *str = (char *)g_malloc(G_ASCII_DTOSTR_BUF_SIZE);
1860
1861 g_ascii_formatd(str, G_ASCII_DTOSTR_BUF_SIZE, "%08f", long_min);
1862 gchar *long_str = g_strdup_printf("%d,%s%c", long_deg, str, long_dir);
1863 g_ascii_formatd(str, G_ASCII_DTOSTR_BUF_SIZE, "%08f", lat_min);
1864 gchar *lat_str = g_strdup_printf("%d,%s%c", lat_deg, str, lat_dir);
1865
1866 xmpData["Xmp.exif.GPSVersionID"] = "2.2.0.0";
1867 xmpData["Xmp.exif.GPSLongitude"] = long_str;
1868 xmpData["Xmp.exif.GPSLatitude"] = lat_str;
1869 dt_free(long_str);
1870 dt_free(lat_str);
1871 dt_free(str);
1872 }
1873 if(!isnan(altitude))
1874 {
1875 xmpData["Xmp.exif.GPSAltitudeRef"] = (altitude < 0) ? "1" : "0";
1876
1877 long ele_dm = (int)floor(fabs(10.0 * altitude));
1878 gchar *ele_str = g_strdup_printf("%ld/10", ele_dm);
1879 xmpData["Xmp.exif.GPSAltitude"] = ele_str;
1880 dt_free(ele_str);
1881 }
1882}
1883
1885{
1886 Exiv2::XmpData *xmpData;
1887 gboolean export_flag;
1889
1890static void _xmp_append_metadata(void *user_data, const int keyid, const char *value)
1891{
1892 _xmp_metadata_ctx_t *ctx = (_xmp_metadata_ctx_t *)user_data;
1893 Exiv2::XmpData &xmpData = *ctx->xmpData;
1894
1896 {
1897 const gchar *name = dt_metadata_get_name(keyid);
1898 gchar *setting = dt_util_dstrcat(NULL, "plugins/lighttable/metadata/%s_flag", name);
1899 const uint32_t flag = dt_conf_get_int(setting);
1900 dt_free(setting);
1902 xmpData[dt_metadata_get_key(keyid)] = value;
1903 }
1904 else
1905 xmpData[dt_metadata_get_key(keyid)] = value;
1906}
1907
1908static void _xmp_append_colorlabel(void *user_data, const int color)
1909{
1910 Exiv2::Value *v = (Exiv2::Value *)user_data;
1911 char val[2048];
1912 snprintf(val, sizeof(val), "%d", color);
1913 v->read(val);
1914}
1915
1916static void dt_set_xmp_dt_metadata(Exiv2::XmpData &xmpData, const int32_t imgid, const gboolean export_flag)
1917{
1918 // metadata
1919 _xmp_metadata_ctx_t meta_ctx = { &xmpData, export_flag };
1921
1922 // color labels
1923 std::unique_ptr<Exiv2::Value> v(Exiv2::Value::create(Exiv2::xmpSeq)); // or xmpBag or xmpAlt.
1924
1925 /* Already initialized v = Exiv2::Value::create(Exiv2::xmpSeq); // or xmpBag or xmpAlt.*/
1927 if(v->count() > 0) xmpData.add(Exiv2::XmpKey("Xmp.darktable.colorlabels"), v.get());
1928}
1929
1930// helper to create an xmp data thing. throws exiv2 exceptions if stuff goes wrong.
1931static void _exif_xmp_append_history_hash(Exiv2::XmpData &xmpData, const int32_t imgid,
1932 const dt_image_t *image)
1933{
1934 const dt_image_t *cached = image;
1935 if(IS_NULL_PTR(cached))
1936 cached = dt_image_cache_get(imgid, 'r');
1937
1938 if(cached)
1939 {
1940 if(cached->history_hash != UINT64_MAX)
1941 {
1942 const uint64_t be_hash = GUINT64_TO_BE(cached->history_hash);
1943 char *value = dt_exif_xmp_encode(reinterpret_cast<const unsigned char *>(&be_hash), sizeof(be_hash), NULL);
1944 if(value)
1945 {
1946 xmpData["Xmp.darktable.history_current_hash"] = value;
1947 dt_free(value);
1948 }
1949 }
1950 if(IS_NULL_PTR(image))
1952 }
1953}
1954
1955static void _exif_xmp_read_data(Exiv2::XmpData &xmpData, const int32_t imgid, const dt_image_t *image)
1956{
1957 const int xmp_version = DT_XMP_EXIF_VERSION;
1958 int stars = 1, raw_params = 0, history_end = -1;
1959 double longitude = NAN, latitude = NAN, altitude = NAN;
1960 gchar *filename = NULL;
1961 gchar *iop_order_list = NULL;
1962 GTimeSpan gts = 0;
1963
1964 // get stars and raw params from db
1967 {
1968 filename = row.filename;
1969 stars = row.flags;
1970 raw_params = row.raw_parameters;
1971 if(row.has_longitude) longitude = row.longitude;
1972 if(row.has_latitude) latitude = row.latitude;
1973 if(row.has_altitude) altitude = row.altitude;
1974 history_end = row.history_end;
1975 gts = row.datetime_taken;
1976 }
1977
1978 // The pipe order only matters when history entries are exported. After
1979 // deleting history, avoid rebuilding a default order just to write an empty
1980 // sidecar: the default will be selected again when the image is opened.
1981 dt_iop_order_t iop_order_version = DT_IOP_ORDER_ANSEL_RAW;
1982 if(history_end > 0)
1983 {
1984 iop_order_version = dt_ioppr_get_iop_order_version(imgid);
1985 GList *iop_list = dt_ioppr_get_iop_order_list(imgid, TRUE);
1986
1987 if(iop_order_version == DT_IOP_ORDER_CUSTOM || dt_ioppr_has_multiple_instances(iop_list))
1988 {
1989 iop_order_list = dt_ioppr_serialize_text_iop_order_list(iop_list);
1990 }
1991 g_list_free_full(iop_list, dt_free_gpointer);
1992 iop_list = NULL;
1993 }
1994
1995 // Store datetime_taken as DateTimeOriginal to take into account the user's selected date/time
1996 gchar exif_datetime[DT_DATETIME_LENGTH];
1997 dt_datetime_gtimespan_to_exif(exif_datetime, sizeof(exif_datetime), gts);
1998 xmpData["Xmp.exif.DateTimeOriginal"] = exif_datetime;
1999
2000 // We have to erase the old ratings first as exiv2 seems to not change it otherwise.
2001 Exiv2::XmpData::iterator pos = xmpData.findKey(Exiv2::XmpKey("Xmp.xmp.Rating"));
2002 if(pos != xmpData.end()) xmpData.erase(pos);
2003 xmpData["Xmp.xmp.Rating"] = dt_image_get_xmp_rating_from_flags(stars);
2004
2005 // The original file name
2006 if(filename) xmpData["Xmp.xmpMM.DerivedFrom"] = filename;
2007
2008 // timestamps
2009 set_xmp_timestamps(xmpData, imgid);
2010
2011 // GPS data
2012 dt_set_xmp_exif_geotag(xmpData, longitude, latitude, altitude);
2013
2014 // the meta data
2015 dt_set_xmp_dt_metadata(xmpData, imgid, FALSE);
2016
2017 // get tags from db, store in dublin core
2018 std::unique_ptr<Exiv2::Value> v1(Exiv2::Value::create(Exiv2::xmpBag));
2019
2020 std::unique_ptr<Exiv2::Value> v2(Exiv2::Value::create(Exiv2::xmpBag));
2021
2022 GList *tags = dt_tag_get_list(imgid);
2023 try
2024 {
2025 for(GList *tag = tags; tag; tag = g_list_next(tag))
2026 {
2027 v1->read((char *)tag->data);
2028 }
2029 }
2030 catch(...)
2031 {
2032 g_list_free_full(tags, dt_free_gpointer);
2033 throw;
2034 }
2035 if(v1->count() > 0) xmpData.add(Exiv2::XmpKey("Xmp.dc.subject"), v1.get());
2036 g_list_free_full(tags, dt_free_gpointer);
2037 tags = NULL;
2038
2039 GList *hierarchical = dt_tag_get_hierarchical(imgid);
2040 try
2041 {
2042 for(GList *hier = hierarchical; hier; hier = g_list_next(hier))
2043 {
2044 v2->read((char *)hier->data);
2045 }
2046 }
2047 catch(...)
2048 {
2049 g_list_free_full(hierarchical, dt_free_gpointer);
2050 throw;
2051 }
2052 if(v2->count() > 0) xmpData.add(Exiv2::XmpKey("Xmp.lr.hierarchicalSubject"), v2.get());
2053 g_list_free_full(hierarchical, dt_free_gpointer);
2054 hierarchical = NULL;
2055 /* TODO: Add tags to IPTC namespace as well */
2056
2057 xmpData["Xmp.darktable.xmp_version"] = xmp_version;
2058 xmpData["Xmp.darktable.raw_params"] = raw_params;
2060 xmpData["Xmp.darktable.auto_presets_applied"] = 1;
2061 else
2062 xmpData["Xmp.darktable.auto_presets_applied"] = 0;
2063 dt_set_xmp_dt_history(xmpData, imgid, history_end);
2064
2065 if(history_end > 0)
2066 {
2067 xmpData["Xmp.darktable.iop_order_version"] = iop_order_version;
2068 if(iop_order_list) xmpData["Xmp.darktable.iop_order_list"] = iop_order_list;
2069 }
2070
2071 _exif_xmp_append_history_hash(xmpData, imgid, image);
2072
2073 dt_free(iop_order_list);
2075}
2076
2077// helper to create an xmp data thing. throws exiv2 exceptions if stuff goes wrong.
2078static void _exif_xmp_read_data_export(Exiv2::XmpData &xmpData, const int32_t imgid, dt_export_metadata_t *metadata)
2079{
2080 const int xmp_version = DT_XMP_EXIF_VERSION;
2081 int stars = 1, raw_params = 0, history_end = -1;
2082 double longitude = NAN, latitude = NAN, altitude = NAN;
2083 gchar *filename = NULL;
2084 GTimeSpan gts = 0;
2085 gchar *iop_order_list = NULL;
2086
2087 // get stars and raw params from db
2090 {
2091 filename = row.filename;
2092 stars = row.flags;
2093 raw_params = row.raw_parameters;
2094 if(row.has_longitude) longitude = row.longitude;
2095 if(row.has_latitude) latitude = row.latitude;
2096 if(row.has_altitude) altitude = row.altitude;
2097 history_end = row.history_end;
2098 gts = row.datetime_taken;
2099 }
2100
2101 // The pipe order only matters when history entries are exported. After
2102 // deleting history, avoid rebuilding a default order just to write an empty
2103 // sidecar: the default will be selected again when the image is opened.
2104 dt_iop_order_t iop_order_version = DT_IOP_ORDER_ANSEL_RAW;
2105 if(history_end > 0)
2106 {
2107 iop_order_version = dt_ioppr_get_iop_order_version(imgid);
2108 GList *iop_list = dt_ioppr_get_iop_order_list(imgid, TRUE);
2109
2110 if(iop_order_version == DT_IOP_ORDER_CUSTOM || dt_ioppr_has_multiple_instances(iop_list))
2111 {
2112 iop_order_list = dt_ioppr_serialize_text_iop_order_list(iop_list);
2113 }
2114 g_list_free_full(iop_list, dt_free_gpointer);
2115 iop_list = NULL;
2116 }
2117
2118 if(metadata->flags & DT_META_METADATA)
2119 {
2120 // Store datetime_taken as DateTimeOriginal to take into account the user's selected date/time
2121 if (!(metadata->flags & DT_META_EXIF))
2122 {
2123 gchar exif_datetime[DT_DATETIME_LENGTH];
2124 dt_datetime_gtimespan_to_exif(exif_datetime, sizeof(exif_datetime), gts);
2125 xmpData["Xmp.exif.DateTimeOriginal"] = exif_datetime;
2126 }
2127 // We have to erase the old ratings first as exiv2 seems to not change it otherwise.
2128 Exiv2::XmpData::iterator pos = xmpData.findKey(Exiv2::XmpKey("Xmp.xmp.Rating"));
2129 if(pos != xmpData.end()) xmpData.erase(pos);
2130 xmpData["Xmp.xmp.Rating"] = dt_image_get_xmp_rating_from_flags(stars);
2131
2132 // The original file name
2133 if(filename) xmpData["Xmp.xmpMM.DerivedFrom"] = filename;
2134 }
2135
2136 // GPS data
2137 if (metadata->flags & DT_META_GEOTAG)
2138 dt_set_xmp_exif_geotag(xmpData, longitude, latitude, altitude);
2139 else
2141
2142
2143 // the meta data
2144 if (metadata->flags & DT_META_METADATA)
2145 dt_set_xmp_dt_metadata(xmpData, imgid, TRUE);
2146
2147 // tags
2148 if (metadata->flags & DT_META_TAG)
2149 {
2150 // get tags from db, store in dublin core
2151 std::unique_ptr<Exiv2::Value> v1(Exiv2::Value::create(Exiv2::xmpBag));
2152 GList *tags = dt_tag_get_list_export(imgid, metadata->flags);
2153 try
2154 {
2155 for(GList *tag = tags; tag; tag = g_list_next(tag))
2156 {
2157 v1->read((char *)tag->data);
2158 }
2159 }
2160 catch(...)
2161 {
2162 g_list_free_full(tags, dt_free_gpointer);
2163 throw;
2164 }
2165 if(v1->count() > 0) xmpData.add(Exiv2::XmpKey("Xmp.dc.subject"), v1.get());
2166 g_list_free_full(tags, dt_free_gpointer);
2167 tags = NULL;
2168 }
2169
2170 if (metadata->flags & DT_META_HIERARCHICAL_TAG)
2171 {
2172 std::unique_ptr<Exiv2::Value> v2(Exiv2::Value::create(Exiv2::xmpBag));
2173 GList *hierarchical = dt_tag_get_hierarchical_export(imgid, metadata->flags);
2174 try
2175 {
2176 for(GList *hier = hierarchical; hier; hier = g_list_next(hier))
2177 {
2178 v2->read((char *)hier->data);
2179 }
2180 }
2181 catch(...)
2182 {
2183 g_list_free_full(hierarchical, dt_free_gpointer);
2184 throw;
2185 }
2186 if(v2->count() > 0) xmpData.add(Exiv2::XmpKey("Xmp.lr.hierarchicalSubject"), v2.get());
2187 g_list_free_full(hierarchical, dt_free_gpointer);
2188 hierarchical = NULL;
2189 }
2190
2191 if (metadata->flags & DT_META_DT_HISTORY)
2192 {
2193 xmpData["Xmp.darktable.xmp_version"] = xmp_version;
2194 xmpData["Xmp.darktable.raw_params"] = raw_params;
2196 xmpData["Xmp.darktable.auto_presets_applied"] = 1;
2197 else
2198 xmpData["Xmp.darktable.auto_presets_applied"] = 0;
2199 dt_set_xmp_dt_history(xmpData, imgid, history_end);
2200
2201 if(history_end > 0)
2202 {
2203 xmpData["Xmp.darktable.iop_order_version"] = iop_order_version;
2204 if(iop_order_list) xmpData["Xmp.darktable.iop_order_list"] = iop_order_list;
2205 }
2206 _exif_xmp_append_history_hash(xmpData, imgid, NULL);
2207 }
2208
2209 dt_free(iop_order_list);
2211}
2212
2213#if EXIV2_TEST_VERSION(0,27,0)
2214#define ERROR_CODE(a) (static_cast<Exiv2::ErrorCode>((a)))
2215#else
2216#define ERROR_CODE(a) (a)
2217#endif
2218
2219char *dt_exif_xmp_read_string(const int32_t imgid)
2220{
2221 try
2222 {
2223 char input_filename[DT_PATH_MAX] = { 0 };
2224 gboolean from_cache = FALSE;
2225 dt_image_full_path(imgid, input_filename, sizeof(input_filename), &from_cache, __FUNCTION__);
2226
2227 // first take over the data from the source image
2228 Exiv2::XmpData xmpData;
2229 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2230 {
2231 std::string xmpPacket;
2232
2233 if(_read_xmp_packet(input_filename, xmpPacket))
2234 {
2235 Exiv2::XmpParser::decode(xmpData, xmpPacket);
2236 // because XmpSeq or XmpBag are added to the list, we first have
2237 // to remove these so that we don't end up with a string of duplicates
2238 dt_remove_known_keys(xmpData);
2239 }
2240 }
2241
2242 // now add whatever we have in the sidecar XMP. this overwrites stuff from the source image
2243 dt_image_path_append_version(imgid, input_filename, sizeof(input_filename));
2244 g_strlcat(input_filename, ".xmp", sizeof(input_filename));
2245 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2246 {
2247 Exiv2::XmpData sidecarXmpData;
2248 std::string xmpPacket;
2249
2250 if(_read_xmp_packet(input_filename, xmpPacket))
2251 {
2252 Exiv2::XmpParser::decode(sidecarXmpData, xmpPacket);
2253
2254 for(Exiv2::XmpData::const_iterator it = sidecarXmpData.begin(); it != sidecarXmpData.end(); ++it)
2255 xmpData.add(*it);
2256 }
2257 }
2258
2259 dt_remove_known_keys(xmpData); // is this needed?
2260
2261 // last but not least attach what we have in DB to the XMP. in theory that should be
2262 // the same as what we just copied over from the sidecar file, but you never know ...
2263 _exif_xmp_read_data(xmpData, imgid, NULL);
2264
2265 // serialize the xmp data and output the xmp packet
2266 std::string xmpPacket;
2267 if(Exiv2::XmpParser::encode(xmpPacket, xmpData,
2268 Exiv2::XmpParser::useCompactFormat | Exiv2::XmpParser::omitPacketWrapper) != 0)
2269 {
2270 throw Exiv2::Error(ERROR_CODE(1), "[xmp_write] failed to serialize xmp data");
2271 }
2272 return g_strdup(xmpPacket.c_str());
2273 }
2274 catch(const std::exception &e)
2275 {
2276 std::cerr << "[xmp_read_blob] caught exiv2 exception '" << e.what() << "'\n";
2277 return NULL;
2278 }
2279}
2280
2281static void dt_remove_xmp_key(Exiv2::XmpData &xmp, const char *key)
2282{
2283 try
2284 {
2285 Exiv2::XmpData::iterator pos = xmp.findKey(Exiv2::XmpKey(key));
2286 if (pos != xmp.end())
2287 xmp.erase(pos);
2288 }
2289 catch(const std::exception &e)
2290 {
2291 }
2292}
2293
2294static void _remove_xmp_keys(Exiv2::XmpData &xmpData, const char *key)
2295{
2296 try
2297 {
2298 const std::string needle = key;
2299 for(Exiv2::XmpData::iterator i = xmpData.begin(); i != xmpData.end();)
2300 {
2301 if(i->key().compare(0, needle.length(), needle) == 0)
2302 i = xmpData.erase(i);
2303 else
2304 ++i;
2305 }
2306 }
2307 catch(const std::exception &e)
2308 {
2309 }
2310}
2311
2312static void dt_remove_exif_key(Exiv2::ExifData &exif, const char *key)
2313{
2314 try
2315 {
2316 Exiv2::ExifData::iterator pos = exif.findKey(Exiv2::ExifKey(key));
2317 if (pos != exif.end())
2318 exif.erase(pos);
2319 }
2320 catch(const std::exception &e)
2321 {
2322 }
2323}
2324
2325static void dt_remove_iptc_key(Exiv2::IptcData &iptc, const char *key)
2326{
2327 try
2328 {
2329 Exiv2::IptcData::iterator pos;
2330 while((pos = iptc.findKey(Exiv2::IptcKey(key))) != iptc.end())
2331 iptc.erase(pos);
2332 }
2333 catch(const std::exception &e)
2334 {
2335 }
2336}
2337
2338int dt_exif_xmp_attach_export(const int32_t imgid, const char *filename, void *metadata)
2339{
2341 try
2342 {
2343 char input_filename[DT_PATH_MAX] = { 0 };
2344 gboolean from_cache = TRUE;
2345 dt_image_full_path(imgid, input_filename, sizeof(input_filename), &from_cache, __FUNCTION__);
2346
2347 std::unique_ptr<Exiv2::Image> img(Exiv2::ImageFactory::open(WIDEN(filename)));
2348 // unfortunately it seems we have to read the metadata, to not erase the exif (which we just wrote).
2349 // will make export slightly slower, oh well.
2350 // img->clearXmpPacket();
2351 img->readMetadata();
2352
2353 try
2354 {
2355 // initialize XMP and IPTC data with the one from the original file
2356 std::unique_ptr<Exiv2::Image> input_image(Exiv2::ImageFactory::open(WIDEN(input_filename)));
2357 if(input_image.get() != 0)
2358 {
2359 input_image->readMetadata();
2360 img->setIptcData(input_image->iptcData());
2361 img->setXmpData(input_image->xmpData());
2362 }
2363 }
2364 catch(const std::exception &e)
2365 {
2366 std::cerr << "[xmp_attach] " << input_filename << ": caught exiv2 exception '" << e.what() << "'\n";
2367 }
2368
2369 Exiv2::XmpData &xmpData = img->xmpData();
2370
2371 // now add whatever we have in the sidecar XMP. this overwrites stuff from the source image
2372 dt_image_path_append_version(imgid, input_filename, sizeof(input_filename));
2373 g_strlcat(input_filename, ".xmp", sizeof(input_filename));
2374 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2375 {
2376 Exiv2::XmpData sidecarXmpData;
2377 std::string xmpPacket;
2378
2379 if(_read_xmp_packet(input_filename, xmpPacket))
2380 {
2381 Exiv2::XmpParser::decode(sidecarXmpData, xmpPacket);
2382
2383 for(Exiv2::XmpData::const_iterator it = sidecarXmpData.begin(); it != sidecarXmpData.end(); ++it)
2384 xmpData.add(*it);
2385 }
2386 }
2387
2388 dt_remove_known_keys(xmpData); // is this needed?
2389
2390 {
2391 // We also want to make sure to not have some tags that might
2392 // have come in from XMP files created by digikam or similar
2393 static const char *keys[] = {
2394 "Xmp.tiff.Orientation"
2395 };
2396 static const guint n_keys = G_N_ELEMENTS(keys);
2397 dt_remove_xmp_keys(xmpData, keys, n_keys);
2398 }
2399
2400 // last but not least attach what we have in DB to the XMP. in theory that should be
2401 // the same as what we just copied over from the sidecar file, but you never know ...
2402 // make sure to remove all geotags if necessary
2403 if(m)
2404 {
2405 Exiv2::ExifData exifOldData;
2406 Exiv2::ExifData &exifData = img->exifData();
2407 if(!(m->flags & DT_META_EXIF))
2408 {
2409 for(Exiv2::ExifData::const_iterator i = exifData.begin(); i != exifData.end() ; ++i)
2410 {
2411 exifOldData[i->key()] = i->value();
2412 }
2413 img->clearExifData();
2414 }
2415
2416 _exif_xmp_read_data_export(xmpData, imgid, m);
2417
2418 Exiv2::IptcData &iptcData = img->iptcData();
2419
2420 if(!(m->flags & DT_META_GEOTAG))
2421 dt_remove_exif_geotag(exifData);
2422 // calculated metadata
2425 params->filename = input_filename;
2426 params->jobcode = "infos";
2427 params->sequence = 0;
2428 params->imgid = imgid;
2429
2431 for (GList *tags = m->list; tags; tags = g_list_next(tags))
2432 {
2433 gchar *tagname = (gchar *)tags->data;
2434 tags = g_list_next(tags);
2435 if (IS_NULL_PTR(tags)) break;
2436 gchar *formula = (gchar *)tags->data;
2437 if (formula[0])
2438 {
2439 if(!(m->flags & DT_META_EXIF) && (formula[0] == '=') && g_str_has_prefix(tagname, "Exif."))
2440 {
2441 // remove this specific exif
2442 Exiv2::ExifData::const_iterator pos;
2443 if(dt_exif_read_exif_tag(exifOldData, &pos, tagname))
2444 {
2445 exifData[tagname] = pos->value();
2446 }
2447 }
2448 else
2449 {
2450 gchar *result = dt_variables_expand(params, formula, FALSE);
2451 if(result && result[0])
2452 {
2453 if(g_str_has_prefix(tagname, "Xmp."))
2454 {
2455 const char *type = _exif_get_exiv2_tag_type(tagname);
2456 // if xmpBag or xmpSeq, split the list when necessary
2457 // else provide the string as is (can be a list of strings)
2458 if(!g_strcmp0(type, "XmpBag") || !g_strcmp0(type, "XmpSeq"))
2459 {
2460 char *tuple = g_strrstr(result, ",");
2461 while(tuple)
2462 {
2463 tuple[0] = '\0';
2464 tuple++;
2465 xmpData[tagname] = tuple;
2466 tuple = g_strrstr(result, ",");
2467 }
2468 }
2469 xmpData[tagname] = result;
2470 }
2471 else if(g_str_has_prefix(tagname, "Iptc."))
2472 {
2473 const char *type = _exif_get_exiv2_tag_type(tagname);
2474 if(!g_strcmp0(type, "String-R"))
2475 {
2476 // clean up the original tags before giving new values
2477 dt_remove_iptc_key(iptcData, tagname);
2478 // convert the input list (separator ", ") into different tags
2479 // FIXME if an element of the list contains a ", " it is not correctly exported
2480 Exiv2::IptcKey key(tagname);
2481 Exiv2::Iptcdatum id(key);
2482 gchar **values = g_strsplit(result, ", ", 0);
2483 if(values)
2484 {
2485 gchar **entry = values;
2486 while (*entry)
2487 {
2488 char *e = g_strstrip(*entry);
2489 if(*e)
2490 {
2491 id.setValue(e);
2492 iptcData.add(id);
2493 }
2494 entry++;
2495 }
2496 }
2497 g_strfreev(values);
2498 }
2499 else iptcData[tagname] = result;
2500 }
2501 else if(g_str_has_prefix(tagname, "Exif."))
2502 {
2503 const char *type = _exif_get_exiv2_tag_type(tagname);
2504 if((!g_strcmp0(type, "Rational") || !g_strcmp0(type, "SRational")) &&
2505 (strstr(result, "/") == NULL))
2506 {
2507 float float_value = (float)std::atof(result);
2508 if(!isnan(float_value))
2509 {
2510 dt_free(result);
2511 int int_value = (int)float_value;
2512 int divisor = 1;
2513 while(fabs(float_value - int_value) > 0.000001)
2514 {
2515 divisor *= 10;
2516 float_value *= 10.0;
2517 int_value = (int)float_value;
2518 }
2519 result = g_strdup_printf("%d/%d", (int)float_value, divisor);
2520 }
2521 }
2522 exifData[tagname] = result;
2523 }
2524 }
2525 dt_free(result);
2526 }
2527 }
2528 else
2529 {
2530 if (g_str_has_prefix(tagname, "Xmp."))
2531 dt_remove_xmp_key(xmpData, tagname);
2532 else if (g_str_has_prefix(tagname, "Exif."))
2533 dt_remove_exif_key(exifData, tagname);
2534 else if (g_str_has_prefix(tagname, "Iptc."))
2535 dt_remove_iptc_key(iptcData, tagname);
2536 }
2537 }
2539 }
2540
2541 try
2542 {
2543 img->writeMetadata();
2544 }
2545 catch(Exiv2::AnyError &e)
2546 {
2547#if EXIV2_TEST_VERSION(0,27,0)
2548 if(e.code() == Exiv2::ErrorCode::kerTooLargeJpegSegment)
2549#else
2550 if(e.code() == 37)
2551#endif
2552 {
2553 _remove_xmp_keys(xmpData, "Xmp.darktable.history");
2554 _remove_xmp_keys(xmpData, "Xmp.darktable.masks_history");
2555 _remove_xmp_keys(xmpData, "Xmp.darktable.auto_presets_applied");
2556 _remove_xmp_keys(xmpData, "Xmp.darktable.iop_order");
2557 try
2558 {
2559 img->writeMetadata();
2560 }
2561 catch(const std::exception &e2)
2562 {
2563 std::cerr << "[dt_exif_xmp_attach_export] without history " << filename << ": caught exiv2 exception '" << e2.what() << "'\n";
2564 return -1;
2565 }
2566 }
2567 else
2568 throw;
2569 }
2570 catch(const std::exception &e)
2571 {
2572 std::cerr << "[dt_exif_xmp_attach_export] " << filename << ": caught exception '" << e.what() << "'\n";
2573 return -1;
2574 }
2575 return 0;
2576 }
2577 catch(const std::exception &e)
2578 {
2579 std::cerr << "[dt_exif_xmp_attach_export] " << filename << ": caught exiv2 exception '" << e.what() << "'\n";
2580 return -1;
2581 }
2582}
2583
2584// write xmp sidecar file:
2585int dt_exif_xmp_write_with_imgpath(const dt_image_t *image, const char *filename,
2586 const char *imgpath)
2587{
2588 // refuse to write sidecar for non-existent image:
2589 if(IS_NULL_PTR(image) || image->id <= 0) return 1;
2590 if(IS_NULL_PTR(imgpath) || !*imgpath) return 1;
2591 if(!g_file_test(imgpath, G_FILE_TEST_IS_REGULAR)) return 1;
2592 const int32_t imgid = image->id;
2593
2594 try
2595 {
2596 Exiv2::XmpData xmpData;
2597 std::string xmpPacket;
2598 char *checksum_old = NULL;
2599 if(g_file_test(filename, G_FILE_TEST_EXISTS))
2600 {
2601 // we want to avoid writing the sidecar file if it didn't change to avoid issues when using the same images
2602 // from different computers. sample use case: images on NAS, several computers using them NOT AT THE SAME TIME and
2603 // the xmp crawler is used to find changed sidecars.
2604 errno = 0;
2605 size_t end;
2606 unsigned char *content = (unsigned char*)dt_read_file(filename, &end);
2607 if(content)
2608 {
2609 if(end > 1000000)
2610 {
2612 _("The XMP file \n'%s'\n weighs %.2f MB. Writing it will take some time."),
2613 filename, (float)end / 1000000);
2614 fprintf(stdout, "The XMP file '%s' weighs %.2f MB. Writing it will take some time.\n", filename, (float)end / 1000000);
2615 }
2616
2617 checksum_old = g_compute_checksum_for_data(G_CHECKSUM_MD5, content, end);
2618
2619 // The packet the parser wants is the same bytes we just hashed, so decode those
2620 // rather than read the file a second time.
2621 xmpPacket.assign((const char *)content, end);
2622 dt_free(content);
2623
2624 Exiv2::XmpParser::decode(xmpData, xmpPacket);
2625 // because XmpSeq or XmpBag are added to the list, we first have
2626 // to remove these so that we don't end up with a string of duplicates
2627 dt_remove_known_keys(xmpData);
2628 }
2629 else
2630 {
2631 fprintf(stderr, "cannot read xmp file '%s': '%s'\n", filename, strerror(errno));
2632 dt_metadata_notify(DT_METADATA_NOTICE_MESSAGE, _("cannot read xmp file '%s': '%s'"),
2633 filename, strerror(errno));
2634 }
2635 }
2636
2637 // initialize xmp data:
2638 _exif_xmp_read_data(xmpData, imgid, image);
2639
2640 // serialize the xmp data and output the xmp packet
2641 if(Exiv2::XmpParser::encode(xmpPacket, xmpData,
2642 Exiv2::XmpParser::useCompactFormat | Exiv2::XmpParser::omitPacketWrapper) != 0)
2643 {
2644 throw Exiv2::Error(ERROR_CODE(1), "[xmp_write] failed to serialize xmp data");
2645 }
2646
2647 // hash the new data and compare it to the old hash (if applicable)
2648 const char *xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
2649 gboolean write_sidecar = TRUE;
2650 if(checksum_old)
2651 {
2652 GChecksum *checksum = g_checksum_new(G_CHECKSUM_MD5);
2653 if(checksum)
2654 {
2655 g_checksum_update(checksum, (unsigned char*)xml_header, -1);
2656 g_checksum_update(checksum, (unsigned char*)xmpPacket.c_str(), -1);
2657 const char *checksum_new = g_checksum_get_string(checksum);
2658 write_sidecar = g_strcmp0(checksum_old, checksum_new) != 0;
2659 g_checksum_free(checksum);
2660 }
2661 dt_free(checksum_old);
2662 }
2663
2664 if(write_sidecar)
2665 {
2666 // using std::ofstream isn't possible here -- on Windows it doesn't support Unicode filenames with mingw
2667 errno = 0;
2668 FILE *fout = g_fopen(filename, "wb");
2669 if(fout)
2670 {
2671 fprintf(fout, "%s", xml_header);
2672 fprintf(fout, "%s", xmpPacket.c_str());
2673 fclose(fout);
2674 }
2675 else
2676 {
2677 fprintf(stderr, "cannot write xmp file '%s': '%s'\n", filename, strerror(errno));
2678 dt_metadata_notify(DT_METADATA_NOTICE_MESSAGE, _("cannot write xmp file '%s': '%s'"),
2679 filename, strerror(errno));
2680 return -1;
2681 }
2682 }
2683
2684 return 0;
2685 }
2686 catch(const std::exception &e)
2687 {
2688 std::cerr << "[dt_exif_xmp_write] " << filename << ": caught exiv2 exception '" << e.what() << "'\n";
2689 return -1;
2690 }
2691}
2692
2693// clang-format off
2694// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
2695// vim: shiftwidth=2 expandtab tabstop=2 cindent
2696// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
2697// clang-format on
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
#define m
Definition basecurve.c:283
void dt_colorlabel_repository_foreach(const int32_t imgid, void(*cb)(void *, const int), void *user_data)
Every colour label of imgid, one call per row, in the order the rows come back.
main.color_labels: which of the five colour labels are set on an image.
const int t
const float v
static dt_aligned_pixel_t sRGB
const dt_colormatrix_t dt_aligned_pixel_t out
static const int row
int dt_conf_get_int(const char *name)
Integer for name, clamped to the bounds declared in the XML.
void dt_image_path_append_version(const int32_t imgid, char *pathname, size_t pathname_len)
int dt_image_get_xmp_rating_from_flags(const int flags)
void dt_image_full_path(const int32_t imgid, char *pathname, size_t pathname_len, gboolean *from_cache, const char *calling_func)
Get the full path of an image out of the database.
const char darktable_package_string[]
const char * dt_database_get_last_error(void)
Definition database.c:3784
void dt_database_rollback_transaction(void)
Definition database.c:4907
#define dt_database_start_transaction()
Definition database.h:290
#define dt_database_release_transaction()
Definition database.h:291
gboolean dt_datetime_img_to_exif(char *exif, const size_t exif_size, const dt_image_t *img)
Definition datetime.c:255
void dt_datetime_now_to_exif(char *exif)
Definition datetime.c:225
gboolean dt_datetime_gtimespan_to_exif(char *sdt, const size_t sdt_size, const GTimeSpan gts)
Definition datetime.c:405
GTimeSpan dt_datetime_gdatetime_to_gtimespan(GDateTime *gdt)
Definition datetime.c:459
#define DT_DATETIME_LENGTH
Definition datetime.h:38
#define DT_DATETIME_EXIF_LENGTH
Definition datetime.h:39
int dt_deprecated(const char *op)
Modules without a proper IOP order should throw errors and log, except if they are deprecated definit...
dev roi_request value
const int res
Definition dtpthread.h:351
bool dt_exif_read_exif_tag(Exiv2::ExifData &exifData, Exiv2::ExifData::const_iterator *pos, string key)
Read one EXIF tag into pos, FALSE if the image does not carry it.
Definition exif.cc:676
unsigned char * dt_exif_xmp_decode(const char *input, const int len, int *output_len)
Definition exif.cc:2207
bool dt_exif_decode_xmp_data(dt_image_t *img, Exiv2::XmpData &xmpData, int version, bool exif_read)
Apply one XMP packet's metadata, tags and colour labels to img.
Definition exif.cc:428
char * dt_exif_xmp_encode(const unsigned char *input, const int len, int *output_len)
Definition exif.cc:2116
void dt_remove_exif_keys(Exiv2::ExifData &exif, const char *keys[], unsigned int n_keys)
Strip the given EXIF keys from exif, ignoring any that are absent.
Definition exif.cc:389
What a photograph says about itself: the EXIF, IPTC and XMP tags a camera and a cataloguer write,...
The seam between metadata/exif.cc and common/xmp_sidecar.cc.
#define WIDEN(s)
const GList * dt_exif_get_exiv2_taglist()
Definition exif.cc:361
memcpy(roundtrip, origin, sizeof(roundtrip))
void * data
Definition geometry.h:111
gboolean enabled
Definition geometry.h:103
dt_iop_roi_t in
Definition geometry.h:117
GdkRGBA color[]
Definition geotagging.c:541
void dt_history_repository_foreach_mask_item(const int32_t imgid, dt_history_repository_mask_cb cb, void *user_data)
gboolean dt_history_repository_shift_nums(const int32_t imgid, const int delta)
gboolean dt_history_repository_write_item(const int32_t imgid, const int num, const char *operation, const void *op_params, const int op_params_size, const int module_version, const gboolean enabled, const void *blendop_params, const int blendop_params_size, const int blendop_version, const int multi_priority, const char *multi_name)
gboolean dt_history_repository_write_mask_item(const int32_t imgid, const int num, const int formid, const int form, const char *name, const int version, const void *points, const int points_len, const int points_count, const void *source, const int source_len)
int dt_history_repository_count_mask_items(const int32_t imgid)
gboolean dt_history_repository_delete_masks_history(const int32_t imgid)
gboolean dt_history_repository_delete_history(const int32_t imgid)
gboolean dt_history_repository_set_end(const int32_t imgid, const int32_t history_end)
int32_t dt_history_repository_get_next_num(const int32_t imgid)
void dt_history_repository_foreach_row(const int32_t imgid, dt_history_repository_row_cb cb, void *user_data)
const char flag
Definition image.h:320
@ DT_IMAGE_NO_LEGACY_PRESETS
Definition image.h:141
@ DT_IMAGE_AUTO_PRESETS_APPLIED
Definition image.h:139
@ DT_IMAGE_REMOVE
Definition image.h:137
dt_image_t * dt_image_cache_get(const int32_t imgid, char mode)
void dt_image_cache_read_release(const dt_image_t *img)
void dt_image_repository_xmp_row_cleanup(dt_image_xmp_row_t *row)
Release what dt_image_repository_get_xmp_row() allocated.
gboolean dt_image_repository_get_timestamps(const int32_t imgid, dt_image_timestamps_t *ts)
Read imgid's four timestamps. FALSE when there is no such image.
gboolean dt_image_repository_get_xmp_row(const int32_t imgid, dt_image_xmp_row_t *row)
Read imgid's sidecar fields. FALSE when there is no such image.
Reading and writing one dt_image_t to and from the library database. The SQL half of what used to be ...
GList * dt_ioppr_get_iop_order_list(int32_t imgid, gboolean sorted)
Load the order list for an image from the DB.
Definition iop_order.c:1100
gboolean dt_ioppr_has_multiple_instances(GList *iop_order_list)
Detect whether multiple instances are grouped for a non-custom order.
Definition iop_order.c:991
GList * dt_ioppr_get_iop_order_list_version(dt_iop_order_t version)
Return the built-in order list for a given version.
Definition iop_order.c:1066
gint dt_sort_iop_list_by_order_f(gconstpointer a, gconstpointer b)
Compare two list nodes holding modules by iop_order.
Definition iop_order.c:939
gboolean dt_ioppr_write_iop_order_list(GList *iop_order_list, const int32_t imgid)
Persist an order list to the DB for a given image.
Definition iop_order.c:1035
char * dt_ioppr_serialize_text_iop_order_list(GList *iop_order_list)
Serialize an order list to a text representation.
Definition iop_order.c:2507
GList * dt_ioppr_deserialize_text_iop_order_list(const char *buf)
Deserialize an order list from a text representation.
Definition iop_order.c:2562
GList * dt_ioppr_insert_missing_modules(GList *iop_order_list)
Insert modules that postdate a deserialized/custom order list.
Definition iop_order.c:773
dt_iop_order_t dt_ioppr_get_iop_order_version(const int32_t imgid)
Fetch the IOP order version stored for an image.
Definition iop_order.c:811
GList * dt_ioppr_get_iop_order_link(GList *iop_order_list, const char *op_name, const int multi_priority)
Find a list link matching an operation and instance.
Definition iop_order.c:875
dt_iop_order_t
Definition iop_order.h:149
@ DT_IOP_ORDER_ANSEL_RAW
Definition iop_order.h:154
@ DT_IOP_ORDER_LEGACY
Definition iop_order.h:151
@ DT_IOP_ORDER_CUSTOM
Definition iop_order.h:150
static const dt_iop_drawlayer_params_t * params
Definition layers.h:80
_lib_location_type_t type
Definition location.c:1
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
@ DT_MASKS_NON_CLONE
Definition masks_types.h:71
@ DT_MASKS_CLONE
Definition masks_types.h:67
@ DT_MASKS_GROUP
Definition masks_types.h:66
static void dt_free_gpointer(gpointer ptr)
g_free() one pointer, with the signature GDestroyNotify wants.
Definition mem_alloc.h:184
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
char * key
const char * dt_metadata_get_key(const uint32_t keyid)
const char * dt_metadata_get_name(const uint32_t keyid)
GList * dt_metadata_get(const int id, const char *key, uint32_t *count)
int dt_metadata_get_type(const uint32_t keyid)
void dt_metadata_notify(const dt_metadata_notice_t kind, const char *format,...)
Everything this module says to the outside world: messages for the user, and the fact that the tag vo...
@ DT_METADATA_NOTICE_MESSAGE
@ DT_METADATA_FLAG_HIDDEN
Definition metadata.h:79
@ DT_METADATA_FLAG_PRIVATE
Definition metadata.h:80
@ DT_METADATA_TYPE_INTERNAL
Definition metadata.h:65
@ DT_META_HIERARCHICAL_TAG
@ DT_META_TAG
@ DT_META_EXIF
@ DT_META_METADATA
@ DT_META_DT_HISTORY
@ DT_META_GEOTAG
void dt_metadata_repository_foreach(const int32_t imgid, dt_metadata_repository_row_cb cb, void *user_data)
Every metadata row of imgid, in row order.
main.meta_data: the free-text image metadata (title, description, creator, publisher,...
g_strfreev(lines)
#define DT_PATH_MAX
Buffer size for a filesystem path anywhere in Ansel.
Definition paths.h:57
const char * name
Definition pdf.h:90
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
char * dt_variables_expand(dt_variables_params_t *params, gchar *source, gboolean iterate)
void dt_variables_params_destroy(dt_variables_params_t *params)
void dt_variables_params_init(dt_variables_params_t **params)
void dt_variables_set_tags_flags(dt_variables_params_t *params, uint32_t flags)
unsigned __int64 uint64_t
Definition strptime.c:75
Exiv2::XmpData * xmpData
Exiv2::XmpData * xmpData
Exiv2::XmpData * xmpData
double latitude
Definition image.h:341
double elevation
Definition image.h:341
double longitude
Definition image.h:341
GTimeSpan export_timestamp
Definition image.h:424
uint64_t history_hash
Definition image.h:413
dt_image_geoloc_t geoloc
Definition image.h:443
int32_t flags
Definition image.h:410
GTimeSpan change_timestamp
Definition image.h:424
GTimeSpan print_timestamp
Definition image.h:424
dt_image_raw_parameters_t legacy_flip
Definition image.h:440
int32_t id
Definition image.h:410
The four timestamps, each with whether it is set at all.
The main.images fields an XMP sidecar carries.
Definition iop_order.h:160
char operation[20]
Definition iop_order.h:166
double iop_order_f
Definition iop_order.h:162
int32_t instance
Definition iop_order.h:167
union dt_iop_order_entry_t::@217102360217337143327010131367370331353176364152 o
unsigned char * blendop_params
int blendop_params_len
int blendop_version
int modversion
double iop_order
int multi_priority
gboolean have_operation
gboolean enabled
char * multi_name
int num
gboolean have_params
gboolean have_modversion
unsigned char * params
int params_len
char * operation
int mask_type
int mask_id
int mask_nb
int mask_version
int mask_num
unsigned char * mask_points
char * mask_name
int version
int mask_src_len
int mask_points_len
gboolean already_added
unsigned char * mask_src
GList * dt_tag_get_list_export(int32_t imgid, int32_t flags)
Definition tags.c:720
GList * dt_tag_get_list(int32_t imgid)
Definition tags.c:623
GList * dt_tag_get_hierarchical_export(int32_t imgid, int32_t flags)
Definition tags.c:800
GList * dt_tag_get_hierarchical(int32_t imgid)
Definition tags.c:667
typedef double((*spd)(unsigned long int wavelength, double TempK))
#define MIN(a, b)
Definition thinplate.c:32
#define MAX(a, b)
Definition thinplate.c:29
static const int mask_id
Definition useless.c:215
static const char * mask_name
Definition useless.c:216
char * dt_read_file(const char *const filename, size_t *filesize)
Definition utility.c:919
gchar * dt_util_dstrcat(gchar *str, const gchar *format,...)
Definition utility.c:122
static const char * _exif_get_exiv2_tag_type(const char *tagname)
int dt_exif_xmp_attach_export(const int32_t imgid, const char *filename, void *metadata)
static void dt_remove_xmp_key(Exiv2::XmpData &xmp, const char *key)
static void _xmp_append_metadata(void *user_data, const int keyid, const char *value)
static void dt_remove_known_keys(Exiv2::XmpData &xmp)
static void free_history_entry(gpointer data)
static void read_xmp_timestamps(Exiv2::XmpData &xmpData, dt_image_t *img, const int xmp_version)
static void dt_set_xmp_exif_geotag(Exiv2::XmpData &xmpData, double longitude, double latitude, double altitude)
static void dt_set_xmp_dt_history(Exiv2::XmpData &xmpData, const int32_t imgid, int history_end)
static void dt_remove_xmp_keys(Exiv2::XmpData &xmp, const char *keys[], unsigned int n_keys)
static void _xmp_append_colorlabel(void *user_data, const int color)
static GList * read_masks_v3(Exiv2::XmpData &xmpData, const char *filename, const int version)
static bool _read_xmp_packet(const char *filename, std::string &packet)
Read an XMP packet off disk into packet, FALSE if the file cannot be read.
int dt_exif_xmp_write_with_imgpath(const dt_image_t *image, const char *filename, const char *imgpath)
GTimeSpan _convert_unix_to_gtimespan(const time_t unix)
static GList * read_history_v1(const std::string &xmpPacket, const char *filename, const int superold)
static void print_history_entry(history_entry_t *entry) __attribute__((unused))
int dt_exif_xmp_read(dt_image_t *img, const char *filename, const int history_only)
static void _xmp_append_history(void *user_data, const int32_t row_imgid, const int hist_num, const int modversion, const char *operation, const void *op_params, const int params_len, const gboolean enabled, const void *blendop_params_blob, const int blendop_params_len, const int blendop_version, const int multi_priority, const char *multi_name, const char *preset_name)
static void dt_remove_xmp_exif_geotag(Exiv2::XmpData &xmpData)
static void add_mask_entry_to_db(int32_t imgid, mask_entry_t *entry)
#define DT_XMP_EXIF_VERSION
static void _remove_xmp_keys(Exiv2::XmpData &xmpData, const char *key)
const char * dt_xmp_keys[]
int dt_exif_read_blob(uint8_t **buf, const char *path, const int32_t imgid, const int sRGB, const int out_width, const int out_height, const int dng_mode)
static void dt_remove_exif_key(Exiv2::ExifData &exif, const char *key)
char * dt_exif_xmp_read_string(const int32_t imgid)
static void _exif_xmp_read_data_export(Exiv2::XmpData &xmpData, const int32_t imgid, dt_export_metadata_t *metadata)
int _get_max_multi_priority(GList *history, const char *operation)
static void _exif_xmp_append_history_hash(Exiv2::XmpData &xmpData, const int32_t imgid, const dt_image_t *image)
static void dt_remove_iptc_key(Exiv2::IptcData &iptc, const char *key)
static GList * read_history_v2(Exiv2::XmpData &xmpData, const char *filename)
static GHashTable * read_masks(Exiv2::XmpData &xmpData, const char *filename, const int version)
static void add_non_clone_mask_entries_to_db(gpointer key, gpointer value, gpointer user_data)
#define ERROR_CODE(a)
static void _exif_xmp_read_data(Exiv2::XmpData &xmpData, const int32_t imgid, const dt_image_t *image)
void free_mask_entry(gpointer data)
static const guint dt_xmp_keys_n
static void _xmp_append_mask(void *user_data, const int mask_num, const int mask_id, const int mask_type, const char *mask_name, const int mask_version, const void *points, const int points_len, const int mask_nb, const void *source, const int source_len)
static void set_xmp_timestamps(Exiv2::XmpData &xmpData, const int32_t imgid)
static void dt_set_xmp_dt_metadata(Exiv2::XmpData &xmpData, const int32_t imgid, const gboolean export_flag)
static void add_mask_entries_to_db(int32_t imgid, GHashTable *mask_entries, int mask_id)
static void dt_remove_exif_geotag(Exiv2::ExifData &exifData)
The XMP document that carries an image's development: history stack, mask shapes, module order,...