aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2010-03-17 00:12:41 +0100
committerOlivier Tilloy <olivier@tilloy.net>2010-03-17 00:12:41 +0100
commit9843496fb95d5d142632a3239af9d322e166f3e9 (patch)
tree6832336af4f6ea10e2d80713fbe5dbc0ffdcd710 /src
parentcf80099581568c17a49160b594ee17e690e64d5c (diff)
downloadpyexiv2-9843496fb95d5d142632a3239af9d322e166f3e9.tar.gz
New data_buffer() method to get the image buffer.
Diffstat (limited to 'src')
-rw-r--r--src/exiv2wrapper.cpp45
-rw-r--r--src/exiv2wrapper.hpp3
-rw-r--r--src/exiv2wrapper_python.cpp2
-rw-r--r--src/pyexiv2/metadata.py8
4 files changed, 58 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)
{
diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp
index c0fb46c..5443f2b 100644
--- a/src/exiv2wrapper.hpp
+++ b/src/exiv2wrapper.hpp
@@ -225,6 +225,9 @@ public:
// Copy the metadata to another image.
void copyMetadata(Image& other, bool exif=true, bool iptc=true, bool xmp=true) const;
+ // Return the image data buffer.
+ std::string getDataBuffer() const;
+
private:
std::string _filename;
Exiv2::Image::AutoPtr _image;
diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp
index aab2925..5a03132 100644
--- a/src/exiv2wrapper_python.cpp
+++ b/src/exiv2wrapper_python.cpp
@@ -148,6 +148,8 @@ BOOST_PYTHON_MODULE(libexiv2python)
.def("_previews", &Image::previews)
.def("_copyMetadata", &Image::copyMetadata)
+
+ .def("_getDataBuffer", &Image::getDataBuffer)
;
}
diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py
index f49f92b..61132ab 100644
--- a/src/pyexiv2/metadata.py
+++ b/src/pyexiv2/metadata.py
@@ -387,3 +387,11 @@ class ImageMetadata(object):
other._keys['xmp'] = None
other._tags['xmp'] = {}
+ def data_buffer(self):
+ """
+ Return the image data as a string.
+ If metadata has been modified, the data buffer won't be up-to-date until
+ write() is called.
+ """
+ return self._image._getDataBuffer()
+