metadata: better handling of makernote tags

Tentative fix for #37

(cherry picked from commit 7310eb64978bb1138edbdf02fa58fb64a9326e17)
This commit is contained in:
Alberto Griggio 2020-04-05 05:04:45 -07:00 committed by Lawrence Lee
parent dcc00b40d1
commit c1719fbd7d
No known key found for this signature in database
GPG Key ID: 048FF2B76A63895F
2 changed files with 30 additions and 38 deletions

View File

@ -37,6 +37,17 @@ std::unique_ptr<Exiv2Metadata::ImageCache> Exiv2Metadata::cache_(nullptr);
namespace { namespace {
class Error: public Exiv2::AnyError {
public:
Error(const std::string &msg): msg_(msg) {}
const char *what() const throw() { return msg_.c_str(); }
int code() const throw() { return 0; }
private:
std::string msg_;
};
constexpr size_t IMAGE_CACHE_SIZE = 200; constexpr size_t IMAGE_CACHE_SIZE = 200;
Exiv2::Image::AutoPtr open_exiv2(const Glib::ustring& fname) Exiv2::Image::AutoPtr open_exiv2(const Glib::ustring& fname)
@ -206,11 +217,14 @@ void Exiv2Metadata::saveToImage(const Glib::ustring &path) const
{ {
auto dst = open_exiv2(path); auto dst = open_exiv2(path);
if (image_.get()) { if (image_.get()) {
dst->setMetadata(*image_); dst->setIptcData(image_->iptcData());
dst->setXmpData(image_->xmpData());
if (merge_xmp_) { if (merge_xmp_) {
do_merge_xmp(dst.get()); do_merge_xmp(dst.get());
} }
remove_unwanted(dst.get()); auto srcexif = image_->exifData();
remove_unwanted(srcexif);
dst->setExifData(srcexif);
} else { } else {
dst->setExifData(exif_data_); dst->setExifData(exif_data_);
dst->setIptcData(iptc_data_); dst->setIptcData(iptc_data_);
@ -224,42 +238,29 @@ void Exiv2Metadata::saveToImage(const Glib::ustring &path) const
} }
void Exiv2Metadata::remove_unwanted(Exiv2::Image *dst) const void Exiv2Metadata::remove_unwanted(Exiv2::ExifData &dst) const
{ {
static const std::vector<std::string> keys = { Exiv2::ExifThumb thumb(dst);
"Exif.Image.Orientation", thumb.erase();
"Exif.Image2.JPEGInterchangeFormat",
"Exif.Image2.JPEGInterchangeFormatLength", static const std::vector<std::string> badpatterns = {
"Exif.Photo.MakerNote"
};
for (auto &k : keys) {
auto it = dst->exifData().findKey(Exiv2::ExifKey(k));
if (it != dst->exifData().end()) {
dst->exifData().erase(it);
}
}
static const std::vector<std::string> patterns = {
"Exif.Image.", "Exif.Image.",
"Exif.Photo.", "Exif.SubImage"
"Exif.GPSInfo."
}; };
for (auto it = dst->exifData().begin(); it != dst->exifData().end(); ) {
for (auto it = dst.begin(); it != dst.end(); ) {
bool found = false; bool found = false;
for (auto &pp : patterns) { for (auto &p : badpatterns) {
if (it->key().find(pp) == 0) { if (it->key().find(p) == 0) {
it = dst.erase(it);
found = true; found = true;
break; break;
} }
} }
if (!found) { if (!found) {
it = dst->exifData().erase(it);
} else {
++it; ++it;
} }
} }
Exiv2::ExifThumb thumb(dst->exifData());
thumb.erase();
} }
@ -320,15 +321,6 @@ void Exiv2Metadata::saveToXmp(const Glib::ustring &path) const
} }
} }
class Error: public Exiv2::AnyError {
public:
Error(const std::string &msg): msg_(msg) {}
const char *what() const throw() { return msg_.c_str(); }
int code() const throw() { return 0; }
private:
std::string msg_;
};
if (err) { if (err) {
throw Error("error saving XMP sidecar " + path); throw Error("error saving XMP sidecar " + path);
} }

View File

@ -65,7 +65,7 @@ private:
void do_merge_xmp(Exiv2::Image* dst) const; void do_merge_xmp(Exiv2::Image* dst) const;
void import_exif_pairs(Exiv2::ExifData& out) const; void import_exif_pairs(Exiv2::ExifData& out) const;
void import_iptc_pairs(Exiv2::IptcData& out) const; void import_iptc_pairs(Exiv2::IptcData& out) const;
void remove_unwanted(Exiv2::Image* dst) const; void remove_unwanted(Exiv2::ExifData& dst) const;
Glib::ustring src_; Glib::ustring src_;
bool merge_xmp_; bool merge_xmp_;