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
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "eog-metadata-reader.h"
#include "eog-debug.h"
typedef enum {
EMR_READ = 0,
EMR_READ_SIZE_HIGH_BYTE,
EMR_READ_SIZE_LOW_BYTE,
EMR_READ_MARKER,
EMR_SKIP_BYTES,
EMR_READ_EXIF,
EMR_READ_ICC,
EMR_READ_IPTC,
EMR_FINISHED
} EogMetadataReaderState;
#define EOG_JPEG_MARKER_START 0xFF
#define EOG_JPEG_MARKER_SOI 0xD8
#define EOG_JPEG_MARKER_APP1 0xE1
#define EOG_JPEG_MARKER_APP2 0xE2
#define EOG_JPEG_MARKER_APP14 0xED
#define NO_DEBUG
struct _EogMetadataReaderPrivate {
EogMetadataReaderState state;
/* data fields */
gpointer exif_chunk;
guint exif_len;
gpointer iptc_chunk;
guint iptc_len;
gpointer icc_chunk;
guint icc_len;
/* management fields */
int size;
int last_marker;
int bytes_read;
};
G_DEFINE_TYPE (EogMetadataReader, eog_metadata_reader, G_TYPE_OBJECT)
static void
eog_metadata_reader_finalize (GObject *object)
{
EogMetadataReader *instance = EOG_METADATA_READER (object);
g_free (instance->priv);
instance->priv = NULL;
G_OBJECT_CLASS (eog_metadata_reader_parent_class)->finalize (object);
}
static void
eog_metadata_reader_dispose (GObject *object)
{
EogMetadataReader *emr = EOG_METADATA_READER (object);
if (emr->priv->exif_chunk != NULL) {
g_free (emr->priv->exif_chunk);
emr->priv->exif_chunk = NULL;
}
if (emr->priv->iptc_chunk != NULL) {
g_free (emr->priv->iptc_chunk);
emr->priv->iptc_chunk = NULL;
}
if (emr->priv->icc_chunk != NULL) {
g_free (emr->priv->icc_chunk);
emr->priv->icc_chunk = NULL;
}
G_OBJECT_CLASS (eog_metadata_reader_parent_class)->dispose (object);
}
static void
eog_metadata_reader_init (EogMetadataReader *obj)
{
EogMetadataReaderPrivate *priv;
priv = g_new0 (EogMetadataReaderPrivate, 1);
priv->exif_chunk = NULL;
priv->exif_len = 0;
priv->iptc_chunk = NULL;
priv->iptc_len = 0;
priv->icc_chunk = NULL;
priv->icc_len = 0;
obj->priv = priv;
}
static void
eog_metadata_reader_class_init (EogMetadataReaderClass *klass)
{
GObjectClass *object_class = (GObjectClass*) klass;
object_class->finalize = eog_metadata_reader_finalize;
object_class->dispose = eog_metadata_reader_dispose;
}
EogMetadataReader*
eog_metadata_reader_new (EogMetadataFileType type)
{
EogMetadataReader *emr;
/* CAUTION: check for type if we support more metadat-image-formats in the future */
emr = g_object_new (EOG_TYPE_METADATA_READER, NULL);
return emr;
}
gboolean
eog_metadata_reader_finished (EogMetadataReader *emr)
{
g_return_val_if_fail (EOG_IS_METADATA_READER (emr), TRUE);
return (emr->priv->state == EMR_FINISHED);
}
void
eog_metadata_reader_consume (EogMetadataReader *emr, guchar *buf, guint len)
{
EogMetadataReaderPrivate *priv;
int i;
g_return_if_fail (EOG_IS_METADATA_READER (emr));
priv = emr->priv;
if (priv->state == EMR_FINISHED) return;
for (i = 0; (i < len) && (priv->state != EMR_FINISHED); i++) {
switch (priv->state) {
case EMR_READ:
if (buf[i] == EOG_JPEG_MARKER_START) {
priv->state = EMR_READ_MARKER;
}
else {
priv->state = EMR_FINISHED;
}
break;
case EMR_READ_MARKER:
if ((buf [i] & 0xF0) == 0xE0) { /* we are reading some sort of APPxx marker */
/* these are always followed by 2 bytes of size information */
priv->last_marker = buf [i];
priv->size = 0;
priv->state = EMR_READ_SIZE_HIGH_BYTE;
eog_debug_message (DEBUG_IMAGE_DATA, "APPx Marker Found: %x", priv->last_marker);
}
else {
/* otherwise simply consume the byte */
priv->state = EMR_READ;
}
break;
case EMR_READ_SIZE_HIGH_BYTE:
priv->size = (buf [i] & 0xff) << 8;
priv->state = EMR_READ_SIZE_LOW_BYTE;
break;
case EMR_READ_SIZE_LOW_BYTE:
priv->size |= (buf [i] & 0xff);
if (priv->size > 2) /* ignore the two size-bytes */
priv->size -= 2;
if (priv->size == 0) {
priv->state = EMR_READ;
}
else if (priv->last_marker == EOG_JPEG_MARKER_APP1 &&
priv->exif_chunk == NULL)
{
priv->state = EMR_READ_EXIF;
}
else if (priv->last_marker == EOG_JPEG_MARKER_APP2 &&
priv->icc_chunk == NULL)
{
priv->state = EMR_READ_ICC;
}
else if (priv->last_marker == EOG_JPEG_MARKER_APP14 &&
priv->iptc_chunk == NULL)
{
priv->state = EMR_READ_IPTC;
}
else {
priv->state = EMR_SKIP_BYTES;
}
priv->last_marker = 0;
break;
case EMR_SKIP_BYTES:
eog_debug_message (DEBUG_IMAGE_DATA, "Skip bytes: %i", priv->size);
if (i + priv->size < len) {
i = i + priv->size - 1; /* the for-loop consumes the other byte */
priv->size = 0;
}
else {
priv->size = (i + priv->size) - len;
i = len - 1;
}
if (priv->size == 0) { /* don't need to skip any more bytes */
priv->state = EMR_READ;
}
break;
case EMR_READ_EXIF:
eog_debug_message (DEBUG_IMAGE_DATA, "Read EXIF data, Length: %i", priv->size);
if (priv->exif_chunk == NULL) {
priv->exif_chunk = g_new0 (guchar, priv->size);
priv->exif_len = priv->size;
priv->bytes_read = 0;
}
if (i + priv->size < len) {
/* read data in one block */
memcpy ((guchar*) (priv->exif_chunk) + priv->bytes_read, &buf[i], priv->size);
priv->state = EMR_READ;
i = i + priv->size - 1; /* the for-loop consumes the other byte */
}
else {
int chunk_len = len - i;
memcpy ((guchar*) (priv->exif_chunk) + priv->bytes_read, &buf[i], chunk_len);
priv->bytes_read += chunk_len; /* bytes already read */
priv->size = (i + priv->size) - len; /* remaining data to read */
i = len - 1;
priv->state = EMR_READ_EXIF;
}
if (priv->exif_chunk != NULL && priv->icc_chunk != NULL && priv->iptc_chunk != NULL)
priv->state = EMR_FINISHED;
break;
case EMR_READ_ICC:
eog_debug_message (DEBUG_IMAGE_DATA, "Read ICC data, Length: %i\n", priv->size);
if (priv->icc_chunk == NULL) {
priv->icc_chunk = g_new0 (guchar, priv->size);
priv->icc_len = priv->size;
priv->bytes_read = 0;
}
if (i + priv->size < len) {
/* read data in one block */
memcpy ((guchar*) (priv->icc_chunk) + priv->bytes_read, &buf[i], priv->size);
priv->state = EMR_READ;
i = i + priv->size - 1; /* the for-loop consumes the other byte */
}
else {
int chunk_len = len - i;
memcpy ((guchar*) (priv->icc_chunk) + priv->bytes_read, &buf[i], chunk_len);
priv->bytes_read += chunk_len; /* bytes already read */
priv->size = (i + priv->size) - len; /* remaining data to read */
i = len - 1;
priv->state = EMR_READ_ICC;
}
if (priv->exif_chunk != NULL && priv->icc_chunk != NULL && priv->iptc_chunk != NULL)
priv->state = EMR_FINISHED;
break;
case EMR_READ_IPTC:
if (priv->iptc_chunk == NULL) {
priv->iptc_chunk = g_new0 (guchar, priv->size);
priv->iptc_len = priv->size;
priv->bytes_read = 0;
}
if (i + priv->size < len) {
/* read data in one block */
memcpy ((guchar*) (priv->iptc_chunk) + priv->bytes_read, &buf[i], priv->size);
priv->state = EMR_READ;
}
else {
int chunk_len = len - i;
memcpy ((guchar*) (priv->iptc_chunk) + priv->bytes_read, &buf[i], chunk_len);
priv->bytes_read += chunk_len; /* bytes already read */
priv->size = (i + priv->size) - len; /* remaining data to read */
i = len - 1;
priv->state = EMR_READ_IPTC;
}
if (priv->exif_chunk != NULL && priv->icc_chunk != NULL && priv->iptc_chunk != NULL)
priv->state = EMR_FINISHED;
break;
default:
g_assert_not_reached ();
}
}
}
/* Returns the raw exif data. NOTE: The caller of this function becomes
* the new owner of this piece of memory and is responsible for freeing it!
*/
void
eog_metadata_reader_get_exif_chunk (EogMetadataReader *emr, guchar **data, guint *len)
{
EogMetadataReaderPrivate *priv;
g_return_if_fail (EOG_IS_METADATA_READER (emr));
priv = emr->priv;
*data = (guchar*) priv->exif_chunk;
*len = priv->exif_len;
priv->exif_chunk = NULL;
priv->exif_len = 0;
}
#if HAVE_EXIF
ExifData*
eog_metadata_reader_get_exif_data (EogMetadataReader *emr)
{
EogMetadataReaderPrivate *priv;
ExifData *data = NULL;
g_return_val_if_fail (EOG_IS_METADATA_READER (emr), NULL);
priv = emr->priv;
if (priv->exif_chunk != NULL) {
data = exif_data_new_from_data (priv->exif_chunk, priv->exif_len);
}
return data;
}
#endif
/*
* TODO: very broken, assumes the profile fits in a single chunk. Change to
* parse the sections and construct a single memory chunk, or maybe even parse
* the profile.
*/
void
eog_metadata_reader_get_icc_chunk (EogMetadataReader *emr, guchar **data, guint *len)
{
EogMetadataReaderPrivate *priv;
g_return_if_fail (EOG_IS_METADATA_READER (emr));
priv = emr->priv;
if (priv->icc_chunk) {
*data = (guchar*) priv->icc_chunk + 14;
*len = priv->icc_len - 14;
}
}
|