Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_update_meta.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2021 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_update_meta.h"
10
11 #include "dng_assertions.h"
12 #include "dng_bottlenecks.h"
13 #include "dng_exif.h"
14 #include "dng_fingerprint.h"
15 #include "dng_host.h"
16 #include "dng_negative.h"
17 #include "dng_parse_utils.h"
18 #include "dng_stream.h"
19 #include "dng_tag_codes.h"
20 #include "dng_tag_types.h"
21 #include "dng_tag_values.h"
22 #include "dng_xmp.h"
23
24 #include <algorithm>
25
26 /*****************************************************************************/
27
28 #define qLogDNGUpdateMetadata (qDNGDebug && 0)
29
30 #if qLogDNGUpdateMetadata
31 #include "cr_logging.h"
32 #endif
33
34 /*****************************************************************************/
35
36 class dng_file_updater;
37
38 /*****************************************************************************/
39
40 class dng_tag_updater
41 {
42
43 public:
44
45 uint32 fTagCode = 0;
46 uint32 fTagType = 0;
47 uint64 fTagCount = 0;
48
49 union
50 {
51 uint64 fTagValue64;
52 uint32 fTagValue32 [2];
53 uint16 fTagValue16 [4];
54 uint8 fTagValue8 [8];
55 };
56
57 uint64 fTagOffset = 0;
58
59 public:
60
61 dng_tag_updater ()
62 {
63 fTagValue64 = 0;
64 }
65
66 bool operator== (const dng_tag_updater &rhs) const
67 {
68 return fTagCode == rhs.fTagCode &&
69 fTagType == rhs.fTagType &&
70 fTagCount == rhs.fTagCount &&
71 fTagValue64 == rhs.fTagValue64;
72 }
73
74 bool operator!= (const dng_tag_updater &rhs) const
75 {
76 return !(*this == rhs);
77 }
78
79 bool operator< (const dng_tag_updater &rhs) const
80 {
81 return fTagCode < rhs.fTagCode;
82 }
83
84 uint64 TagSize () const
85 {
86 return TagTypeSize (fTagType) * fTagCount;
87 }
88
89 bool PrepareToSet (dng_file_updater &updater,
90 uint32 tagType,
91 uint64 tagCount,
92 bool allowPatch);
93
94 uint64 Get_uint64 (dng_file_updater &updater) const;
95
96 bool GetArray_uint64 (dng_file_updater &updater,
97 std::vector<uint64> &values) const;
98
99 bool GetArray_Fingerprint (dng_file_updater &updater,
100 std::vector<dng_fingerprint> &values) const;
101
102 };
103
104 /*****************************************************************************/
105
106 class dng_ifd_updater
107 {
108
109 private:
110
111 uint64 fThisOffset = 0;
112 uint64 fNextOffset = 0;
113
114 std::vector<dng_tag_updater> fTag;
115
116 public:
117
118 bool operator== (const dng_ifd_updater &rhs) const
119 {
120 return fThisOffset == rhs.fThisOffset &&
121 fNextOffset == rhs.fNextOffset &&
122 fTag == rhs.fTag;
123 }
124
125 bool operator!= (const dng_ifd_updater &rhs) const
126 {
127 return !(*this == rhs);
128 }
129
130 uint32 EntryCount () const
131 {
132 return (uint32) fTag.size ();
133 }
134
135 uint64 Offset () const
136 {
137 return fThisOffset;
138 }
139
140 void SetOffset (uint64 offset)
141 {
142 fThisOffset = offset;
143 }
144
145 uint32 ByteCount (dng_file_updater &updater) const;
146
147 bool HasTag (uint32 tagCode) const;
148
149 const dng_tag_updater & ExistingTag (uint32 tagCode) const;
150
151 dng_tag_updater & RequiredTag (uint32 tagCode);
152
153 void DeleteTag (dng_file_updater &updater,
154 uint32 tagCode);
155
156 void UpdateTag (dng_file_updater &updater,
157 uint32 tagCode,
158 uint32 tagType,
159 uint64 tagCount,
160 const void *tagData,
161 bool remove = false,
162 bool allowPatch = true);
163
164 void UpdateByteTag (dng_file_updater &updater,
165 uint32 tagCode,
166 uint8 value,
167 bool remove = false)
168 {
169
170 UpdateTag (updater,
171 tagCode,
172 ttByte,
173 1,
174 &value,
175 remove);
176
177 }
178
179 void UpdateShortTag (dng_file_updater &updater,
180 uint32 tagCode,
181 uint16 value,
182 bool remove = false)
183 {
184
185 UpdateTag (updater,
186 tagCode,
187 ttShort,
188 1,
189 &value,
190 remove);
191
192 }
193
194 void UpdateRationalTag (dng_file_updater &updater,
195 uint32 tagCode,
196 const dng_urational &value)
197 {
198
199 UpdateTag (updater,
200 tagCode,
201 ttRational,
202 1,
203 &value,
204 !value.IsValid ());
205
206 }
207
208 void UpdateStringTag (dng_file_updater &updater,
209 uint32 tagCode,
210 const dng_string &s,
211 bool removeIfEmpty = true)
212 {
213
214 UpdateTag (updater,
215 tagCode,
216 ttAscii,
217 s.Length () + 1,
218 s.Get (),
219 removeIfEmpty && s.IsEmpty ());
220
221 }
222
223 void UpdateDualStringTag (dng_file_updater &updater,
224 uint32 tagCode,
225 const dng_string &s1,
226 const dng_string &s2);
227
228 void UpdateEncodedTextTag (dng_file_updater &updater,
229 uint32 tagCode,
230 const dng_string &s);
231
232 void UpdateVersionTag (dng_file_updater &updater,
233 uint32 tagCode,
234 uint32 tagType,
235 uint32 version,
236 bool removeIfZero = true);
237
238 void UpdateDateTimeTag (dng_file_updater &updater,
239 uint32 tagCode,
240 const dng_date_time_info &info);
241
242 void UpdateOffsetTimeTag (dng_file_updater &updater,
243 uint32 tagCode,
244 const dng_date_time_info &info);
245
246 void Parse (dng_file_updater &updater);
247
248 void SortTags ();
249
250 void Write (dng_file_updater &updater) const;
251
252 void ParseBigTableIndex (dng_file_updater &updater,
253 dng_big_table_index &bigTableIndex) const;
254
255 void WriteBigTableIndex (dng_file_updater &updater,
256 dng_big_table_index &bigTableIndex);
257
258 void ParseBigTableGroupIndex (dng_file_updater &updater,
259 dng_big_table_group_index &index) const;
260
261 void WriteBigTableGroupIndex (dng_file_updater &updater,
262 const dng_big_table_group_index &index);
263
264 bool MoveTagToIFD (dng_ifd_updater &dstIFD,
265 uint32 tagCode);
266
267 void UpdateAdobeData (dng_file_updater &updater,
268 const dng_fingerprint &iptcDigest);
269
270 };
271
272 /*****************************************************************************/
273
274 class dng_file_updater
275 {
276
277 private:
278
279 dng_host & fHost;
280
281 dng_stream & fStream;
282
283 bool fTruncateOnFailure;
284
285 uint64 fTruncateToLength;
286
287 bool fBigTIFF = false;
288
289 bool fIsDNG = false;
290
291 dng_ifd_updater fIFD0_new;
292 dng_ifd_updater fIFD0_old;
293
294 dng_ifd_updater fEXIF_new;
295 dng_ifd_updater fEXIF_old;
296
297 dng_ifd_updater fGPS_new;
298 dng_ifd_updater fGPS_old;
299
300 class zero_range
301 {
302
303 public:
304
305 uint64 fOffset = 0;
306 uint64 fCount = 0;
307
308 public:
309
310 bool operator< (const zero_range &rsh) const
311 {
312 return fOffset < rsh.fOffset;
313 }
314
315 };
316
317 std::vector<zero_range> fZeroRanges;
318
319 public:
320
321 dng_file_updater (dng_host &host,
322 dng_stream &stream);
323
324 ~dng_file_updater ();
325
326 dng_host & Host ()
327 {
328 return fHost;
329 }
330
331 dng_stream & Stream ()
332 {
333 return fStream;
334 }
335
336 bool BigTIFF () const
337 {
338 return fBigTIFF;
339 }
340
341 bool IsDNG () const
342 {
343 return fIsDNG;
344 }
345
346 uint32 InlineLimit () const
347 {
348 return fBigTIFF ? 8 : 4;
349 }
350
351 dng_ifd_updater & IFD0 ()
352 {
353 return fIFD0_new;
354 }
355
356 dng_ifd_updater & EXIF ()
357 {
358 return fEXIF_new;
359 }
360
361 dng_ifd_updater & GPS ()
362 {
363 return fGPS_new;
364 }
365
366 void ParseHeader ();
367
368 void PrepareToAppend (uint64 count);
369
370 void MoveIFD0toEXIF (uint32 tagCode);
371
372 void MoveEXIFtoIFD0 (uint32 tagCode);
373
374 void UpdateEXIF (dng_exif &exif);
375
376 bool UpdateIFD (dng_ifd_updater &ifd_new,
377 const dng_ifd_updater &ifd_old);
378
379 void UpdateHeader ();
380
381 void AddZeroRange (uint64 offset,
382 uint64 count);
383
384 void ApplyZeroRanges ();
385
386 };
387
388 /*****************************************************************************/
389
390 bool dng_tag_updater::PrepareToSet (dng_file_updater &updater,
391 uint32 tagType,
392 uint64 tagCount,
393 bool allowPatch)
394 {
395
396 bool isBigTIFF = updater.BigTIFF ();
397
398 uint32 inlineLimit = updater.InlineLimit ();
399
400 uint64 oldSize = TagSize ();
401 uint64 newSize = TagTypeSize (tagType) * tagCount;
402
403 bool wasInline = (oldSize <= inlineLimit);
404 bool canInline = (newSize <= inlineLimit);
405
406 if (canInline)
407 {
408
409 if (!wasInline)
410 {
411
412 DNG_REQUIRE (fTagOffset, "Expected fTagOffset");
413
414 updater.AddZeroRange (fTagOffset, oldSize);
415
416 fTagOffset = 0;
417
418 }
419
420 fTagValue64 = 0;
421
422 }
423
424 else
425 {
426
427 if (allowPatch && fTagType == tagType && fTagCount == tagCount)
428 {
429
430 DNG_REQUIRE (fTagOffset, "Expected fTagOffset");
431
432 updater.Stream ().SetWritePosition (fTagOffset);
433
434 }
435
436 else
437 {
438
439 if (!wasInline)
440 {
441
442 DNG_REQUIRE (fTagOffset, "Expected fTagOffset");
443
444 updater.AddZeroRange (fTagOffset,
445 oldSize);
446
447 }
448
449 updater.PrepareToAppend (newSize);
450
451 fTagOffset = updater.Stream ().Position ();
452
453 if (isBigTIFF)
454 {
455 fTagValue64 = fTagOffset;
456 }
457 else
458 {
459 fTagValue32 [0] = (uint32) fTagOffset;
460 fTagValue32 [1] = 0;
461 }
462
463 }
464
465 }
466
467 fTagType = tagType;
468 fTagCount = tagCount;
469
470 return canInline;
471
472 }
473
474 /*****************************************************************************/
475
476 uint64 dng_tag_updater::Get_uint64 (dng_file_updater &updater) const
477 {
478
479 std::vector<uint64> values;
480
481 if (GetArray_uint64 (updater, values))
482 {
483 return values [0];
484 }
485
486 return 0;
487
488 }
489
490 /*****************************************************************************/
491
492 bool dng_tag_updater::GetArray_uint64 (dng_file_updater &updater,
493 std::vector<uint64> &values) const
494 {
495
496 values.clear ();
497
498 if (fTagCount >= 1)
499 {
500
501 values.reserve (fTagCount);
502
503 if (TagSize () <= updater.InlineLimit ())
504 {
505
506 dng_stream tempStream (fTagValue8, updater.InlineLimit ());
507
508 tempStream.SetSwapBytes (false); // Inline values pre-swapped
509
510 for (uint32 j = 0; j < fTagCount; j++)
511 {
512
513 values.push_back (tempStream.TagValue_uint64 (fTagType));
514
515 }
516
517 }
518
519 else
520 {
521
522 DNG_REQUIRE (fTagOffset, "Expected fTagOffset");
523
524 updater.Stream ().SetReadPosition (fTagOffset);
525
526 for (uint32 j = 0; j < fTagCount; j++)
527 {
528
529 values.push_back (updater.Stream ().TagValue_uint64 (fTagType));
530
531 }
532
533 }
534
535 return true;
536
537 }
538
539 return false;
540
541 }
542
543 /*****************************************************************************/
544
545 bool dng_tag_updater::GetArray_Fingerprint (dng_file_updater &updater,
546 std::vector<dng_fingerprint> &values) const
547 {
548
549 values.clear ();
550
551 if (fTagType == ttByte && fTagCount >= 16)
552 {
553
554 uint64 count = fTagCount >> 4;
555
556 values.reserve (count);
557
558 updater.Stream ().SetReadPosition (fTagOffset);
559
560 for (uint32 j = 0; j < count; j++)
561 {
562
563 dng_fingerprint fingerprint;
564
565 updater.Stream ().Get (fingerprint.data, 16);
566
567 values.push_back (fingerprint);
568
569 }
570
571 return true;
572
573 }
574
575 return false;
576
577 }
578
579 /*****************************************************************************/
580
581 void dng_ifd_updater::Parse (dng_file_updater &updater)
582 {
583
584 bool isBigTIFF = updater.BigTIFF ();
585
586 uint32 inlineLimit = isBigTIFF ? 8 : 4;
587
588 dng_stream &stream (updater.Stream ());
589
590 fThisOffset = stream.Position ();
591
592 uint64 entryCount = isBigTIFF ? stream.Get_uint64 ()
593 : stream.Get_uint16 ();
594
595 if (entryCount > 0xFFFF)
596 {
597 ThrowBadFormat ();
598 }
599
600 fTag.reserve (entryCount);
601
602 for (uint64 j = 0; j < entryCount; j++)
603 {
604
605 dng_tag_updater tag;
606
607 tag.fTagCode = stream.Get_uint16 ();
608 tag.fTagType = stream.Get_uint16 ();
609
610 tag.fTagCount = isBigTIFF ? stream.Get_uint64 ()
611 : stream.Get_uint32 ();
612
613 uint32 tagTypeSize = TagTypeSize (tag.fTagType);
614
615 uint64 tagSize = tag.fTagCount * tagTypeSize;
616
617 if (tagSize > inlineLimit)
618 {
619
620 if (isBigTIFF)
621 {
622 tag.fTagValue64 = stream.Get_uint64 ();
623 tag.fTagOffset = tag.fTagValue64;
624 }
625 else
626 {
627 tag.fTagValue32 [0] = stream.Get_uint32 ();
628 tag.fTagOffset = tag.fTagValue32 [0];
629 }
630
631 }
632
633 else
634 {
635
636 tag.fTagOffset = stream.Position ();
637
638 if (tagTypeSize == 8)
639 {
640 tag.fTagValue64 = stream.Get_uint64 ();
641 }
642
643 else if (tagTypeSize == 4)
644 {
645
646 for (uint32 k = 0; k < inlineLimit >> 2; k++)
647 {
648 tag.fTagValue32 [k] = stream.Get_uint32 ();
649 }
650
651 }
652
653 else if (tagTypeSize == 2)
654 {
655
656 for (uint32 k = 0; k < inlineLimit >> 1; k++)
657 {
658 tag.fTagValue16 [k] = stream.Get_uint16 ();
659 }
660
661 }
662
663 else
664 {
665
666 for (uint32 k = 0; k < inlineLimit; k++)
667 {
668 tag.fTagValue8 [k] = stream.Get_uint8 ();
669 }
670
671 }
672
673 }
674
675 fTag.push_back (tag);
676
677 }
678
679 fNextOffset = isBigTIFF ? stream.Get_uint64 ()
680 : stream.Get_uint32 ();
681
682 }
683
684 /*****************************************************************************/
685
686 uint32 dng_ifd_updater::ByteCount (dng_file_updater &updater) const
687 {
688
689 if (EntryCount () == 0)
690 {
691 return 0;
692 }
693
694 return updater.BigTIFF () ? EntryCount () * 20 + 16
695 : EntryCount () * 12 + 6;
696
697 }
698
699 /*****************************************************************************/
700
701 bool dng_ifd_updater::HasTag (uint32 tagCode) const
702 {
703
704 for (size_t j = 0; j < fTag.size (); j++)
705 {
706
707 if (fTag [j] . fTagCode == tagCode)
708 {
709 return true;
710 }
711
712 }
713
714 return false;
715
716 }
717
718 /*****************************************************************************/
719
720 const dng_tag_updater & dng_ifd_updater::ExistingTag (uint32 tagCode) const
721 {
722
723 for (size_t j = 0; j < fTag.size (); j++)
724 {
725
726 if (fTag [j] . fTagCode == tagCode)
727 {
728 return fTag [j];
729 }
730
731 }
732
733 DNG_REPORT ("Unexpected missing tag");
734
735 static dng_tag_updater dummy;
736
737 return dummy;
738
739 }
740
741 /*****************************************************************************/
742
743 dng_tag_updater & dng_ifd_updater::RequiredTag (uint32 tagCode)
744 {
745
746 for (size_t j = 0; j < fTag.size (); j++)
747 {
748
749 if (fTag [j] . fTagCode == tagCode)
750 {
751 return fTag [j];
752 }
753
754 }
755
756 dng_tag_updater tag;
757
758 tag.fTagCode = tagCode;
759
760 fTag.push_back (tag);
761
762 return fTag [fTag.size () - 1];
763
764 }
765
766 /*****************************************************************************/
767
768 void dng_ifd_updater::DeleteTag (dng_file_updater &updater,
769 uint32 tagCode)
770 {
771
772 #if qLogDNGUpdateMetadata
773
774 const char *tagName = LookupTagCode (tagCode <= 31 ? tcGPSInfo : 0, tagCode);
775
776 #endif
777
778 for (size_t j = 0; j < fTag.size (); j++)
779 {
780
781 dng_tag_updater &tag = fTag [j];
782
783 if (tag.fTagCode == tagCode)
784 {
785
786 #if qLogDNGUpdateMetadata
787
788 LogPrint ("DNGUpdateMetadata: Deleting tag %s, size = %llu, offset = %llu\n",
789 tagName,
790 (unsigned long long)tag.TagSize (),
791 (unsigned long long)tag.fTagOffset);
792
793 #endif
794
795 if (tag.TagSize () > updater.InlineLimit ())
796 {
797
798 DNG_REQUIRE (tag.fTagOffset, "Expected fTagOffset");
799
800 updater.AddZeroRange (tag.fTagOffset, tag.TagSize ());
801
802 }
803
804 fTag.erase (fTag.begin () + j);
805
806 return;
807
808 }
809
810 }
811
812 }
813
814 /*****************************************************************************/
815
816 void dng_ifd_updater::UpdateTag (dng_file_updater &updater,
817 uint32 tagCode,
818 uint32 tagType,
819 uint64 tagCount,
820 const void *tagData,
821 bool remove,
822 bool allowPatch)
823 {
824
825 #if qLogDNGUpdateMetadata
826
827 const char *tagName = LookupTagCode (tagCode <= 31 ? tcGPSInfo : 0, tagCode);
828
829 #endif
830
831 if (remove)
832 {
833
834 if (HasTag (tagCode))
835 {
836
837 DeleteTag (updater, tagCode);
838
839 }
840
841 return;
842
843 }
844
845 uint32 byteSwapWidth = TagTypeSize (tagType);
846
847 if (tagType == ttRational ||
848 tagType == ttSRational ||
849 tagType == ttComplex)
850 {
851
852 // These types are pairs of values.
853
854 byteSwapWidth = byteSwapWidth >> 1;
855
856 }
857
858 if (tagCode == tcIPTC_NAA && tagType == ttLong)
859 {
860
861 // Kludge: The IPTC uses type ttLong to store byte data,
862 // but does not byte do correct byte swapping and actually
863 // treats the data as type ttByte.
864
865 byteSwapWidth = 1;
866
867 }
868
869 if (HasTag (tagCode))
870 {
871
872 dng_tag_updater &oldTag (RequiredTag (tagCode));
873
874 if (oldTag.fTagType == tagType &&
875 oldTag.fTagCount == tagCount)
876 {
877
878 bool dirty = false;
879
880 uint64 tagSize = oldTag.TagSize ();
881
882 if (tagSize <= updater.InlineLimit ())
883 {
884
885 switch (byteSwapWidth)
886 {
887
888 case 1:
889 {
890
891 const uint8 *newData8 = (const uint8 *) tagData;
892
893 for (uint64 j = 0; j < tagSize; j++)
894 {
895
896 if (oldTag.fTagValue8 [j] != newData8 [j])
897 {
898 oldTag.fTagValue8 [j] = newData8 [j];
899 dirty = true;
900 }
901
902 }
903
904 break;
905
906 }
907
908 case 2:
909 {
910
911 const uint16 *newData16 = (const uint16 *) tagData;
912
913 for (uint64 j = 0; j < (tagSize >> 1); j++)
914 {
915
916 if (oldTag.fTagValue16 [j] != newData16 [j])
917 {
918 oldTag.fTagValue16 [j] = newData16 [j];
919 dirty = true;
920 }
921
922 }
923
924 break;
925
926 }
927
928 case 4:
929 {
930
931 const uint32 *newData32 = (const uint32 *) tagData;
932
933 for (uint64 j = 0; j < (tagSize >> 2); j++)
934 {
935
936 if (oldTag.fTagValue32 [j] != newData32 [j])
937 {
938 oldTag.fTagValue32 [j] = newData32 [j];
939 dirty = true;
940 }
941
942 }
943
944 break;
945
946 }
947
948 case 8:
949 {
950
951 const uint64 *newData64 = (const uint64 *) tagData;
952
953 if (oldTag.fTagValue64 != newData64 [0])
954 {
955 oldTag.fTagValue64 = newData64 [0];
956 dirty = true;
957 }
958
959 break;
960
961 }
962
963 default:
964 ThrowProgramError ();
965
966 }
967
968 #if qLogDNGUpdateMetadata
969
970 if (dirty)
971 {
972
973 LogPrint ("DNGUpdateMetadata: Inline tag %s updated\n",
974 tagName);
975
976 }
977
978 #endif
979
980 return;
981
982 }
983
984 else
985 {
986
987 DNG_REQUIRE (oldTag.fTagOffset, "Expected fTagOffset");
988
989 updater.Stream ().SetReadPosition (oldTag.fTagOffset);
990
991 switch (byteSwapWidth)
992 {
993
994 case 1:
995 {
996
997 const uint8 *newData8 = (const uint8 *) tagData;
998
999 for (uint64 j = 0; j < tagSize; j++)
1000 {
1001
1002 if (updater.Stream ().Get_uint8 () != newData8 [j])
1003 {
1004 dirty = true;
1005 break;
1006 }
1007
1008 }
1009
1010 break;
1011
1012 }
1013
1014 case 2:
1015 {
1016
1017 const uint16 *newData16 = (const uint16 *) tagData;
1018
1019 for (uint64 j = 0; j < (tagSize >> 1); j++)
1020 {
1021
1022 if (updater.Stream ().Get_uint16 () != newData16 [j])
1023 {
1024 dirty = true;
1025 break;
1026 }
1027
1028 }
1029
1030 break;
1031
1032 }
1033
1034 case 4:
1035 {
1036
1037 const uint32 *newData32 = (const uint32 *) tagData;
1038
1039 for (uint64 j = 0; j < (tagSize >> 2); j++)
1040 {
1041
1042 if (updater.Stream ().Get_uint32 () != newData32 [j])
1043 {
1044 dirty = true;
1045 break;
1046 }
1047
1048 }
1049
1050 break;
1051
1052 }
1053
1054 case 8:
1055 {
1056
1057 const uint64 *newData64 = (const uint64 *) tagData;
1058
1059 for (uint64 j = 0; j < (tagSize >> 3); j++)
1060 {
1061
1062 if (updater.Stream ().Get_uint64 () != newData64 [j])
1063 {
1064 dirty = true;
1065 break;
1066 }
1067
1068 }
1069
1070 break;
1071
1072 }
1073
1074 default:
1075 ThrowProgramError ();
1076
1077 }
1078
1079 }
1080
1081 if (!dirty)
1082 {
1083
1084 return;
1085
1086 }
1087
1088 if (allowPatch)
1089 {
1090
1091 #if qLogDNGUpdateMetadata
1092
1093 LogPrint ("DNGUpdateMetadata: Patching existing tag %s\n",
1094 tagName);
1095
1096 #endif
1097
1098 updater.Stream ().SetWritePosition (oldTag.fTagOffset);
1099
1100 switch (byteSwapWidth)
1101 {
1102
1103 case 1:
1104 {
1105
1106 const uint8 *newData8 = (const uint8 *) tagData;
1107
1108 for (uint64 j = 0; j < tagSize; j++)
1109 {
1110 updater.Stream ().Put_uint8 (newData8 [j]);
1111 }
1112
1113 break;
1114
1115 }
1116
1117 case 2:
1118 {
1119
1120 const uint16 *newData16 = (const uint16 *) tagData;
1121
1122 for (uint64 j = 0; j < (tagSize >> 1); j++)
1123 {
1124 updater.Stream ().Put_uint16 (newData16 [j]);
1125 }
1126
1127 break;
1128
1129 }
1130
1131 case 4:
1132 {
1133
1134 const uint32 *newData32 = (const uint32 *) tagData;
1135
1136 for (uint64 j = 0; j < (tagSize >> 2); j++)
1137 {
1138 updater.Stream ().Put_uint32 (newData32 [j]);
1139 }
1140
1141 break;
1142
1143 }
1144
1145 case 8:
1146 {
1147
1148 const uint64 *newData64 = (const uint64 *) tagData;
1149
1150 for (uint64 j = 0; j < (tagSize >> 3); j++)
1151 {
1152 updater.Stream ().Put_uint64 (newData64 [j]);
1153 }
1154
1155 break;
1156
1157 }
1158
1159 default:
1160 ThrowProgramError ();
1161
1162 }
1163
1164 return;
1165
1166 }
1167
1168 }
1169
1170 DeleteTag (updater, tagCode);
1171
1172 }
1173
1174 // Add new tag.
1175
1176 dng_tag_updater &newTag (RequiredTag (tagCode));
1177
1178 if (newTag.PrepareToSet (updater,
1179 tagType,
1180 tagCount,
1181 false))
1182 {
1183
1184 uint64 tagSize = newTag.TagSize ();
1185
1186 switch (byteSwapWidth)
1187 {
1188
1189 case 1:
1190 {
1191
1192 const uint8 *newData8 = (const uint8 *) tagData;
1193
1194 for (uint64 j = 0; j < tagSize; j++)
1195 {
1196 newTag.fTagValue8 [j] = newData8 [j];
1197 }
1198
1199 break;
1200
1201 }
1202
1203 case 2:
1204 {
1205
1206 const uint16 *newData16 = (const uint16 *) tagData;
1207
1208 for (uint64 j = 0; j < (tagSize >> 1); j++)
1209 {
1210 newTag.fTagValue16 [j] = newData16 [j];
1211 }
1212
1213 break;
1214
1215 }
1216
1217 case 4:
1218 {
1219
1220 const uint32 *newData32 = (const uint32 *) tagData;
1221
1222 for (uint64 j = 0; j < (tagSize >> 2); j++)
1223 {
1224 newTag.fTagValue32 [j] = newData32 [j];
1225 }
1226
1227 break;
1228
1229 }
1230
1231 case 8:
1232 {
1233
1234 const uint64 *newData64 = (const uint64 *) tagData;
1235
1236 newTag.fTagValue64 = newData64 [0];
1237
1238 break;
1239
1240 }
1241
1242 default:
1243 ThrowProgramError ();
1244
1245 }
1246
1247 }
1248
1249 else
1250 {
1251
1252 uint64 tagSize = newTag.TagSize ();
1253
1254 switch (byteSwapWidth)
1255 {
1256
1257 case 1:
1258 {
1259
1260 if (tagSize > 0xFFFFFFFF)
1261 {
1262 ThrowProgramError ();
1263 }
1264
1265 updater.Stream ().Put (tagData, (uint32) tagSize);
1266
1267 break;
1268
1269 }
1270
1271 case 2:
1272 {
1273
1274 const uint16 *newData16 = (const uint16 *) tagData;
1275
1276 for (uint64 j = 0; j < (tagSize >> 1); j++)
1277 {
1278 updater.Stream ().Put_uint16 (newData16 [j]);
1279 }
1280
1281 break;
1282
1283 }
1284
1285 case 4:
1286 {
1287
1288 const uint32 *newData32 = (const uint32 *) tagData;
1289
1290 for (uint64 j = 0; j < (tagSize >> 2); j++)
1291 {
1292 updater.Stream ().Put_uint32 (newData32 [j]);
1293 }
1294
1295 break;
1296
1297 }
1298
1299 case 8:
1300 {
1301
1302 const uint64 *newData64 = (const uint64 *) tagData;
1303
1304 for (uint64 j = 0; j < (tagSize >> 3); j++)
1305 {
1306 updater.Stream ().Put_uint64 (newData64 [j]);
1307 }
1308
1309 break;
1310
1311 }
1312
1313 default:
1314 ThrowProgramError ();
1315
1316 }
1317
1318 }
1319
1320 #if qLogDNGUpdateMetadata
1321
1322 LogPrint ("DNGUpdateMetadata: Adding tag %s, size = %llu\n",
1323 tagName,
1324 (unsigned long long) newTag.TagSize ());
1325
1326 #endif
1327
1328 }
1329
1330 /*****************************************************************************/
1331
1332 void dng_ifd_updater::UpdateDualStringTag (dng_file_updater &updater,
1333 uint32 tagCode,
1334 const dng_string &s1,
1335 const dng_string &s2)
1336 {
1337
1338 if (s2.IsEmpty ())
1339 {
1340
1341 UpdateStringTag (updater,
1342 tagCode,
1343 s1);
1344
1345 }
1346
1347 else
1348 {
1349
1350 uint32 tagCount = s1.Length () +
1351 s2.Length () + 2;
1352
1353 dng_memory_data buffer (tagCount);
1354
1355 memcpy (buffer.Buffer_char (),
1356 s1.Get (),
1357 s1.Length () + 1);
1358
1359 memcpy (buffer.Buffer_char () + s1.Length () + 1,
1360 s2.Get (),
1361 s2.Length () + 1);
1362
1363 UpdateTag (updater,
1364 tagCode,
1365 ttAscii,
1366 tagCount,
1367 buffer.Buffer_char ());
1368
1369 }
1370
1371 }
1372
1373 /*****************************************************************************/
1374
1375 void dng_ifd_updater::UpdateEncodedTextTag (dng_file_updater &updater,
1376 uint32 tagCode,
1377 const dng_string &s)
1378 {
1379
1380 if (s.IsEmpty ())
1381 {
1382
1383 DeleteTag (updater, tagCode);
1384
1385 }
1386
1387 else if (s.IsASCII ())
1388 {
1389
1390 uint32 tagCount = 8 + s.Length ();
1391
1392 dng_memory_data buffer (tagCount);
1393
1394 memcpy (buffer.Buffer_char (),
1395 "ASCII\000\000\000",
1396 8);
1397
1398 memcpy (buffer.Buffer_char () + 8,
1399 s.Get (),
1400 s.Length ());
1401
1402 UpdateTag (updater,
1403 tagCode,
1404 ttUndefined,
1405 tagCount,
1406 buffer.Buffer ());
1407
1408 }
1409
1410 else
1411 {
1412
1413 dng_memory_data utf16;
1414
1415 uint32 utf16_length = s.Get_UTF16 (utf16);
1416
1417 uint32 tagCount = 8 + utf16_length * 2;
1418
1419 if (updater.Stream ().SwapBytes ())
1420 {
1421
1422 DoSwapBytes16 (utf16.Buffer_uint16 (),
1423 utf16_length);
1424
1425 }
1426
1427 dng_memory_data buffer (tagCount);
1428
1429 memcpy (buffer.Buffer_char (),
1430 "UNICODE\000",
1431 8);
1432
1433 memcpy (buffer.Buffer_char () + 8,
1434 utf16.Buffer_char (),
1435 utf16_length * 2);
1436
1437 UpdateTag (updater,
1438 tagCode,
1439 ttUndefined,
1440 tagCount,
1441 buffer.Buffer ());
1442
1443 }
1444
1445 }
1446
1447 /*****************************************************************************/
1448
1449 void dng_ifd_updater::UpdateVersionTag (dng_file_updater &updater,
1450 uint32 tagCode,
1451 uint32 tagType,
1452 uint32 version,
1453 bool removeIfZero)
1454 {
1455
1456 uint8 b [4];
1457
1458 b [0] = (uint8) (version >> 24);
1459 b [1] = (uint8) (version >> 16);
1460 b [2] = (uint8) (version >> 8);
1461 b [3] = (uint8) (version );
1462
1463 UpdateTag (updater,
1464 tagCode,
1465 tagType,
1466 4,
1467 b,
1468 removeIfZero && version == 0);
1469
1470 }
1471
1472 /*****************************************************************************/
1473
1474 void dng_ifd_updater::UpdateDateTimeTag (dng_file_updater &updater,
1475 uint32 tagCode,
1476 const dng_date_time_info &info)
1477 {
1478
1479 const dng_date_time &dt (info.DateTime ());
1480
1481 if (dt.IsValid ())
1482 {
1483
1484 char buffer [20];
1485
1486 snprintf (buffer,
1487 20,
1488 "%04d:%02d:%02d %02d:%02d:%02d",
1489 (int) dt.fYear,
1490 (int) dt.fMonth,
1491 (int) dt.fDay,
1492 (int) dt.fHour,
1493 (int) dt.fMinute,
1494 (int) dt.fSecond);
1495
1496 UpdateTag (updater,
1497 tagCode,
1498 ttAscii,
1499 20,
1500 buffer);
1501
1502 }
1503
1504 else
1505 {
1506
1507 DeleteTag (updater, tagCode);
1508
1509 }
1510
1511 }
1512
1513 /*****************************************************************************/
1514
1515 void dng_ifd_updater::UpdateOffsetTimeTag (dng_file_updater &updater,
1516 uint32 tagCode,
1517 const dng_date_time_info &info)
1518 {
1519
1520 if (info.DateTime ().IsValid () &&
1521 info.TimeZone ().IsValid ())
1522 {
1523
1524 UpdateStringTag (updater,
1525 tagCode,
1526 info.OffsetTime ());
1527
1528 }
1529
1530 else
1531 {
1532
1533 DeleteTag (updater, tagCode);
1534
1535 }
1536
1537 }
1538
1539 /*****************************************************************************/
1540
1541 void dng_ifd_updater::SortTags ()
1542 {
1543
1544 if (fTag.size () > 1)
1545 {
1546
1547 std::stable_sort (fTag.begin (), fTag.end ());
1548
1549 }
1550
1551 }
1552
1553 /*****************************************************************************/
1554
1555 void dng_ifd_updater::Write (dng_file_updater &updater) const
1556 {
1557
1558 bool isBigTIFF = updater.BigTIFF ();
1559
1560 uint32 inlineLimit = isBigTIFF ? 8 : 4;
1561
1562 dng_stream &stream (updater.Stream ());
1563
1564 stream.SetWritePosition (fThisOffset);
1565
1566 if (isBigTIFF)
1567 {
1568 stream.Put_uint64 (EntryCount ());
1569 }
1570 else
1571 {
1572 stream.Put_uint16 ((uint16) EntryCount ());
1573 }
1574
1575 for (uint32 j = 0; j < EntryCount (); j++)
1576 {
1577
1578 const dng_tag_updater &tag (fTag [j]);
1579
1580 stream.Put_uint16 (tag.fTagCode);
1581 stream.Put_uint16 (tag.fTagType);
1582
1583 if (isBigTIFF)
1584 {
1585 stream.Put_uint64 (tag.fTagCount);
1586 }
1587 else
1588 {
1589 stream.Put_uint32 ((uint32) tag.fTagCount);
1590 }
1591
1592 uint32 tagTypeSize = TagTypeSize (tag.fTagType);
1593
1594 uint64 tagSize = tag.fTagCount * tagTypeSize;
1595
1596 if (tagSize > inlineLimit)
1597 {
1598
1599 if (isBigTIFF)
1600 {
1601 stream.Put_uint64 (tag.fTagValue64);
1602 }
1603 else
1604 {
1605 stream.Put_uint32 (tag.fTagValue32 [0]);
1606 }
1607
1608 }
1609
1610 else
1611 {
1612
1613 if (tagTypeSize == 8)
1614 {
1615 stream.Put_uint64 (tag.fTagValue64);
1616 }
1617
1618 else if (tagTypeSize == 4)
1619 {
1620
1621 for (uint32 k = 0; k < inlineLimit >> 2; k++)
1622 {
1623 stream.Put_uint32 (tag.fTagValue32 [k]);
1624 }
1625
1626 }
1627
1628 else if (tagTypeSize == 2)
1629 {
1630
1631 for (uint32 k = 0; k < inlineLimit >> 1; k++)
1632 {
1633 stream.Put_uint16 (tag.fTagValue16 [k]);
1634 }
1635
1636 }
1637
1638 else
1639 {
1640
1641 for (uint32 k = 0; k < inlineLimit; k++)
1642 {
1643 stream.Put_uint8 (tag.fTagValue8 [k]);
1644 }
1645
1646 }
1647
1648 }
1649
1650 }
1651
1652 if (isBigTIFF)
1653 {
1654 stream.Put_uint64 (fNextOffset);
1655 }
1656 else
1657 {
1658 stream.Put_uint32 ((uint32) fNextOffset);
1659 }
1660
1661 }
1662
1663 /*****************************************************************************/
1664
1665 void dng_ifd_updater::ParseBigTableIndex (dng_file_updater &updater,
1666 dng_big_table_index &bigTableIndex) const
1667 {
1668
1669 if (HasTag (tcBigTableDigests ) &&
1670 HasTag (tcBigTableOffsets ) &&
1671 HasTag (tcBigTableByteCounts))
1672 {
1673
1674 const dng_tag_updater &digestsTag = ExistingTag (tcBigTableDigests);
1675 const dng_tag_updater &offsetsTag = ExistingTag (tcBigTableOffsets);
1676 const dng_tag_updater &countsTag = ExistingTag (tcBigTableByteCounts);
1677
1678 std::vector<dng_fingerprint> digests;
1679
1680 if (digestsTag.GetArray_Fingerprint (updater, digests))
1681 {
1682
1683 uint64 existingCount = digests.size ();
1684
1685 if (offsetsTag.fTagCount == existingCount &&
1686 countsTag .fTagCount == existingCount)
1687 {
1688
1689 std::vector<uint64> offsets;
1690 std::vector<uint64> counts;
1691
1692 if (offsetsTag.GetArray_uint64 (updater, offsets) &&
1693 countsTag .GetArray_uint64 (updater, counts ))
1694 {
1695
1696 for (uint32 j = 0; j < existingCount; j++)
1697 {
1698
1699 if (digests [j].IsValid () && offsets [j] && counts [j])
1700 {
1701
1702 if (offsets [j] + counts [j] <= updater.Stream ().Length ())
1703 {
1704
1705 bigTableIndex.AddEntry (digests [j],
1706 (uint32) counts [j],
1707 offsets [j]);
1708
1709 }
1710
1711 }
1712
1713 }
1714
1715 }
1716
1717 }
1718
1719 }
1720
1721 }
1722
1723 }
1724
1725 /*****************************************************************************/
1726
1727 void dng_ifd_updater::WriteBigTableIndex (dng_file_updater &updater,
1728 dng_big_table_index &bigTableIndex)
1729 {
1730
1731 uint32 updatedCount = (uint32) bigTableIndex.Map ().size ();
1732
1733 std::vector<dng_fingerprint> digests (updatedCount);
1734 std::vector<uint64> offsets64 (updatedCount);
1735 std::vector<uint32> offsets32 (updatedCount);
1736 std::vector<uint32> counts (updatedCount);
1737
1738 uint32 index = 0;
1739
1740 for (auto it = bigTableIndex.Map ().cbegin ();
1741 it != bigTableIndex.Map ().cend ();
1742 ++it)
1743 {
1744
1745 digests [index] = it->first;
1746 offsets64 [index] = it->second.fTableOffset;
1747 offsets32 [index] = (uint32) it->second.fTableOffset;
1748 counts [index] = it->second.fTableSize;
1749
1750 index++;
1751
1752 }
1753
1754 UpdateTag (updater,
1755 tcBigTableDigests,
1756 ttByte,
1757 16 * updatedCount,
1758 &digests [0],
1759 updatedCount == 0,
1760 false);
1761
1762 if (updater.BigTIFF ())
1763 {
1764
1765 UpdateTag (updater,
1766 tcBigTableOffsets,
1767 ttLong8,
1768 updatedCount,
1769 &offsets64 [0],
1770 updatedCount == 0,
1771 false);
1772
1773 }
1774
1775 else
1776 {
1777
1778 UpdateTag (updater,
1779 tcBigTableOffsets,
1780 ttLong,
1781 updatedCount,
1782 &offsets32 [0],
1783 updatedCount == 0,
1784 false);
1785
1786 }
1787
1788 UpdateTag (updater,
1789 tcBigTableByteCounts,
1790 ttLong,
1791 updatedCount,
1792 &counts [0],
1793 updatedCount == 0,
1794 false);
1795
1796 }
1797
1798 /*****************************************************************************/
1799
1800 void dng_ifd_updater::ParseBigTableGroupIndex (dng_file_updater &updater,
1801 dng_big_table_group_index &index) const
1802 {
1803
1804 if (HasTag (tcBigTableGroupIndex))
1805 {
1806
1807 const dng_tag_updater &tag = ExistingTag (tcBigTableGroupIndex);
1808
1809 std::vector<dng_fingerprint> digests;
1810
1811 if (tag.GetArray_Fingerprint (updater, digests))
1812 {
1813
1814 const uint32 existingCount = uint32 (digests.size ());
1815
1816 // This value should always be even, because this tag holds pairs
1817 // of digests.
1818
1819 if ((existingCount & uint64 (1)) != 0)
1820 return;
1821
1822 const uint32 numGroups = existingCount >> 1;
1823
1824 for (uint32 i = 0; i < numGroups; i++)
1825 {
1826
1827 index.AddEntry (digests [2 * i ],
1828 digests [2 * i + 1]);
1829
1830 }
1831
1832 }
1833
1834 }
1835
1836 }
1837
1838 /*****************************************************************************/
1839
1840 void dng_ifd_updater::WriteBigTableGroupIndex (dng_file_updater &updater,
1841 const dng_big_table_group_index &index)
1842 {
1843
1844 const uint32 updatedCount = (uint32) index.Map ().size ();
1845
1846 std::vector<dng_fingerprint> digests;
1847
1848 digests.reserve (updatedCount * 2);
1849
1850 for (const auto &group : index.Map ())
1851 {
1852 digests.push_back (group.first);
1853 digests.push_back (group.second);
1854 }
1855
1856 const uint64 tagCount = uint64 (digests.size () * 16);
1857
1858 const void *data = (!digests.empty ()) ? (&digests [0]) : nullptr;
1859
1860 const bool doRemove = (updatedCount == 0);
1861
1862 UpdateTag (updater,
1863 tcBigTableGroupIndex,
1864 ttByte,
1865 tagCount,
1866 data,
1867 doRemove,
1868 false);
1869
1870 }
1871
1872 /*****************************************************************************/
1873
1874 bool dng_ifd_updater::MoveTagToIFD (dng_ifd_updater &dstIFD,
1875 uint32 tagCode)
1876 {
1877
1878 for (size_t j = 0; j < fTag.size (); j++)
1879 {
1880
1881 if (fTag [j] . fTagCode == tagCode)
1882 {
1883
1884 if (!dstIFD.HasTag (tagCode))
1885 {
1886
1887 dng_tag_updater tag = fTag [j];
1888
1889 dstIFD.fTag.push_back (tag);
1890
1891 }
1892
1893 fTag.erase (fTag.begin () + j);
1894
1895 return true;
1896
1897 }
1898
1899 }
1900
1901 return false;
1902
1903 }
1904
1905 /*****************************************************************************/
1906
1907 void dng_ifd_updater::UpdateAdobeData (dng_file_updater &updater,
1908 const dng_fingerprint &iptcDigest)
1909 {
1910
1911 if (HasTag (tcAdobeData))
1912 {
1913
1914 const dng_tag_updater &adobeData (ExistingTag (tcAdobeData));
1915
1916 dng_stream &stream (updater.Stream ());
1917
1918 TempBigEndian tempBigEndian (stream);
1919
1920 stream.SetReadPosition (adobeData.fTagOffset);
1921
1922 uint64 nextOffset = stream.Position ();
1923 uint64 lastOffset = nextOffset + adobeData.TagSize ();
1924
1925 while (nextOffset + 12 <= lastOffset)
1926 {
1927
1928 stream.SetReadPosition (nextOffset);
1929
1930 // Tag signature (4-character code):
1931
1932 uint32 tagSignature = stream.Get_uint32 ();
1933
1934 // Tag ID (16-bit integer):
1935
1936 uint32 tagID = stream.Get_uint16 ();
1937
1938 // Tag name (pascal string):
1939
1940 char tagName [256];
1941
1942 {
1943
1944 uint32 tagNameCount = stream.Get_uint8 ();
1945
1946 stream.Get (tagName, tagNameCount);
1947
1948 tagName [tagNameCount] = 0;
1949
1950 if ((tagNameCount & 1) == 0)
1951 {
1952 (void) stream.Get_uint8 ();
1953 }
1954
1955 }
1956
1957 // Tag data size:
1958
1959 uint32 tagSize = stream.Get_uint32 ();
1960
1961 // Check validity of size.
1962
1963 if (stream.Position () + tagSize > lastOffset)
1964 {
1965 return;
1966 }
1967
1968 // Find next offset.
1969
1970 nextOffset = stream.Position () + RoundUp2 (tagSize);
1971
1972 if (tagSignature == DNG_CHAR4 ('8', 'B', 'I', 'M') &&
1973 tagID == 1061 &&
1974 tagSize == 16)
1975 {
1976
1977 dng_fingerprint oldDigest;
1978
1979 stream.Get (oldDigest.data, 16);
1980
1981 if (iptcDigest != oldDigest)
1982 {
1983
1984 stream.SetWritePosition (stream.Position () - 16);
1985
1986 #if qLogDNGUpdateMetadata
1987
1988 LogPrint ("DNGUpdateMetadata: Updating Adobe IPTC Digest, offset = %llu\n",
1989 (unsigned long long) stream.Position ());
1990
1991 #endif
1992
1993 stream.Put (iptcDigest.data, 16);
1994
1995 }
1996
1997 return;
1998
1999 }
2000
2001 }
2002
2003 }
2004
2005 }
2006
2007 /*****************************************************************************/
2008
2009 dng_file_updater::dng_file_updater (dng_host &host,
2010 dng_stream &stream)
2011
2012 : fHost (host)
2013 , fStream (stream)
2014
2015 , fTruncateOnFailure (true)
2016 , fTruncateToLength (fStream.Length ())
2017
2018 {
2019
2020 }
2021
2022 /*****************************************************************************/
2023
2024 dng_file_updater::~dng_file_updater ()
2025 {
2026
2027 if (fTruncateOnFailure)
2028 {
2029
2030 try
2031 {
2032 fStream.Flush ();
2033 }
2034 catch (...)
2035 {
2036 }
2037
2038 try
2039 {
2040 fStream.SetLength (fTruncateToLength);
2041 }
2042 catch (...)
2043 {
2044 }
2045
2046 }
2047
2048 }
2049
2050 /*****************************************************************************/
2051
2052 void dng_file_updater::ParseHeader ()
2053 {
2054
2055 Stream ().SetReadPosition (0);
2056
2057 uint16 byteOrder = Stream ().Get_uint16 ();
2058
2059 if (byteOrder == byteOrderII)
2060 {
2061 Stream ().SetLittleEndian ();
2062 }
2063
2064 else if (byteOrder == byteOrderMM)
2065 {
2066 Stream ().SetBigEndian ();
2067 }
2068
2069 else
2070 {
2071 ThrowBadFormat ();
2072 }
2073
2074 uint16 magic = Stream ().Get_uint16 ();
2075
2076 if (magic != magicTIFF &&
2077 magic != magicBigTIFF)
2078 {
2079 ThrowBadFormat ();
2080 }
2081
2082 fBigTIFF = (magic == magicBigTIFF);
2083
2084 if (fBigTIFF)
2085 {
2086
2087 uint16 byteSize = Stream ().Get_uint16 ();
2088 uint16 zeroPad = Stream ().Get_uint16 ();
2089
2090 if (byteSize != 8 || zeroPad != 0)
2091 {
2092 ThrowBadFormat ();
2093 }
2094
2095 }
2096
2097 // Parse IFD 0.
2098
2099 uint64 ifd0_offset = fBigTIFF ? Stream ().Get_uint64 ()
2100 : Stream ().Get_uint32 ();
2101
2102 Stream ().SetReadPosition (ifd0_offset);
2103
2104 IFD0 ().Parse (*this);
2105
2106 fIFD0_old = IFD0 ();
2107
2108 // See if this is a DNG format file (vs a normal TIFF file).
2109
2110 fIsDNG = false;
2111
2112 if (IFD0 ().HasTag (tcDNGVersion))
2113 {
2114
2115 const dng_tag_updater &dngVersionTag (IFD0 ().ExistingTag (tcDNGVersion));
2116
2117 uint32 dngVersion = (((uint32) dngVersionTag.fTagValue8 [0]) << 24) |
2118 (((uint32) dngVersionTag.fTagValue8 [1]) << 16) |
2119 (((uint32) dngVersionTag.fTagValue8 [2]) << 8) |
2120 (((uint32) dngVersionTag.fTagValue8 [3]) );
2121
2122 fIsDNG = (dngVersion != 0);
2123
2124 }
2125
2126 // Parse EXIF IFD, if any.
2127
2128 if (IFD0 ().HasTag (tcExifIFD))
2129 {
2130
2131 const dng_tag_updater &exifTag (IFD0 ().ExistingTag (tcExifIFD));
2132
2133 uint64 exif_offset = exifTag.Get_uint64 (*this);
2134
2135 if (exif_offset)
2136 {
2137
2138 Stream ().SetReadPosition (exif_offset);
2139
2140 EXIF ().Parse (*this);
2141
2142 fEXIF_old = EXIF ();
2143
2144 }
2145
2146 }
2147
2148 // Parse GPS IFD, if any.
2149
2150 if (IFD0 ().HasTag (tcGPSInfo))
2151 {
2152
2153 const dng_tag_updater &gpsTag (IFD0 ().ExistingTag (tcGPSInfo));
2154
2155 uint64 gps_offset = gpsTag.Get_uint64 (*this);
2156
2157 if (gps_offset)
2158 {
2159
2160 Stream ().SetReadPosition (gps_offset);
2161
2162 GPS ().Parse (*this);
2163
2164 fGPS_old = GPS ();
2165
2166 }
2167
2168 }
2169
2170 }
2171
2172 /*****************************************************************************/
2173
2174 void dng_file_updater::PrepareToAppend (uint64 count)
2175 {
2176
2177 uint64 length = Stream ().Length ();
2178
2179 if (!BigTIFF ())
2180 {
2181
2182 if (length + (length & 1) + count > 0x0FFFFFFFFL)
2183 {
2184
2185 if (IsDNG ())
2186 {
2187 ThrowImageTooBigDNG ();
2188 }
2189 else
2190 {
2191 ThrowImageTooBigTIFF ();
2192 }
2193
2194 }
2195
2196 }
2197
2198 Stream ().SetWritePosition (length);
2199
2200 Stream ().PadAlign2 ();
2201
2202 }
2203
2204 /*****************************************************************************/
2205
2206 void dng_file_updater::MoveIFD0toEXIF (uint32 tagCode)
2207 {
2208
2209 #if qLogDNGUpdateMetadata
2210 bool moved =
2211 #endif
2212
2213 IFD0 ().MoveTagToIFD (EXIF (), tagCode);
2214
2215 #if qLogDNGUpdateMetadata
2216 if (moved)
2217 {
2218 LogPrint ("DNGUpdateMetadata: Moving tag %s from IFD0 to EXIF\n",
2219 LookupTagCode (0, tagCode));
2220 }
2221 #endif
2222
2223 }
2224
2225 /*****************************************************************************/
2226
2227 void dng_file_updater::MoveEXIFtoIFD0 (uint32 tagCode)
2228 {
2229
2230 #if qLogDNGUpdateMetadata
2231 bool moved =
2232 #endif
2233
2234 EXIF ().MoveTagToIFD (IFD0 (), tagCode);
2235
2236 #if qLogDNGUpdateMetadata
2237 if (moved)
2238 {
2239 LogPrint ("DNGUpdateMetadata: Moving tag %s from EXIF to IFD0\n",
2240 LookupTagCode (0, tagCode));
2241 }
2242 #endif
2243
2244 }
2245
2246 /*****************************************************************************/
2247
2248 void dng_file_updater::UpdateEXIF (dng_exif &exif)
2249 {
2250
2251 // DNG and TIFF-EP allow EXIF tags in IFD0 as an option. Move to
2252 // preferred IFDs for update.
2253
2254 // Updatable EXIF tags
2255
2256 MoveIFD0toEXIF (tcExifVersion);
2257 MoveIFD0toEXIF (tcDateTimeOriginal);
2258 MoveIFD0toEXIF (tcDateTimeDigitized);
2259 MoveIFD0toEXIF (tcSubsecTime);
2260 MoveIFD0toEXIF (tcSubsecTimeOriginal);
2261 MoveIFD0toEXIF (tcSubsecTimeDigitized);
2262 MoveIFD0toEXIF (tcOffsetTime);
2263 MoveIFD0toEXIF (tcOffsetTimeOriginal);
2264 MoveIFD0toEXIF (tcOffsetTimeDigitized);
2265 MoveIFD0toEXIF (tcUserComment);
2266 MoveIFD0toEXIF (tcCameraOwnerNameExif);
2267 MoveIFD0toEXIF (tcLensModelExif);
2268
2269 // Read only EXIF tags
2270
2271 MoveIFD0toEXIF (tcExposureTime);
2272 MoveIFD0toEXIF (tcShutterSpeedValue);
2273 MoveIFD0toEXIF (tcFNumber);
2274 MoveIFD0toEXIF (tcApertureValue);
2275 MoveIFD0toEXIF (tcBrightnessValue);
2276 MoveIFD0toEXIF (tcExposureBiasValue);
2277 MoveIFD0toEXIF (tcMaxApertureValue);
2278 MoveIFD0toEXIF (tcSubjectDistance);
2279 MoveIFD0toEXIF (tcFocalLength);
2280 MoveIFD0toEXIF (tcISOSpeedRatings);
2281 MoveIFD0toEXIF (tcSensitivityType);
2282 MoveIFD0toEXIF (tcStandardOutputSensitivity);
2283 MoveIFD0toEXIF (tcRecommendedExposureIndex);
2284 MoveIFD0toEXIF (tcISOSpeed);
2285 MoveIFD0toEXIF (tcISOSpeedLatitudeyyy);
2286 MoveIFD0toEXIF (tcISOSpeedLatitudezzz);
2287 MoveIFD0toEXIF (tcFlash);
2288 MoveIFD0toEXIF (tcExposureProgram);
2289 MoveIFD0toEXIF (tcMeteringMode);
2290 MoveIFD0toEXIF (tcLightSource);
2291 MoveIFD0toEXIF (tcSensingMethodExif);
2292 MoveIFD0toEXIF (tcFocalLengthIn35mmFilm);
2293 MoveIFD0toEXIF (tcFileSource);
2294 MoveIFD0toEXIF (tcSceneType);
2295 MoveIFD0toEXIF (tcCFAPatternExif);
2296 MoveIFD0toEXIF (tcCustomRendered);
2297 MoveIFD0toEXIF (tcExposureMode);
2298 MoveIFD0toEXIF (tcWhiteBalance);
2299 MoveIFD0toEXIF (tcGainControl);
2300 MoveIFD0toEXIF (tcContrast);
2301 MoveIFD0toEXIF (tcSaturation);
2302 MoveIFD0toEXIF (tcSharpness);
2303 MoveIFD0toEXIF (tcSubjectDistanceRange);
2304 MoveIFD0toEXIF (tcDigitalZoomRatio);
2305 MoveIFD0toEXIF (tcExposureIndexExif);
2306 MoveIFD0toEXIF (tcColorSpace);
2307 MoveIFD0toEXIF (tcFocalPlaneXResolutionExif);
2308 MoveIFD0toEXIF (tcFocalPlaneYResolutionExif);
2309 MoveIFD0toEXIF (tcFocalPlaneResolutionUnitExif);
2310 MoveIFD0toEXIF (tcSubjectArea);
2311 MoveIFD0toEXIF (tcMakerNote);
2312 MoveIFD0toEXIF (tcImageUniqueID);
2313 MoveIFD0toEXIF (tcCameraSerialNumberExif);
2314 MoveIFD0toEXIF (tcLensSpecificationExif);
2315 MoveIFD0toEXIF (tcLensMakeExif);
2316 MoveIFD0toEXIF (tcLensSerialNumberExif);
2317 MoveIFD0toEXIF (tcTemperature);
2318 MoveIFD0toEXIF (tcHumidity);
2319 MoveIFD0toEXIF (tcPressure);
2320 MoveIFD0toEXIF (tcWaterDepth);
2321 MoveIFD0toEXIF (tcAcceleration);
2322 MoveIFD0toEXIF (tcCameraElevationAngle);
2323
2324 // Updatable IFD0 tags
2325
2326 MoveEXIFtoIFD0 (tcDateTime);
2327 MoveEXIFtoIFD0 (tcArtist);
2328 MoveEXIFtoIFD0 (tcSoftware);
2329 MoveEXIFtoIFD0 (tcCopyright);
2330 MoveEXIFtoIFD0 (tcImageDescription);
2331 MoveEXIFtoIFD0 (tcImageNumber);
2332
2333 // Read only IFD0 tags
2334
2335 MoveEXIFtoIFD0 (tcMake);
2336 MoveEXIFtoIFD0 (tcModel);
2337 MoveEXIFtoIFD0 (tcCameraSerialNumber);
2338 MoveEXIFtoIFD0 (tcMakerNoteSafety);
2339 MoveEXIFtoIFD0 (tcLensInfo);
2340 MoveEXIFtoIFD0 (tcSelfTimerMode);
2341 MoveEXIFtoIFD0 (tcBatteryLevel);
2342
2343 // Update EXIF version tag
2344
2345 EXIF ().UpdateVersionTag (*this,
2346 tcExifVersion,
2347 ttUndefined,
2348 exif.fExifVersion);
2349
2350 // Update DateTime tags.
2351
2352 IFD0 ().UpdateDateTimeTag (*this,
2353 tcDateTime,
2354 exif.fDateTime);
2355
2356 EXIF ().UpdateStringTag (*this,
2357 tcSubsecTime,
2358 exif.fDateTime.Subseconds ());
2359
2360 if (exif.AtLeastVersion0231 () || EXIF ().HasTag (tcOffsetTime))
2361 {
2362
2363 EXIF ().UpdateOffsetTimeTag (*this,
2364 tcOffsetTime,
2365 exif.fDateTime);
2366
2367 }
2368
2369 // Update DateTimeOriginal tags.
2370
2371 EXIF ().UpdateDateTimeTag (*this,
2372 tcDateTimeOriginal,
2373 exif.fDateTimeOriginal);
2374
2375 EXIF ().UpdateStringTag (*this,
2376 tcSubsecTimeOriginal,
2377 exif.fDateTimeOriginal.Subseconds ());
2378
2379 if (exif.AtLeastVersion0231 () || EXIF ().HasTag (tcOffsetTimeOriginal))
2380 {
2381
2382 EXIF ().UpdateOffsetTimeTag (*this,
2383 tcOffsetTimeOriginal,
2384 exif.fDateTimeOriginal);
2385
2386 }
2387
2388 // Update DateTimeDigitized tags.
2389
2390 EXIF ().UpdateDateTimeTag (*this,
2391 tcDateTimeDigitized,
2392 exif.fDateTimeDigitized);
2393
2394 EXIF ().UpdateStringTag (*this,
2395 tcSubsecTimeDigitized,
2396 exif.fDateTimeDigitized.Subseconds ());
2397
2398 if (exif.AtLeastVersion0231 () || EXIF ().HasTag (tcOffsetTimeDigitized))
2399 {
2400
2401 EXIF ().UpdateOffsetTimeTag (*this,
2402 tcOffsetTimeDigitized,
2403 exif.fDateTimeDigitized);
2404
2405 }
2406
2407 // Updatable string tags.
2408
2409 IFD0 ().UpdateStringTag (*this,
2410 tcImageDescription,
2411 exif.fImageDescription);
2412
2413 IFD0 ().UpdateStringTag (*this,
2414 tcArtist,
2415 exif.fArtist);
2416
2417 IFD0 ().UpdateStringTag (*this,
2418 tcSoftware,
2419 exif.fSoftware);
2420
2421 IFD0 ().UpdateDualStringTag (*this,
2422 tcCopyright,
2423 exif.fCopyright,
2424 exif.fCopyright2);
2425
2426 EXIF ().UpdateEncodedTextTag (*this,
2427 tcUserComment,
2428 exif.fUserComment);
2429
2430 // OwnerName can be updated.
2431
2432 if (exif.AtLeastVersion0230 () || EXIF ().HasTag (tcCameraOwnerNameExif))
2433 {
2434
2435 EXIF ().UpdateStringTag (*this,
2436 tcCameraOwnerNameExif,
2437 exif.fOwnerName);
2438
2439 }
2440
2441 // LensModel can be updated.
2442
2443 if (exif.AtLeastVersion0230 () || EXIF ().HasTag (tcLensModelExif))
2444 {
2445
2446 EXIF ().UpdateStringTag (*this,
2447 tcLensModelExif,
2448 exif.fLensName);
2449
2450 }
2451
2452 // ImageNumber can be updated.
2453
2454 if (IsDNG () || IFD0 ().HasTag (tcImageNumber))
2455 {
2456
2457 IFD0 ().UpdateTag (*this,
2458 tcImageNumber,
2459 ttLong,
2460 1,
2461 &exif.fImageNumber,
2462 exif.fImageNumber == 0xFFFFFFFF);
2463
2464 }
2465
2466 // All the GPS tags can be updated.
2467
2468 GPS ().UpdateVersionTag (*this,
2469 tcGPSVersionID,
2470 ttByte,
2471 exif.fGPSVersionID);
2472
2473 GPS ().UpdateStringTag (*this,
2474 tcGPSLatitudeRef,
2475 exif.fGPSLatitudeRef);
2476
2477 GPS ().UpdateTag (*this,
2478 tcGPSLatitude,
2479 ttRational,
2480 3,
2481 exif.fGPSLatitude,
2482 !exif.fGPSLatitude [0].IsValid ());
2483
2484 GPS ().UpdateStringTag (*this,
2485 tcGPSLongitudeRef,
2486 exif.fGPSLongitudeRef);
2487
2488 GPS ().UpdateTag (*this,
2489 tcGPSLongitude,
2490 ttRational,
2491 3,
2492 exif.fGPSLongitude,
2493 !exif.fGPSLongitude [0].IsValid ());
2494
2495 GPS ().UpdateByteTag (*this,
2496 tcGPSAltitudeRef,
2497 (uint8) exif.fGPSAltitudeRef,
2498 exif.fGPSAltitudeRef == 0xFFFFFFFF);
2499
2500 GPS ().UpdateRationalTag (*this,
2501 tcGPSAltitude,
2502 exif.fGPSAltitude);
2503
2504 GPS ().UpdateTag (*this,
2505 tcGPSTimeStamp,
2506 ttRational,
2507 3,
2508 exif.fGPSTimeStamp,
2509 !exif.fGPSTimeStamp [0].IsValid ());
2510
2511 GPS ().UpdateStringTag (*this,
2512 tcGPSSatellites,
2513 exif.fGPSSatellites);
2514
2515 GPS ().UpdateStringTag (*this,
2516 tcGPSStatus,
2517 exif.fGPSStatus);
2518
2519 GPS ().UpdateStringTag (*this,
2520 tcGPSMeasureMode,
2521 exif.fGPSMeasureMode);
2522
2523 GPS ().UpdateRationalTag (*this,
2524 tcGPSDOP,
2525 exif.fGPSDOP);
2526
2527 GPS ().UpdateStringTag (*this,
2528 tcGPSSpeedRef,
2529 exif.fGPSSpeedRef);
2530
2531 GPS ().UpdateRationalTag (*this,
2532 tcGPSSpeed,
2533 exif.fGPSSpeed);
2534
2535 GPS ().UpdateStringTag (*this,
2536 tcGPSTrackRef,
2537 exif.fGPSTrackRef);
2538
2539 GPS ().UpdateRationalTag (*this,
2540 tcGPSTrack,
2541 exif.fGPSTrack);
2542
2543 GPS ().UpdateStringTag (*this,
2544 tcGPSImgDirectionRef,
2545 exif.fGPSImgDirectionRef);
2546
2547 GPS ().UpdateRationalTag (*this,
2548 tcGPSImgDirection,
2549 exif.fGPSImgDirection);
2550
2551 GPS ().UpdateStringTag (*this,
2552 tcGPSMapDatum,
2553 exif.fGPSMapDatum);
2554
2555 GPS ().UpdateStringTag (*this,
2556 tcGPSDestLatitudeRef,
2557 exif.fGPSDestLatitudeRef);
2558
2559 GPS ().UpdateTag (*this,
2560 tcGPSDestLatitude,
2561 ttRational,
2562 3,
2563 exif.fGPSDestLatitude,
2564 !exif.fGPSDestLatitude [0].IsValid ());
2565
2566 GPS ().UpdateStringTag (*this,
2567 tcGPSDestLongitudeRef,
2568 exif.fGPSDestLongitudeRef);
2569
2570 GPS ().UpdateTag (*this,
2571 tcGPSDestLongitude,
2572 ttRational,
2573 3,
2574 exif.fGPSDestLongitude,
2575 !exif.fGPSDestLongitude [0].IsValid ());
2576
2577 GPS ().UpdateStringTag (*this,
2578 tcGPSDestBearingRef,
2579 exif.fGPSDestBearingRef);
2580
2581 GPS ().UpdateRationalTag (*this,
2582 tcGPSDestBearing,
2583 exif.fGPSDestBearing);
2584
2585 GPS ().UpdateStringTag (*this,
2586 tcGPSDestDistanceRef,
2587 exif.fGPSDestDistanceRef);
2588
2589 GPS ().UpdateRationalTag (*this,
2590 tcGPSDestDistance,
2591 exif.fGPSDestDistance);
2592
2593 GPS ().UpdateEncodedTextTag (*this,
2594 tcGPSProcessingMethod,
2595 exif.fGPSProcessingMethod);
2596
2597 GPS ().UpdateEncodedTextTag (*this,
2598 tcGPSAreaInformation,
2599 exif.fGPSAreaInformation);
2600
2601 GPS ().UpdateStringTag (*this,
2602 tcGPSDateStamp,
2603 exif.fGPSDateStamp);
2604
2605 GPS ().UpdateShortTag (*this,
2606 tcGPSDifferential,
2607 (uint16) exif.fGPSDifferential,
2608 exif.fGPSDifferential == 0xFFFFFFFF);
2609
2610 GPS ().UpdateRationalTag (*this,
2611 tcGPSHPositioningError,
2612 exif.fGPSHPositioningError);
2613
2614 }
2615
2616 /*****************************************************************************/
2617
2618 bool dng_file_updater::UpdateIFD (dng_ifd_updater &ifd_new,
2619 const dng_ifd_updater &ifd_old)
2620 {
2621
2622 // We don't attempt to preserve sort order while editing, so just
2623 // sort them here.
2624
2625 ifd_new.SortTags ();
2626
2627 if (ifd_new != ifd_old)
2628 {
2629
2630 uint32 newByteCount = ifd_new.ByteCount (*this);
2631 uint32 oldByteCount = ifd_old.ByteCount (*this);
2632
2633 if (newByteCount > oldByteCount)
2634 {
2635
2636 PrepareToAppend (newByteCount);
2637
2638 ifd_new.SetOffset (Stream ().Position ());
2639
2640 ifd_new.Write (*this);
2641
2642 AddZeroRange (ifd_old.Offset (),
2643 oldByteCount);
2644
2645 }
2646
2647 else if (newByteCount == 0)
2648 {
2649
2650 ifd_new.SetOffset (0);
2651
2652 AddZeroRange (ifd_old.Offset (),
2653 oldByteCount);
2654
2655 }
2656
2657 else
2658 {
2659
2660 fTruncateOnFailure = false;
2661
2662 ifd_new.Write (*this);
2663
2664 AddZeroRange (ifd_new.Offset () + newByteCount,
2665 oldByteCount - newByteCount);
2666
2667 }
2668
2669 return true;
2670
2671 }
2672
2673 return false;
2674
2675 }
2676
2677 /*****************************************************************************/
2678
2679 void dng_file_updater::UpdateHeader ()
2680 {
2681
2682 // Do we need to update EXIF IFD?
2683
2684 if (UpdateIFD (EXIF (), fEXIF_old))
2685 {
2686
2687 #if qLogDNGUpdateMetadata
2688 LogPrint ("DNGUpdateMetadata: Replaced EXIF IFD, old entries = %u, new entries = %u\n",
2689 fEXIF_old.EntryCount (),
2690 EXIF ().EntryCount ());
2691 #endif
2692
2693 if (EXIF ().Offset () != fEXIF_old.Offset ())
2694 {
2695
2696 if (BigTIFF ())
2697 {
2698
2699 uint64 offset64 = EXIF ().Offset ();
2700
2701 IFD0 ().UpdateTag (*this,
2702 tcExifIFD,
2703 ttLong8,
2704 1,
2705 &offset64,
2706 EXIF ().EntryCount () == 0);
2707
2708 }
2709
2710 else
2711 {
2712
2713 uint32 offset32 = (uint32) EXIF ().Offset ();
2714
2715 IFD0 ().UpdateTag (*this,
2716 tcExifIFD,
2717 ttLong,
2718 1,
2719 &offset32,
2720 EXIF ().EntryCount () == 0);
2721
2722 }
2723
2724 }
2725
2726 }
2727
2728 // Do we need to update GPS IFD?
2729
2730 if (UpdateIFD (GPS (), fGPS_old))
2731 {
2732
2733 #if qLogDNGUpdateMetadata
2734 LogPrint ("DNGUpdateMetadata: Replaced GPS IFD, old entries = %u, new entries = %u\n",
2735 fGPS_old.EntryCount (),
2736 GPS ().EntryCount ());
2737 #endif
2738
2739 if (GPS ().Offset () != fGPS_old.Offset ())
2740 {
2741
2742 if (BigTIFF ())
2743 {
2744
2745 uint64 offset64 = GPS ().Offset ();
2746
2747 IFD0 ().UpdateTag (*this,
2748 tcGPSInfo,
2749 ttLong8,
2750 1,
2751 &offset64,
2752 GPS ().EntryCount () == 0);
2753
2754 }
2755
2756 else
2757 {
2758
2759 uint32 offset32 = (uint32) GPS ().Offset ();
2760
2761 IFD0 ().UpdateTag (*this,
2762 tcGPSInfo,
2763 ttLong,
2764 1,
2765 &offset32,
2766 GPS ().EntryCount () == 0);
2767
2768 }
2769
2770 }
2771
2772 }
2773
2774 // Do we need to update IFD 0?
2775
2776 if (UpdateIFD (IFD0 (), fIFD0_old))
2777 {
2778
2779 #if qLogDNGUpdateMetadata
2780 LogPrint ("DNGUpdateMetadata: Replaced IFD0, old entries = %u, new entries = %u\n",
2781 fIFD0_old.EntryCount (),
2782 IFD0 ().EntryCount ());
2783 #endif
2784
2785 fTruncateOnFailure = false;
2786
2787 if (IFD0 ().Offset () != fIFD0_old.Offset ())
2788 {
2789
2790 #if qLogDNGUpdateMetadata
2791 LogPrint ("DNGUpdateMetadata: Patching IFD0 offset\n");
2792 #endif
2793
2794 if (BigTIFF ())
2795 {
2796
2797 Stream ().SetWritePosition (8);
2798
2799 Stream ().Put_uint64 (IFD0 ().Offset ());
2800
2801 }
2802
2803 else
2804 {
2805
2806 Stream ().SetWritePosition (4);
2807
2808 Stream ().Put_uint32 ((uint32) IFD0 ().Offset ());
2809
2810 }
2811
2812 }
2813
2814 }
2815
2816 Stream ().Flush ();
2817
2818 fTruncateOnFailure = false;
2819
2820 }
2821
2822 /*****************************************************************************/
2823
2824 void dng_file_updater::AddZeroRange (uint64 offset,
2825 uint64 count)
2826 {
2827
2828 if (count)
2829 {
2830
2831 zero_range range;
2832
2833 range.fOffset = offset;
2834 range.fCount = count;
2835
2836 fZeroRanges.push_back (range);
2837
2838 }
2839
2840 }
2841
2842 /*****************************************************************************/
2843
2844 void dng_file_updater::ApplyZeroRanges ()
2845 {
2846
2847 if (!fZeroRanges.empty ())
2848 {
2849
2850 std::sort (fZeroRanges.begin (), fZeroRanges.end ());
2851
2852 for (size_t j = 0; j < fZeroRanges.size (); j++)
2853 {
2854
2855 #if qLogDNGUpdateMetadata
2856
2857 LogPrint ("DNGUpdateMetadata: Zeroing byte range, offset = %llu, count = %llu\n",
2858 (unsigned long long) fZeroRanges [j].fOffset,
2859 (unsigned long long) fZeroRanges [j].fCount);
2860
2861 #endif
2862
2863 Stream ().SetWritePosition (fZeroRanges [j].fOffset);
2864
2865 Stream ().PutZeros (fZeroRanges [j].fCount);
2866
2867 }
2868
2869 Stream ().Flush ();
2870
2871 }
2872
2873 }
2874
2875 /*****************************************************************************/
2876
2877 void CleanUpMetadataForUpdate (dng_host &host,
2878 dng_metadata &metadata,
2879 bool wantsIPTC)
2880 {
2881
2882 if (metadata.GetXMP () && metadata.GetExif ())
2883 {
2884
2885 dng_xmp &newXMP (*metadata.GetXMP ());
2886 dng_exif &newEXIF (*metadata.GetExif ());
2887
2888 // Resync EXIF, remove EXIF tags from XMP.
2889
2890 newXMP.SyncExif (newEXIF,
2891 metadata.GetOriginalExif (),
2892 false,
2893 true);
2894
2895 if (!wantsIPTC)
2896 {
2897
2898 metadata.ClearIPTC ();
2899
2900 newXMP.ClearIPTCDigest ();
2901
2902 }
2903
2904 else
2905 {
2906
2907 metadata.RebuildIPTC (host.Allocator (),
2908 true);
2909
2910 if (metadata.IPTCLength ())
2911 {
2912
2913 dng_fingerprint digest = metadata.IPTCDigest ();
2914
2915 newXMP.SetIPTCDigest (digest);
2916
2917 }
2918
2919 else
2920 {
2921
2922 newXMP.ClearIPTCDigest ();
2923
2924 }
2925
2926 }
2927
2928 }
2929
2930 }
2931
2932 /*****************************************************************************/
2933
2934 void DNGUpdateMetadata (dng_host &host,
2935 dng_stream &stream,
2936 const dng_negative &negative,
2937 const dng_metadata &metadata)
2938 {
2939
2940 dng_file_updater updater (host, stream);
2941
2942 // Read header and IFDs.
2943
2944 updater.ParseHeader ();
2945
2946 // Clean up metadata for update.
2947
2948 AutoPtr<dng_metadata> updatedMetadata (metadata.Clone (host.Allocator ()));
2949
2950 CleanUpMetadataForUpdate (host,
2951 *updatedMetadata,
2952 !updater.IsDNG ());
2953
2954 // Some DNG files do not contain a RawDataUniqueID field,
2955 // Add tag with the computed value we are using.
2956
2957 if (updater.IsDNG ())
2958 {
2959
2960 dng_fingerprint rawDataUniqueID = negative.BaseRawDataUniqueID ();
2961
2962 if (rawDataUniqueID.IsValid ())
2963 {
2964
2965 updater.IFD0 ().UpdateTag (updater,
2966 tcRawDataUniqueID,
2967 ttByte,
2968 16,
2969 rawDataUniqueID.data);
2970
2971 }
2972
2973 }
2974
2975 // Do we have big tables to embed?
2976
2977 if (!metadata.BigTableDictionary ().IsEmpty ())
2978 {
2979
2980 // Read in existing big table index, if any.
2981
2982 dng_big_table_index bigTableIndex;
2983
2984 updater.IFD0 ().ParseBigTableIndex (updater, bigTableIndex);
2985
2986 // Write any new big tables to file and add to index.
2987
2988 bool indexDirty = false;
2989
2990 for (auto it = metadata.BigTableDictionary ().Map ().cbegin ();
2991 it != metadata.BigTableDictionary ().Map ().cend ();
2992 ++it)
2993 {
2994
2995 const dng_fingerprint &fingerprint = it->first;
2996
2997 if (!bigTableIndex.HasEntry (fingerprint))
2998 {
2999
3000 const dng_ref_counted_block &block = it->second;
3001
3002 uint32 blockSize = block.LogicalSize ();
3003
3004 updater.PrepareToAppend (blockSize);
3005
3006 uint64 blockOffset = stream.Position ();
3007
3008 stream.Put (block.Buffer (), blockSize);
3009
3010 #if qLogDNGUpdateMetadata
3011
3012 LogPrint ("DNGUpdateMetadata: Adding big table <%s>, size = %u, offset = %llu\n",
3013 dng_xmp::EncodeFingerprint (fingerprint).Get (),
3014 blockSize,
3015 (unsigned long long) blockOffset);
3016
3017 #endif
3018
3019 bigTableIndex.AddEntry (fingerprint,
3020 blockSize,
3021 blockOffset);
3022
3023 indexDirty = true;
3024
3025 }
3026
3027 }
3028
3029 // Write updated index back to ifd if dirty.
3030
3031 if (indexDirty)
3032 {
3033
3034 updater.IFD0 ().WriteBigTableIndex (updater,
3035 bigTableIndex);
3036
3037 }
3038
3039 // Read in existing big table group index, if any.
3040
3041 dng_big_table_group_index groupIndex;
3042
3043 updater.IFD0 ().ParseBigTableGroupIndex (updater,
3044 groupIndex);
3045
3046 // Write any new groups to the group index.
3047
3048 bool groupIndexDirty = false;
3049
3050 for (const auto &group : metadata.BigTableGroupIndex ().Map ())
3051 {
3052
3053 const auto &groupDigest = group.first;
3054
3055 const auto &instanceDigest = group.second;
3056
3057 if (!groupIndex.HasEntry (groupDigest))
3058 {
3059
3060 // Also check that the group refers to an existing instance.
3061
3062 if (bigTableIndex.HasEntry (instanceDigest))
3063 {
3064
3065 groupIndex.AddEntry (groupDigest,
3066 instanceDigest);
3067
3068 groupIndexDirty = true;
3069
3070 }
3071
3072 }
3073
3074 } // for all groups
3075
3076 // Write updated group index back to ifd if dirty.
3077
3078 if (groupIndexDirty)
3079 {
3080
3081 updater.IFD0 ().WriteBigTableGroupIndex (updater,
3082 groupIndex);
3083
3084 }
3085
3086 }
3087
3088 // Update XMP.
3089
3090 if (updatedMetadata->GetXMP ())
3091 {
3092
3093 uint32 existingSize = 0;
3094
3095 if (updater.IFD0 ().HasTag (tcXMP))
3096 {
3097
3098 const dng_tag_updater &xmpTag (updater.IFD0 ().ExistingTag (tcXMP));
3099
3100 if (xmpTag.TagSize () <= 0xFFFFFFFF)
3101 {
3102 existingSize = (uint32) xmpTag.TagSize ();
3103 }
3104
3105 }
3106
3107 AutoPtr<dng_memory_block> xmpBlock (updatedMetadata->GetXMP ()->Serialize (true,
3108 existingSize));
3109
3110 if (xmpBlock.Get ())
3111 {
3112
3113 updater.IFD0 ().UpdateTag (updater,
3114 tcXMP,
3115 ttByte,
3116 xmpBlock->LogicalSize (),
3117 xmpBlock->Buffer (),
3118 xmpBlock->LogicalSize () == 0);
3119
3120 }
3121
3122 }
3123
3124 // Update IPTC.
3125
3126 updater.IFD0 ().UpdateTag (updater,
3127 tcIPTC_NAA,
3128 ttLong,
3129 updatedMetadata->IPTCLength () >> 2,
3130 updatedMetadata->IPTCData (),
3131 updatedMetadata->IPTCLength () == 0);
3132
3133 // Update IPTC Digest inside Adobe data, if preset.
3134
3135 {
3136
3137 dng_fingerprint iptcDigest;
3138
3139 if (updatedMetadata->IPTCLength ())
3140 {
3141
3142 iptcDigest = updatedMetadata->IPTCDigest ();
3143
3144 }
3145
3146 updater.IFD0 ().UpdateAdobeData (updater,
3147 iptcDigest);
3148
3149 }
3150
3151 // Update the EXIF/GPS metadata.
3152
3153 if (updatedMetadata->GetExif ())
3154 {
3155
3156 updater.UpdateEXIF (*updatedMetadata->GetExif ());
3157
3158 }
3159
3160 // Update orientation tag.
3161
3162 {
3163
3164 uint16 orientation = (uint16) negative.ComputeOrientation (*updatedMetadata).GetTIFF ();
3165
3166 updater.IFD0 ().UpdateShortTag (updater,
3167 tcOrientation,
3168 orientation);
3169
3170 }
3171
3172 // Update any modified IFDs.
3173
3174 updater.UpdateHeader ();
3175
3176 // Zero now unused data.
3177
3178 updater.ApplyZeroRanges ();
3179
3180 }
3181
3182 /*****************************************************************************/