Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 0d5915a1 authored by Vangelis Tasoulas's avatar Vangelis Tasoulas Committed by Albert Astals Cid

Fix Gwenview Importer doesn't use date/time from EXIF

Summary:
Gwenview Importer only uses date/time from the filesystem metadata when renaming imported photos.
Not the EXIF information that holds the correct date/time that the picture has been taken.

After looking into the code, I found out that the datetime is determined by calling the function 'TimeUtils::dateTimeForFileItem'.

Consequently, the function 'TimeUtils::dateTimeForFileItem' is calling the function 'updateFromExif()' which in turn calls the function 'Exiv2ImageLoader::load' that returns 'false'. Then it falls back at reading the datetime from the filesystem metadata.

After printing out the error messages that cause the function 'Exiv2ImageLoader::load' to return 'false', I found that the Exiv2 error message is 'Failed to read image data' (doesn't help much). However, after I started playing with the size of the 'QByteArray header' (at the line where the file is read) that is passed as an argument to the 'Exiv2ImageLoader::load' function, I observed different errors until I managed to read the exif info from the file properly.

Some example sizes and different reported errors by Exiv2:

65536: Failed to read image data
66000: JPEG format error, rc = 5
131072 It reads fine. No error.

Didn't check numbers between 66000 and 131072 in order to find the exact threshold where I don't get a problem/error reading the exif information, but why should gwenview even bother about this number since exiv2 can do the job if we only provide the url (filepath) of the file to the Exiv2::ImageFactory::open() function? I guess exiv2 probably does the image reading in an even more efficient way according to the JPEG (or other) standard specifications (other than passing it a fixed number of bytes from an image as gwenview currently does).

So attached you can find a proposed patch that does exactly that: Adds a second instance of the function "Exiv2ImageLoader::load()" that acceps a "QString filePath" parameter and lets the Exiv2 library to handle the rest based on the given filePath.

BUGS: 383520

Reviewers: gateau, lukas, ngraham, #kde_applications, aacid, broulik

Reviewed By: gateau, ngraham

Subscribers: alexeymin, cfeck

Differential Revision: https://phabricator.kde.org/D7317
parent c4a84dad
......@@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// Qt
#include <QByteArray>
#include <QString>
#include <QFile>
// KDE
......@@ -52,22 +53,26 @@ Exiv2ImageLoader::~Exiv2ImageLoader()
delete d;
}
bool Exiv2ImageLoader::load(const QString& filePath)
{
QByteArray filePathByteArray = QFile::encodeName(filePath);
try {
d->mImage = Exiv2::ImageFactory::open(filePathByteArray.constData());
d->mImage->readMetadata();
} catch (const Exiv2::Error& error) {
d->mErrorMessage = error.what();
return false;
}
return true;
}
bool Exiv2ImageLoader::load(const QByteArray& data)
{
try {
d->mImage = Exiv2::ImageFactory::open((unsigned char*)data.constData(), data.size());
d->mImage->readMetadata();
#if EXIV2_VERSION >= EXIV2_MAKE_VERSION(0, 14, 0)
// For some unknown reason, trying to catch Exiv2::Error fails with Exiv2
// >=0.14. For now, just catch std::exception. I would welcome any
// explanation.
} catch (const std::exception& error) {
d->mErrorMessage = error.what();
#else
// In libexiv2 0.12, Exiv2::Error::what() returns an std::string.
} catch (const Exiv2::Error& error) {
d->mErrorMessage = error.what().c_str();
#endif
d->mErrorMessage = error.what();
return false;
}
return true;
......
......@@ -51,6 +51,7 @@ public:
Exiv2ImageLoader();
~Exiv2ImageLoader();
bool load(const QString&);
bool load(const QByteArray&);
QString errorMessage() const;
Exiv2::Image::AutoPtr popImage();
......
......@@ -88,17 +88,8 @@ struct CacheItem
}
QString path = url.path();
Exiv2ImageLoader loader;
QByteArray header;
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Could not open" << path << "for reading";
return false;
}
header = file.read(65536); // FIXME: Is this big enough?
}
if (!loader.load(header)) {
if (!loader.load(path)) {
return false;
}
Exiv2::Image::AutoPtr img = loader.popImage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment