Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README-SAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,39 @@ Read an XMP packet from a file, parse and re-serialize it.



### Encoding an XMP sidecar before writing the destination

`ImageFactory::create(type, path)` creates or overwrites the file immediately.
Applications exporting a new sidecar can instead use the existing in-memory
factory, then transfer the completed packet to the destination:

```cpp
auto sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp);
sidecar->setExifData(source.exifData());
sidecar->setIptcData(source.iptcData());
sidecar->setXmpData(source.xmpData());
sidecar->writeMetadata(); // Converts and encodes entirely in memory; may throw.

Exiv2::FileIo destination(path);
destination.transfer(sidecar->io()); // Only reached after successful encoding.
```

Handle errors from both operations in the calling application. Conversion or
encoding failure leaves an existing destination unchanged and a new destination
absent. Invalid text is reported as an error; no encoding is guessed and no
property is dropped to recover from the error. The final transfer uses the normal
file-writing behavior: an I/O failure can leave a partially written destination.
This sequence does not provide atomic replacement or power-loss protection.

Use `ImageFactory::open(path)` and `readMetadata()` when editing an existing
sidecar and retaining its metadata. `writeMetadata()` also stages that packet in
memory before transferring it, so encoding failure preserves the existing bytes.

Applications such as digiKam that currently create a filesystem sidecar before
encoding must change their call sequence to gain this protection. The immediate
creation contract is unchanged; no new factory mode is required. The CLI uses
in-memory creation for new sidecar exports and opens existing sidecars normally.

Robin Mills<br>
robin@clanmills.com<br>
Revised: 2021-09-21
9 changes: 8 additions & 1 deletion app/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,11 +1685,14 @@ int metacopy(const std::string& source, const std::string& tgt, Exiv2::ImageType
std::string target(bStdout ? temporaryPath() : tgt);

std::unique_ptr<Exiv2::Image> targetImage;
bool stageXmp = false;
if (Exiv2::fileExists(target)) {
targetImage = Exiv2::ImageFactory::open(target);
targetImage->readMetadata();
} else {
targetImage = Exiv2::ImageFactory::create(targetType, target);
// Finish XMP conversion and encoding before creating the destination.
stageXmp = targetType == Exiv2::ImageType::xmp;
targetImage = stageXmp ? Exiv2::ImageFactory::create(targetType) : Exiv2::ImageFactory::create(targetType, target);
}

// Copy each type of metadata
Expand Down Expand Up @@ -1763,6 +1766,10 @@ int metacopy(const std::string& source, const std::string& tgt, Exiv2::ImageType
if (rc < 0)
try {
targetImage->writeMetadata();
if (stageXmp) {
Exiv2::FileIo destination(target);
destination.transfer(targetImage->io());
}
rc = 0;
} catch (const Exiv2::Error& e) {
std::cerr << tgt << ": " << _("Could not write metadata to file") << ": " << e << "\n";
Expand Down
13 changes: 11 additions & 2 deletions include/exiv2/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ class XmpData;
// *****************************************************************************
// free functions, template and inline definitions

//! Convert (copy) Exif tags to XMP properties.
/*!
@brief Convert (copy) Exif tags to XMP properties.
@note DeviceSettingDescription retains its native byte order when parsed and
copied as Exif data. Manually supplied raw values with ambiguous byte
order cannot be converted safely and leave the destination unchanged.
*/
EXIV2API void copyExifToXmp(const ExifData& exifData, XmpData& xmpData);
//! Convert (move) Exif tags to XMP properties, remove converted Exif tags.
EXIV2API void moveExifToXmp(ExifData& exifData, XmpData& xmpData);

//! Convert (copy) XMP properties to Exif tags.
/*!
@brief Convert (copy) XMP properties to Exif tags.
@note DeviceSettings text must be representable as UCS-2. Invalid dimensions
or unrepresentable text leave that destination tag unchanged.
*/
EXIV2API void copyXmpToExif(const XmpData& xmpData, ExifData& exifData);
//! Convert (move) XMP properties to Exif tags, remove converted XMP properties.
EXIV2API void moveXmpToExif(XmpData& xmpData, ExifData& exifData);
Expand Down
5 changes: 5 additions & 0 deletions include/exiv2/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// *****************************************************************************
// namespace extensions
namespace Exiv2 {
namespace Internal {
struct XmpArrayValueAccess;
}
// *****************************************************************************
// class definitions

Expand Down Expand Up @@ -726,6 +729,8 @@ class EXIV2API XmpTextValue : public XmpValue {
Uses a vector of std::string to store the value(s).
*/
class EXIV2API XmpArrayValue : public XmpValue {
friend struct Internal::XmpArrayValueAccess;

public:
//! Shortcut for a %XmpArrayValue auto pointer.
using UniquePtr = std::unique_ptr<XmpArrayValue>;
Expand Down
9 changes: 9 additions & 0 deletions include/exiv2/xmpsidecar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class EXIV2API XmpSidecar : public Image {
//! @name Manipulators
//@{
void readMetadata() override;
/*!
@brief Write buffered metadata as an XMP sidecar.
@throw Error(ErrorCode::kerImageWriteFailed) if structured XMP encoding fails.
Encoding failure leaves the bytes present before this call unchanged.
@note The complete packet is staged in memory before BasicIo::transfer writes
it to the destination. Transfer errors may leave partially written data.
ImageFactory::create(type, path) still overwrites the file immediately;
create an in-memory image to defer that change until encoding succeeds.
*/
void writeMetadata() override;
/*!
@brief Not supported. XMP sidecar files do not contain a comment.
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ add_library(
cr2header_int.hpp
crwimage_int.cpp
crwimage_int.hpp
devicesettings_int.cpp
devicesettings_int.hpp
fujimn_int.cpp
fujimn_int.hpp
helper_functions.cpp
Expand Down Expand Up @@ -54,6 +56,7 @@ add_library(
tifffwd_int.hpp
utils.hpp
utils.cpp
value_int.hpp
)

set(PUBLIC_HEADERS
Expand Down
Loading
Loading