forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif.cc
5991 lines (5377 loc) · 201 KB
/
exif.cc
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
/*
This file is part of darktable,
Copyright (C) 2009-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/dtpthread.h"
#define __STDC_FORMAT_MACROS
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <glib.h>
#include <sqlite3.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <zlib.h>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <exiv2/exiv2.hpp>
#include "control/control.h"
#if defined(_WIN32) && defined(EXV_UNICODE_PATH)
#define WIDEN(s) pugi::as_wide(s)
#else
#define WIDEN(s) (s)
#endif
#include <pugixml.hpp>
using namespace std;
#include "common/color_harmony.h"
#include "common/colorlabels.h"
#include "common/darktable.h"
#include "common/debug.h"
#include "common/dng_opcode.h"
#include "common/image_cache.h"
#include "common/exif.h"
#include "common/metadata.h"
#include "common/ratings.h"
#include "common/tags.h"
#include "common/iop_order.h"
#include "common/variables.h"
#include "common/utility.h"
#include "common/history.h"
#include "common/datetime.h"
#include "control/conf.h"
#include "develop/imageop.h"
#include "develop/blend.h"
#include "develop/masks.h"
#include "imageio/imageio_common.h"
#include "imageio/imageio_jpeg.h"
#define DT_XMP_EXIF_VERSION 5
#if EXIV2_TEST_VERSION(0,28,0)
#define AnyError Error
#define toLong toInt64
#endif
// For these models we can't calculate the correct crop factor or, for some we could, but we
// prefer to take it from here, rather than complicate the calculation code with exceptions
static const struct dt_model_cropfactor dt_cropfactors[] = {
{.model = "FinePix SL1000", // exiv2 doesn't yet read the tags we need to calculate correctly
.cropfactor = 5.6f
},
{.model = "FinePix E550", // tags contain incorrect data, so formula gives us incorrect result
.cropfactor = 4.65f
},
{.model = "XF10", // resolution was calculated relative to dimensions not typical of Fuji
.cropfactor = 1.5f
},
{.model = "FinePix S1", // resolution was calculated relative to dimensions not typical of Fuji
.cropfactor = 5.6f
},
{.model = "FinePix HS10 HS11", // calculation gives a slightly inaccurate result
.cropfactor = 5.64f
},
};
// Persistent list of Exiv2 tags. Set up in dt_init().
static GList *exiv2_taglist = NULL;
static const char *_get_exiv2_type(const int type)
{
switch(type)
{
case 1:
return "Byte";
case 2:
return "Ascii";
case 3:
return "Short";
case 4:
return "Long";
case 5:
return "Rational"; // two LONGs: numerator and denumerator of a fraction
case 6:
return "SByte";
case 7:
return "Undefined";
case 8:
return "SShort";
case 9:
return "SLong";
case 10:
return "SRational"; // two SLONGs: numerator and denumerator of a fraction.
case 11:
return "Float"; // single precision (4-byte) IEEE format
case 12:
return "Double"; // double precision (8-byte) IEEE format.
case 13:
return "Ifd"; // 32-bit (4-byte) unsigned integer
case 16:
return "LLong"; // 64-bit (8-byte) unsigned integer
case 17:
return "LLong"; // 64-bit (8-byte) signed integer
case 18:
return "Ifd8"; // 64-bit (8-byte) unsigned integer
case 0x10000:
return "String";
case 0x10001:
return "Date";
case 0x10002:
return "Time";
case 0x10003:
return "Comment";
case 0x10004:
return "Directory";
case 0x10005:
return "XmpText";
case 0x10006:
return "XmpAlt";
case 0x10007:
return "XmpBag";
case 0x10008:
return "XmpSeq";
case 0x10009:
return "LangAlt";
case 0x1fffe:
return "Invalid";
case 0x1ffff:
return "LastType";
default:
return "Invalid";
}
}
static void _get_xmp_tags(const char *prefix,
GList **taglist)
{
const Exiv2::XmpPropertyInfo *pl = Exiv2::XmpProperties::propertyList(prefix);
if(pl)
{
for(int i = 0; pl[i].name_ != 0; ++i)
{
char *tag = g_strdup_printf("Xmp.%s.%s,%s",
prefix, pl[i].name_, _get_exiv2_type(pl[i].typeId_));
*taglist = g_list_prepend(*taglist, tag);
}
}
}
/*
The correction matrices are taken from http://www.brucelindbloom.com - chromatic Adaption.
using Bradford method: found Illuminant -> D65
ISOStudioTungsten, DaylightFluorescent, DayWhiteFluorescent, CoolWhiteFluorescent,
WhiteFluorescent and WarmWhiteFluorescent were calculated with xy coord from DNG SDK as reference -> XYZ -> D65
xy coord and temperatures are the same as in DNG SDK and habe not been changed in dt.
*/
static const struct dt_dng_illuminant_data_t illuminant_data[DT_LS_Other + 1] =
{
{ 0, "Unknown Illuminant", {0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}},
{ 5500, "Daylight", {0.3324, 0.3474}, {0.9726856, -0.0135482, 0.0361731, -0.0167463, 1.0049102, 0.0120598, 0.0070026, -0.0116372, 1.1869548}},
{ 4230, "Fluorescent", {0.37208, 0.37529}, {0.9212269, -0.0449128, 0.1211620, -0.0553723, 1.0277243, 0.0403563, 0.0235086, -0.0391019, 1.6390644}},
{ 2850, "Tungsten", {0.4476, 0.4074}, {0.8446965, -0.1179225, 0.3948108, -0.1366303, 1.1041226, 0.1291718, 0.0798489, -0.1348999, 3.1924009}},
{ 5500, "Flash", {0.3324, 0.3474}, {0.9726856, -0.0135482, 0.0361731, -0.0167463, 1.0049102, 0.0120598, 0.0070026, -0.0116372, 1.1869548}},
{ 0, "Unknown Illuminant5", {0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}},
{ 0, "Unknown Illuminant6", {0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}},
{ 0, "Unknown Illuminant7", {0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}},
{ 0, "Unknown Illuminant8", {0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}},
{ 5500, "FineWeather", {0.3324, 0.3474}, {0.9726856, -0.0135482, 0.0361731, -0.0167463, 1.0049102, 0.0120598, 0.0070026, -0.0116372, 1.1869548}},
{ 6500, "CloudyWeather", {0.3127, 0.3290}, {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}},
{ 7500, "Shade", {0.2990, 0.3149}, {1.0206905, 0.0091588, -0.0228796, 0.0115005, 0.9984917, -0.0076762, -0.0043619, 0.0072053, 0.8853432}},
{ 6430, "DaylightFluorescent", {0.31310, 0.33727}, {1.0096114, 0.0061501, 0.0068113, 0.0102539, 0.9888663, 0.0015575, 0.0023119, -0.0044823, 1.0525915}},
{ 5000, "DayWhiteFluorescent", {0.34588, 0.35875}, {0.9554129, -0.0231280, 0.0637169, -0.0283629, 1.0099053, 0.0211824, 0.0124188, -0.0206922, 1.3330592}},
{ 4150, "CoolWhiteFluorescent", {0.37417, 0.37281}, {0.9147843, -0.0492842, 0.1202810, -0.0622085, 1.034984, 0.0404480, 0.0228014, -0.0375807, 1.6259804}},
{ 3450, "WhiteFluorescent", {0.40910, 0.39430}, {0.8805388, -0.0774890, 0.2293784, -0.0932136, 1.0589267, 0.0757827, 0.0453660, -0.0760107, 2.2417979}},
{ 2940, "WarmWhiteFluorescent", {0.44018, 0.40329}, {0.8488316, -0.1107439, 0.3471428, -0.1310107, 1.0986874, 0.1141548, 0.0694025, -0.1167541, 2.9109462}},
{ 2850, "StandardLightA", {0.4476, 0.4074}, {0.8446965, -0.1179225, 0.3948108, -0.1366303, 1.1041226, 0.1291718, 0.0798489, -0.1348999, 3.1924009}},
{ 4871, "StandardLightB", {0.348483, 0.351747}, {0.9415037, -0.0321240, 0.0584672, -0.0428238, 1.0250998, 0.0203309, 0.0101511, -0.0161170, 1.2847354}},
{ 6774, "StandardLightC", {0.310061, 0.316150}, {0.9904476, -0.0071683, -0.0116156, -0.0123712, 1.0155950, -0.0029282, -0.0035635, 0.0067697, 0.9181569}},
{ 5500, "D55", {0.3324, 0.3474}, {0.9726856, -0.0135482, 0.0361731, -0.0167463, 1.0049102, 0.0120598, 0.0070026, -0.0116372, 1.1869548}},
{ 6500, "D65", {0.3127, 0.3290}, {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}},
{ 7500, "D75", {0.2990, 0.3149}, {1.0206905, 0.0091588, -0.0228796, 0.0115005, 0.9984917, -0.0076762, -0.0043619, 0.0072053, 0.8853432}},
{ 5000, "D50", {0.3457, 0.3585}, {0.9555766, -0.0230393, 0.0631636, -0.0282895, 1.0099416, 0.0210077, 0.0122982, -0.0204830, 1.3299098}},
{ 3200, "ISOStudioTungsten", {0.40910, 0.39430}, {0.8663030, -0.0913083, 0.2771784, -0.1090504, 1.0746895, 0.0913841, 0.0550856, -0.0924636, 2.5119387}},
{ 6500, "Other illuminant", {0.3127, 0.3290}, {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}}
};
static inline const char *_illu_to_str(const dt_dng_illuminant_t illu)
{
return illuminant_data[illu].name;
}
static inline int _illu_to_temp(const dt_dng_illuminant_t illu)
{
return illuminant_data[illu].temp;
}
// we append i immediately after the title only if it's in the range 1-3
static inline void _print_matrix_data(const char *title, const int i, float *M)
{
dt_print(DT_DEBUG_IMAGEIO, "%s%s%s%s = %.4f %.4f %.4f | %.4f %.4f %.4f | %.4f %.4f %.4f",
title,
i == 1 ? "1" : "",
i == 2 ? "2" : "",
i == 3 ? "3" : "",
M[0], M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8]);
}
void dt_exif_set_exiv2_taglist()
{
if(exiv2_taglist)
return;
try
{
const Exiv2::GroupInfo *groupList = Exiv2::ExifTags::groupList();
if(groupList)
{
while(groupList->tagList_)
{
const std::string groupName(groupList->groupName_);
if(groupName.substr(0, 3) != "Sub" &&
groupName != "Image2" &&
groupName != "Image3" &&
groupName != "Thumbnail"
)
{
const Exiv2::TagInfo *tagInfo = groupList->tagList_();
while(tagInfo->tag_ != 0xFFFF)
{
char *tag = g_strdup_printf("Exif.%s.%s,%s",
groupList->groupName_,
tagInfo->name_,
_get_exiv2_type(tagInfo->typeId_));
exiv2_taglist = g_list_prepend(exiv2_taglist, tag);
tagInfo++;
}
}
groupList++;
}
}
const Exiv2::DataSet *iptcEnvelopeList = Exiv2::IptcDataSets::envelopeRecordList();
while(iptcEnvelopeList->number_ != 0xFFFF)
{
char *tag = g_strdup_printf("Iptc.Envelope.%s,%s%s",
iptcEnvelopeList->name_,
_get_exiv2_type(iptcEnvelopeList->type_),
iptcEnvelopeList->repeatable_ ? "-R" : "");
exiv2_taglist = g_list_prepend(exiv2_taglist, tag);
iptcEnvelopeList++;
}
const Exiv2::DataSet *iptcApplication2List =
Exiv2::IptcDataSets::application2RecordList();
while(iptcApplication2List->number_ != 0xFFFF)
{
char *tag = g_strdup_printf("Iptc.Application2.%s,%s%s",
iptcApplication2List->name_,
_get_exiv2_type(iptcApplication2List->type_),
iptcApplication2List->repeatable_ ? "-R" : "");
exiv2_taglist = g_list_prepend(exiv2_taglist, tag);
iptcApplication2List++;
}
_get_xmp_tags("dc", &exiv2_taglist);
_get_xmp_tags("xmp", &exiv2_taglist);
_get_xmp_tags("xmpRights", &exiv2_taglist);
_get_xmp_tags("xmpMM", &exiv2_taglist);
_get_xmp_tags("xmpBJ", &exiv2_taglist);
_get_xmp_tags("xmpTPg", &exiv2_taglist);
_get_xmp_tags("xmpDM", &exiv2_taglist);
_get_xmp_tags("pdf", &exiv2_taglist);
_get_xmp_tags("photoshop", &exiv2_taglist);
_get_xmp_tags("crs", &exiv2_taglist);
_get_xmp_tags("tiff", &exiv2_taglist);
_get_xmp_tags("exif", &exiv2_taglist);
_get_xmp_tags("exifEX", &exiv2_taglist);
_get_xmp_tags("aux", &exiv2_taglist);
_get_xmp_tags("iptc", &exiv2_taglist);
_get_xmp_tags("iptcExt", &exiv2_taglist);
_get_xmp_tags("plus", &exiv2_taglist);
_get_xmp_tags("mwg-rs", &exiv2_taglist);
_get_xmp_tags("mwg-kw", &exiv2_taglist);
_get_xmp_tags("dwc", &exiv2_taglist);
_get_xmp_tags("dcterms", &exiv2_taglist);
_get_xmp_tags("digiKam", &exiv2_taglist);
_get_xmp_tags("kipi", &exiv2_taglist);
_get_xmp_tags("GPano", &exiv2_taglist);
_get_xmp_tags("lr", &exiv2_taglist);
_get_xmp_tags("MP", &exiv2_taglist);
_get_xmp_tags("MPRI", &exiv2_taglist);
_get_xmp_tags("MPReg", &exiv2_taglist);
_get_xmp_tags("acdsee", &exiv2_taglist);
_get_xmp_tags("mediapro", &exiv2_taglist);
_get_xmp_tags("expressionmedia", &exiv2_taglist);
_get_xmp_tags("MicrosoftPhoto", &exiv2_taglist);
}
catch (Exiv2::AnyError& e)
{
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO, "[exiv2 taglist] %s", errstring);
}
}
const GList* dt_exif_get_exiv2_taglist()
{
if(!exiv2_taglist)
dt_exif_set_exiv2_taglist();
return exiv2_taglist;
}
static const char *_exif_get_exiv2_tag_type(const char *tagname)
{
if(!tagname) return NULL;
for(GList *tag = exiv2_taglist; tag; tag = g_list_next(tag))
{
char *t = (char *)tag->data;
if(g_str_has_prefix(t, tagname) && t[strlen(tagname)] == ',')
{
if(t)
{
t += strlen(tagname) + 1;
return t;
}
}
}
return NULL;
}
// Exiv2's readMetadata is not thread safe in 0.26. So we lock it.
// Since readMetadata might throw an exception we wrap it into
// some C++ magic to make sure we unlock in all cases. Well, actually
// not magic but basic RAII.
// FIXME: Check again once we rely on 0.27.
class Lock
{
public:
Lock() { dt_pthread_mutex_lock(&darktable.exiv2_threadsafe); }
~Lock() { dt_pthread_mutex_unlock(&darktable.exiv2_threadsafe); }
};
#define read_metadata_threadsafe(image) \
{ \
Lock lock; \
image->readMetadata(); \
}
static void _exif_import_tags(dt_image_t *img, Exiv2::XmpData::iterator &pos);
static void _read_xmp_timestamps(Exiv2::XmpData &xmpData,
dt_image_t *img,
const int xmp_version);
static void _read_xmp_harmony_guide(Exiv2::XmpData &xmpData,
dt_image_t *img,
const int xmp_version);
// This array should contain all XmpBag and XmpSeq keys used by dt
const char *dt_xmp_keys[]
= { "Xmp.dc.subject", "Xmp.lr.hierarchicalSubject",
"Xmp.darktable.colorlabels", "Xmp.darktable.history",
"Xmp.darktable.history_modversion", "Xmp.darktable.history_enabled",
"Xmp.darktable.history_end", "Xmp.darktable.iop_order_version",
"Xmp.darktable.iop_order_list", "Xmp.darktable.history_operation",
"Xmp.darktable.history_params", "Xmp.darktable.blendop_params",
"Xmp.darktable.blendop_version", "Xmp.darktable.multi_priority",
"Xmp.darktable.multi_name", "Xmp.darktable.multi_name_hand_edited",
"Xmp.darktable.iop_order", "Xmp.darktable.xmp_version",
"Xmp.darktable.raw_params", "Xmp.darktable.auto_presets_applied",
"Xmp.darktable.mask_id", "Xmp.darktable.mask_type",
"Xmp.darktable.mask_name", "Xmp.darktable.masks_history",
"Xmp.darktable.mask_num", "Xmp.darktable.mask_points",
"Xmp.darktable.mask_version", "Xmp.darktable.mask",
"Xmp.darktable.mask_nb", "Xmp.darktable.mask_src",
"Xmp.darktable.history_basic_hash", "Xmp.darktable.history_auto_hash",
"Xmp.darktable.history_current_hash", "Xmp.darktable.import_timestamp",
"Xmp.darktable.change_timestamp", "Xmp.darktable.export_timestamp",
"Xmp.darktable.print_timestamp", "Xmp.darktable.version_name",
"Xmp.darktable.harmony_guide_type", "Xmp.darktable.harmony_guide_rotation",
"Xmp.darktable.harmony_guide_width" };
// The number of XmpBag XmpSeq keys that dt uses
static const guint dt_xmp_keys_n = G_N_ELEMENTS(dt_xmp_keys);
// Inspired by ufraw_exiv2.cc:
static void _strlcpy_to_utf8(char *dest,
const size_t dest_max,
Exiv2::ExifData::const_iterator &pos,
Exiv2::ExifData &exifData)
{
std::string str = pos->print(&exifData);
char *s = g_locale_to_utf8(str.c_str(), str.length(), NULL, NULL, NULL);
if(s != NULL)
{
g_strlcpy(dest, s, dest_max);
g_free(s);
}
else
{
g_strlcpy(dest, str.c_str(), dest_max);
}
g_strstrip(dest);
}
// If a numerical Exif value n has no string representation in Exiv2
// then the returned string is a useless "(n)" which can be discarded
static void _exif_value_str(const int value,
char *dest,
const size_t dest_max,
Exiv2::ExifData::const_iterator &pos,
Exiv2::ExifData &exifData)
{
_strlcpy_to_utf8(dest, dest_max, pos, exifData);
gchar *str_value = g_strdup_printf("(%d)", value);
if(g_strcmp0(dest, str_value) == 0)
{
dest[0] = '\0';
}
g_free(str_value);
}
static void _deleteXmpTag(Exiv2::XmpData &xmp, const char *tag)
{
Exiv2::XmpData::iterator pos = xmp.findKey(Exiv2::XmpKey(tag));
while(pos != xmp.end())
{
std::string key = pos->key();
const char *ckey = key.c_str();
size_t len = key.size();
// Stop iterating once the key no longer matches what we are
// trying to delete. This assumes sorted input.
if(!(g_str_has_prefix(ckey, tag)
&& (ckey[len] == '[' || ckey[len] == '\0')))
break;
pos = xmp.erase(pos);
}
}
// Function to remove known dt keys and subtrees from xmpdata, so not
// to append them twice. This should work because dt first reads all
// known keys.
static void _remove_known_keys(Exiv2::XmpData &xmp)
{
xmp.sortByKey();
// dt internal tags
for(unsigned int i = 0; i < dt_xmp_keys_n; i++)
_deleteXmpTag(xmp, dt_xmp_keys[i]);
// now the tags from the metadata editor
dt_pthread_mutex_lock(&darktable.metadata_threadsafe);
for(GList *iter = dt_metadata_get_list(); iter; iter = iter->next)
{
const dt_metadata_t *metadata = (dt_metadata_t *)iter->data;
_deleteXmpTag(xmp, metadata->tagname);
}
dt_pthread_mutex_unlock(&darktable.metadata_threadsafe);
}
static void _remove_exif_keys(Exiv2::ExifData &exif,
const char *keys[],
const unsigned int n_keys)
{
for(unsigned int i = 0; i < n_keys; i++)
{
try
{
Exiv2::ExifData::iterator pos;
while((pos = exif.findKey(Exiv2::ExifKey(keys[i]))) != exif.end())
exif.erase(pos);
}
catch(Exiv2::AnyError &e)
{
// The only exception we may get is "invalid" tag, which is not
// important enough to either stop the function, or even display
// a message (it's probably the tag that is not implemented in
// the Exiv2 version used).
}
}
}
static void _remove_xmp_keys(Exiv2::XmpData &xmp,
const char *keys[],
const unsigned int n_keys)
{
for(unsigned int i = 0; i < n_keys; i++)
{
try
{
Exiv2::XmpData::iterator pos;
while((pos = xmp.findKey(Exiv2::XmpKey(keys[i]))) != xmp.end())
xmp.erase(pos);
}
catch(Exiv2::AnyError &e)
{
// The only exception we may get is "invalid" tag, which is not
// important enough to either stop the function, or even display
// a message (it's probably the tag that is not implemented in
// the Exiv2 version used).
}
}
}
static bool _exif_read_xmp_tag(Exiv2::XmpData &xmpData,
Exiv2::XmpData::iterator *pos,
string key)
{
try
{
return (*pos = xmpData.findKey(Exiv2::XmpKey(key))) != xmpData.end()
&& (*pos)->size();
}
catch(Exiv2::AnyError &e)
{
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO, "[exiv2 read_xmp_tag] %s", errstring);
return false;
}
}
#define FIND_XMP_TAG(key) _exif_read_xmp_tag(xmpData, &pos, key)
// FIXME: according to
// http://www.exiv2.org/doc/classExiv2_1_1Metadatum.html#63c2b87249ba96679c29e01218169124
// there is no need to pass xmpData
// version = -1 --> version ignored
static bool _exif_decode_xmp_data(dt_image_t *img,
Exiv2::XmpData &xmpData,
const int version,
const bool exif_read)
{
// As this can be called several times during the image lifetime, clean up first
GList *imgs = NULL;
imgs = g_list_prepend(imgs, GINT_TO_POINTER(img->id));
try
{
Exiv2::XmpData::iterator pos;
// Older darktable version did not write this data correctly: the
// reasoning behind strdup'ing all the strings before passing it
// to sqlite3 is, that they are somehow corrupt after the call to
// sqlite3_prepare_v2() -- don't ask me why for they don't get
// passed to that function.
if(version == -1 || version > 0)
{
dt_pthread_mutex_lock(&darktable.metadata_threadsafe);
if(!exif_read) dt_metadata_clear(imgs, FALSE);
for(GList *iter = dt_metadata_get_list(); iter; iter = iter->next)
{
dt_metadata_t *metadata = (dt_metadata_t *)iter->data;
if(FIND_XMP_TAG(metadata->tagname))
{
char *value = strdup(pos->toString().c_str());
char *adr = value;
// Skip any lang="" or charset=xxx
while(!strncmp(value, "lang=", 5) || !strncmp(value, "charset=", 8))
{
while(*value != ' ' && *value) value++;
while(*value == ' ') value++;
}
dt_metadata_set_import(img->id, metadata->tagname, value);
free(adr);
}
}
dt_pthread_mutex_unlock(&darktable.metadata_threadsafe);
}
// If XMP file(s) are found, read the rating from here.
if(FIND_XMP_TAG("Xmp.darktable.xmp_version")
|| !dt_conf_get_bool("ui_last/ignore_exif_rating"))
{
if(FIND_XMP_TAG("Xmp.xmp.Rating"))
{
const int stars = pos->toLong();
dt_image_set_xmp_rating(img, stars);
}
else
dt_image_set_xmp_rating(img, -2);
}
if(!exif_read) dt_colorlabels_remove_all_labels(img->id);
if(FIND_XMP_TAG("Xmp.xmp.Label"))
{
std::string label = pos->toString();
if(label == "Red") // Is it really called like that in XMP files?
dt_colorlabels_set_label(img->id, 0);
else if(label == "Yellow") // Is it really called like that in XMP files?
dt_colorlabels_set_label(img->id, 1);
else if(label == "Green")
dt_colorlabels_set_label(img->id, 2);
else if(label == "Blue") // Is it really called like that in XMP files?
dt_colorlabels_set_label(img->id, 3);
else if(label == "Purple") // Is it really called like that in XMP files?
dt_colorlabels_set_label(img->id, 4);
}
// If Xmp.xmp.Label not managed from an external app use dt colors
else if(FIND_XMP_TAG("Xmp.darktable.colorlabels"))
{
// color labels
const int cnt = pos->count();
for(int i = 0; i < cnt; i++)
{
dt_colorlabels_set_label(img->id, pos->toLong(i));
}
}
if((dt_image_get_xmp_mode() != DT_WRITE_XMP_NEVER) ||
dt_conf_get_bool("ui_last/import_last_tags_imported"))
{
GList *tags = NULL;
// Preserve dt tags which are not saved in xmp file
if(!exif_read) dt_tag_set_tags(tags, imgs, TRUE, TRUE, FALSE);
if(FIND_XMP_TAG("Xmp.lr.hierarchicalSubject"))
_exif_import_tags(img, pos);
else if(FIND_XMP_TAG("Xmp.dc.subject"))
_exif_import_tags(img, pos);
}
// Read GPS location
if(FIND_XMP_TAG("Xmp.exif.GPSLatitude"))
{
img->geoloc.latitude = dt_util_gps_string_to_number(pos->toString().c_str());
}
if(FIND_XMP_TAG("Xmp.exif.GPSLongitude"))
{
img->geoloc.longitude = dt_util_gps_string_to_number(pos->toString().c_str());
}
if(FIND_XMP_TAG("Xmp.exif.GPSAltitude"))
{
Exiv2::XmpData::const_iterator ref =
xmpData.findKey(Exiv2::XmpKey("Xmp.exif.GPSAltitudeRef"));
if(ref != xmpData.end() && ref->size())
{
std::string sign_str = ref->toString();
const char *sign = sign_str.c_str();
double elevation = 0.0;
if(dt_util_gps_elevation_to_number(pos->toRational(0).first,
pos->toRational(0).second,
sign[0], &elevation))
img->geoloc.elevation = elevation;
}
}
// Read lens type from Xmp.exifEX.LensModel
if(FIND_XMP_TAG("Xmp.exifEX.LensModel"))
{
// Lens model
char *lens = strdup(pos->toString().c_str());
char *adr = lens;
if(strncmp(lens, "lang=", 5) == 0)
{
lens = strchr(lens, ' ');
if(lens != NULL) lens++;
}
// No need to do any Unicode<->locale conversion, the field is specified as ASCII
g_strlcpy(img->exif_lens, lens, sizeof(img->exif_lens));
free(adr);
}
// Read timestamp from Xmp.exif.DateTimeOriginal
if(FIND_XMP_TAG("Xmp.exif.DateTimeOriginal")
|| FIND_XMP_TAG("Xmp.photoshop.DateCreated"))
{
char *datetime = strdup(pos->toString().c_str());
dt_datetime_exif_to_img(img, datetime);
free(datetime);
}
if(imgs) g_list_free(imgs);
imgs = NULL;
return true;
}
catch(Exiv2::AnyError &e)
{
if(imgs) g_list_free(imgs);
imgs = NULL;
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO,
"[exiv2 _exif_decode_xmp_data] %s: %s",
img->filename,
errstring);
return false;
}
}
static bool _exif_read_iptc_tag(Exiv2::IptcData &iptcData,
Exiv2::IptcData::const_iterator *pos,
string key)
{
try
{
return (*pos = iptcData.findKey(Exiv2::IptcKey(key)))
!= iptcData.end() && (*pos)->size();
}
catch(Exiv2::AnyError &e)
{
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO, "[exiv2 read_iptc_tag] %s", errstring);
return false;
}
}
#define FIND_IPTC_TAG(key) _exif_read_iptc_tag(iptcData, &pos, key)
// FIXME: according to
// http://www.exiv2.org/doc/classExiv2_1_1Metadatum.html#63c2b87249ba96679c29e01218169124
// there is no need to pass iptcData
static bool _exif_decode_iptc_data(dt_image_t *img,
Exiv2::IptcData &iptcData)
{
try
{
Exiv2::IptcData::const_iterator pos;
iptcData.sortByKey(); // this helps to quickly find all Iptc.Application2.Keywords
if((pos = iptcData.findKey(Exiv2::IptcKey("Iptc.Application2.Keywords")))
!= iptcData.end())
{
while(pos != iptcData.end())
{
std::string key = pos->key();
if(g_strcmp0(key.c_str(), "Iptc.Application2.Keywords")) break;
std::string str = pos->print();
char *tag = dt_util_foo_to_utf8(str.c_str());
guint tagid = 0;
dt_tag_new(tag, &tagid);
dt_tag_attach(tagid, img->id, FALSE, FALSE);
g_free(tag);
++pos;
}
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_TAG_CHANGED);
}
if(FIND_IPTC_TAG("Iptc.Application2.Caption"))
{
std::string str = pos->print(/*&iptcData*/);
dt_metadata_set_import_lock(img->id, "Xmp.dc.description", str.c_str());
}
if(FIND_IPTC_TAG("Iptc.Application2.Copyright"))
{
std::string str = pos->print(/*&iptcData*/);
dt_metadata_set_import_lock(img->id, "Xmp.dc.rights", str.c_str());
}
if(FIND_IPTC_TAG("Iptc.Application2.Byline"))
{
std::string str = pos->print(/*&iptcData*/);
dt_metadata_set_import_lock(img->id, "Xmp.dc.creator", str.c_str());
}
else if(FIND_IPTC_TAG("Iptc.Application2.Writer"))
{
std::string str = pos->print(/*&iptcData*/);
dt_metadata_set_import_lock(img->id, "Xmp.dc.creator", str.c_str());
}
else if(FIND_IPTC_TAG("Iptc.Application2.Contact"))
{
std::string str = pos->print(/*&iptcData*/);
dt_metadata_set_import_lock(img->id, "Xmp.dc.creator", str.c_str());
}
if(FIND_IPTC_TAG("Iptc.Application2.DateCreated"))
{
// Exiv2 already converts IPTC date and time into ISO 8601 format
GString *datetime = g_string_new(pos->toString().c_str());
datetime = g_string_append(datetime, "T");
if(FIND_IPTC_TAG("Iptc.Application2.TimeCreated"))
{
gchar *time = g_strdup(pos->toString().c_str());
datetime = g_string_append(datetime, time);
g_free(time);
}
else
datetime = g_string_append(datetime, "00:00:00");
dt_datetime_exif_to_img(img, datetime->str);
g_string_free(datetime, TRUE);
}
return true;
}
catch(Exiv2::AnyError &e)
{
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO,
"[exiv2 _exif_decode_iptc_data] %s: %s",
img->filename,
errstring);
return false;
}
}
static bool _exif_read_exif_tag(Exiv2::ExifData &exifData,
Exiv2::ExifData::const_iterator *pos,
string key)
{
try
{
return (*pos = exifData.findKey(Exiv2::ExifKey(key)))
!= exifData.end() && (*pos)->size();
}
catch(Exiv2::AnyError &e)
{
const char *errstring = e.what();
dt_print(DT_DEBUG_IMAGEIO, "[exiv2 read_exif_tag] %s", errstring);
return false;
}
}
#define FIND_EXIF_TAG(key) _exif_read_exif_tag(exifData, &pos, key)
// Support DefaultUserCrop, what is the safe Exif tag?
// DefaultUserCrop is known by name only from Exiv2 0.27.4.
// Magic-nr taken from DNG spec, the spec also says it has 4 floats (top,left,bottom,right).
// We only take them if a) we find a value != the default *and* b) data are plausible.
static bool _check_usercrop(Exiv2::ExifData &exifData,
dt_image_t *img)
{
Exiv2::ExifData::const_iterator pos =
exifData.findKey(Exiv2::ExifKey("Exif.SubImage1.0xc7b5"));
// DNGs without an embedded preview have the raw image tags under
// Exif.Image instead of Exif.SubImage1.
if(pos == exifData.end())
pos = exifData.findKey(Exiv2::ExifKey("Exif.Image.0xc7b5"));
if(pos != exifData.end() && pos->count() == 4 && pos->size())
{
dt_boundingbox_t crop;
for(int i = 0; i < 4; i++) crop[i] = pos->toFloat(i);
if(((crop[0] > 0)
||(crop[1] > 0)
||(crop[2] < 1)
||(crop[3] < 1))
&& (crop[2] - crop[0] > 0.05f)
&& (crop[3] - crop[1] > 0.05f))
{
for(int i=0; i<4; i++) img->usercrop[i] = crop[i];
return TRUE;
}
}
return FALSE;
}
static void _check_linear_response_limit(Exiv2::ExifData &exifData,
dt_image_t *img)
{
bool found_one = false;
// currently this only reads the dng tag, could also be used for other raws
Exiv2::ExifData::const_iterator linr =
exifData.findKey(Exiv2::ExifKey("Exif.Image.LinearResponseLimit"));
if(linr != exifData.end() && linr->count() == 1)
{
img->linear_response_limit = linr->toFloat();
found_one = true;
}
if(found_one)
dt_print(DT_DEBUG_IMAGEIO, "[exif] `%s` has LinearResponseLimit %.4f",
img->filename, img->linear_response_limit);
}
static gboolean _check_dng_opcodes(Exiv2::ExifData &exifData,
dt_image_t *img)
{
gboolean has_opcodes = FALSE;
Exiv2::ExifData::const_iterator pos =
exifData.findKey(Exiv2::ExifKey("Exif.SubImage1.OpcodeList2"));
// DNGs without an embedded preview have the opcodes under
// Exif.Image instead of Exif.SubImage1.
if(pos == exifData.end())
pos = exifData.findKey(Exiv2::ExifKey("Exif.Image.OpcodeList2"));
if(pos != exifData.end())
{
uint8_t *data = (uint8_t *)g_try_malloc(pos->size());
if(data)
{
pos->copy(data, Exiv2::invalidByteOrder);
dt_dng_opcode_process_opcode_list_2(data, pos->size(), img);
g_free(data);
has_opcodes = TRUE;
}
}
Exiv2::ExifData::const_iterator posb =
exifData.findKey(Exiv2::ExifKey("Exif.SubImage1.OpcodeList3"));
// DNGs without an embedded preview have the opcodes under
// Exif.Image instead of Exif.SubImage1.
if(posb == exifData.end())
posb = exifData.findKey(Exiv2::ExifKey("Exif.Image.OpcodeList3"));
if(posb != exifData.end())
{
uint8_t *data = (uint8_t *)g_try_malloc(posb->size());
if(data)
{
posb->copy(data, Exiv2::invalidByteOrder);
dt_dng_opcode_process_opcode_list_3(data, posb->size(), img);
g_free(data);
has_opcodes = TRUE;
}
}
return has_opcodes;
}
static gboolean _check_lens_correction_data(Exiv2::ExifData &exifData,
dt_image_t *img)
{
Exiv2::ExifData::const_iterator pos, posd, posc, posv;
/*
* Sony lens correction data
*/
if(Exiv2::versionNumber() >= EXIV2_MAKE_VERSION(0, 27, 4)
&& _exif_read_exif_tag(exifData, &posd, "Exif.SubImage1.DistortionCorrParams")
&& _exif_read_exif_tag(exifData, &posc, "Exif.SubImage1.ChromaticAberrationCorrParams")
&& _exif_read_exif_tag(exifData, &posv, "Exif.SubImage1.VignettingCorrParams"))
{
// Validate
const int nc = posd->toLong(0);
if(nc <= 16 && 2*nc == posc->toLong(0) && nc == posv->toLong(0))
{
img->exif_correction_type = CORRECTION_TYPE_SONY;
img->exif_correction_data.sony.nc = nc;
for(int i = 0; i < nc; i++)
{
img->exif_correction_data.sony.distortion[i] = posd->toLong(i + 1);
img->exif_correction_data.sony.ca_r[i] = posc->toLong(i + 1);
img->exif_correction_data.sony.ca_b[i] = posc->toLong(nc + i + 1);
img->exif_correction_data.sony.vignetting[i] = posv->toLong(i + 1);
}
}
}
/*
* Fuji lens correction data
*/
if(Exiv2::versionNumber() >= EXIV2_MAKE_VERSION(0, 27, 4)
&& _exif_read_exif_tag(exifData, &posd, "Exif.Fujifilm.GeometricDistortionParams")
&& _exif_read_exif_tag(exifData, &posc, "Exif.Fujifilm.ChromaticAberrationParams")
&& _exif_read_exif_tag(exifData, &posv, "Exif.Fujifilm.VignettingParams"))
{
// X-Trans IV/V
if(posd->count() == 19 && posc->count() == 29 && posv->count() == 19)
{
const int nc = 9;
img->exif_correction_type = CORRECTION_TYPE_FUJI;
img->exif_correction_data.fuji.nc = nc;
for(int i = 0; i < nc; i++)