From b953a4e9da72035964ed8ecfe0e4844325d6afe3 Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Wed, 25 Jan 2006 06:48:44 +0000 Subject: [PATCH] Added fixiso action. Implements feature #450. --- src/actions.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/actions.hpp | 20 +++++++++++++++- src/exiv2.1 | 5 ++++ src/exiv2.cpp | 11 +++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/actions.cpp b/src/actions.cpp index 7c4834ad..47b007e7 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -178,6 +178,7 @@ namespace Action { registerTask(extract, Task::AutoPtr(new Extract)); registerTask(insert, Task::AutoPtr(new Insert)); registerTask(modify, Task::AutoPtr(new Modify)); + registerTask(fixiso, Task::AutoPtr(new FixIso)); } // TaskFactory c'tor Task::AutoPtr TaskFactory::create(TaskType type) @@ -1252,6 +1253,67 @@ namespace Action { return 0; } // Adjust::adjustDateTime + int FixIso::run(const std::string& path) + try { + if (!Exiv2::fileExists(path, true)) { + std::cerr << path + << ": Failed to open the file\n"; + return -1; + } + Timestamp ts; + if (Params::instance().preserve_) { + ts.read(path); + } + Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path); + assert(image.get() != 0); + image->readMetadata(); + Exiv2::ExifData &exifData = image->exifData(); + if (exifData.empty()) { + std::cerr << path + << ": No Exif data found in the file\n"; + return -3; + } + Exiv2::ExifKey key("Exif.Nikon3.ISOSpeed"); + Exiv2::ExifData::iterator md = exifData.findKey(key); + if (md == exifData.end()) { + key = Exiv2::ExifKey("Exif.Nikon2.ISOSpeed"); + md = exifData.findKey(key); + } + if (md == exifData.end()) { + key = Exiv2::ExifKey("Exif.Nikon1.ISOSpeed"); + md = exifData.findKey(key); + } + if (md != exifData.end()) { + std::ostringstream os; + os << *md; + if (Params::instance().verbose_) { + std::cout << "Setting Exif ISO value to " << os.str() << "\n"; + } + exifData["Exif.Photo.ISOSpeedRatings"] = os.str(); + } + image->writeMetadata(); + if (Params::instance().preserve_) { + ts.touch(path); + } + return 0; + } + catch(const Exiv2::AnyError& e) + { + std::cerr << "Exiv2 exception in fixiso action for file " << path + << ":\n" << e << "\n"; + return 1; + } // FixIso::run + + FixIso::AutoPtr FixIso::clone() const + { + return AutoPtr(clone_()); + } + + FixIso* FixIso::clone_() const + { + return new FixIso(*this); + } + } // namespace Action // ***************************************************************************** diff --git a/src/actions.hpp b/src/actions.hpp index e12c5c21..83b29a85 100644 --- a/src/actions.hpp +++ b/src/actions.hpp @@ -58,7 +58,8 @@ namespace Exiv2 { namespace Action { //! Enumerates all tasks - enum TaskType { none, adjust, print, rename, erase, extract, insert, modify }; + enum TaskType { none, adjust, print, rename, erase, extract, insert, + modify, fixiso }; // ***************************************************************************** // class definitions @@ -327,6 +328,23 @@ namespace Action { Exiv2::Image::AutoPtr image_; //!< Image to modify }; // class Modify + /*! + @brief %Copy ISO settings from any of the Nikon makernotes to the + regular Exif tag, Exif.Photo.ISOSpeedRatings. + */ + class FixIso : public Task { + public: + virtual ~FixIso() {} + virtual int run(const std::string& path); + typedef std::auto_ptr AutoPtr; + AutoPtr clone() const; + + private: + virtual FixIso* clone_() const; + std::string path_; + + }; // class FixIso + } // namespace Action #endif // #ifndef ACTIONS_HPP_ diff --git a/src/exiv2.1 b/src/exiv2.1 index 694d244f..40f437a1 100644 --- a/src/exiv2.1 +++ b/src/exiv2.1 @@ -62,6 +62,11 @@ files. Requires option \fB\-c\fP, \fB\-m\fP or \fB\-M\fP. Rename files and/or set file timestamps according to the EXIF create timestamp. The filename format can be set with \fB\-r\fP \fIfmt\fP, timestamp options are \fB\-t\fP and \fB\-T\fP. +.TP +.B fi | fixiso +Copy the ISO setting from any of the proprietary Nikon makernote ISO +tags to the regular Exif ISO tag, Exif.Photo.ISOSpeedRatings. Overwrites +an existing Exif ISO tag. .SH OPTIONS .TP .B \-h diff --git a/src/exiv2.cpp b/src/exiv2.cpp index 1a35b91e..b8c6108c 100644 --- a/src/exiv2.cpp +++ b/src/exiv2.cpp @@ -212,6 +212,8 @@ void Params::help(std::ostream& os) const << " mo | modify Apply commands to modify (add, set, delete) the Exif and\n" << " Iptc metadata of image files or set the Jpeg comment.\n" << " Requires option -c, -m or -M.\n" + << " fi | fixiso Copy ISO setting from the Nikon Makernote to the regular\n" + << " Exif tag.\n" << "\nOptions:\n" << " -h Display this help and exit.\n" << " -V Show the program version and exit.\n" @@ -557,6 +559,15 @@ int Params::nonoption(const std::string& argv) action = true; action_ = Action::modify; } + if (argv == "fi" || argv == "fixiso") { + if (action_ != Action::none && action_ != Action::fixiso) { + std::cerr << progname() << ": Action fixiso is not " + << "compatible with the given options\n"; + rc = 1; + } + action = true; + action_ = Action::fixiso; + } if (action_ == Action::none) { // if everything else fails, assume print as the default action action_ = Action::print;