diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-01-08 09:23:32 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-01-08 09:23:32 +0100 |
commit | 8b3562b019794802f26ceb4f0761b47c876b2e08 (patch) | |
tree | 974202e416c5ebea5c229befe5b7347492ae0fe3 | |
parent | 1e734d541139d3f7183bcf55b105d9b44930940b (diff) | |
download | pyexiv2-8b3562b019794802f26ceb4f0761b47c876b2e08.tar.gz |
Preview (thumbnail) extraction.
Read-only at the moment.
-rw-r--r-- | src/exiv2wrapper.cpp | 41 | ||||
-rw-r--r-- | src/exiv2wrapper.hpp | 17 | ||||
-rw-r--r-- | src/exiv2wrapper_python.cpp | 11 | ||||
-rw-r--r-- | src/pyexiv2/metadata.py | 4 |
4 files changed, 73 insertions, 0 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index 914657a..3dbd8b2 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -398,6 +398,24 @@ void Image::deleteXmpTag(std::string key) throw Exiv2::Error(KEY_NOT_FOUND, key); } +boost::python::list Image::previews() +{ + CHECK_METADATA_READ + + boost::python::list previews; + Exiv2::PreviewManager pm(*_image); + Exiv2::PreviewPropertiesList props = pm.getPreviewProperties(); + for (Exiv2::PreviewPropertiesList::const_iterator i = props.begin(); + i != props.end(); + ++i) + { + previews.append(Preview(pm.getPreviewImage(*i))); + } + + return previews; +} + + /* boost::python::tuple Image::getThumbnailData() { @@ -774,6 +792,29 @@ const boost::python::dict XmpTag::getLangAltValue() } +Preview::Preview(const Exiv2::PreviewImage& previewImage) +{ + _mimeType = previewImage.mimeType(); + _extension = previewImage.extension(); + _size = previewImage.size(); + _dimensions = boost::python::make_tuple(previewImage.width(), + previewImage.height()); + // 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. + const Exiv2::byte* pData = previewImage.pData(); + // First allocate the memory for the whole string... + _data = std::string(_size, ' '); + // ... then fill it with the raw data. + for(unsigned int i = 0; i < _size; ++i) + { + _data[i] = pData[i]; + } +} + + // TODO: update the errors code to reflect changes from src/error.cpp in libexiv2 void translateExiv2Error(Exiv2::Error const& error) { diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp index 26fb784..eaffeaa 100644 --- a/src/exiv2wrapper.hpp +++ b/src/exiv2wrapper.hpp @@ -29,6 +29,7 @@ #include <string> #include "exiv2/image.hpp" +#include "exiv2/preview.hpp" #include "boost/python.hpp" @@ -129,6 +130,19 @@ private: }; +class Preview +{ +public: + Preview(const Exiv2::PreviewImage& previewImage); + + std::string _mimeType; + std::string _extension; + unsigned int _size; + boost::python::tuple _dimensions; + std::string _data; +}; + + class Image { public: @@ -196,6 +210,9 @@ public: // Read and write access to the thumbnail embedded in the image. + // Read-only ATM. + boost::python::list previews(); + // Return a tuple containing the format of the thumbnail ("TIFF" or // "JPEG") and the thumbnail raw data as a string buffer. // Throw an exception if the thumbnail data cannot be accessed. diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp index bce46fa..a5f9d6d 100644 --- a/src/exiv2wrapper_python.cpp +++ b/src/exiv2wrapper_python.cpp @@ -93,6 +93,15 @@ BOOST_PYTHON_MODULE(libexiv2python) .def("_getLangAltValue", &XmpTag::getLangAltValue) ; + class_<Preview>("Preview", init<Exiv2::PreviewImage>()) + + .def_readonly("mime_type", &Preview::_mimeType) + .def_readonly("extension", &Preview::_extension) + .def_readonly("size", &Preview::_size) + .def_readonly("dimensions", &Preview::_dimensions) + .def_readonly("data", &Preview::_data) + ; + class_<Image>("Image", init<std::string>()) .def("readMetadata", &Image::readMetadata) @@ -115,6 +124,8 @@ BOOST_PYTHON_MODULE(libexiv2python) .def("setXmpTagLangAltValue", &Image::setXmpTagLangAltValue) .def("deleteXmpTag", &Image::deleteXmpTag) + .def("previews", &Image::previews) + // .def("getThumbnailData", &Image::getThumbnailData) // .def("setThumbnailData", &Image::setThumbnailData) // .def("deleteThumbnail", &Image::deleteThumbnail) diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py index 05e762b..77900d3 100644 --- a/src/pyexiv2/metadata.py +++ b/src/pyexiv2/metadata.py @@ -301,3 +301,7 @@ class ImageMetadata(object): except AttributeError: raise KeyError(key) + @property + def previews(self): + return self._image.previews() + |