-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathtiffvisitor_int.cpp
1717 lines (1529 loc) · 58.8 KB
/
tiffvisitor_int.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
// *****************************************************************************
// included header files
#include "config.h"
#include "tiffcomposite_int.hpp" // Do not change the order of these 2 includes,
#include "tiffvisitor_int.hpp" // see bug #487
#include "tiffimage_int.hpp"
#include "image_int.hpp"
#include "makernote_int.hpp"
#include "exif.hpp"
#include "enforce.hpp"
#include "iptc.hpp"
#include "value.hpp"
#include "image.hpp"
#include "jpgimage.hpp"
#include "sonymn_int.hpp"
#include "i18n.h" // NLS support.
// + standard includes
#include <string>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <limits>
#include <ostream>
// *****************************************************************************
namespace {
//! Unary predicate that matches an Exifdatum with a given group and index.
class FindExifdatum2 {
public:
//! Constructor, initializes the object with the group and index to look for.
FindExifdatum2(Exiv2::Internal::IfdId group, int idx)
: groupName_(Exiv2::Internal::groupName(group)), idx_(idx) {}
//! Returns true if group and index match.
bool operator()(const Exiv2::Exifdatum& md) const
{
return idx_ == md.idx() && 0 == strcmp(md.groupName().c_str(), groupName_);
}
private:
const char* groupName_;
int idx_;
}; // class FindExifdatum2
Exiv2::ByteOrder stringToByteOrder(const std::string& val)
{
Exiv2::ByteOrder bo = Exiv2::invalidByteOrder;
if (0 == strcmp("II", val.c_str())) bo = Exiv2::littleEndian;
else if (0 == strcmp("MM", val.c_str())) bo = Exiv2::bigEndian;
return bo;
}
} // namespace
// *****************************************************************************
// class member definitions
namespace Exiv2 {
namespace Internal {
TiffVisitor::TiffVisitor()
{
go_.fill(true);
}
void TiffVisitor::setGo(GoEvent event, bool go)
{
assert(event >= 0 && static_cast<int>(event) < events_);
go_[event] = go;
}
bool TiffVisitor::go(GoEvent event) const
{
assert(event >= 0 && static_cast<int>(event) < events_);
return go_[event];
}
void TiffVisitor::visitDirectoryNext(TiffDirectory* /*object*/)
{
}
void TiffVisitor::visitDirectoryEnd(TiffDirectory* /*object*/)
{
}
void TiffVisitor::visitIfdMakernoteEnd(TiffIfdMakernote* /*object*/)
{
}
void TiffVisitor::visitBinaryArrayEnd(TiffBinaryArray* /*object*/)
{
}
void TiffFinder::init(uint16_t tag, IfdId group)
{
tag_ = tag;
group_ = group;
tiffComponent_ = nullptr;
setGo(geTraverse, true);
}
void TiffFinder::findObject(TiffComponent* object)
{
if (object->tag() == tag_ && object->group() == group_) {
tiffComponent_ = object;
setGo(geTraverse, false);
}
}
void TiffFinder::visitEntry(TiffEntry* object)
{
findObject(object);
}
void TiffFinder::visitDataEntry(TiffDataEntry* object)
{
findObject(object);
}
void TiffFinder::visitImageEntry(TiffImageEntry* object)
{
findObject(object);
}
void TiffFinder::visitSizeEntry(TiffSizeEntry* object)
{
findObject(object);
}
void TiffFinder::visitDirectory(TiffDirectory* object)
{
findObject(object);
}
void TiffFinder::visitSubIfd(TiffSubIfd* object)
{
findObject(object);
}
void TiffFinder::visitMnEntry(TiffMnEntry* object)
{
findObject(object);
}
void TiffFinder::visitIfdMakernote(TiffIfdMakernote* object)
{
findObject(object);
}
void TiffFinder::visitBinaryArray(TiffBinaryArray* object)
{
findObject(object);
}
void TiffFinder::visitBinaryElement(TiffBinaryElement* object)
{
findObject(object);
}
TiffCopier::TiffCopier( TiffComponent* pRoot,
uint32_t root,
const TiffHeaderBase* pHeader,
const PrimaryGroups* pPrimaryGroups)
: pRoot_(pRoot),
root_(root),
pHeader_(pHeader),
pPrimaryGroups_(pPrimaryGroups)
{
assert(pRoot_ != 0);
assert(pHeader_ != 0);
assert(pPrimaryGroups_ != 0);
}
void TiffCopier::copyObject(TiffComponent* object)
{
assert(object != 0);
if (pHeader_->isImageTag(object->tag(), object->group(), pPrimaryGroups_)) {
TiffComponent::UniquePtr clone = object->clone();
// Assumption is that the corresponding TIFF entry doesn't exist
TiffPath tiffPath;
TiffCreator::getPath(tiffPath, object->tag(), object->group(), root_);
pRoot_->addPath(object->tag(), tiffPath, pRoot_, std::move(clone));
#ifdef EXIV2_DEBUG_MESSAGES
ExifKey key(object->tag(), groupName(object->group()));
std::cerr << "Copied " << key << "\n";
#endif
}
}
void TiffCopier::visitEntry(TiffEntry* object)
{
copyObject(object);
}
void TiffCopier::visitDataEntry(TiffDataEntry* object)
{
copyObject(object);
}
void TiffCopier::visitImageEntry(TiffImageEntry* object)
{
copyObject(object);
}
void TiffCopier::visitSizeEntry(TiffSizeEntry* object)
{
copyObject(object);
}
void TiffCopier::visitDirectory(TiffDirectory* /*object*/)
{
// Do not copy directories (avoids problems with SubIfds)
}
void TiffCopier::visitSubIfd(TiffSubIfd* object)
{
copyObject(object);
}
void TiffCopier::visitMnEntry(TiffMnEntry* object)
{
copyObject(object);
}
void TiffCopier::visitIfdMakernote(TiffIfdMakernote* object)
{
copyObject(object);
}
void TiffCopier::visitBinaryArray(TiffBinaryArray* object)
{
copyObject(object);
}
void TiffCopier::visitBinaryElement(TiffBinaryElement* object)
{
copyObject(object);
}
TiffDecoder::TiffDecoder(
ExifData& exifData,
IptcData& iptcData,
XmpData& xmpData,
TiffComponent* const pRoot,
FindDecoderFct findDecoderFct
)
: exifData_(exifData),
iptcData_(iptcData),
xmpData_(xmpData),
pRoot_(pRoot),
findDecoderFct_(findDecoderFct),
decodedIptc_(false)
{
assert(pRoot != 0);
// #1402 Fujifilm RAF. Search for the make
// Find camera make in existing metadata (read from the JPEG)
ExifKey key("Exif.Image.Make");
if ( exifData_.findKey(key) != exifData_.end( ) ){
make_ = exifData_.findKey(key)->toString();
} else {
// Find camera make by looking for tag 0x010f in IFD0
TiffFinder finder(0x010f, ifd0Id);
pRoot_->accept(finder);
auto te = dynamic_cast<TiffEntryBase*>(finder.result());
if (te && te->pValue()) {
make_ = te->pValue()->toString();
}
}
}
void TiffDecoder::visitEntry(TiffEntry* object)
{
decodeTiffEntry(object);
}
void TiffDecoder::visitDataEntry(TiffDataEntry* object)
{
decodeTiffEntry(object);
}
void TiffDecoder::visitImageEntry(TiffImageEntry* object)
{
decodeTiffEntry(object);
}
void TiffDecoder::visitSizeEntry(TiffSizeEntry* object)
{
decodeTiffEntry(object);
}
void TiffDecoder::visitDirectory(TiffDirectory* /* object */ )
{
// Nothing to do
}
void TiffDecoder::visitSubIfd(TiffSubIfd* object)
{
decodeTiffEntry(object);
}
void TiffDecoder::visitMnEntry(TiffMnEntry* object)
{
// Always decode binary makernote tag
decodeTiffEntry(object);
}
void TiffDecoder::visitIfdMakernote(TiffIfdMakernote* object)
{
assert(object != 0);
exifData_["Exif.MakerNote.Offset"] = object->mnOffset();
switch (object->byteOrder()) {
case littleEndian:
exifData_["Exif.MakerNote.ByteOrder"] = "II";
break;
case bigEndian:
exifData_["Exif.MakerNote.ByteOrder"] = "MM";
break;
case invalidByteOrder:
assert(object->byteOrder() != invalidByteOrder);
break;
}
}
void TiffDecoder::getObjData(byte const*& pData,
long& size,
uint16_t tag,
IfdId group,
const TiffEntryBase* object)
{
if (object && object->tag() == tag && object->group() == group) {
pData = object->pData();
size = object->size();
return;
}
TiffFinder finder(tag, group);
pRoot_->accept(finder);
TiffEntryBase const* te = dynamic_cast<TiffEntryBase*>(finder.result());
if (te) {
pData = te->pData();
size = te->size();
return;
}
}
void TiffDecoder::decodeXmp(const TiffEntryBase* object)
{
// add Exif tag anyway
decodeStdTiffEntry(object);
byte const* pData = nullptr;
long size = 0;
getObjData(pData, size, 0x02bc, ifd0Id, object);
if (pData) {
std::string xmpPacket;
xmpPacket.assign(reinterpret_cast<const char*>(pData), size);
std::string::size_type idx = xmpPacket.find_first_of('<');
if (idx != std::string::npos && idx > 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Removing " << static_cast<unsigned long>(idx)
<< " characters from the beginning of the XMP packet\n";
#endif
xmpPacket = xmpPacket.substr(idx);
}
if (XmpParser::decode(xmpData_, xmpPacket)) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to decode XMP metadata.\n";
#endif
}
}
} // TiffDecoder::decodeXmp
void TiffDecoder::decodeIptc(const TiffEntryBase* object)
{
// add Exif tag anyway
decodeStdTiffEntry(object);
// All tags are read at this point, so the first time we come here,
// find the relevant IPTC tag and decode IPTC if found
if (decodedIptc_) {
return;
}
decodedIptc_ = true;
// 1st choice: IPTCNAA
byte const* pData = nullptr;
long size = 0;
getObjData(pData, size, 0x83bb, ifd0Id, object);
if (pData) {
if (0 == IptcParser::decode(iptcData_, pData, size)) {
return;
}
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to decode IPTC block found in "
<< "Directory Image, entry 0x83bb\n";
#endif
}
// 2nd choice if no IPTCNAA record found or failed to decode it:
// ImageResources
pData = nullptr;
size = 0;
getObjData(pData, size, 0x8649, ifd0Id, object);
if (pData) {
byte const* record = nullptr;
uint32_t sizeHdr = 0;
uint32_t sizeData = 0;
if (0 != Photoshop::locateIptcIrb(pData, size,
&record, &sizeHdr, &sizeData)) {
return;
}
if (0 == IptcParser::decode(iptcData_, record + sizeHdr, sizeData)) {
return;
}
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to decode IPTC block found in "
<< "Directory Image, entry 0x8649\n";
#endif
}
} // TiffMetadataDecoder::decodeIptc
static const TagInfo* findTag(const TagInfo* pList,uint16_t tag)
{
while ( pList->tag_ != 0xffff && pList->tag_ != tag ) pList++;
return pList->tag_ != 0xffff ? pList : nullptr;
}
void TiffDecoder::decodeCanonAFInfo(const TiffEntryBase* object) {
// report Exif.Canon.AFInfo as usual
TiffDecoder::decodeStdTiffEntry(object);
if ( object->pValue()->count() < 3 || object->pValue()->typeId() != unsignedShort ) return; // insufficient data
// create vector of signedShorts from unsignedShorts in Exif.Canon.AFInfo
std::vector<int16_t> ints;
std::vector<uint16_t> uint;
for (long i = 0; i < object->pValue()->count(); i++) {
ints.push_back(static_cast<int16_t>(object->pValue()->toLong(i)));
uint.push_back(static_cast<uint16_t>(object->pValue()->toLong(i)));
}
// Check this is AFInfo2 (ints[0] = bytes in object)
if ( ints.at(0) != object->pValue()->count()*2 ) return ;
std::string familyGroup(std::string("Exif.") + groupName(object->group()) + ".");
const uint16_t nPoints = uint.at(2);
const uint16_t nMasks = (nPoints+15)/(sizeof(uint16_t) * 8);
int nStart = 0;
struct {
uint16_t tag ;
uint16_t size ;
bool bSigned;
} records[] = {
{ 0x2600 , 1 , true }, // AFInfoSize
{ 0x2601 , 1 , true }, // AFAreaMode
{ 0x2602 , 1 , true }, // AFNumPoints
{ 0x2603 , 1 , true }, // AFValidPoints
{ 0x2604 , 1 , true }, // AFCanonImageWidth
{ 0x2605 , 1 , true }, // AFCanonImageHeight
{ 0x2606 , 1 , true }, // AFImageWidth"
{ 0x2607 , 1 , true }, // AFImageHeight
{ 0x2608 , nPoints , true }, // AFAreaWidths
{ 0x2609 , nPoints , true }, // AFAreaHeights
{ 0x260a , nPoints , true }, // AFXPositions
{ 0x260b , nPoints , true }, // AFYPositions
{ 0x260c , nMasks , false }, // AFPointsInFocus
{ 0x260d , nMasks , false }, // AFPointsSelected
{ 0x260e , nMasks , false }, // AFPointsUnusable
};
// check we have enough data!
uint16_t count = 0;
for (auto&& record : records) {
count += record.size;
if (count > ints.size())
return;
}
for (auto&& record : records) {
const TagInfo* pTags = ExifTags::tagList("Canon") ;
const TagInfo* pTag = findTag(pTags, record.tag);
if ( pTag ) {
auto v = Exiv2::Value::create(record.bSigned ? Exiv2::signedShort : Exiv2::unsignedShort);
std::ostringstream s;
if (record.bSigned) {
for (uint16_t k = 0; k < record.size; k++)
s << " " << ints.at(nStart++);
} else {
for (uint16_t k = 0; k < record.size; k++)
s << " " << uint.at(nStart++);
}
v->read(s.str());
exifData_[familyGroup + pTag->name_] = *v;
}
}
}
void TiffDecoder::decodeTiffEntry(const TiffEntryBase* object)
{
assert(object != 0);
// Don't decode the entry if value is not set
if (!object->pValue()) return;
const DecoderFct decoderFct = findDecoderFct_(make_,
object->tag(),
object->group());
// skip decoding if decoderFct == 0
if (decoderFct) {
EXV_CALL_MEMBER_FN(*this, decoderFct)(object);
}
} // TiffDecoder::decodeTiffEntry
void TiffDecoder::decodeStdTiffEntry(const TiffEntryBase* object)
{
assert(object != 0);
ExifKey key(object->tag(), groupName(object->group()));
key.setIdx(object->idx());
exifData_.add(key, object->pValue());
} // TiffDecoder::decodeTiffEntry
void TiffDecoder::visitBinaryArray(TiffBinaryArray* object)
{
if (object->cfg() == nullptr || !object->decoded()) {
decodeTiffEntry(object);
}
}
void TiffDecoder::visitBinaryElement(TiffBinaryElement* object)
{
decodeTiffEntry(object);
}
TiffEncoder::TiffEncoder(ExifData exifData, const IptcData& iptcData, const XmpData& xmpData, TiffComponent* pRoot,
const bool isNewImage, const PrimaryGroups* pPrimaryGroups, const TiffHeaderBase* pHeader,
FindEncoderFct findEncoderFct)
: exifData_(std::move(exifData)),
iptcData_(iptcData),
xmpData_(xmpData),
del_(true),
pHeader_(pHeader),
pRoot_(pRoot),
isNewImage_(isNewImage),
pPrimaryGroups_(pPrimaryGroups),
pSourceTree_(nullptr),
findEncoderFct_(findEncoderFct),
dirty_(false),
writeMethod_(wmNonIntrusive)
{
assert(pRoot != 0);
assert(pPrimaryGroups != 0);
assert(pHeader != 0);
byteOrder_ = pHeader->byteOrder();
origByteOrder_ = byteOrder_;
encodeIptc();
encodeXmp();
// Find camera make
ExifKey key("Exif.Image.Make");
auto pos = exifData_.findKey(key);
if (pos != exifData_.end()) {
make_ = pos->toString();
}
if (make_.empty() && pRoot_) {
TiffFinder finder(0x010f, ifd0Id);
pRoot_->accept(finder);
auto te = dynamic_cast<TiffEntryBase*>(finder.result());
if (te && te->pValue()) {
make_ = te->pValue()->toString();
}
}
}
void TiffEncoder::encodeIptc()
{
// Update IPTCNAA Exif tag, if it exists. Delete the tag if there
// is no IPTC data anymore.
// If there is new IPTC data and Exif.Image.ImageResources does
// not exist, create a new IPTCNAA Exif tag.
bool del = false;
ExifKey iptcNaaKey("Exif.Image.IPTCNAA");
auto pos = exifData_.findKey(iptcNaaKey);
if (pos != exifData_.end()) {
iptcNaaKey.setIdx(pos->idx());
exifData_.erase(pos);
del = true;
}
DataBuf rawIptc = IptcParser::encode(iptcData_);
ExifKey irbKey("Exif.Image.ImageResources");
pos = exifData_.findKey(irbKey);
if (pos != exifData_.end()) {
irbKey.setIdx(pos->idx());
}
if (rawIptc.size() != 0 && (del || pos == exifData_.end())) {
Value::UniquePtr value = Value::create(unsignedLong);
DataBuf buf;
if (rawIptc.size() % 4 != 0) {
// Pad the last unsignedLong value with 0s
buf.alloc((rawIptc.size() / 4) * 4 + 4);
buf.clear();
buf.copyBytes(0, rawIptc.c_data(), rawIptc.size());
}
else {
buf = std::move(rawIptc); // Note: This resets rawIptc
}
value->read(buf.data(), buf.size(), byteOrder_);
Exifdatum iptcDatum(iptcNaaKey, value.get());
exifData_.add(iptcDatum);
pos = exifData_.findKey(irbKey); // needed after add()
}
// Also update IPTC IRB in Exif.Image.ImageResources if it exists,
// but don't create it if not.
if (pos != exifData_.end()) {
DataBuf irbBuf(pos->value().size());
pos->value().copy(irbBuf.data(), invalidByteOrder);
irbBuf = Photoshop::setIptcIrb(irbBuf.c_data(), irbBuf.size(), iptcData_);
exifData_.erase(pos);
if (irbBuf.size() != 0) {
Value::UniquePtr value = Value::create(unsignedByte);
value->read(irbBuf.data(), irbBuf.size(), invalidByteOrder);
Exifdatum iptcDatum(irbKey, value.get());
exifData_.add(iptcDatum);
}
}
} // TiffEncoder::encodeIptc
void TiffEncoder::encodeXmp()
{
#ifdef EXV_HAVE_XMP_TOOLKIT
ExifKey xmpKey("Exif.Image.XMLPacket");
// Remove any existing XMP Exif tag
auto pos = exifData_.findKey(xmpKey);
if (pos != exifData_.end()) {
xmpKey.setIdx(pos->idx());
exifData_.erase(pos);
}
std::string xmpPacket;
if ( xmpData_.usePacket() ) {
xmpPacket = xmpData_.xmpPacket();
} else {
if (XmpParser::encode(xmpPacket, xmpData_) > 1) {
#ifndef SUPPRESS_WARNINGS
EXV_ERROR << "Failed to encode XMP metadata.\n";
#endif
}
}
if (!xmpPacket.empty()) {
// Set the XMP Exif tag to the new value
Value::UniquePtr value = Value::create(unsignedByte);
value->read(reinterpret_cast<const byte*>(&xmpPacket[0]),
static_cast<long>(xmpPacket.size()),
invalidByteOrder);
Exifdatum xmpDatum(xmpKey, value.get());
exifData_.add(xmpDatum);
}
#endif
} // TiffEncoder::encodeXmp
void TiffEncoder::setDirty(bool flag)
{
dirty_ = flag;
setGo(geTraverse, !flag);
}
bool TiffEncoder::dirty() const
{
return dirty_ || exifData_.count() > 0;
}
void TiffEncoder::visitEntry(TiffEntry* object)
{
encodeTiffComponent(object);
}
void TiffEncoder::visitDataEntry(TiffDataEntry* object)
{
encodeTiffComponent(object);
}
void TiffEncoder::visitImageEntry(TiffImageEntry* object)
{
encodeTiffComponent(object);
}
void TiffEncoder::visitSizeEntry(TiffSizeEntry* object)
{
encodeTiffComponent(object);
}
void TiffEncoder::visitDirectory(TiffDirectory* /*object*/)
{
// Nothing to do
}
void TiffEncoder::visitDirectoryNext(TiffDirectory* object)
{
// Update type and count in IFD entries, in case they changed
assert(object != 0);
byte* p = object->start() + 2;
for (auto&& component : object->components_) {
p += updateDirEntry(p, byteOrder(), component);
}
}
uint32_t TiffEncoder::updateDirEntry(byte* buf, ByteOrder byteOrder, TiffComponent* pTiffComponent)
{
assert(buf);
assert(pTiffComponent);
auto pTiffEntry = dynamic_cast<TiffEntryBase*>(pTiffComponent);
assert(pTiffEntry);
us2Data(buf + 2, pTiffEntry->tiffType(), byteOrder);
ul2Data(buf + 4, pTiffEntry->count(), byteOrder);
// Move data to offset field, if it fits and is not yet there.
if (pTiffEntry->size() <= 4 && buf + 8 != pTiffEntry->pData()) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Copying data for tag " << pTiffEntry->tag()
<< " to offset area.\n";
#endif
memset(buf + 8, 0x0, 4);
if (pTiffEntry->size() > 0) {
memcpy(buf + 8, pTiffEntry->pData(), pTiffEntry->size());
memset(const_cast<byte*>(pTiffEntry->pData()), 0x0, pTiffEntry->size());
}
}
return 12;
}
void TiffEncoder::visitSubIfd(TiffSubIfd* object)
{
encodeTiffComponent(object);
}
void TiffEncoder::visitMnEntry(TiffMnEntry* object)
{
// Test is required here as well as in the callback encoder function
if (!object->mn_) {
encodeTiffComponent(object);
}
else if (del_) {
// The makernote is made up of decoded tags, delete binary tag
ExifKey key(object->tag(), groupName(object->group()));
auto pos = exifData_.findKey(key);
if (pos != exifData_.end()) exifData_.erase(pos);
}
}
void TiffEncoder::visitIfdMakernote(TiffIfdMakernote* object)
{
assert(object != 0);
auto pos = exifData_.findKey(ExifKey("Exif.MakerNote.ByteOrder"));
if (pos != exifData_.end()) {
// Set Makernote byte order
ByteOrder bo = stringToByteOrder(pos->toString());
if (bo != invalidByteOrder && bo != object->byteOrder()) {
object->setByteOrder(bo);
setDirty();
}
if (del_) exifData_.erase(pos);
}
if (del_) {
// Remove remaining synthesized tags
static const char* synthesizedTags[] = {
"Exif.MakerNote.Offset",
};
for (auto&& synthesizedTag : synthesizedTags) {
pos = exifData_.findKey(ExifKey(synthesizedTag));
if (pos != exifData_.end()) exifData_.erase(pos);
}
}
// Modify encoder for Makernote peculiarities, byte order
byteOrder_ = object->byteOrder();
} // TiffEncoder::visitIfdMakernote
void TiffEncoder::visitIfdMakernoteEnd(TiffIfdMakernote* /*object*/)
{
// Reset byte order back to that from the c'tor
byteOrder_ = origByteOrder_;
} // TiffEncoder::visitIfdMakernoteEnd
void TiffEncoder::visitBinaryArray(TiffBinaryArray* object)
{
if (object->cfg() == nullptr || !object->decoded()) {
encodeTiffComponent(object);
}
}
void TiffEncoder::visitBinaryArrayEnd(TiffBinaryArray* object)
{
assert(object != 0);
if (object->cfg() == nullptr || !object->decoded())
return;
int32_t size = object->TiffEntryBase::doSize();
if (size == 0) return;
if (!object->initialize(pRoot_)) return;
// Re-encrypt buffer if necessary
CryptFct cryptFct = object->cfg()->cryptFct_;
if ( cryptFct == sonyTagDecipher ) {
cryptFct = sonyTagEncipher;
}
if (cryptFct != nullptr) {
const byte* pData = object->pData();
DataBuf buf = cryptFct(object->tag(), pData, size, pRoot_);
if (buf.size() > 0) {
pData = buf.c_data();
size = buf.size();
}
if (!object->updOrigDataBuf(pData, size)) {
setDirty();
}
}
}
void TiffEncoder::visitBinaryElement(TiffBinaryElement* object)
{
// Temporarily overwrite byteorder according to that of the binary element
ByteOrder boOrig = byteOrder_;
if (object->elByteOrder() != invalidByteOrder) byteOrder_ = object->elByteOrder();
encodeTiffComponent(object);
byteOrder_ = boOrig;
}
bool TiffEncoder::isImageTag(uint16_t tag, IfdId group) const
{
return !isNewImage_ && pHeader_->isImageTag(tag, group, pPrimaryGroups_);
}
void TiffEncoder::encodeTiffComponent(
TiffEntryBase* object,
const Exifdatum* datum
)
{
assert(object != 0);
auto pos = exifData_.end();
const Exifdatum* ed = datum;
if (ed == nullptr) {
// Non-intrusive writing: find matching tag
ExifKey key(object->tag(), groupName(object->group()));
pos = exifData_.findKey(key);
if (pos != exifData_.end()) {
ed = &(*pos);
if (object->idx() != pos->idx()) {
// Try to find exact match (in case of duplicate tags)
auto pos2 =
std::find_if(exifData_.begin(), exifData_.end(),
FindExifdatum2(object->group(), object->idx()));
if (pos2 != exifData_.end() && pos2->key() == key.key()) {
ed = &(*pos2);
pos = pos2; // make sure we delete the correct tag below
}
}
}
else {
setDirty();
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "DELETING " << key << ", idx = " << object->idx() << "\n";
#endif
}
} else {
// For intrusive writing, the index is used to preserve the order of
// duplicate tags
object->idx_ = ed->idx();
}
// Skip encoding image tags of existing TIFF image - they were copied earlier -
// but encode image tags of new images (creation)
if (ed && !isImageTag(object->tag(), object->group())) {
const EncoderFct fct = findEncoderFct_(make_, object->tag(), object->group());
if (fct) {
// If an encoding function is registered for the tag, use it
EXV_CALL_MEMBER_FN(*this, fct)(object, ed);
}
else {
// Else use the encode function at the object (results in a double-dispatch
// to the appropriate encoding function of the encoder.
object->encode(*this, ed);
}
}
if (del_ && pos != exifData_.end()) {
exifData_.erase(pos);
}
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "\n";
#endif
} // TiffEncoder::encodeTiffComponent
void TiffEncoder::encodeBinaryArray(TiffBinaryArray* object, const Exifdatum* datum)
{
encodeOffsetEntry(object, datum);
} // TiffEncoder::encodeBinaryArray
void TiffEncoder::encodeBinaryElement(TiffBinaryElement* object, const Exifdatum* datum)
{
encodeTiffEntryBase(object, datum);
} // TiffEncoder::encodeArrayElement
void TiffEncoder::encodeDataEntry(TiffDataEntry* object, const Exifdatum* datum)
{
encodeOffsetEntry(object, datum);
if (!dirty_ && writeMethod() == wmNonIntrusive) {
assert(object);
assert(object->pValue());
if ( object->sizeDataArea_
< static_cast<uint32_t>(object->pValue()->sizeDataArea())) {
#ifdef EXIV2_DEBUG_MESSAGES
ExifKey key(object->tag(), groupName(object->group()));
std::cerr << "DATAAREA GREW " << key << "\n";
#endif
setDirty();
}
else {
// Write the new dataarea, fill with 0x0
#ifdef EXIV2_DEBUG_MESSAGES
ExifKey key(object->tag(), groupName(object->group()));
std::cerr << "Writing data area for " << key << "\n";
#endif
DataBuf buf = object->pValue()->dataArea();
if ( buf.size() > 0 ) {
memcpy(object->pDataArea_, buf.c_data(), buf.size());
if (object->sizeDataArea_ > static_cast<size_t>(buf.size())) {
memset(object->pDataArea_ + buf.size(),
0x0, object->sizeDataArea_ - buf.size());
}
}
}
}
} // TiffEncoder::encodeDataEntry
void TiffEncoder::encodeTiffEntry(TiffEntry* object, const Exifdatum* datum)
{
encodeTiffEntryBase(object, datum);
} // TiffEncoder::encodeTiffEntry
void TiffEncoder::encodeImageEntry(TiffImageEntry* object, const Exifdatum* datum)
{
encodeOffsetEntry(object, datum);
uint32_t sizeDataArea = object->pValue()->sizeDataArea();
if (sizeDataArea > 0 && writeMethod() == wmNonIntrusive) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "\t DATAAREA IS SET (NON-INTRUSIVE WRITING)";
#endif
setDirty();
}
if (sizeDataArea > 0 && writeMethod() == wmIntrusive) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "\t DATAAREA IS SET (INTRUSIVE WRITING)";
#endif
// Set pseudo strips (without a data pointer) from the size tag
ExifKey key(object->szTag(), groupName(object->szGroup()));
auto pos = exifData_.findKey(key);
const byte* zero = nullptr;
if (pos == exifData_.end()) {
#ifndef SUPPRESS_WARNINGS
EXV_ERROR << "Size tag " << key
<< " not found. Writing only one strip.\n";
#endif
object->strips_.clear();
object->strips_.emplace_back(zero, sizeDataArea);
}
else {
uint32_t sizeTotal = 0;
object->strips_.clear();
for (long i = 0; i < pos->count(); ++i) {
uint32_t len = pos->toLong(i);