diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-03-17 00:12:41 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-03-17 00:12:41 +0100 |
commit | 9843496fb95d5d142632a3239af9d322e166f3e9 (patch) | |
tree | 6832336af4f6ea10e2d80713fbe5dbc0ffdcd710 /src/exiv2wrapper.cpp | |
parent | cf80099581568c17a49160b594ee17e690e64d5c (diff) | |
download | pyexiv2-9843496fb95d5d142632a3239af9d322e166f3e9.tar.gz |
New data_buffer() method to get the image buffer.
Diffstat (limited to 'src/exiv2wrapper.cpp')
-rw-r--r-- | src/exiv2wrapper.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index a0b6e59..8ee13de 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -484,6 +484,51 @@ void Image::copyMetadata(Image& other, bool exif, bool iptc, bool xmp) const other._xmpData = _xmpData; } +std::string Image::getDataBuffer() const +{ + Exiv2::BasicIo& io = _image->io(); + long size = io.size(); + long pos = -1; + + if (io.isopen()) + { + // Remember the current position in the stream + pos = io.tell(); + // Go to the beginning of the stream + io.seek(0, Exiv2::BasicIo::beg); + } + else + { + io.open(); + } + + // Copy the data buffer in a string. Since the data buffer can contain null + // characters ('\x00'), the string cannot be simply constructed like that: + // _data = std::string((char*) previewImage.pData()); + // because it would be truncated after the first occurence of a null + // character. Therefore, it has to be copied character by character. + // First allocate the memory for the whole string... + std::string buffer(size, ' '); + // ... then fill it with the raw data. + for (unsigned long i = 0; i < size; ++i) + { + io.read((Exiv2::byte*) &buffer[i], 1); + } + + if (pos == -1) + { + // The stream was initially closed + io.close(); + } + else + { + // Reset to the initial position in the stream + io.seek(pos, Exiv2::BasicIo::beg); + } + + return buffer; +} + ExifTag::ExifTag(const std::string& key, Exiv2::Exifdatum* datum, Exiv2::ExifData* data): _key(key) { |