forked from dchesterton/goexiv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.cpp
388 lines (312 loc) · 7.91 KB
/
helper.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
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
#include "helper.h"
#include <exiv2/image.hpp>
#include <exiv2/error.hpp>
#include <stdio.h>
#define DEFINE_STRUCT(name,wrapped_type,member_name) \
struct _##name { \
_##name(wrapped_type member_name) \
: member_name(member_name) {} \
wrapped_type member_name; \
};
#define DEFINE_FREE_FUNCTION(name,type) \
void name##_free(type x) \
{ \
delete x; \
}
DEFINE_STRUCT(Exiv2ImageFactory, Exiv2::ImageFactory*, factory);
DEFINE_STRUCT(Exiv2Image, Exiv2::Image::AutoPtr, image);
DEFINE_STRUCT(Exiv2XmpData, const Exiv2::XmpData&, data);
DEFINE_STRUCT(Exiv2XmpDatum, const Exiv2::Xmpdatum&, datum);
DEFINE_STRUCT(Exiv2ExifData, const Exiv2::ExifData&, data);
DEFINE_STRUCT(Exiv2ExifDatum, const Exiv2::Exifdatum&, datum);
struct _Exiv2ExifDatumIterator {
_Exiv2ExifDatumIterator(Exiv2::ExifMetadata::const_iterator i, Exiv2::ExifMetadata::const_iterator e) : it(i), end(e) {}
Exiv2::ExifMetadata::const_iterator it;
Exiv2::ExifMetadata::const_iterator end;
bool has_next() const;
Exiv2ExifDatum* next();
};
DEFINE_STRUCT(Exiv2IptcData, const Exiv2::IptcData&, data);
DEFINE_STRUCT(Exiv2IptcDatum, const Exiv2::Iptcdatum&, datum);
struct _Exiv2IptcDatumIterator {
_Exiv2IptcDatumIterator(Exiv2::IptcMetadata::const_iterator i, Exiv2::IptcMetadata::const_iterator e) : it(i), end(e) {}
Exiv2::IptcMetadata::const_iterator it;
Exiv2::IptcMetadata::const_iterator end;
bool has_next() const;
Exiv2IptcDatum* next();
};
DEFINE_FREE_FUNCTION(exiv2_iptc_datum_iterator, Exiv2IptcDatumIterator*);
DEFINE_FREE_FUNCTION(exiv2_exif_datum_iterator, Exiv2ExifDatumIterator*);
struct _Exiv2Error {
_Exiv2Error(const Exiv2::Error &error);
int code;
char *what;
};
_Exiv2Error::_Exiv2Error(const Exiv2::Error &error)
: code(error.code())
, what(strdup(error.what()))
{
}
Exiv2Image*
exiv2_image_factory_open(const char *path, Exiv2Error **error)
{
Exiv2Image *p = 0;
try {
p = new Exiv2Image(Exiv2::ImageFactory::open(path));
return p;
} catch (Exiv2::Error &e) {
delete p;
if (error) {
*error = new Exiv2Error(e);
}
}
return 0;
}
Exiv2Image*
exiv2_image_factory_open_bytes(const unsigned char *bytes, long size, Exiv2Error **error)
{
Exiv2Image *p = 0;
try {
p = new Exiv2Image(Exiv2::ImageFactory::open(bytes, size));
return p;
} catch (Exiv2::Error &e) {
delete p;
if (error) {
*error = new Exiv2Error(e);
}
}
return 0;
}
void
exiv2_image_read_metadata(Exiv2Image *img, Exiv2Error **error)
{
try {
img->image->readMetadata();
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
}
}
void
exiv2_image_set_exif_string(Exiv2Image *img, char *key, char *value, Exiv2Error **error)
{
Exiv2::ExifData exifData = img->image->exifData();
try {
Exiv2::ExifKey keyObject(key);
Exiv2::Value::AutoPtr valueObject = Exiv2::Value::create(Exiv2::asciiString);
valueObject->read(value);
exifData.add(keyObject, valueObject.get());
img->image->setExifData(exifData);
img->image->writeMetadata();
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
}
}
void
exiv2_image_set_iptc_string(Exiv2Image *img, char *key, char *value, Exiv2Error **error)
{
Exiv2::IptcData iptcData = img->image->iptcData();
try {
Exiv2::StringValue valueObject;
valueObject.read(value);
iptcData[key] = valueObject;
img->image->setIptcData(iptcData);
img->image->writeMetadata();
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
}
}
DEFINE_FREE_FUNCTION(exiv2_image, Exiv2Image*);
int exiv2_image_get_pixel_width(Exiv2Image *img) {
return img->image->pixelWidth();
}
int exiv2_image_get_pixel_height(Exiv2Image *img) {
return img->image->pixelHeight();
}
const unsigned char* exiv2_image_icc_profile(Exiv2Image *img)
{
if (img->image->iccProfileDefined()) {
return img->image->iccProfile()->pData_;
}
return NULL;
}
long exiv2_image_icc_profile_size(Exiv2Image *img)
{
if (img->image->iccProfileDefined()) {
return img->image->iccProfile()->size_;
}
return 0;
}
// XMP
Exiv2XmpData*
exiv2_image_get_xmp_data(const Exiv2Image *img)
{
return new Exiv2XmpData(img->image->xmpData());
}
Exiv2XmpDatum*
exiv2_xmp_data_find_key(const Exiv2XmpData *data, const char *key, Exiv2Error **error)
{
try {
const Exiv2::XmpData::const_iterator it = data->data.findKey(Exiv2::XmpKey(key));
if (it == data->data.end()) {
return 0;
}
return new Exiv2XmpDatum(*it);
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
return 0;
}
}
DEFINE_FREE_FUNCTION(exiv2_xmp_data, Exiv2XmpData*);
char*
exiv2_xmp_datum_to_string(const Exiv2XmpDatum *datum)
{
Exiv2::TypeId typeId = datum->datum.typeId();
std::string strval;
if (typeId == Exiv2::xmpBag) {
strval = datum->datum.toString();
} else {
strval = datum->datum.toString(0);
}
return strdup(strval.c_str());
}
DEFINE_FREE_FUNCTION(exiv2_xmp_datum, Exiv2XmpDatum*);
// IPTC
Exiv2IptcData*
exiv2_image_get_iptc_data(const Exiv2Image *img)
{
return new Exiv2IptcData(img->image->iptcData());
}
Exiv2IptcDatum*
exiv2_iptc_data_find_key(const Exiv2IptcData *data, const char *key, Exiv2Error **error)
{
try {
const Exiv2::IptcData::const_iterator it = data->data.findKey(Exiv2::IptcKey(key));
if (it == data->data.end()) {
return 0;
}
return new Exiv2IptcDatum(*it);
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
return 0;
}
}
Exiv2IptcDatumIterator* exiv2_iptc_data_iterator(const Exiv2IptcData *data)
{
return new Exiv2IptcDatumIterator(data->data.begin(), data->data.end());
}
bool Exiv2IptcDatumIterator::has_next() const
{
return it != end;
}
int exiv2_iptc_data_iterator_has_next(const Exiv2IptcDatumIterator *iter)
{
return iter->has_next() ? 1 : 0;
}
Exiv2IptcDatum* Exiv2IptcDatumIterator::next()
{
if (it == end) {
return 0;
}
return new Exiv2IptcDatum(*it++);
}
Exiv2IptcDatum* exiv2_iptc_datum_iterator_next(Exiv2IptcDatumIterator *iter)
{
return iter->next();
}
DEFINE_FREE_FUNCTION(exiv2_iptc_data, Exiv2IptcData*);
const char* exiv2_iptc_datum_key(const Exiv2IptcDatum *datum)
{
return datum->datum.key().c_str();
}
const char* exiv2_iptc_datum_to_string(const Exiv2IptcDatum *datum)
{
const std::string strval = datum->datum.toString();
return strdup(strval.c_str());
}
DEFINE_FREE_FUNCTION(exiv2_iptc_datum, Exiv2IptcDatum*);
// EXIF
Exiv2ExifData*
exiv2_image_get_exif_data(const Exiv2Image *img)
{
return new Exiv2ExifData(img->image->exifData());
}
Exiv2ExifDatum*
exiv2_exif_data_find_key(const Exiv2ExifData *data, const char *key, Exiv2Error **error)
{
try {
const Exiv2::ExifData::const_iterator it = data->data.findKey(Exiv2::ExifKey(key));
if (it == data->data.end()) {
return 0;
}
return new Exiv2ExifDatum(*it);
} catch (Exiv2::Error &e) {
if (error) {
*error = new Exiv2Error(e);
}
return 0;
}
}
Exiv2ExifDatumIterator* exiv2_exif_data_iterator(const Exiv2ExifData *data)
{
return new Exiv2ExifDatumIterator(data->data.begin(), data->data.end());
}
bool Exiv2ExifDatumIterator::has_next() const
{
return it != end;
}
int exiv2_exif_data_iterator_has_next(const Exiv2ExifDatumIterator *iter)
{
return iter->has_next() ? 1 : 0;
}
Exiv2ExifDatum* Exiv2ExifDatumIterator::next()
{
if (it == end) {
return 0;
}
return new Exiv2ExifDatum(*it++);
}
Exiv2ExifDatum* exiv2_exif_datum_iterator_next(Exiv2ExifDatumIterator *iter)
{
return iter->next();
}
DEFINE_FREE_FUNCTION(exiv2_exif_data, Exiv2ExifData*);
const char* exiv2_exif_datum_key(const Exiv2ExifDatum *datum)
{
return datum->datum.key().c_str();
}
const char* exiv2_exif_datum_to_string(const Exiv2ExifDatum *datum)
{
const std::string strval = datum->datum.toString();
return strdup(strval.c_str());
}
DEFINE_FREE_FUNCTION(exiv2_exif_datum, Exiv2ExifDatum*);
// ERRORS
int
exiv2_error_code(const Exiv2Error *error)
{
return error->code;
}
const char*
exiv2_error_what(const Exiv2Error *error)
{
return error->what;
}
void
exiv2_error_free(Exiv2Error *e)
{
if (e == 0) {
return;
}
free(e->what);
delete e;
}