-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexiv2api.cpp
More file actions
460 lines (404 loc) · 14.7 KB
/
exiv2api.cpp
File metadata and controls
460 lines (404 loc) · 14.7 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <pybind11/pybind11.h>
#include <exiv2/exiv2.hpp>
#include <string>
#include <sstream>
#include <iostream>
namespace py = pybind11;
const char *EXCEPTION_HINT = "Caught Exiv2 exception: ";
std::stringstream error_log;
void logHandler(int level, const char *msg)
{
switch (level)
{
case Exiv2::LogMsg::debug:
case Exiv2::LogMsg::info:
case Exiv2::LogMsg::warn:
std::cout << msg << std::endl;
break;
case Exiv2::LogMsg::error:
// For unknown reasons, the exception thrown here cannot be caught by pybind11, so temporarily save it to error_log.
// throw std::exception(msg);
error_log << msg;
break;
default:
return;
}
}
/* The error log should be checked at the end of each operation.
If there is a C++ error, it is converted to a Python exception. */
void check_error_log()
{
std::string str = error_log.str();
if(str != ""){
error_log.clear(); // Clear it so that it can be used again
error_log.str("");
throw std::runtime_error(str);
}
}
void init()
{
Exiv2::LogMsg::setHandler(logHandler);
}
void set_log_level(int level)
{
if (level == 0)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::debug);
if (level == 1)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::info);
if (level == 2)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::warn);
if (level == 3)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::error);
if (level == 4)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::mute);
}
py::str version()
{
return Exiv2::version();
}
// Ensure the current exiv2 version is equal to or greater than 0.27.4, which adds function Exiv2::enableBMFF().
#if EXIV2_TEST_VERSION(0,27,4)
bool enableBMFF(bool enable)
{
return Exiv2::enableBMFF(enable);
}
#endif
#define read_block \
{ \
py::list table; \
for (; i != end; ++i) \
{ \
py::list line; \
line.append(py::bytes(i->key())); \
\
std::stringstream _value; \
_value << i->value(); \
line.append(py::bytes(_value.str())); \
\
const char *typeName = i->typeName(); \
line.append(py::bytes((typeName ? typeName : "Unknown"))); \
table.append(line); \
} \
check_error_log(); \
return table; \
}
class Buffer{
public:
char *data;
long size;
Buffer(const char *data_, long size_){
size = size_;
data = (char *)calloc(size, sizeof(char));
if(data == NULL)
throw std::runtime_error("Failed to allocate memory.");
memcpy(data, data_, size);
}
void destroy(){
if(data){
free(data);
data = NULL;
}
}
py::bytes dump(){
return py::bytes((char *)data, size);
}
};
class Image{
public:
Exiv2::Image::AutoPtr *img = new Exiv2::Image::AutoPtr;
Image(const char *filename){
*img = Exiv2::ImageFactory::open(filename);
if (img->get() == 0)
throw Exiv2::Error(Exiv2::kerErrorMessage, "Can not open this image.");
(*img)->readMetadata(); // Calling readMetadata() reads all types of metadata supported by the image
check_error_log();
}
Image(Buffer buffer){
*img = Exiv2::ImageFactory::open((Exiv2::byte *)buffer.data, buffer.size);
if (img->get() == 0)
throw Exiv2::Error(Exiv2::kerErrorMessage, "Can not open this image.");
(*img)->readMetadata();
check_error_log();
}
void close_image()
{
delete img;
check_error_log();
}
py::bytes get_bytes()
{
Exiv2::BasicIo &io = (*img)->io();
return py::bytes((char *)io.mmap(), io.size());
}
std::string get_mime_type()
{
return (*img)->mimeType();
}
py::dict get_access_mode()
{
/* Get the access mode to various metadata.
Reference:
AccessMode Exiv2::Image::checkMode(MetadataId metadataId) const
enum MetadataId { mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8, mdIccProfile=16 };
enum AccessMode { amNone=0, amRead=1, amWrite=2, amReadWrite=3 };
*/
auto mode = py::dict();
mode["exif"] = int((*img)->checkMode(Exiv2::mdExif));
mode["iptc"] = int((*img)->checkMode(Exiv2::mdIptc));
mode["xmp"] = int((*img)->checkMode(Exiv2::mdXmp));
mode["comment"] = int((*img)->checkMode(Exiv2::mdComment));
// mode["icc"] = int((*img)->checkMode(Exiv2::mdIccProfile)); // Exiv2 will not check ICC
return mode;
}
py::object read_exif()
{
Exiv2::ExifData &data = (*img)->exifData();
Exiv2::ExifData::iterator i = data.begin();
Exiv2::ExifData::iterator end = data.end();
read_block;
}
py::object read_iptc()
{
Exiv2::IptcData &data = (*img)->iptcData();
Exiv2::IptcData::iterator i = data.begin();
Exiv2::IptcData::iterator end = data.end();
read_block;
}
py::object read_xmp()
{
Exiv2::XmpData &data = (*img)->xmpData();
Exiv2::XmpData::iterator i = data.begin();
Exiv2::XmpData::iterator end = data.end();
read_block;
}
py::object read_raw_xmp()
{
/*
When readMetadata() is called, Exiv2 reads the raw XMP text,
stores it in a string called XmpPacket, then parses it into an XmpData instance.
*/
return py::bytes((*img)->xmpPacket());
}
py::object read_comment()
{
return py::bytes((*img)->comment());
}
py::object read_icc()
{
Exiv2::DataBuf *buf = (*img)->iccProfile();
return py::bytes((char*)buf->pData_, buf->size_);
}
py::object read_thumbnail()
{
Exiv2::ExifThumb exifThumb((*img)->exifData());
Exiv2::DataBuf buf = exifThumb.copy();
return py::bytes((char*)buf.pData_, buf.size_);
}
void modify_exif(py::list table, py::str encoding)
{
// Create an empty container for storing data
Exiv2::ExifData &exifData = (*img)->exifData();
// Iterate the input table. each line contains a key and a value
for (auto _line : table){
// Convert _line from auto type to py::list type
py::list line;
for (auto field : _line)
line.append(field);
// Extract the fields in line
std::string key = py::bytes(line[0].attr("encode")(encoding));
std::string value = py::bytes(line[1].attr("encode")(encoding));
std::string typeName = py::bytes(line[2].attr("encode")(encoding));
// Locate the key
Exiv2::ExifData::iterator key_pos = exifData.findKey(Exiv2::ExifKey(key));
// Delete the existing key to set a new value, otherwise the key may contain multiple values.
if (key_pos != exifData.end())
exifData.erase(key_pos);
if (typeName == "_delete")
continue;
else if (typeName == "string")
exifData[key] = value;
}
(*img)->setExifData(exifData);
(*img)->writeMetadata(); // Save the metadata from memory to disk
check_error_log();
}
void modify_iptc(py::list table, py::str encoding)
{
Exiv2::IptcData &iptcData = (*img)->iptcData();
for (auto _line : table){
py::list line;
for (auto field : _line)
line.append(field);
std::string key = py::bytes(line[0].attr("encode")(encoding));
std::string typeName = py::bytes(line[2].attr("encode")(encoding));
Exiv2::IptcData::iterator key_pos = iptcData.findKey(Exiv2::IptcKey(key));
while (key_pos != iptcData.end()){ // Use the while loop because the iptc key may repeat
iptcData.erase(key_pos);
key_pos = iptcData.findKey(Exiv2::IptcKey(key));
}
if (typeName == "_delete")
continue;
else if (typeName == "string")
{
std::string value = py::bytes(line[1].attr("encode")(encoding));
iptcData[key] = value;
}
else if (typeName == "array")
{
Exiv2::Value::AutoPtr value = Exiv2::Value::create(Exiv2::string);
for (auto item: line[1]){
std::string item_str = py::bytes(py::str(item).attr("encode")(encoding));
value->read(item_str);
iptcData.add(Exiv2::IptcKey(key), value.get());
}
}
}
(*img)->setIptcData(iptcData);
(*img)->writeMetadata();
check_error_log();
}
void modify_xmp(py::list table, py::str encoding)
{
Exiv2::XmpData &xmpData = (*img)->xmpData();
for (auto _line : table){
py::list line;
for (auto field : _line)
line.append(field);
std::string key = py::bytes(line[0].attr("encode")(encoding));
std::string typeName = py::bytes(line[2].attr("encode")(encoding));
Exiv2::XmpData::iterator key_pos = xmpData.findKey(Exiv2::XmpKey(key));
if (key_pos != xmpData.end())
xmpData.erase(key_pos);
if (typeName == "_delete")
continue;
else if (typeName == "string")
{
std::string value = py::bytes(line[1].attr("encode")(encoding));
xmpData[key] = value;
}
else if (typeName == "array")
{
Exiv2::Value::AutoPtr value = Exiv2::Value::create(Exiv2::xmpSeq);
for (auto item: line[1]){
std::string item_str = py::bytes(py::str(item).attr("encode")(encoding));
value->read(item_str);
}
xmpData.add(Exiv2::XmpKey(key), value.get());
}
}
(*img)->setXmpData(xmpData);
(*img)->writeMetadata();
check_error_log();
}
void modify_raw_xmp(py::str data, py::str encoding)
{
std::string data_str = py::bytes(data.attr("encode")(encoding));
(*img)->setXmpPacket(data_str);
(*img)->writeMetadata();
(*img)->writeXmpFromPacket(); // Refresh the parsed XMP data in memory
check_error_log();
}
void modify_comment(py::str data, py::str encoding)
{
std::string data_str = py::bytes(data.attr("encode")(encoding));
(*img)->setComment(data_str);
(*img)->writeMetadata();
check_error_log();
}
void modify_icc(const char *data, long size)
{
Exiv2::DataBuf buf((Exiv2::byte *) data, size);
(*img)->setIccProfile(buf);
(*img)->writeMetadata();
check_error_log();
}
void modify_thumbnail(const char *data, long size)
{
Exiv2::ExifThumb exifThumb((*img)->exifData());
exifThumb.setJpegThumbnail((Exiv2::byte *) data, size);
(*img)->writeMetadata();
check_error_log();
}
void clear_exif()
{
(*img)->clearExifData();
(*img)->writeMetadata();
check_error_log();
}
void clear_iptc()
{
(*img)->clearIptcData();
(*img)->writeMetadata();
check_error_log();
}
void clear_xmp()
{
(*img)->clearXmpData();
(*img)->writeMetadata();
check_error_log();
}
void clear_comment()
{
(*img)->clearComment();
(*img)->writeMetadata();
check_error_log();
}
void clear_icc()
{
(*img)->clearIccProfile();
(*img)->writeMetadata();
check_error_log();
}
void clear_thumbnail()
{
Exiv2::ExifThumb exifThumb((*img)->exifData());
exifThumb.erase();
(*img)->writeMetadata();
check_error_log();
}
};
// Declare the API that needs to be mapped, to convert this CPP file into a Python module.
PYBIND11_MODULE(exiv2api, m)
{
m.doc() = "Expose the API of exiv2 to Python.";
m.def("init" , &init);
m.def("version" , &version);
m.def("registerNs" , &Exiv2::XmpProperties::registerNs);
#if EXIV2_TEST_VERSION(0,27,4)
m.def("enableBMFF" , &enableBMFF);
#endif
m.def("set_log_level", &set_log_level);
py::class_<Buffer>(m, "Buffer")
.def(py::init<const char *, long>())
.def_readonly("data" , &Buffer::data)
.def_readonly("size" , &Buffer::size)
.def("destroy" , &Buffer::destroy)
.def("dump" , &Buffer::dump);
py::class_<Image>(m, "Image")
.def(py::init<const char *>())
.def(py::init<Buffer &>())
.def("close_image" , &Image::close_image)
.def("get_bytes" , &Image::get_bytes)
.def("get_mime_type" , &Image::get_mime_type)
.def("get_access_mode" , &Image::get_access_mode)
.def("read_exif" , &Image::read_exif)
.def("read_iptc" , &Image::read_iptc)
.def("read_xmp" , &Image::read_xmp)
.def("read_raw_xmp" , &Image::read_raw_xmp)
.def("read_comment" , &Image::read_comment)
.def("read_icc" , &Image::read_icc)
.def("read_thumbnail" , &Image::read_thumbnail)
.def("modify_exif" , &Image::modify_exif)
.def("modify_iptc" , &Image::modify_iptc)
.def("modify_xmp" , &Image::modify_xmp)
.def("modify_raw_xmp" , &Image::modify_raw_xmp)
.def("modify_comment" , &Image::modify_comment)
.def("modify_icc" , &Image::modify_icc)
.def("modify_thumbnail" , &Image::modify_thumbnail)
.def("clear_exif" , &Image::clear_exif)
.def("clear_iptc" , &Image::clear_iptc)
.def("clear_xmp" , &Image::clear_xmp)
.def("clear_comment" , &Image::clear_comment)
.def("clear_icc" , &Image::clear_icc)
.def("clear_thumbnail" , &Image::clear_thumbnail);
}