Skip to content

Commit a031bc8

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

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/tiffvisitor_int.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ void TiffDecoder::visitBinaryElement(TiffBinaryElement* object) {
447447
TiffEncoder::TiffEncoder(ExifData& exifData, IptcData& iptcData, XmpData& xmpData, TiffComponent* pRoot,
448448
bool isNewImage, PrimaryGroups pPrimaryGroups, const TiffHeaderBase* pHeader,
449449
FindEncoderFct findEncoderFct) :
450-
exifData_(exifData),
450+
exifData_(std::make_unique<ExifData>(exifData)),
451451
iptcData_(iptcData),
452452
xmpData_(xmpData),
453453
pHeader_(pHeader),
@@ -462,7 +462,7 @@ TiffEncoder::TiffEncoder(ExifData& exifData, IptcData& iptcData, XmpData& xmpDat
462462

463463
// Find camera make
464464
ExifKey key("Exif.Image.Make");
465-
if (auto pos = exifData_.findKey(key); pos != exifData_.end()) {
465+
if (auto pos = exifData_->findKey(key); pos != exifData_->end()) {
466466
make_ = pos->toString();
467467
}
468468
if (make_.empty() && pRoot_) {
@@ -475,26 +475,28 @@ TiffEncoder::TiffEncoder(ExifData& exifData, IptcData& iptcData, XmpData& xmpDat
475475
}
476476
}
477477

478+
TiffEncoder::~TiffEncoder() = default;
479+
478480
void TiffEncoder::encodeIptc() {
479481
// Update IPTCNAA Exif tag, if it exists. Delete the tag if there
480482
// is no IPTC data anymore.
481483
// If there is new IPTC data and Exif.Image.ImageResources does
482484
// not exist, create a new IPTCNAA Exif tag.
483485
bool del = false;
484486
ExifKey iptcNaaKey("Exif.Image.IPTCNAA");
485-
auto pos = exifData_.findKey(iptcNaaKey);
486-
if (pos != exifData_.end()) {
487+
auto pos = exifData_->findKey(iptcNaaKey);
488+
if (pos != exifData_->end()) {
487489
iptcNaaKey.setIdx(pos->idx());
488-
exifData_.erase(pos);
490+
exifData_->erase(pos);
489491
del = true;
490492
}
491493
DataBuf rawIptc = IptcParser::encode(iptcData_);
492494
ExifKey irbKey("Exif.Image.ImageResources");
493-
pos = exifData_.findKey(irbKey);
494-
if (pos != exifData_.end()) {
495+
pos = exifData_->findKey(irbKey);
496+
if (pos != exifData_->end()) {
495497
irbKey.setIdx(pos->idx());
496498
}
497-
if (!rawIptc.empty() && (del || pos == exifData_.end())) {
499+
if (!rawIptc.empty() && (del || pos == exifData_->end())) {
498500
auto value = Value::create(unsignedLong);
499501
DataBuf buf;
500502
if (rawIptc.size() % 4 != 0) {
@@ -506,21 +508,21 @@ void TiffEncoder::encodeIptc() {
506508
}
507509
value->read(buf.data(), buf.size(), byteOrder_);
508510
Exifdatum iptcDatum(iptcNaaKey, value.get());
509-
exifData_.add(iptcDatum);
510-
pos = exifData_.findKey(irbKey); // needed after add()
511+
exifData_->add(iptcDatum);
512+
pos = exifData_->findKey(irbKey); // needed after add()
511513
}
512514
// Also update IPTC IRB in Exif.Image.ImageResources if it exists,
513515
// but don't create it if not.
514-
if (pos != exifData_.end()) {
516+
if (pos != exifData_->end()) {
515517
DataBuf irbBuf(pos->value().size());
516518
pos->value().copy(irbBuf.data(), invalidByteOrder);
517519
irbBuf = Photoshop::setIptcIrb(irbBuf.c_data(), irbBuf.size(), iptcData_);
518-
exifData_.erase(pos);
520+
exifData_->erase(pos);
519521
if (!irbBuf.empty()) {
520522
auto value = Value::create(unsignedByte);
521523
value->read(irbBuf.data(), irbBuf.size(), invalidByteOrder);
522524
Exifdatum iptcDatum(irbKey, value.get());
523-
exifData_.add(iptcDatum);
525+
exifData_->add(iptcDatum);
524526
}
525527
}
526528
} // TiffEncoder::encodeIptc
@@ -529,9 +531,9 @@ void TiffEncoder::encodeXmp() {
529531
#ifdef EXV_HAVE_XMP_TOOLKIT
530532
ExifKey xmpKey("Exif.Image.XMLPacket");
531533
// Remove any existing XMP Exif tag
532-
if (auto pos = exifData_.findKey(xmpKey); pos != exifData_.end()) {
534+
if (auto pos = exifData_->findKey(xmpKey); pos != exifData_->end()) {
533535
xmpKey.setIdx(pos->idx());
534-
exifData_.erase(pos);
536+
exifData_->erase(pos);
535537
}
536538
std::string xmpPacket;
537539
if (xmpData_.usePacket()) {
@@ -548,7 +550,7 @@ void TiffEncoder::encodeXmp() {
548550
auto value = Value::create(unsignedByte);
549551
value->read(reinterpret_cast<const byte*>(xmpPacket.data()), xmpPacket.size(), invalidByteOrder);
550552
Exifdatum xmpDatum(xmpKey, value.get());
551-
exifData_.add(xmpDatum);
553+
exifData_->add(xmpDatum);
552554
}
553555
#endif
554556
} // TiffEncoder::encodeXmp
@@ -559,7 +561,7 @@ void TiffEncoder::setDirty(bool flag) {
559561
}
560562

561563
bool TiffEncoder::dirty() const {
562-
return dirty_ || !exifData_.empty();
564+
return dirty_ || !exifData_->empty();
563565
}
564566

565567
void TiffEncoder::visitEntry(TiffEntry* object) {
@@ -621,33 +623,33 @@ void TiffEncoder::visitMnEntry(TiffMnEntry* object) {
621623
} else if (del_) {
622624
// The makernote is made up of decoded tags, delete binary tag
623625
ExifKey key(object->tag(), groupName(object->group()));
624-
auto pos = exifData_.findKey(key);
625-
if (pos != exifData_.end())
626-
exifData_.erase(pos);
626+
auto pos = exifData_->findKey(key);
627+
if (pos != exifData_->end())
628+
exifData_->erase(pos);
627629
}
628630
}
629631

630632
void TiffEncoder::visitIfdMakernote(TiffIfdMakernote* object) {
631-
auto pos = exifData_.findKey(ExifKey("Exif.MakerNote.ByteOrder"));
632-
if (pos != exifData_.end()) {
633+
auto pos = exifData_->findKey(ExifKey("Exif.MakerNote.ByteOrder"));
634+
if (pos != exifData_->end()) {
633635
// Set Makernote byte order
634636
ByteOrder bo = stringToByteOrder(pos->toString());
635637
if (bo != invalidByteOrder && bo != object->byteOrder()) {
636638
object->setByteOrder(bo);
637639
setDirty();
638640
}
639641
if (del_)
640-
exifData_.erase(pos);
642+
exifData_->erase(pos);
641643
}
642644
if (del_) {
643645
// Remove remaining synthesized tags
644646
static constexpr auto synthesizedTags = std::array{
645647
"Exif.MakerNote.Offset",
646648
};
647649
for (auto synthesizedTag : synthesizedTags) {
648-
pos = exifData_.findKey(ExifKey(synthesizedTag));
649-
if (pos != exifData_.end())
650-
exifData_.erase(pos);
650+
pos = exifData_->findKey(ExifKey(synthesizedTag));
651+
if (pos != exifData_->end())
652+
exifData_->erase(pos);
651653
}
652654
}
653655
// Modify encoder for Makernote peculiarities, byte order
@@ -708,18 +710,18 @@ bool TiffEncoder::isImageTag(uint16_t tag, IfdId group) const {
708710
}
709711

710712
void TiffEncoder::encodeTiffComponent(TiffEntryBase* object, const Exifdatum* datum) {
711-
auto pos = exifData_.end();
713+
auto pos = exifData_->end();
712714
const Exifdatum* ed = datum;
713715
if (!ed) {
714716
// Non-intrusive writing: find matching tag
715717
ExifKey key(object->tag(), groupName(object->group()));
716-
pos = exifData_.findKey(key);
717-
if (pos != exifData_.end()) {
718+
pos = exifData_->findKey(key);
719+
if (pos != exifData_->end()) {
718720
ed = &(*pos);
719721
if (object->idx() != pos->idx()) {
720722
// Try to find exact match (in case of duplicate tags)
721-
auto pos2 = std::find_if(exifData_.begin(), exifData_.end(), FindExifdatum2(object->group(), object->idx()));
722-
if (pos2 != exifData_.end() && pos2->key() == key.key()) {
723+
auto pos2 = std::find_if(exifData_->begin(), exifData_->end(), FindExifdatum2(object->group(), object->idx()));
724+
if (pos2 != exifData_->end() && pos2->key() == key.key()) {
723725
ed = &(*pos2);
724726
pos = pos2; // make sure we delete the correct tag below
725727
}
@@ -747,8 +749,8 @@ void TiffEncoder::encodeTiffComponent(TiffEntryBase* object, const Exifdatum* da
747749
object->encode(*this, ed);
748750
}
749751
}
750-
if (del_ && pos != exifData_.end()) {
751-
exifData_.erase(pos);
752+
if (del_ && pos != exifData_->end()) {
753+
exifData_->erase(pos);
752754
}
753755
#ifdef EXIV2_DEBUG_MESSAGES
754756
std::cerr << "\n";
@@ -813,9 +815,9 @@ void TiffEncoder::encodeImageEntry(TiffImageEntry* object, const Exifdatum* datu
813815
#endif
814816
// Set pseudo strips (without a data pointer) from the size tag
815817
ExifKey key(object->szTag(), groupName(object->szGroup()));
816-
auto pos = exifData_.findKey(key);
818+
auto pos = exifData_->findKey(key);
817819
const byte* zero = nullptr;
818-
if (pos == exifData_.end()) {
820+
if (pos == exifData_->end()) {
819821
#ifndef SUPPRESS_WARNINGS
820822
EXV_ERROR << "Size tag " << key << " not found. Writing only one strip.\n";
821823
#endif
@@ -924,8 +926,8 @@ void TiffEncoder::add(TiffComponent* pRootDir, TiffComponent::UniquePtr pSourceD
924926
// iterate over all remaining entries.
925927
del_ = false;
926928

927-
auto posBo = exifData_.end();
928-
for (auto i = exifData_.begin(); i != exifData_.end(); ++i) {
929+
auto posBo = exifData_->end();
930+
for (auto i = exifData_->begin(); i != exifData_->end(); ++i) {
929931
IfdId group = groupId(i->groupName());
930932
// Skip synthesized info tags
931933
if (group == IfdId::mnId) {
@@ -962,7 +964,7 @@ void TiffEncoder::add(TiffComponent* pRootDir, TiffComponent::UniquePtr pSourceD
962964
visit/encodeIfdMakernote is not called in this case and there
963965
can't be an Exif tag which corresponds to this component.
964966
*/
965-
if (posBo == exifData_.end())
967+
if (posBo == exifData_->end())
966968
return;
967969

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

src/tiffvisitor_int.hpp

Lines changed: 2 additions & 4 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

@@ -354,7 +352,7 @@ class TiffEncoder : public TiffVisitor {
354352
TiffEncoder(const TiffEncoder&) = delete;
355353
TiffEncoder& operator=(const TiffEncoder&) = delete;
356354
//! Virtual destructor
357-
~TiffEncoder() override = default;
355+
~TiffEncoder() override;
358356
//@}
359357

360358
//! @name Manipulators
@@ -508,7 +506,7 @@ class TiffEncoder : public TiffVisitor {
508506
//@}
509507

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

0 commit comments

Comments
 (0)