forked from jcelaya/hdrmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExifTransfer.cpp
180 lines (159 loc) · 5.65 KB
/
ExifTransfer.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* HDRMerge - HDR exposure merging software.
* Copyright 2012 Javier Celaya
*
* This file is part of HDRMerge.
*
* HDRMerge 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.
*
* HDRMerge 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 HDRMerge. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <exiv2/exiv2.hpp>
#include "ExifTransfer.hpp"
#include "Log.hpp"
using namespace hdrmerge;
using namespace Exiv2;
using namespace std;
class ExifTransfer {
public:
ExifTransfer(const QString & srcFile, const QString & dstFile,
const uint8_t * data, size_t dataSize)
: srcFile(srcFile), dstFile(dstFile), data(data), dataSize(dataSize) {}
void copyMetadata();
private:
QString srcFile, dstFile;
const uint8_t * data;
size_t dataSize;
Exiv2::Image::AutoPtr src, dst;
void copyXMP();
void copyIPTC();
void copyEXIF();
};
void hdrmerge::Exif::transfer(const QString & srcFile, const QString & dstFile,
const uint8_t * data, size_t dataSize) {
ExifTransfer exif(srcFile, dstFile, data, dataSize);
exif.copyMetadata();
}
void ExifTransfer::copyMetadata() {
try {
dst = Exiv2::ImageFactory::open(BasicIo::AutoPtr(new MemIo(data, dataSize)));
dst->readMetadata();
} catch (Exiv2::Error & e) {
std::cerr << "Exiv2 error: " << e.what() << std::endl;
return;
}
try {
src = Exiv2::ImageFactory::open(srcFile.toLocal8Bit().constData());
src->readMetadata();
copyXMP();
copyIPTC();
copyEXIF();
} catch (Exiv2::Error & e) {
std::cerr << "Exiv2 error: " << e.what() << std::endl;
// At least we have to set the SubImage1 file type to Primary Image
dst->exifData()["Exif.SubImage1.NewSubfileType"] = 0;
}
try {
dst->writeMetadata();
FileIo fileIo(dstFile.toLocal8Bit().constData());
fileIo.open("wb");
fileIo.write(dst->io());
fileIo.close();
} catch (Exiv2::Error & e) {
std::cerr << "Exiv2 error: " << e.what() << std::endl;
}
}
void ExifTransfer::copyXMP() {
const Exiv2::XmpData & srcXmp = src->xmpData();
Exiv2::XmpData & dstXmp = dst->xmpData();
for (const auto & datum : srcXmp) {
if (datum.groupName() != "tiff" && dstXmp.findKey(Exiv2::XmpKey(datum.key())) == dstXmp.end()) {
dstXmp.add(datum);
}
}
}
void ExifTransfer::copyIPTC() {
const Exiv2::IptcData & srcIptc = src->iptcData();
Exiv2::IptcData & dstIptc = dst->iptcData();
for (const auto & datum : srcIptc) {
if (dstIptc.findKey(Exiv2::IptcKey(datum.key())) == dstIptc.end()) {
dstIptc.add(datum);
}
}
}
static bool excludeExifDatum(const Exifdatum & datum) {
static const char * previewKeys[] {
"Exif.OlympusCs.PreviewImageStart",
"Exif.OlympusCs.PreviewImageLength",
"Exif.Thumbnail.JPEGInterchangeFormat",
"Exif.Thumbnail.JPEGInterchangeFormatLength",
"Exif.NikonPreview.JPEGInterchangeFormat",
"Exif.NikonPreview.JPEGInterchangeFormatLength",
"Exif.Pentax.PreviewOffset",
"Exif.Pentax.PreviewLength",
"Exif.PentaxDng.PreviewOffset",
"Exif.PentaxDng.PreviewLength",
"Exif.Minolta.ThumbnailOffset",
"Exif.Minolta.ThumbnailLength",
"Exif.SonyMinolta.ThumbnailOffset",
"Exif.SonyMinolta.ThumbnailLength",
"Exif.Olympus.ThumbnailImage",
"Exif.Olympus2.ThumbnailImage",
"Exif.Minolta.Thumbnail",
"Exif.PanasonicRaw.PreviewImage",
"Exif.SamsungPreview.JPEGInterchangeFormat",
"Exif.SamsungPreview.JPEGInterchangeFormatLength"
};
for (const char * pkey : previewKeys) {
if (datum.key() == pkey) {
return true;
}
}
return
datum.groupName().substr(0, 5) == "Thumb" ||
datum.groupName().substr(0, 8) == "SubThumb" ||
datum.groupName().substr(0, 5) == "Image" ||
datum.groupName().substr(0, 8) == "SubImage";
}
void ExifTransfer::copyEXIF() {
static const char * includeImageKeys[] = {
// Correct Make and Model, from the input files
// It is needed so that makernote tags are correctly copied
"Exif.Image.Make",
"Exif.Image.Model",
"Exif.Image.Artist",
"Exif.Image.Copyright",
"Exif.Image.DNGPrivateData",
// Opcodes generated by Adobe DNG converter
"Exif.SubImage1.OpcodeList1",
"Exif.SubImage1.OpcodeList2",
"Exif.SubImage1.OpcodeList3"
};
const Exiv2::ExifData & srcExif = src->exifData();
Exiv2::ExifData & dstExif = dst->exifData();
for (const char * keyName : includeImageKeys) {
auto iterator = srcExif.findKey(Exiv2::ExifKey(keyName));
if (iterator != srcExif.end()) {
dstExif[keyName] = *iterator;
}
}
// Now we set the SubImage1 file type to Primary Image
// Exiv2 wouldn't modify SubImage1 tags if it was set before
dstExif["Exif.SubImage1.NewSubfileType"] = 0;
for (const auto & datum : srcExif) {
if (!excludeExifDatum(datum) && dstExif.findKey(Exiv2::ExifKey(datum.key())) == dstExif.end()) {
dstExif.add(datum);
}
}
}