diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-03-16 19:51:59 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-03-16 19:51:59 +0100 |
commit | cf80099581568c17a49160b594ee17e690e64d5c (patch) | |
tree | 94c94704c8dcd43cb84ae441993a6b1eb4da8757 | |
parent | bf16b218062b0279fab4e4a7e8ee4d0108bed17a (diff) | |
download | pyexiv2-cf80099581568c17a49160b594ee17e690e64d5c.tar.gz |
Instantiate an image from a data buffer.
-rw-r--r-- | src/exiv2wrapper.cpp | 24 | ||||
-rw-r--r-- | src/exiv2wrapper.hpp | 3 | ||||
-rw-r--r-- | src/exiv2wrapper_python.cpp | 1 | ||||
-rw-r--r-- | src/pyexiv2/metadata.py | 16 |
4 files changed, 39 insertions, 5 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index ad145d1..a0b6e59 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -41,7 +41,7 @@ namespace exiv2wrapper { -void Image::_instantiate_image() +void Image::_instantiate_image(Exiv2::byte* data, long size) { // If an exception is thrown, it has to be done outside of the // Py_{BEGIN,END}_ALLOW_THREADS block. @@ -53,7 +53,14 @@ void Image::_instantiate_image() try { - _image = Exiv2::ImageFactory::open(_filename); + if (data != 0) + { + _image = Exiv2::ImageFactory::open(data, size); + } + else + { + _image = Exiv2::ImageFactory::open(_filename); + } } catch (Exiv2::Error& err) { @@ -81,6 +88,19 @@ Image::Image(const std::string& filename) _instantiate_image(); } +// From buffer constructor +Image::Image(const std::string& buffer, long size) +{ + // Deep copy of the data buffer + Exiv2::byte* data = new Exiv2::byte[size]; + for (unsigned long i = 0; i < size; ++i) + { + data[i] = buffer[i]; + } + + _instantiate_image(data, size); +} + // Copy constructor Image::Image(const Image& image) { diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp index 48b7b3a..c0fb46c 100644 --- a/src/exiv2wrapper.hpp +++ b/src/exiv2wrapper.hpp @@ -151,6 +151,7 @@ class Image public: // Constructors Image(const std::string& filename); + Image(const std::string& buffer, long size); Image(const Image& image); void readMetadata(); @@ -235,7 +236,7 @@ private: // false otherwise bool _dataRead; - void _instantiate_image(); + void _instantiate_image(Exiv2::byte* data=0, long size=0); }; diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp index 4cdd34b..aab2925 100644 --- a/src/exiv2wrapper_python.cpp +++ b/src/exiv2wrapper_python.cpp @@ -118,6 +118,7 @@ BOOST_PYTHON_MODULE(libexiv2python) ; class_<Image>("_Image", init<std::string>()) + .def(init<std::string, long>()) .def("_readMetadata", &Image::readMetadata) .def("_writeMetadata", &Image::writeMetadata) diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py index 15b32f3..f49f92b 100644 --- a/src/pyexiv2/metadata.py +++ b/src/pyexiv2/metadata.py @@ -68,9 +68,21 @@ class ImageMetadata(object): raise IOError(ENOENT, "%s: '%s'" % (os.strerror(ENOENT), filename)) return libexiv2python._Image(filename) + @classmethod + def from_buffer(cls, data): + """ + Instantiate an image container from a data buffer. + + :param data: a buffer containing image data + :type data: string + """ + obj = cls(None) + obj._image = libexiv2python._Image(data, len(data)) + return obj + def read(self): """ - Read the metadata embedded in the associated image file. + Read the metadata embedded in the associated image. It is necessary to call this method once before attempting to access the metadata (an exception will be raised if trying to access metadata before calling this method). @@ -81,7 +93,7 @@ class ImageMetadata(object): def write(self): """ - Write the metadata back to the image file. + Write the metadata back to the image. """ self._image._writeMetadata() |