exiv2imageloader.cpp 3 KB
Newer Older
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
3
/*
Gwenview: an image viewer
Aurélien Gâteau's avatar
Aurélien Gâteau committed
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Dirk Mueller's avatar
Dirk Mueller committed
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20
21
22
23
24
25
26

*/
// Self
#include "exiv2imageloader.h"

// Qt
#include <QByteArray>
#include <QString>
27
#include <QFile>
28
29
30
31

// KDE

// Exiv2
32
#include <exiv2/exiv2.hpp>
33
34

// Local
35
#include "gwenview_exiv2_debug.h"
36

37
38
namespace Gwenview
{
39

Aurélien Gâteau's avatar
Aurélien Gâteau committed
40
41
struct Exiv2ImageLoaderPrivate
{
42
    std::unique_ptr<Exiv2::Image> mImage;
43
    QString mErrorMessage;
44
45
};

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
struct Exiv2LogHandler {
    static void handleMessage(int level, const char *message) {
        switch(level) {
        case Exiv2::LogMsg::debug:
            qCDebug(GWENVIEW_EXIV2_LOG) << message;
            break;
        case Exiv2::LogMsg::info:
            qCInfo(GWENVIEW_EXIV2_LOG) << message;
            break;
        case Exiv2::LogMsg::warn:
        case Exiv2::LogMsg::error:
        case Exiv2::LogMsg::mute:
            qCWarning(GWENVIEW_EXIV2_LOG) << message;
            break;
        default:
            qCWarning(GWENVIEW_EXIV2_LOG) << "unhandled log level" << level << message;
            break;
        }
    }

    Exiv2LogHandler() {
        Exiv2::LogMsg::setHandler(&Exiv2LogHandler::handleMessage);
    }
};


72
Exiv2ImageLoader::Exiv2ImageLoader()
73
74
: d(new Exiv2ImageLoaderPrivate)
{
75
76
    // This is a threadsafe way to ensure that we only register it once
    static Exiv2LogHandler handler;
77
78
}

79
80
81
Exiv2ImageLoader::~Exiv2ImageLoader()
{
    delete d;
82
83
}

84
85
86
87
bool Exiv2ImageLoader::load(const QString& filePath)
{
    QByteArray filePathByteArray = QFile::encodeName(filePath);
    try {
88
        d->mImage.reset(Exiv2::ImageFactory::open(filePathByteArray.constData()).release());
89
90
        d->mImage->readMetadata();
    } catch (const Exiv2::Error& error) {
91
        d->mErrorMessage = QString::fromUtf8(error.what());
92
93
94
95
96
        return false;
    }
    return true;
}

97
98
99
bool Exiv2ImageLoader::load(const QByteArray& data)
{
    try {
100
        d->mImage.reset(Exiv2::ImageFactory::open((unsigned char*)data.constData(), data.size()).release());
101
102
        d->mImage->readMetadata();
    } catch (const Exiv2::Error& error) {
103
        d->mErrorMessage = QString::fromUtf8(error.what());
104
105
106
        return false;
    }
    return true;
107
108
}

109
110
111
QString Exiv2ImageLoader::errorMessage() const
{
    return d->mErrorMessage;
112
113
}

114
std::unique_ptr<Exiv2::Image> Exiv2ImageLoader::popImage()
115
{
116
    return std::move(d->mImage);
117
118
119
}

} // namespace