-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMetadataCache.cpp
141 lines (116 loc) · 4.2 KB
/
MetadataCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2013-2015 Ofer Kashayov <[email protected]>
* This file is part of Phototonic Image Viewer.
*
* Phototonic 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 3 of the License, or
* (at your option) any later version.
*
* Phototonic 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 Phototonic. If not, see <http://www.gnu.org/licenses/>.
*/
#include <exiv2/exiv2.hpp>
#include "Settings.h"
#include "MetadataCache.h"
void MetadataCache::updateImageTags(QString &imageFileName, QSet<QString> tags) {
cache[imageFileName].tags = tags;
}
bool MetadataCache::removeTagFromImage(QString &imageFileName, const QString &tagName) {
return cache[imageFileName].tags.remove(tagName);
}
void MetadataCache::removeImage(QString &imageFileName) {
cache.remove(imageFileName);
}
QSet<QString> &MetadataCache::getImageTags(QString &imageFileName) {
return cache[imageFileName].tags;
}
long MetadataCache::getImageOrientation(QString &imageFileName) {
if (cache.contains(imageFileName) || loadImageMetadata(imageFileName)) {
return cache[imageFileName].orientation;
}
return 0;
}
void MetadataCache::setImageTags(const QString &imageFileName, QSet<QString> tags) {
ImageMetadata imageMetadata;
imageMetadata.tags = tags;
cache.insert(imageFileName, imageMetadata);
}
void MetadataCache::addTagToImage(QString &imageFileName, QString &tagName) {
if (cache[imageFileName].tags.contains(tagName)) {
return;
}
cache[imageFileName].tags.insert(tagName);
}
void MetadataCache::clear() {
cache.clear();
}
bool MetadataCache::loadImageMetadata(const QString &imageFullPath) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#if EXIV2_TEST_VERSION(0,28,0)
Exiv2::Image::UniquePtr exifImage;
#else
Exiv2::Image::AutoPtr exifImage;
#endif
#pragma clang diagnostic pop
QSet<QString> tags;
long orientation = 0;
try {
exifImage = Exiv2::ImageFactory::open(imageFullPath.toStdString());
exifImage->readMetadata();
} catch (Exiv2::Error &error) {
qWarning() << "Error loading image for reading metadata" << error.what();
return false;
}
if (!exifImage->good()) {
return false;
}
if (exifImage->supportsMetadata(Exiv2::mdExif)) try {
Exiv2::ExifData::const_iterator it = Exiv2::orientation(exifImage->exifData());
if (it != exifImage->exifData().end()) {
#if EXIV2_TEST_VERSION(0,28,0)
orientation = it->toUint32();
#else
orientation = it->toLong();
#endif
}
} catch (Exiv2::Error &error) {
qWarning() << "Failed to read Exif metadata" << error.what();
}
if (exifImage->supportsMetadata(Exiv2::mdIptc)) try {
Exiv2::IptcData &iptcData = exifImage->iptcData();
if (!iptcData.empty()) {
QString key;
Exiv2::IptcData::iterator end = iptcData.end();
// Finds the first ID, but we need to loop over the rest in case there are more
Exiv2::IptcData::iterator iptcIt = iptcData.findId(Exiv2::IptcDataSets::Keywords);
for (; iptcIt != end; ++iptcIt) {
if (iptcIt->tag() != Exiv2::IptcDataSets::Keywords) {
continue;
}
QString tagName = QString::fromUtf8(iptcIt->toString().c_str());
tags.insert(tagName);
Settings::knownTags.insert(tagName);
}
}
} catch (Exiv2::Error &error) {
qWarning() << "Failed to read Iptc metadata";
}
ImageMetadata imageMetadata;
if (tags.size()) {
imageMetadata.tags = tags;
}
if (orientation) {
imageMetadata.orientation = orientation;
}
if (tags.size() || orientation) {
cache.insert(imageFullPath, imageMetadata);
}
return true;
}