diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-01-02 13:09:38 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-01-02 13:09:38 +0100 |
commit | 6e518b753b151e4a473bf55d27f57ad4e7ebb7c5 (patch) | |
tree | 311d6850cfd7ef4aa3f5fb32e1d7ed74d1b30f8d /src | |
parent | e6f5b6ae52871606a339bcf3c933f78bfa337875 (diff) | |
download | pyexiv2-6e518b753b151e4a473bf55d27f57ad4e7ebb7c5.tar.gz |
Always check if the metadata has been read beforehand.
Diffstat (limited to 'src')
-rw-r--r-- | src/exiv2wrapper.cpp | 234 |
1 files changed, 115 insertions, 119 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index 4a3f0f6..92d254d 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -87,41 +87,39 @@ void Image::readMetadata() void Image::writeMetadata() { - if(_dataRead) + if (!_dataRead) { - // Release the GIL to allow other python threads to run - // while writing metadata. - Py_BEGIN_ALLOW_THREADS + throw Exiv2::Error(METADATA_NOT_READ); + } - _image->setExifData(_exifData); - _image->setIptcData(_iptcData); - _image->setXmpData(_xmpData); - _image->writeMetadata(); + // Release the GIL to allow other python threads to run + // while writing metadata. + Py_BEGIN_ALLOW_THREADS - // Re-acquire the GIL - Py_END_ALLOW_THREADS - } - else - throw Exiv2::Error(METADATA_NOT_READ); + _image->setExifData(_exifData); + _image->setIptcData(_iptcData); + _image->setXmpData(_xmpData); + _image->writeMetadata(); + + // Re-acquire the GIL + Py_END_ALLOW_THREADS } boost::python::list Image::exifKeys() { - boost::python::list keys; - if(_dataRead) + if (!_dataRead) { - for(Exiv2::ExifMetadata::iterator i = _exifData.begin(); - i != _exifData.end(); - ++i) - { - keys.append(i->key()); - } - return keys; + throw Exiv2::Error(METADATA_NOT_READ); } - else + + boost::python::list keys; + for(Exiv2::ExifMetadata::iterator i = _exifData.begin(); + i != _exifData.end(); + ++i) { - throw Exiv2::Error(METADATA_NOT_READ); + keys.append(i->key()); } + return keys; } const ExifTag Image::getExifTag(std::string key) @@ -170,26 +168,24 @@ void Image::deleteExifTag(std::string key) boost::python::list Image::iptcKeys() { + if (!_dataRead) + { + throw Exiv2::Error(METADATA_NOT_READ); + } + boost::python::list keys; - if(_dataRead) + for(Exiv2::IptcMetadata::iterator i = _iptcData.begin(); + i != _iptcData.end(); + ++i) { - for(Exiv2::IptcMetadata::iterator i = _iptcData.begin(); - i != _iptcData.end(); - ++i) + // The key is appended to the list if and only if it is not already + // present. + if (keys.count(i->key()) == 0) { - // The key is appended to the list if and only if it is not already - // present. - if (keys.count(i->key()) == 0) - { - keys.append(i->key()); - } + keys.append(i->key()); } - return keys; - } - else - { - throw Exiv2::Error(METADATA_NOT_READ); } + return keys; } const IptcTag Image::getIptcTag(std::string key) @@ -221,36 +217,36 @@ const IptcTag Image::getIptcTag(std::string key) /*void Image::setIptcTag(std::string key, std::string value, unsigned int index=0) { - if(_dataRead) + if (!_dataRead) { - unsigned int indexCounter = index; - Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key); - Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey); - while ((indexCounter > 0) && (dataIterator != _iptcData.end())) - { - dataIterator = std::find_if(++dataIterator, _iptcData.end(), - Exiv2::FindMetadatumById::FindMetadatumById(iptcKey.tag(), iptcKey.record())); - --indexCounter; - } - if (dataIterator != _iptcData.end()) - { - // The tag at given index already exists, override it - dataIterator->setValue(value); - } - else - { - // Either index is greater than the index of the last repetition - // of the tag, or the tag does not exist yet. - // In both cases, it is created. - Exiv2::Iptcdatum iptcDatum(iptcKey); - iptcDatum.setValue(value); - int state = _iptcData.add(iptcDatum); - if (state == 6) - throw Exiv2::Error(NON_REPEATABLE); - } + throw Exiv2::Error(METADATA_NOT_READ); + } + + unsigned int indexCounter = index; + Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key); + Exiv2::IptcMetadata::iterator dataIterator = _iptcData.findKey(iptcKey); + while ((indexCounter > 0) && (dataIterator != _iptcData.end())) + { + dataIterator = std::find_if(++dataIterator, _iptcData.end(), + Exiv2::FindMetadatumById::FindMetadatumById(iptcKey.tag(), iptcKey.record())); + --indexCounter; + } + if (dataIterator != _iptcData.end()) + { + // The tag at given index already exists, override it + dataIterator->setValue(value); } else - throw Exiv2::Error(METADATA_NOT_READ); + { + // Either index is greater than the index of the last repetition + // of the tag, or the tag does not exist yet. + // In both cases, it is created. + Exiv2::Iptcdatum iptcDatum(iptcKey); + iptcDatum.setValue(value); + int state = _iptcData.add(iptcDatum); + if (state == 6) + throw Exiv2::Error(NON_REPEATABLE); + } }*/ void Image::setIptcTagValues(std::string key, boost::python::list values) @@ -323,21 +319,19 @@ void Image::deleteIptcTag(std::string key) boost::python::list Image::xmpKeys() { - boost::python::list keys; - if(_dataRead) + if (!_dataRead) { - for(Exiv2::XmpMetadata::iterator i = _xmpData.begin(); - i != _xmpData.end(); - ++i) - { - keys.append(i->key()); - } - return keys; + throw Exiv2::Error(METADATA_NOT_READ); } - else + + boost::python::list keys; + for(Exiv2::XmpMetadata::iterator i = _xmpData.begin(); + i != _xmpData.end(); + ++i) { - throw Exiv2::Error(METADATA_NOT_READ); + keys.append(i->key()); } + return keys; } const XmpTag Image::getXmpTag(std::string key) @@ -427,76 +421,78 @@ void Image::deleteXmpTag(std::string key) /* boost::python::tuple Image::getThumbnailData() { - if(_dataRead) + if (!_dataRead) { - Exiv2::Thumbnail::AutoPtr thumbnail = _exifData.getThumbnail(); - if (thumbnail.get() != 0) + throw Exiv2::Error(METADATA_NOT_READ); + } + + Exiv2::Thumbnail::AutoPtr thumbnail = _exifData.getThumbnail(); + if (thumbnail.get() != 0) + { + std::string format(_exifData.thumbnailFormat()); + // Copy the data buffer in a string. Since the data buffer can + // contain null char ('\x00'), the string cannot be simply + // constructed like that: + // std::string data((char*) dataBuffer.pData_); + // because it would be truncated after the first occurence of a + // null char. Therefore, it has to be copied char by char. + Exiv2::DataBuf dataBuffer = _exifData.copyThumbnail(); + char* charData = (char*) dataBuffer.pData_; + long dataLen = dataBuffer.size_; + // First allocate the memory for the whole string... + std::string data(dataLen, ' '); + // ... then fill it with the raw jpeg data. + for(long i = 0; i < dataLen; ++i) { - std::string format(_exifData.thumbnailFormat()); - // Copy the data buffer in a string. Since the data buffer can - // contain null char ('\x00'), the string cannot be simply - // constructed like that: - // std::string data((char*) dataBuffer.pData_); - // because it would be truncated after the first occurence of a - // null char. Therefore, it has to be copied char by char. - Exiv2::DataBuf dataBuffer = _exifData.copyThumbnail(); - char* charData = (char*) dataBuffer.pData_; - long dataLen = dataBuffer.size_; - // First allocate the memory for the whole string... - std::string data(dataLen, ' '); - // ... then fill it with the raw jpeg data. - for(long i = 0; i < dataLen; ++i) - { - data[i] = charData[i]; - } - return boost::python::make_tuple(format, data); + data[i] = charData[i]; } - else - throw Exiv2::Error(THUMB_ACCESS); + return boost::python::make_tuple(format, data); } else - throw Exiv2::Error(METADATA_NOT_READ); + throw Exiv2::Error(THUMB_ACCESS); } void Image::setThumbnailData(std::string data) { - if(_dataRead) + if (!_dataRead) { - const Exiv2::byte* dataBuf = (const Exiv2::byte*) data.c_str(); - _exifData.setJpegThumbnail(dataBuf, data.size()); - } - else throw Exiv2::Error(METADATA_NOT_READ); + } + + const Exiv2::byte* dataBuf = (const Exiv2::byte*) data.c_str(); + _exifData.setJpegThumbnail(dataBuf, data.size()); } void Image::deleteThumbnail() { - if(_dataRead) - _exifData.eraseThumbnail(); - else + if (!_dataRead) + { throw Exiv2::Error(METADATA_NOT_READ); + } + + _exifData.eraseThumbnail(); } void Image::dumpThumbnailToFile(const std::string path) { - if(_dataRead) + if (!_dataRead) { - int result = _exifData.writeThumbnail(path); - if (result == 8) - throw Exiv2::Error(NO_THUMBNAIL); - } - else throw Exiv2::Error(METADATA_NOT_READ); + } + + int result = _exifData.writeThumbnail(path); + if (result == 8) + throw Exiv2::Error(NO_THUMBNAIL); } void Image::setThumbnailFromJpegFile(const std::string path) { - if(_dataRead) + if (!_dataRead) { - _exifData.setJpegThumbnail(path); - } - else throw Exiv2::Error(METADATA_NOT_READ); + } + + _exifData.setJpegThumbnail(path); } */ |