73#define __STDC_FORMAT_MACROS
92#include <glib/gstdio.h>
103#include <exiv2/exiv2.hpp>
105#include <pugixml.hpp>
135#define DT_XMP_EXIF_VERSION 5
137#if EXIV2_TEST_VERSION(0,28,0)
138#define AnyError Error
139#define toLong toInt64
146 const size_t tagname_len = strlen(tagname);
152 const char *
t = (
const char *)tag->data;
157 if(strncmp(
t, tagname, tagname_len) == 0 &&
t[tagname_len] ==
',')
158 return t + tagname_len + 1;
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" };
203 fprintf(stderr,
"cannot read xmp file '%s': '%s'\n", filename, strerror(errno));
207 packet.assign(content, length);
219 Exiv2::XmpData::iterator pos = xmp.findKey(Exiv2::XmpKey(
dt_xmp_keys[
i]));
221 while(pos != xmp.end())
223 std::string
key = pos->key();
224 const char *ckey =
key.c_str();
225 size_t len =
key.size();
227 if(!(g_str_has_prefix(ckey,
dt_xmp_keys[
i]) && (ckey[len] ==
'[' || ckey[len] ==
'\0')))
229 pos = xmp.erase(pos);
236 for(
unsigned int i = 0;
i < n_keys;
i++)
240 Exiv2::XmpData::iterator pos;
241 while((pos = xmp.findKey(Exiv2::XmpKey(keys[
i]))) != xmp.end())
244 catch(
const std::exception &e)
256 static const char *keys[] =
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"
266 static const guint n_keys = G_N_ELEMENTS(keys);
271 const int out_height,
const int dng_mode)
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();
282 Exiv2::ExifThumb(exifData).erase();
283 Exiv2::ExifData::const_iterator pos;
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"
303 static const guint n_keys = G_N_ELEMENTS(keys);
310 for(Exiv2::ExifData::iterator
i = exifData.begin();
i != exifData.end();)
312 static const std::string needle =
"Exif.SubImage";
313 if(
i->key().compare(0, needle.length(), needle) == 0)
314 i = exifData.erase(
i);
320 static const char *keys[] = {
322 "Exif.Canon.ColorSpace",
323 "Exif.Canon.ColorData",
326 "Exif.Nikon3.Preview",
327 "Exif.NikonPreview.JPEGInterchangeFormat",
330 "Exif.Image.CFARepeatPatternDim",
331 "Exif.Image.CFAPattern",
332 "Exif.Image.InterColorProfile",
333 "Exif.Image.SpectralSensitivity",
335 "Exif.Image.SpatialFrequencyResponse",
337 "Exif.Image.SensingMethod",
338 "Exif.Image.TIFFEPStandardID",
339 "Exif.Photo.SpectralSensitivity",
341 "Exif.Photo.SpatialFrequencyResponse",
342 "Exif.Photo.SensingMethod",
343 "Exif.Photo.CFAPattern",
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",
377 "Exif.Photo.MakerNote",
380 "Exif.Pentax.PreviewResolution",
381 "Exif.Pentax.PreviewLength",
382 "Exif.Pentax.PreviewOffset",
383 "Exif.PentaxDng.PreviewResolution",
384 "Exif.PentaxDng.PreviewLength",
385 "Exif.PentaxDng.PreviewOffset",
387 "Exif.PentaxDng.ColorInfo",
390 "Exif.Minolta.Thumbnail",
391 "Exif.Minolta.ThumbnailOffset",
392 "Exif.Minolta.ThumbnailLength",
395 "Exif.SonyMinolta.ThumbnailOffset",
396 "Exif.SonyMinolta.ThumbnailLength",
399 "Exif.Olympus.Thumbnail",
400 "Exif.Olympus.ThumbnailOffset",
401 "Exif.Olympus.ThumbnailLength",
403 "Exif.Image.BaselineExposureOffset",
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"
423 static const guint n_keys = G_N_ELEMENTS(keys);
427 static const char *dngkeys[] = {
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"
456 static const guint n_dngkeys = G_N_ELEMENTS(dngkeys);
461 exifData[
"Exif.Photo.ColorSpace"] = uint16_t(1);
463 exifData[
"Exif.Photo.ColorSpace"] = uint16_t(0xFFFF);
467 if(!dng_mode) exifData[
"Exif.Image.Orientation"] = uint16_t(1);
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;
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);
487 static const char * keys[] = {
489 "Exif.Image.ImageDescription",
490 "Exif.Photo.UserComment",
491 "Exif.Image.Copyright",
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"
503 static const guint n_keys = G_N_ELEMENTS(keys);
509 exifData[
"Exif.Image.Artist"] = (
char *)
res->data;
517 char *desc = (
char *)
res->data;
518 if(g_str_is_ascii(desc))
519 exifData[
"Exif.Image.ImageDescription"] = desc;
521 exifData[
"Exif.Photo.UserComment"] = desc;
525#if EXIV2_TEST_VERSION(0,27,4)
529 exifData[
"Exif.Image.ImageDescription"] =
"";
535 exifData[
"Exif.Image.Copyright"] = (
char *)
res->data;
539#if EXIV2_TEST_VERSION(0,27,4)
543 exifData[
"Exif.Image.Copyright"] =
"";
549 const int rating = GPOINTER_TO_INT(
res->data) + 1;
550 exifData[
"Exif.Image.Rating"] = rating;
560 std::cerr <<
"[exiv2 dt_exif_read_blob] " << path <<
": could not load image " << imgid
561 <<
", no Exif data written" << std::endl;
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";
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;
583 exifData[
"Exif.GPSInfo.GPSVersionID"] =
"02 02 00 00";
584 exifData[
"Exif.GPSInfo.GPSAltitudeRef"] = (cimg->
geoloc.
elevation < 0) ?
"1" :
"0";
587 gchar *ele_str = g_strdup_printf(
"%ld/10", ele_dm);
588 exifData[
"Exif.GPSInfo.GPSAltitude"] = ele_str;
598 exifData[
"Exif.Image.DateTime"] = new_datetime;
602 exifData[
"Exif.Image.DateTimeOriginal"] = datetime;
603 exifData[
"Exif.Photo.DateTimeOriginal"] = datetime;
611 Exiv2::ExifParser::encode(blob, Exiv2::bigEndian, exifData);
612 const size_t length = blob.size();
613 *buf = (uint8_t *)malloc(length);
618 memcpy(*buf, &(blob[0]), length);
621 catch(
const std::exception &e)
624 std::string s(e.what());
625 std::cerr <<
"[exiv2 dt_exif_read_blob] " << path <<
": " << s << std::endl;
673 std::cout <<
"malformed entry" << std::endl;
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;
702static GList *
read_history_v1(
const std::string &xmpPacket,
const char *filename,
const int superold)
704 GList *history_entries = NULL;
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());
710 pugi::xml_parse_result result = doc.load(xmpPacket.c_str());
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;
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");
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");
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();
785 for(pugi::xml_node operation_iter: operation.node().children())
789 history_entries = g_list_append(history_entries, current_entry);
791 current_entry->
operation = g_strdup(operation_iter.child_value());
793 current_entry->
enabled = g_strcmp0(enabled_iter->child_value(),
"0") != 0;
795 current_entry->
modversion = atoi(modversion_iter->child_value());
797 const char *params_text = params_iter->child_value();
801 if(multi_name && multi_name_iter != multi_name.node().children().end())
803 current_entry->
multi_name = g_strdup(multi_name_iter->child_value());
807 if(multi_priority && multi_priority_iter != multi_priority.node().children().end())
809 current_entry->
multi_priority = atoi(multi_priority_iter->child_value());
810 multi_priority_iter++;
813 if(blendop_version && blendop_version_iter != blendop_version.node().children().end())
815 current_entry->
blendop_version = atoi(blendop_version_iter->child_value());
816 blendop_version_iter++;
819 if(blendop_params && blendop_params_iter != blendop_params.node().children().end())
821 const char *blendop_text = blendop_params_iter->child_value();
824 blendop_params_iter++;
834 return history_entries;
839 GList *history_entries = NULL;
842 for(
auto history = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.history")); history != xmpData.end(); history++)
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["))
853 key_iter += strlen(
"Xmp.darktable.history[");
855 unsigned int n = strtol(key_iter, &key_iter, 10);
858 std::cerr <<
"error reading history from '" <<
key <<
"' (" << filename <<
")" << std::endl;
860 history_entries = NULL;
866 if(*(key_iter++) !=
']')
868 std::cerr <<
"error reading history from '"
869 <<
key <<
"' (" << filename <<
")" << std::endl;
871 history_entries = NULL;
875 if(*(key_iter++) !=
'/')
goto skip;
876 if(*key_iter ==
'?') key_iter++;
879 unsigned int length = g_list_length(history_entries);
885 history_entries = g_list_append(history_entries, current_entry);
896 if(g_str_has_prefix(key_iter,
"darktable:operation"))
899 std::string value_item = history->toString();
900 current_entry->
operation = g_strdup(value_item.c_str());
902 else if(g_str_has_prefix(key_iter,
"darktable:num"))
904 current_entry->
num = history->toLong();
906 else if(g_str_has_prefix(key_iter,
"darktable:enabled"))
908 current_entry->
enabled = history->toLong() == 1;
910 else if(g_str_has_prefix(key_iter,
"darktable:modversion"))
913 current_entry->
modversion = history->toLong();
915 else if(g_str_has_prefix(key_iter,
"darktable:params"))
918 std::string value_item = history->toString();
929 else if(g_str_has_prefix(key_iter,
"darktable:multi_name"))
931 std::string value_item = history->toString();
932 current_entry->
multi_name = g_strdup(value_item.c_str());
934 else if(g_str_has_prefix(key_iter,
"darktable:multi_priority"))
938 else if(g_str_has_prefix(key_iter,
"darktable:iop_order"))
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);
948 else if(g_str_has_prefix(key_iter,
"darktable:blendop_version"))
952 else if(g_str_has_prefix(key_iter,
"darktable:blendop_params"))
954 std::string value_item = history->toString();
966 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
971 std::cerr <<
"[exif] error: reading history from '" << filename <<
"' failed due to missing tags" << std::endl;
973 history_entries = NULL;
978 return history_entries;
990static GHashTable *
read_masks(Exiv2::XmpData &xmpData,
const char *filename,
const int version)
992 GHashTable *mask_entries = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
free_mask_entry);
995 Exiv2::XmpData::iterator mask;
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())
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)
1021 for(
size_t i = 0;
i < cnt;
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());
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();
1041 entry->
mask_nb = mask_nb->toLong(
i);
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();
1048 g_hash_table_insert(mask_entries, &entry->
mask_id, (gpointer)entry);
1053 return mask_entries;
1056static GList *
read_masks_v3(Exiv2::XmpData &xmpData,
const char *filename,
const int version)
1058 GList *history_entries = NULL;
1061 for(
auto history = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.masks_history")); history != xmpData.end(); history++)
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["))
1072 key_iter += strlen(
"Xmp.darktable.masks_history[");
1074 unsigned int n = strtol(key_iter, &key_iter, 10);
1077 std::cerr <<
"error reading masks history from '" <<
key <<
"' (" << filename <<
")" << std::endl;
1079 history_entries = NULL;
1085 if(*(key_iter++) !=
']')
1087 std::cerr <<
"error reading masks history from '" <<
key <<
"' (" << filename <<
")" << std::endl;
1089 history_entries = NULL;
1093 if(*(key_iter++) !=
'/')
goto skip;
1094 if(*key_iter ==
'?') key_iter++;
1097 unsigned int length = g_list_length(history_entries);
1101 current_entry->
version = version;
1102 history_entries = g_list_append(history_entries, current_entry);
1109 current_entry = (
mask_entry_t *)g_list_nth_data(history_entries,
n - 1);
1113 if(g_str_has_prefix(key_iter,
"darktable:mask_num"))
1115 current_entry->
mask_num = history->toLong();
1117 else if(g_str_has_prefix(key_iter,
"darktable:mask_id"))
1119 current_entry->
mask_id = history->toLong();
1121 else if(g_str_has_prefix(key_iter,
"darktable:mask_type"))
1123 current_entry->
mask_type = history->toLong();
1125 else if(g_str_has_prefix(key_iter,
"darktable:mask_name"))
1127 std::string value_item = history->toString();
1128 current_entry->
mask_name = g_strdup(value_item.c_str());
1130 else if(g_str_has_prefix(key_iter,
"darktable:mask_version"))
1134 else if(g_str_has_prefix(key_iter,
"darktable:mask_points"))
1136 std::string value_item = history->toString();
1141 else if(g_str_has_prefix(key_iter,
"darktable:mask_nb"))
1143 current_entry->
mask_nb = history->toLong();
1145 else if(g_str_has_prefix(key_iter,
"darktable:mask_src"))
1147 std::string value_item = history->toString();
1158 return history_entries;
1167 const int mask_num = 0;
1169 const int mask_num_used = (entry->
version < 3) ? mask_num : entry->
mask_num;
1170 const gboolean inserted =
1192 int32_t imgid = *(
int *)user_data;
1212 fprintf(stderr,
"[masks] error loading masks from xmp file, bad binary blob size.\n");
1227 for(GList *iter = history; iter; iter = g_list_next(iter))
1231 if(!strcmp(entry->
operation, operation))
1252 const size_t filename_len = strlen(filename);
1253 if(filename_len >= 4 && !strcmp(filename + filename_len - 4,
".pfm"))
return 1;
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();
1262 Exiv2::XmpData::iterator pos;
1264 int xmp_version = 0;
1265 GList *iop_order_list = NULL;
1269 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.xmp_version"))) != xmpData.end())
1270 xmp_version = pos->toLong();
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);
1282 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.raw_params"))) != xmpData.end())
1288 raw_params.in = pos->toLong();
1289 const int32_t user_flip = raw_params.out.user_flip;
1294 int32_t preset_applied = 0;
1296 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.auto_presets_applied"))) != xmpData.end())
1298 preset_applied = pos->toLong();
1303 else if(xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.xmp_version")) == xmpData.end())
1317 if(xmp_version == 4 || xmp_version == 5)
1319 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.iop_order_version"))) != xmpData.end())
1324 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.iop_order_list"))) != xmpData.end())
1334 else if(xmp_version == 3)
1338 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.iop_order_version"))) != xmpData.end())
1355 GHashTable *mask_entries = NULL;
1356 GList *mask_entries_v3 = NULL;
1363 mask_entries =
read_masks(xmpData, filename, xmp_version);
1365 mask_entries_v3 =
read_masks_v3(xmpData, filename, xmp_version);
1377 for(GList *m_entries = g_list_first(mask_entries_v3); m_entries; m_entries = g_list_next(m_entries))
1389 gboolean all_ok =
TRUE;
1390 GList *history_entries = NULL;
1394 std::string &xmpPacket = image->xmpPacket();
1396 if(!history_entries)
1399 else if(xmp_version == 2 || xmp_version == 3 || xmp_version == 4 || xmp_version == 5 )
1403 std::cerr <<
"error: Xmp schema version " << xmp_version <<
" in " << filename <<
" not supported" << std::endl;
1404 g_hash_table_destroy(mask_entries);
1412 fprintf(stderr,
"[exif] error deleting history for image %d\n", img->
id);
1418 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
1421 const int db_num = (xmp_version < 3) ? num : entry->
num;
1440 fprintf(stderr,
"[exif] error adding history entry for image %d\n", img->
id);
1456 for(GList *iter = history_entries; iter; iter = g_list_next(iter))
1476 "[exif] cannot get iop-order for module '%s', XMP may be corrupted\n",
1479 iop_order_list = NULL;
1481 history_entries = NULL;
1483 mask_entries_v3 = NULL;
1484 if(mask_entries) g_hash_table_destroy(mask_entries);
1499 if(link) iop_order_list = g_list_delete_link(iop_order_list, link);
1501 iop_order_list = g_list_append(iop_order_list, e);
1519 fprintf(stderr,
"[exif] error shifting history nums for image %d\n", img->
id);
1523 if(!
dt_history_repository_write_item(img->
id, 0,
"mask_manager", NULL, 0, 1, 0, NULL, 0, 0, 0,
""))
1525 fprintf(stderr,
"[exif] error adding mask history entry for image %d\n", img->
id);
1536 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.history_end"))) != xmpData.end() && num > 0)
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;
1543 fprintf(stderr,
"[exif] error writing history_end for image %d\n", img->
id);
1551 if(preset_applied) preset_applied = -1;
1555 fprintf(stderr,
"[exif] error writing history_end for image %d\n", img->
id);
1563 fprintf(stderr,
"[exif] error writing iop_list for image %d\n", img->
id);
1575 if(preset_applied > 0)
1584 if(preset_applied < 0)
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);
1591 iop_order_list = NULL;
1593 history_entries = NULL;
1595 mask_entries_v3 = NULL;
1596 if(mask_entries) g_hash_table_destroy(mask_entries);
1603 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.history_current_hash"))) != xmpData.end())
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))
1614 memcpy(&be_hash, decoded,
sizeof(be_hash));
1622 std::cerr <<
"[exif] error reading history from '" << filename <<
"'" << std::endl;
1628 catch(
const std::exception &e)
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)
1651 Exiv2::XmpData &xmpData = *ctx->
xmpData;
1653 const int num = ctx->
num;
1656 char *mask_src =
dt_exif_xmp_encode((
const unsigned char *)source, source_len, NULL);
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);
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);
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;
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)
1696 Exiv2::XmpData &xmpData = *ctx->
xmpData;
1698 const int num = ctx->
num;
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);
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);
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;
1719 if(blendop_params_blob)
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;
1742 Exiv2::XmpTextValue tvm(
"");
1743 tvm.setXmpArrayType(Exiv2::XmpValue::xaSeq);
1744 xmpData.add(Exiv2::XmpKey(
"Xmp.darktable.masks_history"), &tvm);
1753 Exiv2::XmpTextValue tv(
"");
1754 tv.setXmpArrayType(Exiv2::XmpValue::xaSeq);
1755 xmpData.add(Exiv2::XmpKey(
"Xmp.darktable.history"), &tv);
1759 if(history_end == -1) history_end = num - 1;
1760 else history_end =
MIN(history_end, num - 1);
1761 xmpData[
"Xmp.darktable.history_end"] = history_end;
1767 static const char *keys[] =
1769 "Xmp.darktable.import_timestamp",
1770 "Xmp.darktable.change_timestamp",
1771 "Xmp.darktable.export_timestamp",
1772 "Xmp.darktable.print_timestamp"
1774 static const guint n_keys = G_N_ELEMENTS(keys);
1789 GDateTime *gdt = g_date_time_new_from_unix_utc(unix);
1793 g_date_time_unref(gdt);
1802 Exiv2::XmpData::iterator pos;
1805 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.change_timestamp"))) != xmpData.end())
1809 else if(pos->toLong() >= 1)
1812 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.export_timestamp"))) != xmpData.end())
1816 else if(pos->toLong() >= 1)
1819 if((pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.darktable.print_timestamp"))) != xmpData.end())
1823 else if(pos->toLong() >= 1)
1830 static const char *keys[] =
1832 "Xmp.exif.GPSVersionID",
1833 "Xmp.exif.GPSLongitude",
1834 "Xmp.exif.GPSLatitude",
1835 "Xmp.exif.GPSAltitudeRef",
1836 "Xmp.exif.GPSAltitude"
1838 static const guint n_keys = G_N_ELEMENTS(keys);
1845 if(!isnan(longitude) && !isnan(latitude))
1847 char long_dir =
'E', lat_dir =
'N';
1848 if(longitude < 0) long_dir =
'W';
1849 if(latitude < 0) lat_dir =
'S';
1851 longitude = fabs(longitude);
1852 latitude = fabs(latitude);
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;
1859 char *str = (
char *)g_malloc(G_ASCII_DTOSTR_BUF_SIZE);
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);
1866 xmpData[
"Xmp.exif.GPSVersionID"] =
"2.2.0.0";
1867 xmpData[
"Xmp.exif.GPSLongitude"] = long_str;
1868 xmpData[
"Xmp.exif.GPSLatitude"] = lat_str;
1873 if(!isnan(altitude))
1875 xmpData[
"Xmp.exif.GPSAltitudeRef"] = (altitude < 0) ?
"1" :
"0";
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;
1893 Exiv2::XmpData &xmpData = *ctx->
xmpData;
1910 Exiv2::Value *
v = (Exiv2::Value *)user_data;
1912 snprintf(val,
sizeof(val),
"%d",
color);
1923 std::unique_ptr<Exiv2::Value>
v(Exiv2::Value::create(Exiv2::xmpSeq));
1927 if(
v->count() > 0) xmpData.add(Exiv2::XmpKey(
"Xmp.darktable.colorlabels"),
v.get());
1946 xmpData[
"Xmp.darktable.history_current_hash"] =
value;
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;
1968 filename =
row.filename;
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;
1998 xmpData[
"Xmp.exif.DateTimeOriginal"] = exif_datetime;
2001 Exiv2::XmpData::iterator pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.xmp.Rating"));
2002 if(pos != xmpData.end()) xmpData.erase(pos);
2006 if(filename) xmpData[
"Xmp.xmpMM.DerivedFrom"] = filename;
2018 std::unique_ptr<Exiv2::Value> v1(Exiv2::Value::create(Exiv2::xmpBag));
2020 std::unique_ptr<Exiv2::Value> v2(Exiv2::Value::create(Exiv2::xmpBag));
2025 for(GList *tag = tags; tag; tag = g_list_next(tag))
2027 v1->read((
char *)tag->data);
2035 if(v1->count() > 0) xmpData.add(Exiv2::XmpKey(
"Xmp.dc.subject"), v1.get());
2042 for(GList *hier = hierarchical; hier; hier = g_list_next(hier))
2044 v2->read((
char *)hier->data);
2052 if(v2->count() > 0) xmpData.add(Exiv2::XmpKey(
"Xmp.lr.hierarchicalSubject"), v2.get());
2054 hierarchical = NULL;
2057 xmpData[
"Xmp.darktable.xmp_version"] = xmp_version;
2058 xmpData[
"Xmp.darktable.raw_params"] = raw_params;
2060 xmpData[
"Xmp.darktable.auto_presets_applied"] = 1;
2062 xmpData[
"Xmp.darktable.auto_presets_applied"] = 0;
2067 xmpData[
"Xmp.darktable.iop_order_version"] = iop_order_version;
2068 if(iop_order_list) xmpData[
"Xmp.darktable.iop_order_list"] = iop_order_list;
2081 int stars = 1, raw_params = 0, history_end = -1;
2082 double longitude = NAN, latitude = NAN, altitude = NAN;
2083 gchar *filename = NULL;
2085 gchar *iop_order_list = NULL;
2091 filename =
row.filename;
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;
2125 xmpData[
"Xmp.exif.DateTimeOriginal"] = exif_datetime;
2128 Exiv2::XmpData::iterator pos = xmpData.findKey(Exiv2::XmpKey(
"Xmp.xmp.Rating"));
2129 if(pos != xmpData.end()) xmpData.erase(pos);
2133 if(filename) xmpData[
"Xmp.xmpMM.DerivedFrom"] = filename;
2151 std::unique_ptr<Exiv2::Value> v1(Exiv2::Value::create(Exiv2::xmpBag));
2155 for(GList *tag = tags; tag; tag = g_list_next(tag))
2157 v1->read((
char *)tag->data);
2165 if(v1->count() > 0) xmpData.add(Exiv2::XmpKey(
"Xmp.dc.subject"), v1.get());
2172 std::unique_ptr<Exiv2::Value> v2(Exiv2::Value::create(Exiv2::xmpBag));
2176 for(GList *hier = hierarchical; hier; hier = g_list_next(hier))
2178 v2->read((
char *)hier->data);
2186 if(v2->count() > 0) xmpData.add(Exiv2::XmpKey(
"Xmp.lr.hierarchicalSubject"), v2.get());
2188 hierarchical = NULL;
2193 xmpData[
"Xmp.darktable.xmp_version"] = xmp_version;
2194 xmpData[
"Xmp.darktable.raw_params"] = raw_params;
2196 xmpData[
"Xmp.darktable.auto_presets_applied"] = 1;
2198 xmpData[
"Xmp.darktable.auto_presets_applied"] = 0;
2203 xmpData[
"Xmp.darktable.iop_order_version"] = iop_order_version;
2204 if(iop_order_list) xmpData[
"Xmp.darktable.iop_order_list"] = iop_order_list;
2213#if EXIV2_TEST_VERSION(0,27,0)
2214#define ERROR_CODE(a) (static_cast<Exiv2::ErrorCode>((a)))
2216#define ERROR_CODE(a) (a)
2224 gboolean from_cache =
FALSE;
2225 dt_image_full_path(imgid, input_filename,
sizeof(input_filename), &from_cache, __FUNCTION__);
2228 Exiv2::XmpData xmpData;
2229 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2231 std::string xmpPacket;
2235 Exiv2::XmpParser::decode(xmpData, xmpPacket);
2244 g_strlcat(input_filename,
".xmp",
sizeof(input_filename));
2245 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2247 Exiv2::XmpData sidecarXmpData;
2248 std::string xmpPacket;
2252 Exiv2::XmpParser::decode(sidecarXmpData, xmpPacket);
2254 for(Exiv2::XmpData::const_iterator it = sidecarXmpData.begin(); it != sidecarXmpData.end(); ++it)
2266 std::string xmpPacket;
2267 if(Exiv2::XmpParser::encode(xmpPacket, xmpData,
2268 Exiv2::XmpParser::useCompactFormat | Exiv2::XmpParser::omitPacketWrapper) != 0)
2270 throw Exiv2::Error(
ERROR_CODE(1),
"[xmp_write] failed to serialize xmp data");
2272 return g_strdup(xmpPacket.c_str());
2274 catch(
const std::exception &e)
2276 std::cerr <<
"[xmp_read_blob] caught exiv2 exception '" << e.what() <<
"'\n";
2285 Exiv2::XmpData::iterator pos = xmp.findKey(Exiv2::XmpKey(
key));
2286 if (pos != xmp.end())
2289 catch(
const std::exception &e)
2298 const std::string needle =
key;
2299 for(Exiv2::XmpData::iterator
i = xmpData.begin();
i != xmpData.end();)
2301 if(
i->key().compare(0, needle.length(), needle) == 0)
2302 i = xmpData.erase(
i);
2307 catch(
const std::exception &e)
2316 Exiv2::ExifData::iterator pos = exif.findKey(Exiv2::ExifKey(
key));
2317 if (pos != exif.end())
2320 catch(
const std::exception &e)
2329 Exiv2::IptcData::iterator pos;
2330 while((pos = iptc.findKey(Exiv2::IptcKey(
key))) != iptc.end())
2333 catch(
const std::exception &e)
2344 gboolean from_cache =
TRUE;
2345 dt_image_full_path(imgid, input_filename,
sizeof(input_filename), &from_cache, __FUNCTION__);
2347 std::unique_ptr<Exiv2::Image> img(Exiv2::ImageFactory::open(
WIDEN(filename)));
2351 img->readMetadata();
2356 std::unique_ptr<Exiv2::Image> input_image(Exiv2::ImageFactory::open(
WIDEN(input_filename)));
2357 if(input_image.get() != 0)
2359 input_image->readMetadata();
2360 img->setIptcData(input_image->iptcData());
2361 img->setXmpData(input_image->xmpData());
2364 catch(
const std::exception &e)
2366 std::cerr <<
"[xmp_attach] " << input_filename <<
": caught exiv2 exception '" << e.what() <<
"'\n";
2369 Exiv2::XmpData &xmpData = img->xmpData();
2373 g_strlcat(input_filename,
".xmp",
sizeof(input_filename));
2374 if(g_file_test(input_filename, G_FILE_TEST_EXISTS))
2376 Exiv2::XmpData sidecarXmpData;
2377 std::string xmpPacket;
2381 Exiv2::XmpParser::decode(sidecarXmpData, xmpPacket);
2383 for(Exiv2::XmpData::const_iterator it = sidecarXmpData.begin(); it != sidecarXmpData.end(); ++it)
2393 static const char *keys[] = {
2394 "Xmp.tiff.Orientation"
2396 static const guint n_keys = G_N_ELEMENTS(keys);
2405 Exiv2::ExifData exifOldData;
2406 Exiv2::ExifData &exifData = img->exifData();
2409 for(Exiv2::ExifData::const_iterator
i = exifData.begin();
i != exifData.end() ; ++
i)
2411 exifOldData[
i->key()] =
i->value();
2413 img->clearExifData();
2418 Exiv2::IptcData &iptcData = img->iptcData();
2425 params->filename = input_filename;
2426 params->jobcode =
"infos";
2431 for (GList *tags =
m->list; tags; tags = g_list_next(tags))
2433 gchar *tagname = (gchar *)tags->data;
2434 tags = g_list_next(tags);
2436 gchar *formula = (gchar *)tags->data;
2439 if(!(
m->flags &
DT_META_EXIF) && (formula[0] ==
'=') && g_str_has_prefix(tagname,
"Exif."))
2442 Exiv2::ExifData::const_iterator pos;
2445 exifData[tagname] = pos->value();
2451 if(result && result[0])
2453 if(g_str_has_prefix(tagname,
"Xmp."))
2458 if(!g_strcmp0(
type,
"XmpBag") || !g_strcmp0(
type,
"XmpSeq"))
2460 char *tuple = g_strrstr(result,
",");
2465 xmpData[tagname] = tuple;
2466 tuple = g_strrstr(result,
",");
2469 xmpData[tagname] = result;
2471 else if(g_str_has_prefix(tagname,
"Iptc."))
2474 if(!g_strcmp0(
type,
"String-R"))
2480 Exiv2::IptcKey
key(tagname);
2481 Exiv2::Iptcdatum id(
key);
2482 gchar **values = g_strsplit(result,
", ", 0);
2485 gchar **entry = values;
2488 char *e = g_strstrip(*entry);
2499 else iptcData[tagname] = result;
2501 else if(g_str_has_prefix(tagname,
"Exif."))
2504 if((!g_strcmp0(
type,
"Rational") || !g_strcmp0(
type,
"SRational")) &&
2505 (strstr(result,
"/") == NULL))
2507 float float_value = (float)std::atof(result);
2508 if(!isnan(float_value))
2511 int int_value = (int)float_value;
2513 while(fabs(float_value - int_value) > 0.000001)
2516 float_value *= 10.0;
2517 int_value = (int)float_value;
2519 result = g_strdup_printf(
"%d/%d", (
int)float_value, divisor);
2522 exifData[tagname] = result;
2530 if (g_str_has_prefix(tagname,
"Xmp."))
2532 else if (g_str_has_prefix(tagname,
"Exif."))
2534 else if (g_str_has_prefix(tagname,
"Iptc."))
2543 img->writeMetadata();
2545 catch(Exiv2::AnyError &e)
2547#if EXIV2_TEST_VERSION(0,27,0)
2548 if(e.code() == Exiv2::ErrorCode::kerTooLargeJpegSegment)
2559 img->writeMetadata();
2561 catch(
const std::exception &e2)
2563 std::cerr <<
"[dt_exif_xmp_attach_export] without history " << filename <<
": caught exiv2 exception '" << e2.what() <<
"'\n";
2570 catch(
const std::exception &e)
2572 std::cerr <<
"[dt_exif_xmp_attach_export] " << filename <<
": caught exception '" << e.what() <<
"'\n";
2577 catch(
const std::exception &e)
2579 std::cerr <<
"[dt_exif_xmp_attach_export] " << filename <<
": caught exiv2 exception '" << e.what() <<
"'\n";
2586 const char *imgpath)
2591 if(!g_file_test(imgpath, G_FILE_TEST_IS_REGULAR))
return 1;
2592 const int32_t imgid = image->
id;
2596 Exiv2::XmpData xmpData;
2597 std::string xmpPacket;
2598 char *checksum_old = NULL;
2599 if(g_file_test(filename, G_FILE_TEST_EXISTS))
2606 unsigned char *content = (
unsigned char*)
dt_read_file(filename, &end);
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);
2617 checksum_old = g_compute_checksum_for_data(G_CHECKSUM_MD5, content, end);
2621 xmpPacket.assign((
const char *)content, end);
2624 Exiv2::XmpParser::decode(xmpData, xmpPacket);
2631 fprintf(stderr,
"cannot read xmp file '%s': '%s'\n", filename, strerror(errno));
2633 filename, strerror(errno));
2641 if(Exiv2::XmpParser::encode(xmpPacket, xmpData,
2642 Exiv2::XmpParser::useCompactFormat | Exiv2::XmpParser::omitPacketWrapper) != 0)
2644 throw Exiv2::Error(
ERROR_CODE(1),
"[xmp_write] failed to serialize xmp data");
2648 const char *xml_header =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
2649 gboolean write_sidecar =
TRUE;
2652 GChecksum *checksum = g_checksum_new(G_CHECKSUM_MD5);
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);
2668 FILE *fout = g_fopen(filename,
"wb");
2671 fprintf(fout,
"%s", xml_header);
2672 fprintf(fout,
"%s", xmpPacket.c_str());
2677 fprintf(stderr,
"cannot write xmp file '%s': '%s'\n", filename, strerror(errno));
2679 filename, strerror(errno));
2686 catch(
const std::exception &e)
2688 std::cerr <<
"[dt_exif_xmp_write] " << filename <<
": caught exiv2 exception '" << e.what() <<
"'\n";
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.
static dt_aligned_pixel_t sRGB
const dt_colormatrix_t dt_aligned_pixel_t out
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)
void dt_database_rollback_transaction(void)
#define dt_database_start_transaction()
#define dt_database_release_transaction()
gboolean dt_datetime_img_to_exif(char *exif, const size_t exif_size, const dt_image_t *img)
void dt_datetime_now_to_exif(char *exif)
gboolean dt_datetime_gtimespan_to_exif(char *sdt, const size_t sdt_size, const GTimeSpan gts)
GTimeSpan dt_datetime_gdatetime_to_gtimespan(GDateTime *gdt)
#define DT_DATETIME_LENGTH
#define DT_DATETIME_EXIF_LENGTH
int dt_deprecated(const char *op)
Modules without a proper IOP order should throw errors and log, except if they are deprecated definit...
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.
unsigned char * dt_exif_xmp_decode(const char *input, const int len, int *output_len)
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.
char * dt_exif_xmp_encode(const unsigned char *input, const int len, int *output_len)
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.
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.
memcpy(roundtrip, origin, sizeof(roundtrip))
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)
@ DT_IMAGE_NO_LEGACY_PRESETS
@ DT_IMAGE_AUTO_PRESETS_APPLIED
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.
gboolean dt_ioppr_has_multiple_instances(GList *iop_order_list)
Detect whether multiple instances are grouped for a non-custom order.
GList * dt_ioppr_get_iop_order_list_version(dt_iop_order_t version)
Return the built-in order list for a given version.
gint dt_sort_iop_list_by_order_f(gconstpointer a, gconstpointer b)
Compare two list nodes holding modules by iop_order.
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.
char * dt_ioppr_serialize_text_iop_order_list(GList *iop_order_list)
Serialize an order list to a text representation.
GList * dt_ioppr_deserialize_text_iop_order_list(const char *buf)
Deserialize an order list from a text representation.
GList * dt_ioppr_insert_missing_modules(GList *iop_order_list)
Insert modules that postdate a deserialized/custom order list.
dt_iop_order_t dt_ioppr_get_iop_order_version(const int32_t imgid)
Fetch the IOP order version stored for an image.
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.
static const dt_iop_drawlayer_params_t * params
_lib_location_type_t type
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
static void dt_free_gpointer(gpointer ptr)
g_free() one pointer, with the signature GDestroyNotify wants.
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
#define DT_PATH_MAX
Buffer size for a filesystem path anywhere in Ansel.
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...
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
GTimeSpan export_timestamp
GTimeSpan change_timestamp
GTimeSpan print_timestamp
dt_image_raw_parameters_t legacy_flip
The four timestamps, each with whether it is set at all.
The main.images fields an XMP sidecar carries.
union dt_iop_order_entry_t::@217102360217337143327010131367370331353176364152 o
unsigned char * blendop_params
unsigned char * mask_points
typedef double((*spd)(unsigned long int wavelength, double TempK))
static const char * mask_name
char * dt_read_file(const char *const filename, size_t *filesize)
gchar * dt_util_dstrcat(gchar *str, const gchar *format,...)
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)
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,...