Skip to content

Commit c0e1f8b

Browse files
committed
use unique_ptr for ExifData
Avoids needing a definition in header. Signed-off-by: Rosen Penev <[email protected]>
1 parent a8e65f4 commit c0e1f8b

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

src/tiffimage_int.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ WriteMethod TiffParserWorker::encode(BasicIo& io, const byte* pData, size_t size
20712071
auto primaryGroups = findPrimaryGroups(parsedTree);
20722072
if (parsedTree) {
20732073
// Attempt to update existing TIFF components based on metadata entries
2074-
TiffEncoder encoder(exifData, iptcData, xmpData, parsedTree.get(), false, primaryGroups, pHeader, findEncoderFct);
2074+
TiffEncoder encoder(std::make_unique<ExifData>(exifData), iptcData, xmpData, parsedTree.get(), false, primaryGroups, pHeader, findEncoderFct);
20752075
parsedTree->accept(encoder);
20762076
if (!encoder.dirty())
20772077
writeMethod = wmNonIntrusive;
@@ -2084,7 +2084,7 @@ WriteMethod TiffParserWorker::encode(BasicIo& io, const byte* pData, size_t size
20842084
parsedTree->accept(copier);
20852085
}
20862086
// Add entries from metadata to composite
2087-
TiffEncoder encoder(exifData, iptcData, xmpData, createdTree.get(), !parsedTree, std::move(primaryGroups), pHeader,
2087+
TiffEncoder encoder(std::make_unique<ExifData>(exifData), iptcData, xmpData, createdTree.get(), !parsedTree, std::move(primaryGroups), pHeader,
20882088
findEncoderFct);
20892089
encoder.add(createdTree.get(), std::move(parsedTree), root);
20902090
// Write binary representation from the composite tree

src/tiffvisitor_int.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ void TiffDecoder::visitBinaryElement(TiffBinaryElement* object) {
440440
decodeTiffEntry(object);
441441
}
442442

443-
TiffEncoder::TiffEncoder(ExifData exifData, const IptcData& iptcData, const XmpData& xmpData, TiffComponent* pRoot,
443+
TiffEncoder::TiffEncoder(std::unique_ptr<ExifData> exifData, const IptcData& iptcData, const XmpData& xmpData, TiffComponent* pRoot,
444444
bool isNewImage, PrimaryGroups pPrimaryGroups, const TiffHeaderBase* pHeader,
445445
FindEncoderFct findEncoderFct) :
446446
exifData_(std::move(exifData)),
@@ -458,7 +458,7 @@ TiffEncoder::TiffEncoder(ExifData exifData, const IptcData& iptcData, const XmpD
458458

459459
// Find camera make
460460
ExifKey key("Exif.Image.Make");
461-
if (auto pos = exifData_.findKey(key); pos != exifData_.end()) {
461+
if (auto pos = exifData_->findKey(key); pos != exifData_->end()) {
462462
make_ = pos->toString();
463463
}
464464
if (make_.empty() && pRoot_) {
@@ -471,26 +471,28 @@ TiffEncoder::TiffEncoder(ExifData exifData, const IptcData& iptcData, const XmpD
471471
}
472472
}
473473

474+
TiffEncoder::~TiffEncoder() = default;
475+
474476
void TiffEncoder::encodeIptc() {
475477
// Update IPTCNAA Exif tag, if it exists. Delete the tag if there
476478
// is no IPTC data anymore.
477479
// If there is new IPTC data and Exif.Image.ImageResources does
478480
// not exist, create a new IPTCNAA Exif tag.
479481
bool del = false;
480482
ExifKey iptcNaaKey("Exif.Image.IPTCNAA");
481-
auto pos = exifData_.findKey(iptcNaaKey);
482-
if (pos != exifData_.end()) {
483+
auto pos = exifData_->findKey(iptcNaaKey);
484+
if (pos != exifData_->end()) {
483485
iptcNaaKey.setIdx(pos->idx());
484-
exifData_.erase(pos);
486+
exifData_->erase(pos);
485487
del = true;
486488
}
487489
DataBuf rawIptc = IptcParser::encode(iptcData_);
488490
ExifKey irbKey("Exif.Image.ImageResources");
489-
pos = exifData_.findKey(irbKey);
490-
if (pos != exifData_.end()) {
491+
pos = exifData_->findKey(irbKey);
492+
if (pos != exifData_->end()) {
491493
irbKey.setIdx(pos->idx());
492494
}
493-
if (!rawIptc.empty() && (del || pos == exifData_.end())) {
495+
if (!rawIptc.empty() && (del || pos == exifData_->end())) {
494496
auto value = Value::create(unsignedLong);
495497
DataBuf buf;
496498
if (rawIptc.size() % 4 != 0) {
@@ -502,21 +504,21 @@ void TiffEncoder::encodeIptc() {
502504
}
503505
value->read(buf.data(), buf.size(), byteOrder_);
504506
Exifdatum iptcDatum(iptcNaaKey, value.get());
505-
exifData_.add(iptcDatum);
506-
pos = exifData_.findKey(irbKey); // needed after add()
507+
exifData_->add(iptcDatum);
508+
pos = exifData_->findKey(irbKey); // needed after add()
507509
}
508510
// Also update IPTC IRB in Exif.Image.ImageResources if it exists,
509511
// but don't create it if not.
510-
if (pos != exifData_.end()) {
512+
if (pos != exifData_->end()) {
511513
DataBuf irbBuf(pos->value().size());
512514
pos->value().copy(irbBuf.data(), invalidByteOrder);
513515
irbBuf = Photoshop::setIptcIrb(irbBuf.c_data(), irbBuf.size(), iptcData_);
514-
exifData_.erase(pos);
516+
exifData_->erase(pos);
515517
if (!irbBuf.empty()) {
516518
auto value = Value::create(unsignedByte);
517519
value->read(irbBuf.data(), irbBuf.size(), invalidByteOrder);
518520
Exifdatum iptcDatum(irbKey, value.get());
519-
exifData_.add(iptcDatum);
521+
exifData_->add(iptcDatum);
520522
}
521523
}
522524
} // TiffEncoder::encodeIptc
@@ -525,9 +527,9 @@ void TiffEncoder::encodeXmp() {
525527
#ifdef EXV_HAVE_XMP_TOOLKIT
526528
ExifKey xmpKey("Exif.Image.XMLPacket");
527529
// Remove any existing XMP Exif tag
528-
if (auto pos = exifData_.findKey(xmpKey); pos != exifData_.end()) {
530+
if (auto pos = exifData_->findKey(xmpKey); pos != exifData_->end()) {
529531
xmpKey.setIdx(pos->idx());
530-
exifData_.erase(pos);
532+
exifData_->erase(pos);
531533
}
532534
std::string xmpPacket;
533535
if (xmpData_.usePacket()) {
@@ -544,7 +546,7 @@ void TiffEncoder::encodeXmp() {
544546
auto value = Value::create(unsignedByte);
545547
value->read(reinterpret_cast<const byte*>(xmpPacket.data()), xmpPacket.size(), invalidByteOrder);
546548
Exifdatum xmpDatum(xmpKey, value.get());
547-
exifData_.add(xmpDatum);
549+
exifData_->add(xmpDatum);
548550
}
549551
#endif
550552
} // TiffEncoder::encodeXmp
@@ -555,7 +557,7 @@ void TiffEncoder::setDirty(bool flag) {
555557
}
556558

557559
bool TiffEncoder::dirty() const {
558-
return dirty_ || !exifData_.empty();
560+
return dirty_ || !exifData_->empty();
559561
}
560562

561563
void TiffEncoder::visitEntry(TiffEntry* object) {
@@ -617,33 +619,33 @@ void TiffEncoder::visitMnEntry(TiffMnEntry* object) {
617619
} else if (del_) {
618620
// The makernote is made up of decoded tags, delete binary tag
619621
ExifKey key(object->tag(), groupName(object->group()));
620-
auto pos = exifData_.findKey(key);
621-
if (pos != exifData_.end())
622-
exifData_.erase(pos);
622+
auto pos = exifData_->findKey(key);
623+
if (pos != exifData_->end())
624+
exifData_->erase(pos);
623625
}
624626
}
625627

626628
void TiffEncoder::visitIfdMakernote(TiffIfdMakernote* object) {
627-
auto pos = exifData_.findKey(ExifKey("Exif.MakerNote.ByteOrder"));
628-
if (pos != exifData_.end()) {
629+
auto pos = exifData_->findKey(ExifKey("Exif.MakerNote.ByteOrder"));
630+
if (pos != exifData_->end()) {
629631
// Set Makernote byte order
630632
ByteOrder bo = stringToByteOrder(pos->toString());
631633
if (bo != invalidByteOrder && bo != object->byteOrder()) {
632634
object->setByteOrder(bo);
633635
setDirty();
634636
}
635637
if (del_)
636-
exifData_.erase(pos);
638+
exifData_->erase(pos);
637639
}
638640
if (del_) {
639641
// Remove remaining synthesized tags
640642
static constexpr auto synthesizedTags = std::array{
641643
"Exif.MakerNote.Offset",
642644
};
643645
for (auto synthesizedTag : synthesizedTags) {
644-
pos = exifData_.findKey(ExifKey(synthesizedTag));
645-
if (pos != exifData_.end())
646-
exifData_.erase(pos);
646+
pos = exifData_->findKey(ExifKey(synthesizedTag));
647+
if (pos != exifData_->end())
648+
exifData_->erase(pos);
647649
}
648650
}
649651
// Modify encoder for Makernote peculiarities, byte order
@@ -704,18 +706,18 @@ bool TiffEncoder::isImageTag(uint16_t tag, IfdId group) const {
704706
}
705707

706708
void TiffEncoder::encodeTiffComponent(TiffEntryBase* object, const Exifdatum* datum) {
707-
auto pos = exifData_.end();
709+
auto pos = exifData_->end();
708710
const Exifdatum* ed = datum;
709711
if (!ed) {
710712
// Non-intrusive writing: find matching tag
711713
ExifKey key(object->tag(), groupName(object->group()));
712-
pos = exifData_.findKey(key);
713-
if (pos != exifData_.end()) {
714+
pos = exifData_->findKey(key);
715+
if (pos != exifData_->end()) {
714716
ed = &(*pos);
715717
if (object->idx() != pos->idx()) {
716718
// Try to find exact match (in case of duplicate tags)
717-
auto pos2 = std::find_if(exifData_.begin(), exifData_.end(), FindExifdatum2(object->group(), object->idx()));
718-
if (pos2 != exifData_.end() && pos2->key() == key.key()) {
719+
auto pos2 = std::find_if(exifData_->begin(), exifData_->end(), FindExifdatum2(object->group(), object->idx()));
720+
if (pos2 != exifData_->end() && pos2->key() == key.key()) {
719721
ed = &(*pos2);
720722
pos = pos2; // make sure we delete the correct tag below
721723
}
@@ -743,8 +745,8 @@ void TiffEncoder::encodeTiffComponent(TiffEntryBase* object, const Exifdatum* da
743745
object->encode(*this, ed);
744746
}
745747
}
746-
if (del_ && pos != exifData_.end()) {
747-
exifData_.erase(pos);
748+
if (del_ && pos != exifData_->end()) {
749+
exifData_->erase(pos);
748750
}
749751
#ifdef EXIV2_DEBUG_MESSAGES
750752
std::cerr << "\n";
@@ -809,9 +811,9 @@ void TiffEncoder::encodeImageEntry(TiffImageEntry* object, const Exifdatum* datu
809811
#endif
810812
// Set pseudo strips (without a data pointer) from the size tag
811813
ExifKey key(object->szTag(), groupName(object->szGroup()));
812-
auto pos = exifData_.findKey(key);
814+
auto pos = exifData_->findKey(key);
813815
const byte* zero = nullptr;
814-
if (pos == exifData_.end()) {
816+
if (pos == exifData_->end()) {
815817
#ifndef SUPPRESS_WARNINGS
816818
EXV_ERROR << "Size tag " << key << " not found. Writing only one strip.\n";
817819
#endif
@@ -920,8 +922,8 @@ void TiffEncoder::add(TiffComponent* pRootDir, TiffComponent::UniquePtr pSourceD
920922
// iterate over all remaining entries.
921923
del_ = false;
922924

923-
auto posBo = exifData_.end();
924-
for (auto i = exifData_.begin(); i != exifData_.end(); ++i) {
925+
auto posBo = exifData_->end();
926+
for (auto i = exifData_->begin(); i != exifData_->end(); ++i) {
925927
IfdId group = groupId(i->groupName());
926928
// Skip synthesized info tags
927929
if (group == IfdId::mnId) {
@@ -958,7 +960,7 @@ void TiffEncoder::add(TiffComponent* pRootDir, TiffComponent::UniquePtr pSourceD
958960
visit/encodeIfdMakernote is not called in this case and there
959961
can't be an Exif tag which corresponds to this component.
960962
*/
961-
if (posBo == exifData_.end())
963+
if (posBo == exifData_->end())
962964
return;
963965

964966
TiffFinder finder(0x927c, IfdId::exifId);

src/tiffvisitor_int.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#define TIFFVISITOR_INT_HPP_
55

66
// *****************************************************************************
7-
// included header files
8-
#include "exif.hpp"
97

108
#include "tiffcomposite_int.hpp"
119

@@ -349,13 +347,13 @@ class TiffEncoder : public TiffVisitor {
349347
to, the image with the metadata to encode and a function to
350348
find special encoders.
351349
*/
352-
TiffEncoder(ExifData exifData, const IptcData& iptcData, const XmpData& xmpData, TiffComponent* pRoot,
350+
TiffEncoder(std::unique_ptr<ExifData> exifData, const IptcData& iptcData, const XmpData& xmpData, TiffComponent* pRoot,
353351
bool isNewImage, PrimaryGroups pPrimaryGroups, const TiffHeaderBase* pHeader,
354352
FindEncoderFct findEncoderFct);
355353
TiffEncoder(const TiffEncoder&) = delete;
356354
TiffEncoder& operator=(const TiffEncoder&) = delete;
357355
//! Virtual destructor
358-
~TiffEncoder() override = default;
356+
~TiffEncoder() override;
359357
//@}
360358

361359
//! @name Manipulators
@@ -509,7 +507,7 @@ class TiffEncoder : public TiffVisitor {
509507
//@}
510508

511509
// DATA
512-
ExifData exifData_; //!< Copy of the Exif data to encode
510+
std::unique_ptr<ExifData> exifData_; //!< Copy of the Exif data to encode
513511
const IptcData& iptcData_; //!< IPTC data to encode, just a reference
514512
const XmpData& xmpData_; //!< XMP data to encode, just a reference
515513
bool del_{true}; //!< Indicates if Exif data entries should be deleted after encoding

0 commit comments

Comments
 (0)