-
Notifications
You must be signed in to change notification settings - Fork 282
/
convert.cpp
1625 lines (1481 loc) · 56.5 KB
/
convert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
File: convert.cpp
Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
Vladimir Nadvornik (vn) <nadvornik@suse.cz>
History: 17-Mar-08, ahu: created basic converter framework
20-May-08, vn: added actual conversion logic
*/
// *****************************************************************************
// included header files
#include "convert.hpp"
#include "config.h"
#include "error.hpp"
#include "exif.hpp"
#include "futils.hpp"
#include "iptc.hpp"
#include "types.hpp"
#include "xmp_exiv2.hpp"
// + standard includes
#include <algorithm>
#include <functional>
#include <iomanip>
#ifdef EXV_HAVE_ICONV
#include <iconv.h>
#include <cerrno>
#elif defined _WIN32
#include <windows.h>
#endif
// Adobe XMP Toolkit
#ifdef EXV_HAVE_XMP_TOOLKIT
#define TXMP_STRING_TYPE std::string
#ifdef EXV_ADOBE_XMPSDK
#include <XMP.hpp>
#else
#include <XMPSDK.hpp>
#endif
#include <MD5.h>
#endif // EXV_HAVE_XMP_TOOLKIT
// *****************************************************************************
// local declarations
namespace {
#if defined EXV_HAVE_ICONV
// Convert string charset with iconv.
bool convertStringCharsetIconv(std::string& str, const char* from, const char* to);
#elif defined _WIN32
// Convert string charset with Windows functions.
bool convertStringCharsetWindows(std::string& str, const char* from, const char* to);
#endif
/*!
@brief Get the text value of an XmpDatum \em pos.
If \em pos refers to a LangAltValue, \em value is set to the default language
entry without the x-default qualifier. If there is no default but
exactly one entry, \em value is set to this entry, without the qualifier.
The return code indicates if the operation was successful.
*/
bool getTextValue(std::string& value, const Exiv2::XmpData::iterator& pos);
} // namespace
// *****************************************************************************
// class member definitions
namespace Exiv2 {
//! Metadata conversions.
class Converter {
public:
/*!
@brief Type for metadata converter functions, taking two key strings,
\em from and \em to.
These functions have access to both the source and destination metadata
containers and store the result directly in the destination container.
*/
using ConvertFct = void (Converter::*)(const char*, const char*);
//! Structure to define conversions between two keys.
struct Conversion {
MetadataId metadataId_; //!< Type of metadata for the first key.
const char* key1_; //!< First metadata key.
const char* key2_; //!< Second metadata key (always an XMP key for now).
ConvertFct key1ToKey2_; //!< Conversion from first to second key.
ConvertFct key2ToKey1_; //!< Conversion from second to first key.
};
//! @name Creators
//@{
//! Constructor for Exif tags and XMP properties.
Converter(ExifData& exifData, XmpData& xmpData);
//! Constructor for Iptc tags and XMP properties.
Converter(IptcData& iptcData, XmpData& xmpData, const char* iptcCharset = nullptr);
//@}
//! @name Manipulators
//@{
//! Convert Exif tags or IPTC datasets to XMP properties according to the conversion table.
void cnvToXmp();
//! Convert XMP properties to Exif tags or IPTC datasets according to the conversion table.
void cnvFromXmp();
/*!
@brief Set the erase flag.
This flag indicates whether successfully converted source records are erased.
*/
void setErase(bool onoff = true) {
erase_ = onoff;
}
/*!
@brief Set the overwrite flag.
This flag indicates whether existing target records are overwritten.
*/
void setOverwrite(bool onoff = true) {
overwrite_ = onoff;
}
//@}
//! @name Conversion functions (manipulators)
//@{
/*!
@brief Do nothing conversion function.
Use when, for example, a one-way conversion is needed.
*/
void cnvNone(const char*, const char*);
/*!
@brief Simple Exif to XMP conversion function.
Sets the XMP property to an XmpText value containing the Exif value string.
*/
void cnvExifValue(const char* from, const char* to);
/*!
@brief Convert the tag Exif.Photo.UserComment to XMP.
Todo: Convert the Exif comment to UTF-8 if necessary.
*/
void cnvExifComment(const char* from, const char* to);
/*!
@brief Converts Exif tag with multiple components to XMP array.
Converts Exif tag with multiple components to XMP array. This function is
used for ComponentsConfiguration tag.
*/
void cnvExifArray(const char* from, const char* to);
/*!
@brief Exif date to XMP conversion function.
Sets the XMP property to an XmpText value containing date and time. This function
combines values from multiple Exif tags as described in XMP specification. It
is used for DateTime, DateTimeOriginal, DateTimeDigitized and GPSTimeStamp.
*/
void cnvExifDate(const char* from, const char* to);
/*!
@brief Exif version to XMP conversion function.
Converts ExifVersion tag to XmpText value.
*/
void cnvExifVersion(const char* from, const char* to);
/*!
@brief Exif GPS version to XMP conversion function.
Converts GPSVersionID tag to XmpText value.
*/
void cnvExifGPSVersion(const char* from, const char* to);
/*!
@brief Exif Flash to XMP conversion function.
Converts Flash tag to XMP structure.
*/
void cnvExifFlash(const char* from, const char* to);
/*!
@brief Exif GPS coordinate to XMP conversion function.
Converts GPS coordinates tag to XmpText value. It combines multiple Exif tags
as described in XMP specification.
*/
void cnvExifGPSCoord(const char* from, const char* to);
/*!
@brief Simple XMP to Exif conversion function.
Sets the Exif tag according to the XMP property.
For LangAlt values, only the x-default entry is used.
Todo: Escape non-ASCII characters in XMP text values
*/
void cnvXmpValue(const char* from, const char* to);
/*!
@brief Convert the tag Xmp.exif.UserComment to Exif.
*/
void cnvXmpComment(const char* from, const char* to);
/*!
@brief Converts XMP array to Exif tag with multiple components.
Converts XMP array to Exif tag with multiple components. This function is
used for ComponentsConfiguration tag.
*/
void cnvXmpArray(const char* from, const char* to);
/*!
@brief XMP to Exif date conversion function.
Converts the XmpText value to Exif date and time. This function
sets multiple Exif tags as described in XMP specification. It
is used for DateTime, DateTimeOriginal, DateTimeDigitized and GPSTimeStamp.
*/
void cnvXmpDate(const char* from, const char* to);
/*!
@brief XMP to Exif version conversion function.
Converts XmpText value to ExifVersion tag.
*/
void cnvXmpVersion(const char* from, const char* to);
/*!
@brief XMP to Exif GPS version conversion function.
Converts XmpText value to GPSVersionID tag.
*/
void cnvXmpGPSVersion(const char* from, const char* to);
/*!
@brief XMP to Exif Flash conversion function.
Converts XMP structure to Flash tag.
*/
void cnvXmpFlash(const char* from, const char* to);
/*!
@brief XMP to Exif GPS coordinate conversion function.
Converts XmpText value to GPS coordinates tags. It sets multiple Exif tags
as described in XMP specification.
*/
void cnvXmpGPSCoord(const char* from, const char* to);
/*!
@brief IPTC dataset to XMP conversion function.
Multiple IPTC datasets with the same key are converted to an XMP array.
*/
void cnvIptcValue(const char* from, const char* to);
/*!
@brief XMP to IPTC dataset conversion function.
Each array element of an XMP array value is added as one IPTC dataset.
*/
void cnvXmpValueToIptc(const char* from, const char* to);
/*!
@brief Write exif:NativeDigest and tiff:NativeDigest properties to XMP.
Compute digests from Exif values and write them to exif:NativeDigest
and tiff:NativeDigest properties. This should be compatible with XMP SDK.
*/
void writeExifDigest();
/*!
@brief Copies metadata in appropriate direction.
From values of exif:NativeDigest and tiff:NativeDigest detects which of
XMP and Exif was updated more recently and copies metadata in appropriate direction.
*/
void syncExifWithXmp();
//@}
//! @name Accessors
//@{
//! Get the value of the erase flag, see also setErase(bool on).
[[nodiscard]] bool erase() const {
return erase_;
}
//@}
private:
bool prepareExifTarget(const char* to, bool force = false);
bool prepareIptcTarget(const char* to, bool force = false);
bool prepareXmpTarget(const char* to, bool force = false);
std::string computeExifDigest(bool tiff);
// DATA
static const Conversion conversion_[]; ///< Conversion rules
bool erase_{false};
bool overwrite_{true};
ExifData* exifData_;
IptcData* iptcData_;
XmpData* xmpData_;
const char* iptcCharset_;
}; // class Converter
// Order is important for computing digests
const Converter::Conversion Converter::conversion_[] = {
{mdExif, "Exif.Image.ImageWidth", "Xmp.tiff.ImageWidth", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.ImageLength", "Xmp.tiff.ImageLength", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.BitsPerSample", "Xmp.tiff.BitsPerSample", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Compression", "Xmp.tiff.Compression", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.PhotometricInterpretation", "Xmp.tiff.PhotometricInterpretation", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.Orientation", "Xmp.tiff.Orientation", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.SamplesPerPixel", "Xmp.tiff.SamplesPerPixel", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.PlanarConfiguration", "Xmp.tiff.PlanarConfiguration", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.YCbCrSubSampling", "Xmp.tiff.YCbCrSubSampling", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.YCbCrPositioning", "Xmp.tiff.YCbCrPositioning", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.XResolution", "Xmp.tiff.XResolution", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.YResolution", "Xmp.tiff.YResolution", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.ResolutionUnit", "Xmp.tiff.ResolutionUnit", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.TransferFunction", "Xmp.tiff.TransferFunction", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.WhitePoint", "Xmp.tiff.WhitePoint", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.PrimaryChromaticities", "Xmp.tiff.PrimaryChromaticities", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.YCbCrCoefficients", "Xmp.tiff.YCbCrCoefficients", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.ReferenceBlackWhite", "Xmp.tiff.ReferenceBlackWhite", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Image.DateTime", "Xmp.xmp.ModifyDate", &Converter::cnvExifDate,
&Converter::cnvXmpDate}, // MWG Guidelines
{mdExif, "Exif.Image.ImageDescription", "Xmp.dc.description", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Make", "Xmp.tiff.Make", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Model", "Xmp.tiff.Model", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Software", "Xmp.tiff.Software", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Artist", "Xmp.dc.creator", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Rating", "Xmp.xmp.Rating", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Image.Copyright", "Xmp.dc.rights", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ExifVersion", "Xmp.exif.ExifVersion", &Converter::cnvExifVersion, &Converter::cnvXmpVersion},
{mdExif, "Exif.Photo.FlashpixVersion", "Xmp.exif.FlashpixVersion", &Converter::cnvExifVersion,
&Converter::cnvXmpVersion},
{mdExif, "Exif.Photo.ColorSpace", "Xmp.exif.ColorSpace", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ComponentsConfiguration", "Xmp.exif.ComponentsConfiguration", &Converter::cnvExifArray,
&Converter::cnvXmpArray},
{mdExif, "Exif.Photo.CompressedBitsPerPixel", "Xmp.exif.CompressedBitsPerPixel", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.PixelXDimension", "Xmp.exif.PixelXDimension", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.PixelYDimension", "Xmp.exif.PixelYDimension", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.UserComment", "Xmp.exif.UserComment", &Converter::cnvExifComment, &Converter::cnvXmpComment},
{mdExif, "Exif.Photo.RelatedSoundFile", "Xmp.exif.RelatedSoundFile", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.DateTimeOriginal", "Xmp.photoshop.DateCreated", &Converter::cnvExifDate,
&Converter::cnvXmpDate}, // MWG Guidelines
{mdExif, "Exif.Photo.DateTimeDigitized", "Xmp.xmp.CreateDate", &Converter::cnvExifDate,
&Converter::cnvXmpDate}, // MWG Guidelines
{mdExif, "Exif.Photo.ExposureTime", "Xmp.exif.ExposureTime", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FNumber", "Xmp.exif.FNumber", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ExposureProgram", "Xmp.exif.ExposureProgram", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SpectralSensitivity", "Xmp.exif.SpectralSensitivity", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ISOSpeedRatings", "Xmp.exif.ISOSpeedRatings", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.OECF", "Xmp.exif.OECF", &Converter::cnvExifValue, &Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.ShutterSpeedValue", "Xmp.exif.ShutterSpeedValue", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ApertureValue", "Xmp.exif.ApertureValue", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.BrightnessValue", "Xmp.exif.BrightnessValue", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ExposureBiasValue", "Xmp.exif.ExposureBiasValue", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.MaxApertureValue", "Xmp.exif.MaxApertureValue", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SubjectDistance", "Xmp.exif.SubjectDistance", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.MeteringMode", "Xmp.exif.MeteringMode", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.LightSource", "Xmp.exif.LightSource", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.Flash", "Xmp.exif.Flash", &Converter::cnvExifFlash, &Converter::cnvXmpFlash},
{mdExif, "Exif.Photo.FocalLength", "Xmp.exif.FocalLength", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SubjectArea", "Xmp.exif.SubjectArea", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FlashEnergy", "Xmp.exif.FlashEnergy", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SpatialFrequencyResponse", "Xmp.exif.SpatialFrequencyResponse", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.FocalPlaneXResolution", "Xmp.exif.FocalPlaneXResolution", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FocalPlaneYResolution", "Xmp.exif.FocalPlaneYResolution", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FocalPlaneResolutionUnit", "Xmp.exif.FocalPlaneResolutionUnit", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SubjectLocation", "Xmp.exif.SubjectLocation", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ExposureIndex", "Xmp.exif.ExposureIndex", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SensingMethod", "Xmp.exif.SensingMethod", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FileSource", "Xmp.exif.FileSource", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.SceneType", "Xmp.exif.SceneType", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.CFAPattern", "Xmp.exif.CFAPattern", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.CustomRendered", "Xmp.exif.CustomRendered", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ExposureMode", "Xmp.exif.ExposureMode", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.WhiteBalance", "Xmp.exif.WhiteBalance", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.DigitalZoomRatio", "Xmp.exif.DigitalZoomRatio", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.FocalLengthIn35mmFilm", "Xmp.exif.FocalLengthIn35mmFilm", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.SceneCaptureType", "Xmp.exif.SceneCaptureType", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.GainControl", "Xmp.exif.GainControl", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.Contrast", "Xmp.exif.Contrast", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.Saturation", "Xmp.exif.Saturation", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.Sharpness", "Xmp.exif.Sharpness", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.Photo.DeviceSettingDescription", "Xmp.exif.DeviceSettingDescription", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.Photo.SubjectDistanceRange", "Xmp.exif.SubjectDistanceRange", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.Photo.ImageUniqueID", "Xmp.exif.ImageUniqueID", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSVersionID", "Xmp.exif.GPSVersionID", &Converter::cnvExifGPSVersion,
&Converter::cnvXmpGPSVersion},
{mdExif, "Exif.GPSInfo.GPSLatitude", "Xmp.exif.GPSLatitude", &Converter::cnvExifGPSCoord,
&Converter::cnvXmpGPSCoord},
{mdExif, "Exif.GPSInfo.GPSLongitude", "Xmp.exif.GPSLongitude", &Converter::cnvExifGPSCoord,
&Converter::cnvXmpGPSCoord},
{mdExif, "Exif.GPSInfo.GPSAltitudeRef", "Xmp.exif.GPSAltitudeRef", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSAltitude", "Xmp.exif.GPSAltitude", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSTimeStamp", "Xmp.exif.GPSTimeStamp", &Converter::cnvExifDate,
&Converter::cnvXmpDate}, // FIXME ?
{mdExif, "Exif.GPSInfo.GPSSatellites", "Xmp.exif.GPSSatellites", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSStatus", "Xmp.exif.GPSStatus", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSMeasureMode", "Xmp.exif.GPSMeasureMode", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSDOP", "Xmp.exif.GPSDOP", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSSpeedRef", "Xmp.exif.GPSSpeedRef", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSSpeed", "Xmp.exif.GPSSpeed", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSTrackRef", "Xmp.exif.GPSTrackRef", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSTrack", "Xmp.exif.GPSTrack", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSImgDirectionRef", "Xmp.exif.GPSImgDirectionRef", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSImgDirection", "Xmp.exif.GPSImgDirection", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSMapDatum", "Xmp.exif.GPSMapDatum", &Converter::cnvExifValue, &Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSDestLatitude", "Xmp.exif.GPSDestLatitude", &Converter::cnvExifGPSCoord,
&Converter::cnvXmpGPSCoord},
{mdExif, "Exif.GPSInfo.GPSDestLongitude", "Xmp.exif.GPSDestLongitude", &Converter::cnvExifGPSCoord,
&Converter::cnvXmpGPSCoord},
{mdExif, "Exif.GPSInfo.GPSDestBearingRef", "Xmp.exif.GPSDestBearingRef", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSDestBearing", "Xmp.exif.GPSDestBearing", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSDestDistanceRef", "Xmp.exif.GPSDestDistanceRef", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSDestDistance", "Xmp.exif.GPSDestDistance", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdExif, "Exif.GPSInfo.GPSProcessingMethod", "Xmp.exif.GPSProcessingMethod", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.GPSInfo.GPSAreaInformation", "Xmp.exif.GPSAreaInformation", &Converter::cnvExifValue,
&Converter::cnvXmpValue}, // FIXME ?
{mdExif, "Exif.GPSInfo.GPSDifferential", "Xmp.exif.GPSDifferential", &Converter::cnvExifValue,
&Converter::cnvXmpValue},
{mdIptc, "Iptc.Application2.ObjectName", "Xmp.dc.title", &Converter::cnvIptcValue, &Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Urgency", "Xmp.photoshop.Urgency", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Category", "Xmp.photoshop.Category", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.SuppCategory", "Xmp.photoshop.SupplementalCategories", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Keywords", "Xmp.dc.subject", &Converter::cnvIptcValue, &Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.SubLocation", "Xmp.iptc.Location", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.SpecialInstructions", "Xmp.photoshop.Instructions", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.DateCreated", "Xmp.photoshop.DateCreated", &Converter::cnvNone,
&Converter::cnvXmpValueToIptc}, // FIXME to IPTC Date and IPTC Time
{mdIptc, "Iptc.Application2.DigitizationDate", "Xmp.xmp.CreateDate", &Converter::cnvNone,
&Converter::cnvXmpValueToIptc}, // FIXME to IPTC Date and IPTC Time
{mdIptc, "Iptc.Application2.Byline", "Xmp.dc.creator", &Converter::cnvIptcValue, &Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.BylineTitle", "Xmp.photoshop.AuthorsPosition", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.City", "Xmp.photoshop.City", &Converter::cnvIptcValue, &Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.ProvinceState", "Xmp.photoshop.State", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.CountryCode", "Xmp.iptc.CountryCode", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.CountryName", "Xmp.photoshop.Country", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.TransmissionReference", "Xmp.photoshop.TransmissionReference", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Headline", "Xmp.photoshop.Headline", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Credit", "Xmp.photoshop.Credit", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Source", "Xmp.photoshop.Source", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Copyright", "Xmp.dc.rights", &Converter::cnvIptcValue, &Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Caption", "Xmp.dc.description", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc},
{mdIptc, "Iptc.Application2.Writer", "Xmp.photoshop.CaptionWriter", &Converter::cnvIptcValue,
&Converter::cnvXmpValueToIptc}
};
Converter::Converter(ExifData& exifData, XmpData& xmpData) :
exifData_(&exifData), iptcData_(nullptr), xmpData_(&xmpData), iptcCharset_(nullptr) {
}
Converter::Converter(IptcData& iptcData, XmpData& xmpData, const char* iptcCharset) :
exifData_(nullptr), iptcData_(&iptcData), xmpData_(&xmpData), iptcCharset_(iptcCharset) {
}
void Converter::cnvToXmp() {
for (auto&& c : conversion_) {
if ((c.metadataId_ == mdExif && exifData_) || (c.metadataId_ == mdIptc && iptcData_)) {
std::invoke(c.key1ToKey2_, *this, c.key1_, c.key2_);
}
}
}
void Converter::cnvFromXmp() {
for (auto&& c : conversion_) {
if ((c.metadataId_ == mdExif && exifData_) || (c.metadataId_ == mdIptc && iptcData_)) {
std::invoke(c.key2ToKey1_, *this, c.key2_, c.key1_);
}
}
}
void Converter::cnvNone(const char*, const char*) {
}
bool Converter::prepareExifTarget(const char* to, bool force) {
auto pos = exifData_->findKey(ExifKey(to));
if (pos == exifData_->end())
return true;
if (!overwrite_ && !force)
return false;
exifData_->erase(pos);
return true;
}
bool Converter::prepareIptcTarget(const char* to, bool force) {
auto pos = iptcData_->findKey(IptcKey(to));
if (pos == iptcData_->end())
return true;
if (!overwrite_ && !force)
return false;
while ((pos = iptcData_->findKey(IptcKey(to))) != iptcData_->end()) {
iptcData_->erase(pos);
}
return true;
}
bool Converter::prepareXmpTarget(const char* to, bool force) {
auto pos = xmpData_->findKey(XmpKey(to));
if (pos == xmpData_->end())
return true;
if (!overwrite_ && !force)
return false;
xmpData_->erase(pos);
return true;
}
void Converter::cnvExifValue(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
std::string value = pos->toString();
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
if (!prepareXmpTarget(to))
return;
(*xmpData_)[to] = value;
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifComment(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
const auto cv = dynamic_cast<const CommentValue*>(&pos->value());
if (!cv) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
// Todo: Convert to UTF-8 if necessary
try {
(*xmpData_)[to] = cv->comment();
} catch (const Error&) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
}
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifArray(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
for (size_t i = 0; i < pos->count(); ++i) {
std::string value = pos->toString(i);
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
(*xmpData_)[to] = value;
}
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifDate(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int min = 0;
int sec = 0;
std::string subsec;
char buf[30];
if (std::string(from) != "Exif.GPSInfo.GPSTimeStamp") {
std::string value = pos->toString();
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
if (sscanf(value.c_str(), "%d:%d:%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec) != 6) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << ", unable to parse '" << value << "'\n";
#endif
return;
}
} else { // "Exif.GPSInfo.GPSTimeStamp"
bool ok = true;
if (pos->count() != 3)
ok = false;
if (ok) {
for (int i = 0; i < 3; ++i) {
if (pos->toRational(i).second == 0) {
ok = false;
break;
}
}
}
if (!ok) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
double dhour = pos->toFloat(0);
double dmin = pos->toFloat(1);
// Hack: Need Value::toDouble
auto [r, s] = pos->toRational(2);
double dsec = static_cast<double>(r) / s;
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
dsec = dhour * 3600.0 + dmin * 60.0 + dsec;
hour = static_cast<int>(dsec / 3600.0);
dsec -= hour * 3600;
min = static_cast<int>(dsec / 60.0);
dsec -= min * 60;
sec = static_cast<int>(dsec);
dsec -= sec;
snprintf(buf, sizeof(buf), "%.9f", dsec);
buf[sizeof(buf) - 1] = 0;
buf[1] = '.'; // some locales use ','
subsec = buf + 1;
auto datePos = exifData_->findKey(ExifKey("Exif.GPSInfo.GPSDateStamp"));
if (datePos == exifData_->end()) {
datePos = exifData_->findKey(ExifKey("Exif.Photo.DateTimeOriginal"));
}
if (datePos == exifData_->end()) {
datePos = exifData_->findKey(ExifKey("Exif.Photo.DateTimeDigitized"));
}
if (datePos == exifData_->end()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
std::string value = datePos->toString();
if (sscanf(value.c_str(), "%d:%d:%d", &year, &month, &day) != 3) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << ", unable to parse '" << value << "'\n";
#endif
return;
}
}
const char* subsecTag = nullptr;
if (std::string(from) == "Exif.Image.DateTime") {
subsecTag = "Exif.Photo.SubSecTime";
} else if (std::string(from) == "Exif.Photo.DateTimeOriginal") {
subsecTag = "Exif.Photo.SubSecTimeOriginal";
} else if (std::string(from) == "Exif.Photo.DateTimeDigitized") {
subsecTag = "Exif.Photo.SubSecTimeDigitized";
}
if (subsecTag) {
auto subsec_pos = exifData_->findKey(ExifKey(subsecTag));
if (subsec_pos != exifData_->end()) {
if (subsec_pos->typeId() == asciiString) {
std::string ss = subsec_pos->toString();
if (!ss.empty()) {
bool ok = false;
stringTo<long>(ss, ok);
if (ok)
subsec = std::string(".") + ss;
}
}
if (erase_)
exifData_->erase(subsec_pos);
}
}
if (subsec.size() > 10)
subsec.resize(10);
snprintf(buf, sizeof(buf), "%4d-%02d-%02dT%02d:%02d:%02d%s", year, month, day, hour, min, sec, subsec.c_str());
buf[sizeof(buf) - 1] = 0;
(*xmpData_)[to] = buf;
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifVersion(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
std::ostringstream value;
for (size_t i = 0; i < pos->count(); ++i) {
value << static_cast<char>(pos->toInt64(i));
}
(*xmpData_)[to] = value.str();
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifGPSVersion(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
std::ostringstream value;
for (size_t i = 0; i < pos->count(); ++i) {
if (i > 0)
value << '.';
value << pos->toInt64(i);
}
(*xmpData_)[to] = value.str();
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifFlash(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end() || pos->count() == 0)
return;
if (!prepareXmpTarget(to))
return;
auto value = pos->toUint32();
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
(*xmpData_)["Xmp.exif.Flash/exif:Fired"] = static_cast<bool>(value & 1);
(*xmpData_)["Xmp.exif.Flash/exif:Return"] = (value >> 1) & 3;
(*xmpData_)["Xmp.exif.Flash/exif:Mode"] = (value >> 3) & 3;
(*xmpData_)["Xmp.exif.Flash/exif:Function"] = static_cast<bool>((value >> 5) & 1);
(*xmpData_)["Xmp.exif.Flash/exif:RedEyeMode"] = static_cast<bool>((value >> 6) & 1);
if (erase_)
exifData_->erase(pos);
}
void Converter::cnvExifGPSCoord(const char* from, const char* to) {
auto pos = exifData_->findKey(ExifKey(from));
if (pos == exifData_->end())
return;
if (!prepareXmpTarget(to))
return;
if (pos->count() != 3) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
auto refPos = exifData_->findKey(ExifKey(std::string(from) + "Ref"));
if (refPos == exifData_->end()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
double deg[3];
for (int i = 0; i < 3; ++i) {
const auto [z, d] = pos->toRational(i);
if (d == 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
// Hack: Need Value::toDouble
deg[i] = static_cast<double>(z) / d;
}
double min = deg[0] * 60.0 + deg[1] + deg[2] / 60.0;
auto ideg = static_cast<int>(min / 60.0);
min -= ideg * 60;
std::ostringstream oss;
oss << ideg << "," << std::fixed << std::setprecision(7) << min << refPos->toString().c_str()[0];
(*xmpData_)[to] = oss.str();
if (erase_)
exifData_->erase(pos);
if (erase_)
exifData_->erase(refPos);
}
void Converter::cnvXmpValue(const char* from, const char* to) {
auto pos = xmpData_->findKey(XmpKey(from));
if (pos == xmpData_->end())
return;
if (!prepareExifTarget(to))
return;
std::string value;
if (!getTextValue(value, pos)) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
// Todo: Escape non-ASCII characters in XMP text values
ExifKey key(to);
if (auto ed = Exifdatum(key); ed.setValue(value) == 0) {
exifData_->add(ed);
}
if (erase_)
xmpData_->erase(pos);
}
void Converter::cnvXmpComment(const char* from, const char* to) {
if (!prepareExifTarget(to))
return;
auto pos = xmpData_->findKey(XmpKey(from));
if (pos == xmpData_->end())
return;
std::string value;
if (!getTextValue(value, pos)) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
// Assumes the XMP value is encoded in UTF-8, as it should be
(*exifData_)[to] = "charset=Unicode " + value;
if (erase_)
xmpData_->erase(pos);
}
void Converter::cnvXmpArray(const char* from, const char* to) {
if (!prepareExifTarget(to))
return;
auto pos = xmpData_->findKey(XmpKey(from));
if (pos == xmpData_->end())
return;
std::ostringstream array;
for (size_t i = 0; i < pos->count(); ++i) {
std::string value = pos->toString(i);
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
array << value;
if (i != pos->count() - 1)
array << " ";
}
(*exifData_)[to] = array.str();
if (erase_)
xmpData_->erase(pos);
}
void Converter::cnvXmpDate(const char* from, const char* to) {
auto pos = xmpData_->findKey(XmpKey(from));
if (pos == xmpData_->end())
return;
if (!prepareExifTarget(to))
return;
#ifdef EXV_HAVE_XMP_TOOLKIT
std::string value = pos->toString();
if (!pos->value().ok()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
return;
}
XMP_DateTime datetime;
try {
SXMPUtils::ConvertToDate(value, &datetime);
char buf[30];
if (std::string(to) != "Exif.GPSInfo.GPSTimeStamp") {
SXMPUtils::ConvertToLocalTime(&datetime);
snprintf(buf, sizeof(buf), "%4d:%02d:%02d %02d:%02d:%02d", datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second);
buf[sizeof(buf) - 1] = 0;
(*exifData_)[to] = buf;
if (datetime.nanoSecond) {
const char* subsecTag = nullptr;
if (std::string(to) == "Exif.Image.DateTime") {
subsecTag = "Exif.Photo.SubSecTime";
} else if (std::string(to) == "Exif.Photo.DateTimeOriginal") {
subsecTag = "Exif.Photo.SubSecTimeOriginal";
} else if (std::string(to) == "Exif.Photo.DateTimeDigitized") {
subsecTag = "Exif.Photo.SubSecTimeDigitized";
}
if (subsecTag) {
prepareExifTarget(subsecTag, true);
(*exifData_)[subsecTag] = std::to_string(datetime.nanoSecond);
}
}
} else { // "Exif.GPSInfo.GPSTimeStamp"
// Ignore the time zone, assuming the time is in UTC as it should be
URational rhour(datetime.hour, 1);
URational rmin(datetime.minute, 1);
URational rsec(datetime.second, 1);
if (datetime.nanoSecond != 0) {
if (datetime.second != 0) {
// Add the seconds to rmin so that the ns fit into rsec
rmin.second = 60;
rmin.first *= 60;
rmin.first += datetime.second;
}
rsec.second = 1000000000;
rsec.first = datetime.nanoSecond;
}
std::ostringstream array;
array << rhour << " " << rmin << " " << rsec;
(*exifData_)[to] = array.str();
prepareExifTarget("Exif.GPSInfo.GPSDateStamp", true);
snprintf(buf, sizeof(buf), "%4d:%02d:%02d", datetime.year, datetime.month, datetime.day);
buf[sizeof(buf) - 1] = 0;
(*exifData_)["Exif.GPSInfo.GPSDateStamp"] = buf;
}
}
#ifndef SUPPRESS_WARNINGS
catch (const XMP_Error& e) {
EXV_WARNING << "Failed to convert " << from << " to " << to << " (" << e.GetErrMsg() << ")\n";
return;
}
#else
catch (const XMP_Error&) {
return;
}
#endif // SUPPRESS_WARNINGS
if (erase_)
xmpData_->erase(pos);
#else
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to convert " << from << " to " << to << "\n";
#endif
#endif // !EXV_HAVE_XMP_TOOLKIT
}
void Converter::cnvXmpVersion(const char* from, const char* to) {
auto pos = xmpData_->findKey(XmpKey(from));
if (pos == xmpData_->end())
return;