Changed option -eX to write XMP sidecar file (corresponding insert not done yet).

v0.27.3
Andreas Huggel 17 years ago
parent f8a4d46103
commit ef14bf1c31

@ -44,6 +44,7 @@ EXIV2_RCSID("@(#) $Id$")
#include "exiv2.hpp" #include "exiv2.hpp"
#include "image.hpp" #include "image.hpp"
#include "jpgimage.hpp" #include "jpgimage.hpp"
#include "xmpsidecar.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "types.hpp" #include "types.hpp"
#include "exif.hpp" #include "exif.hpp"
@ -1020,10 +1021,11 @@ namespace Action {
if (Params::instance().target_ & Params::ctThumb) { if (Params::instance().target_ & Params::ctThumb) {
rc = writeThumbnail(); rc = writeThumbnail();
} }
if (Params::instance().target_ & Params::ctXmpPacket) { if (Params::instance().target_ & Params::ctXmpSidecar) {
rc = writeXmp(); rc = writeXmpSidecar();
} }
if (Params::instance().target_ & ~Params::ctThumb & ~Params::ctXmpPacket) { if ( !(Params::instance().target_ & Params::ctXmpSidecar)
&& !(Params::instance().target_ & Params::ctThumb)) {
std::string exvPath = newFilePath(path_, ".exv"); std::string exvPath = newFilePath(path_, ".exv");
if (dontOverwrite(exvPath)) return 0; if (dontOverwrite(exvPath)) return 0;
rc = metacopy(path_, exvPath, false); rc = metacopy(path_, exvPath, false);
@ -1037,7 +1039,7 @@ namespace Action {
return 1; return 1;
} // Extract::run } // Extract::run
int Extract::writeXmp() const int Extract::writeXmpSidecar() const
{ {
if (!Exiv2::fileExists(path_, true)) { if (!Exiv2::fileExists(path_, true)) {
std::cerr << path_ std::cerr << path_
@ -1047,26 +1049,32 @@ namespace Action {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_); Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path_);
assert(image.get() != 0); assert(image.get() != 0);
image->readMetadata(); image->readMetadata();
const std::string& xmpPacket = image->xmpPacket();
if (xmpPacket.empty()) { std::string sidecarPath = newFilePath(path_, ".xmp");
return -3; if (dontOverwrite(sidecarPath)) return 0;
}
std::string xmpPath = newFilePath(path_, ".xmp"); // Apply any modification commands to the source image on-the-fly
if (dontOverwrite(xmpPath)) return 0; Action::Modify::applyCommands(image.get());
Exiv2::Image::AutoPtr xmpSidecar =
Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, sidecarPath);
assert(xmpSidecar.get() != 0);
if (Params::instance().verbose_) { if (Params::instance().verbose_) {
std::cout << _("Writing XMP packet from") << " " << path_ std::cout << _("Writing XMP sidecar file ") << " "
<< " " << _("to") << " " << xmpPath << std::endl; << sidecarPath << std::endl;
} }
std::ofstream file(xmpPath.c_str()); if (Params::instance().target_ & Params::ctExif) {
if (!file) { xmpSidecar->setExifData(image->exifData());
std::cerr << Params::instance().progname() << ": " }
<< _("Failed to open file ") << " " << xmpPath << ": " if (Params::instance().target_ & Params::ctIptc) {
<< Exiv2::strError() << "\n"; xmpSidecar->setIptcData(image->iptcData());
return 1; }
if (Params::instance().target_ & Params::ctXmp) {
xmpSidecar->setXmpData(image->xmpData());
} }
file << xmpPacket; xmpSidecar->writeMetadata();
return 0; return 0;
} // Extract::writeXmp } // Extract::writeXmpSidecar
int Extract::writeThumbnail() const int Extract::writeThumbnail() const
{ {
@ -1143,7 +1151,7 @@ namespace Action {
std::string exvPath = newFilePath(path, suffix); std::string exvPath = newFilePath(path, suffix);
rc = metacopy(exvPath, path, true); rc = metacopy(exvPath, path, true);
} }
if (0 == rc && Params::instance().target_ & Params::ctXmpPacket) { if (0 == rc && Params::instance().target_ & Params::ctXmpSidecar) {
rc = insertXmpPacket(path); rc = insertXmpPacket(path);
} }
if (Params::instance().preserve_) { if (Params::instance().preserve_) {

@ -278,8 +278,8 @@ namespace Action {
on the format of the Exif thumbnail image. on the format of the Exif thumbnail image.
*/ */
int writeThumbnail() const; int writeThumbnail() const;
//! Write the XMP packet to a file. //! Write an XMP sidecar file.
int writeXmp() const; int writeXmpSidecar() const;
private: private:
virtual Extract* clone_() const; virtual Extract* clone_() const;

@ -840,9 +840,9 @@ namespace {
case 'e': target |= Params::ctExif; break; case 'e': target |= Params::ctExif; break;
case 'i': target |= Params::ctIptc; break; case 'i': target |= Params::ctIptc; break;
case 'x': target |= Params::ctXmp; break; case 'x': target |= Params::ctXmp; break;
case 'X': target |= Params::ctXmpPacket; break;
case 'c': target |= Params::ctComment; break; case 'c': target |= Params::ctComment; break;
case 't': target |= Params::ctThumb; break; case 't': target |= Params::ctThumb; break;
case 'X': target |= Params::ctXmpSidecar; // fall-through
case 'a': target |= Params::ctExif case 'a': target |= Params::ctExif
| Params::ctIptc | Params::ctIptc
| Params::ctComment | Params::ctComment

@ -141,7 +141,15 @@ public:
}; };
//! Enumerates common targets, bitmap //! Enumerates common targets, bitmap
enum CommonTarget { ctExif = 1, ctIptc = 2, ctComment = 4, ctThumb = 8, ctXmp = 16, ctXmpPacket = 32 }; enum CommonTarget {
ctExif = 1,
ctIptc = 2,
ctComment = 4,
ctThumb = 8,
ctXmp = 16,
ctXmpSidecar = 32
};
//! Enumerates the policies to handle existing files in rename action //! Enumerates the policies to handle existing files in rename action
enum FileExistsPolicy { overwritePolicy, renamePolicy, askPolicy }; enum FileExistsPolicy { overwritePolicy, renamePolicy, askPolicy };

@ -43,6 +43,7 @@ EXIV2_RCSID("@(#) $Id$")
#include "error.hpp" #include "error.hpp"
#include "xmp.hpp" #include "xmp.hpp"
#include "futils.hpp" #include "futils.hpp"
#include "convert.hpp"
// + standard includes // + standard includes
#include <string> #include <string>
@ -53,22 +54,19 @@ EXIV2_RCSID("@(#) $Id$")
// class member definitions // class member definitions
namespace Exiv2 { namespace Exiv2 {
XmpSidecar::XmpSidecar(BasicIo::AutoPtr io) const char* XmpSidecar::xmlHeader_ = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
: Image(ImageType::xmp, mdXmp, io) const long XmpSidecar::xmlHdrCnt_ = 39;
{
} // XmpSidecar::XmpSidecar
void XmpSidecar::setExifData(const ExifData& /*exifData*/) XmpSidecar::XmpSidecar(BasicIo::AutoPtr io, bool create)
: Image(ImageType::xmp, mdXmp, io)
{ {
// Todo: implement me! if (create) {
throw(Error(32, "Exif metadata", "XMP")); if (io_->open() == 0) {
IoCloser closer(*io_);
io_->write(reinterpret_cast<const byte*>(xmlHeader_), xmlHdrCnt_);
} }
void XmpSidecar::setIptcData(const IptcData& /*iptcData*/)
{
// Todo: implement me!
throw(Error(32, "IPTC metadata", "XMP"));
} }
} // XmpSidecar::XmpSidecar
void XmpSidecar::setComment(const std::string& /*comment*/) void XmpSidecar::setComment(const std::string& /*comment*/)
{ {
@ -106,6 +104,8 @@ namespace Exiv2 {
std::cerr << "Warning: Failed to decode XMP metadata.\n"; std::cerr << "Warning: Failed to decode XMP metadata.\n";
#endif #endif
} }
copyXmpToIptc(xmpData_, iptcData_);
copyXmpToExif(xmpData_, exifData_);
} // XmpSidecar::readMetadata } // XmpSidecar::readMetadata
void XmpSidecar::writeMetadata() void XmpSidecar::writeMetadata()
@ -116,7 +116,10 @@ namespace Exiv2 {
IoCloser closer(*io_); IoCloser closer(*io_);
if (writeXmpFromPacket() == false) { if (writeXmpFromPacket() == false) {
if (XmpParser::encode(xmpPacket_, xmpData_, XmpParser::omitPacketWrapper|XmpParser::useCompactFormat)) { copyExifToXmp(exifData_, xmpData_);
copyIptcToXmp(iptcData_, xmpData_);
if (XmpParser::encode(xmpPacket_, xmpData_,
XmpParser::omitPacketWrapper|XmpParser::useCompactFormat)) {
#ifndef SUPPRESS_WARNINGS #ifndef SUPPRESS_WARNINGS
std::cerr << "Error: Failed to encode XMP metadata.\n"; std::cerr << "Error: Failed to encode XMP metadata.\n";
#endif #endif
@ -124,7 +127,7 @@ namespace Exiv2 {
} }
if (xmpPacket_.size() > 0) { if (xmpPacket_.size() > 0) {
if (xmpPacket_.substr(0, 5) != "<?xml") { if (xmpPacket_.substr(0, 5) != "<?xml") {
xmpPacket_ = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmpPacket_; xmpPacket_ = xmlHeader_ + xmpPacket_;
} }
BasicIo::AutoPtr tempIo(io_->temporary()); // may throw BasicIo::AutoPtr tempIo(io_->temporary()); // may throw
assert(tempIo.get() != 0); assert(tempIo.get() != 0);
@ -140,9 +143,9 @@ namespace Exiv2 {
// ************************************************************************* // *************************************************************************
// free functions // free functions
Image::AutoPtr newXmpInstance(BasicIo::AutoPtr io, bool /*create*/) Image::AutoPtr newXmpInstance(BasicIo::AutoPtr io, bool create)
{ {
Image::AutoPtr image(new XmpSidecar(io)); Image::AutoPtr image(new XmpSidecar(io, create));
if (!image->good()) { if (!image->good()) {
image.reset(); image.reset();
} }

@ -66,24 +66,16 @@ namespace Exiv2 {
auto-pointer. Callers should not continue to use the BasicIo auto-pointer. Callers should not continue to use the BasicIo
instance after it is passed to this method. Use the Image::io() instance after it is passed to this method. Use the Image::io()
method to get a temporary reference. method to get a temporary reference.
@param create Specifies if an existing image should be read (false)
or if a new image should be created (true).
*/ */
XmpSidecar(BasicIo::AutoPtr io); XmpSidecar(BasicIo::AutoPtr io, bool create);
//@} //@}
//! @name Manipulators //! @name Manipulators
//@{ //@{
void readMetadata(); void readMetadata();
void writeMetadata(); void writeMetadata();
/*!
@brief Todo: Not supported yet, requires conversion from Exif to XMP.
Calling this function will throw an instance of Error(32).
*/
void setExifData(const ExifData& exifData);
/*!
@brief Todo: Not supported yet, requires conversion from IPTC to XMP.
Calling this function will throw an instance of Error(32).
*/
void setIptcData(const IptcData& iptcData);
/*! /*!
@brief Not supported. XMP sidecar files do not contain a comment. @brief Not supported. XMP sidecar files do not contain a comment.
Calling this function will throw an instance of Error(32). Calling this function will throw an instance of Error(32).
@ -105,6 +97,10 @@ namespace Exiv2 {
XmpSidecar& operator=(const XmpSidecar& rhs); XmpSidecar& operator=(const XmpSidecar& rhs);
//@} //@}
// DATA
static const char* xmlHeader_;
static const long xmlHdrCnt_;
}; // class XmpSidecar }; // class XmpSidecar
// ***************************************************************************** // *****************************************************************************

Loading…
Cancel
Save