3 * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <libexif/exif-mnote-data.h>
24 #include <libexif/exif-data.h>
25 #include <libexif/exif-ifd.h>
26 #include <libexif/exif-mnote-data-priv.h>
27 #include <libexif/exif-utils.h>
28 #include <libexif/exif-loader.h>
29 #include <libexif/exif-log.h>
30 #include <libexif/i18n.h>
32 #include <libexif/olympus/exif-mnote-data-olympus.h>
33 #include <libexif/canon/exif-mnote-data-canon.h>
34 #include <libexif/pentax/exif-mnote-data-pentax.h>
41 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 #if defined(__WATCOMC__) || defined(_MSC_VER)
44 # define strncasecmp strnicmp
47 #undef JPEG_MARKER_SOI
48 #define JPEG_MARKER_SOI 0xd8
49 #undef JPEG_MARKER_APP0
50 #define JPEG_MARKER_APP0 0xe0
51 #undef JPEG_MARKER_APP1
52 #define JPEG_MARKER_APP1 0xe1
54 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
56 struct _ExifDataPrivate
65 unsigned int ref_count;
67 /* Temporarily used while loading data */
68 unsigned int offset_mnote;
70 ExifDataOption options;
71 ExifDataType data_type;
75 exif_data_alloc (ExifData *data, unsigned int i)
79 if (!data || !i) return NULL;
81 d = exif_mem_alloc (data->priv->mem, i);
84 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", i);
89 exif_data_get_mnote_data (ExifData *d)
91 return (d && d->priv) ? d->priv->md : NULL;
97 ExifMem *mem = exif_mem_new_default ();
98 ExifData *d = exif_data_new_mem (mem);
100 exif_mem_unref (mem);
106 exif_data_new_mem (ExifMem *mem)
111 if (!mem) return NULL;
113 data = exif_mem_alloc (mem, sizeof (ExifData));
114 if (!data) return (NULL);
115 data->priv = exif_mem_alloc (mem, sizeof (ExifDataPrivate));
117 exif_mem_free (mem, data);
120 data->priv->ref_count = 1;
122 data->priv->mem = mem;
125 for (i = 0; i < EXIF_IFD_COUNT; i++) {
126 data->ifd[i] = exif_content_new_mem (data->priv->mem);
128 exif_data_free (data);
131 data->ifd[i]->parent = data;
134 /* Default options */
135 exif_data_set_option (data, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
136 exif_data_set_option (data, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
138 /* Default data type: none */
139 exif_data_set_data_type (data, EXIF_DATA_TYPE_COUNT);
145 exif_data_new_from_data (const unsigned char *data, unsigned int size)
149 edata = exif_data_new ();
150 exif_data_load_data (edata, data, size);
155 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
156 const unsigned char *d,
157 unsigned int size, unsigned int offset)
159 unsigned int s, doff;
161 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
162 entry->format = exif_get_short (d + offset + 2, data->priv->order);
163 entry->components = exif_get_long (d + offset + 4, data->priv->order);
165 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
166 "Loading entry 0x%x ('%s')...", entry->tag,
167 exif_tag_get_name (entry->tag));
170 * Size? If bigger than 4 bytes, the actual data is not
171 * in the entry but somewhere else (offset).
173 s = exif_format_get_size (entry->format) * entry->components;
177 doff = exif_get_long (d + offset + 8, data->priv->order);
185 entry->data = exif_data_alloc (data, s);
188 memcpy (entry->data, d + doff, s);
191 /* If this is the MakerNote, remember the offset */
192 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
193 if (entry->size > 6) exif_log (data->priv->log,
194 EXIF_LOG_CODE_DEBUG, "ExifData",
195 "MakerNote found (%02x %02x %02x %02x "
196 "%02x %02x %02x...).",
197 entry->data[0], entry->data[1], entry->data[2],
198 entry->data[3], entry->data[4], entry->data[5],
200 data->priv->offset_mnote = doff;
205 exif_data_save_data_entry (ExifData *data, ExifEntry *e,
206 unsigned char **d, unsigned int *ds,
209 unsigned int doff, s;
211 if (!data || !data->priv) return;
214 * Each entry is 12 bytes long. The memory for the entry has
215 * already been allocated.
217 exif_set_short (*d + 6 + offset + 0,
218 data->priv->order, (ExifShort) e->tag);
219 exif_set_short (*d + 6 + offset + 2,
220 data->priv->order, (ExifShort) e->format);
222 #ifndef EXIF_DONT_CHANGE_MAKER_NOTE
223 /* If this is the maker note tag, update it. */
224 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
225 exif_mem_free (data->priv->mem, e->data);
228 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
229 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
230 e->components = e->size;
234 exif_set_long (*d + 6 + offset + 4,
235 data->priv->order, e->components);
238 * Size? If bigger than 4 bytes, the actual data is not in
239 * the entry but somewhere else.
241 s = exif_format_get_size (e->format) * e->components;
247 * According to the TIFF specification,
248 * the offset must be an even number. If we need to introduce
249 * a padding byte, we set it to 0.
252 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
254 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
257 exif_set_long (*d + 6 + offset + 8, data->priv->order, doff);
258 if (s & 1) *(*d + *ds - 1) = '\0';
263 /* Write the data. Fill unneeded bytes with 0. */
264 memcpy (*d + 6 + doff, e->data, s);
265 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
269 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
270 unsigned int ds, ExifLong offset, ExifLong size)
272 if (ds < offset + size) {
273 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
274 "Bogus thumbnail offset and size: %i < %i + %i.",
275 (int) ds, (int) offset, (int) size);
278 if (data->data) exif_mem_free (data->priv->mem, data->data);
280 data->data = exif_data_alloc (data, data->size);
281 if (!data->data) return;
282 memcpy (data->data, d + offset, data->size);
286 #define CHECK_REC(i) \
288 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
289 "ExifData", "Recursive entry in IFD " \
290 "'%s' detected. Skipping...", \
291 exif_ifd_get_name (i)); \
294 if (data->ifd[(i)]->count) { \
295 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, \
296 "ExifData", "Attemt to load IFD " \
297 "'%s' multiple times detected. " \
299 exif_ifd_get_name (i)); \
304 exif_data_load_data_content (ExifData *data, ExifIfd ifd,
305 const unsigned char *d,
306 unsigned int ds, unsigned int offset, unsigned int recursion_depth)
308 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
314 if (!data || !data->priv) return;
315 if ((ifd < 0) || (ifd >= EXIF_IFD_COUNT)) return;
317 if (recursion_depth > 150) {
318 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
319 "Deep recursion detected!");
323 /* Read the number of entries */
324 if (offset >= ds - 1) return;
325 n = exif_get_short (d + offset, data->priv->order);
326 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
327 "Loading %i entries...", n);
330 /* Check if we have enough data. */
331 if (offset + 12 * n > ds) n = (ds - offset) / 12;
333 for (i = 0; i < n; i++) {
335 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
337 case EXIF_TAG_EXIF_IFD_POINTER:
338 case EXIF_TAG_GPS_INFO_IFD_POINTER:
339 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
340 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
341 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
342 o = exif_get_long (d + offset + 12 * i + 8,
345 case EXIF_TAG_EXIF_IFD_POINTER:
346 CHECK_REC (EXIF_IFD_EXIF);
347 exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
349 case EXIF_TAG_GPS_INFO_IFD_POINTER:
350 CHECK_REC (EXIF_IFD_GPS);
351 exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
353 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
354 CHECK_REC (EXIF_IFD_INTEROPERABILITY);
355 exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
357 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
358 thumbnail_offset = o;
359 if (thumbnail_offset && thumbnail_length)
360 exif_data_load_data_thumbnail (data, d,
361 ds, thumbnail_offset,
364 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
365 thumbnail_length = o;
366 if (thumbnail_offset && thumbnail_length)
367 exif_data_load_data_thumbnail (data, d,
368 ds, thumbnail_offset,
378 * If we don't know the tag, don't fail. It could be that new
379 * versions of the standard have defined additional tags. Note that
380 * 0 is a valid tag in the GPS IFD.
382 if (!exif_tag_get_name_in_ifd (tag, ifd)) {
385 * Special case: Tag and format 0. That's against specification.
386 * At least up to 2.2. But Photoshop writes it anyways.
388 if (!memcmp (d + offset + 12 * i, "\0\0\0\0", 4)) {
389 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
390 "Skipping empty entry at position %i in '%s'.", i,
391 exif_ifd_get_name (ifd));
394 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
395 "Unknown tag 0x%04x (entry %i in '%s'). Please report this tag "
396 "to <libexif-devel@lists.sourceforge.net>.", tag, i,
397 exif_ifd_get_name (ifd));
398 if (data->priv->options & EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS)
401 entry = exif_entry_new_mem (data->priv->mem);
402 exif_data_load_data_entry (data, entry, d, ds,
404 exif_content_add_entry (data->ifd[ifd], entry);
405 exif_entry_unref (entry);
412 cmp_func (const unsigned char *p1, const unsigned char *p2, ExifByteOrder o)
414 ExifShort tag1 = exif_get_short (p1, o);
415 ExifShort tag2 = exif_get_short (p2, o);
417 return (tag1 < tag2) ? -1 : (tag1 > tag2) ? 1 : 0;
421 cmp_func_intel (const void *elem1, const void *elem2)
423 return cmp_func ((const unsigned char *) elem1,
424 (const unsigned char *) elem2, EXIF_BYTE_ORDER_INTEL);
428 cmp_func_motorola (const void *elem1, const void *elem2)
430 return cmp_func ((const unsigned char *) elem1,
431 (const unsigned char *) elem2, EXIF_BYTE_ORDER_MOTOROLA);
435 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
436 unsigned char **d, unsigned int *ds,
439 unsigned int j, n_ptr = 0, n_thumb = 0;
442 if (!data || !data->priv || !ifd || !d || !ds) return;
444 for (i = 0; i < EXIF_IFD_COUNT; i++)
445 if (ifd == data->ifd[i])
447 if (i == EXIF_IFD_COUNT)
451 * Check if we need some extra entries for pointers or the thumbnail.
457 * The pointer to IFD_EXIF is in IFD_0. The pointer to
458 * IFD_INTEROPERABILITY is in IFD_EXIF.
460 if (data->ifd[EXIF_IFD_EXIF]->count ||
461 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
464 /* The pointer to IFD_GPS is in IFD_0. */
465 if (data->ifd[EXIF_IFD_GPS]->count)
474 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
481 * Allocate enough memory for all entries
482 * and the number of entries.
484 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
485 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
487 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData", *ds);
491 /* Save the number of entries */
492 exif_set_short (*d + 6 + offset, data->priv->order,
493 (ExifShort) (ifd->count + n_ptr + n_thumb));
496 /* Save each entry */
497 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
498 "Saving %i entries (IFD '%s', offset: %i)...",
499 ifd->count, exif_ifd_get_name (i), offset);
500 for (j = 0; j < ifd->count; j++)
501 exif_data_save_data_entry (data, ifd->entries[j], d, ds, offset + 12 * j);
503 offset += 12 * ifd->count;
505 /* Now save special entries. */
510 * The pointer to IFD_EXIF is in IFD_0.
511 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
512 * therefore, if IFD_INTEROPERABILITY is not empty, we need
513 * IFD_EXIF even if latter is empty.
515 if (data->ifd[EXIF_IFD_EXIF]->count ||
516 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
517 exif_set_short (*d + 6 + offset + 0, data->priv->order,
518 EXIF_TAG_EXIF_IFD_POINTER);
519 exif_set_short (*d + 6 + offset + 2, data->priv->order,
521 exif_set_long (*d + 6 + offset + 4, data->priv->order,
523 exif_set_long (*d + 6 + offset + 8, data->priv->order,
525 exif_data_save_data_content (data,
526 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
530 /* The pointer to IFD_GPS is in IFD_0, too. */
531 if (data->ifd[EXIF_IFD_GPS]->count) {
532 exif_set_short (*d + 6 + offset + 0, data->priv->order,
533 EXIF_TAG_GPS_INFO_IFD_POINTER);
534 exif_set_short (*d + 6 + offset + 2, data->priv->order,
536 exif_set_long (*d + 6 + offset + 4, data->priv->order,
538 exif_set_long (*d + 6 + offset + 8, data->priv->order,
540 exif_data_save_data_content (data,
541 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
549 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
552 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
553 exif_set_short (*d + 6 + offset + 0, data->priv->order,
554 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
555 exif_set_short (*d + 6 + offset + 2, data->priv->order,
557 exif_set_long (*d + 6 + offset + 4, data->priv->order,
559 exif_set_long (*d + 6 + offset + 8, data->priv->order,
561 exif_data_save_data_content (data,
562 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
571 * Information about the thumbnail (if any) is saved in
576 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
577 exif_set_short (*d + 6 + offset + 0, data->priv->order,
578 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
579 exif_set_short (*d + 6 + offset + 2, data->priv->order,
581 exif_set_long (*d + 6 + offset + 4, data->priv->order,
583 exif_set_long (*d + 6 + offset + 8, data->priv->order,
586 *d = exif_mem_realloc (data->priv->mem, *d, *ds);
588 EXIF_LOG_NO_MEMORY (data->priv->log, "ExifData",
592 memcpy (*d + *ds - data->size, data->data, data->size);
595 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
596 exif_set_short (*d + 6 + offset + 0, data->priv->order,
597 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
598 exif_set_short (*d + 6 + offset + 2, data->priv->order,
600 exif_set_long (*d + 6 + offset + 4, data->priv->order,
602 exif_set_long (*d + 6 + offset + 8, data->priv->order,
612 /* Sort the directory according to TIFF specification */
613 qsort (*d + 6 + offset - (ifd->count + n_ptr + n_thumb) * 12,
614 (ifd->count + n_ptr + n_thumb), 12,
615 (data->priv->order == EXIF_BYTE_ORDER_INTEL) ? cmp_func_intel : cmp_func_motorola);
617 /* Correctly terminate the directory */
618 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
622 * We are saving IFD 0. Tell where IFD 1 starts and save
625 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
626 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
629 exif_set_long (*d + 6 + offset, data->priv->order, 0);
633 EXIF_DATA_TYPE_MAKER_NOTE_NONE = 0,
634 EXIF_DATA_TYPE_MAKER_NOTE_CANON = 1,
635 EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS = 2,
636 EXIF_DATA_TYPE_MAKER_NOTE_PENTAX = 3
637 } ExifDataTypeMakerNote;
639 static ExifDataTypeMakerNote
640 exif_data_get_type_maker_note (ExifData *d)
645 if (!d) return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
647 e = exif_data_get_entry (d, EXIF_TAG_MAKER_NOTE);
648 if (!e) return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
650 /* Olympus & Nikon */
651 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5) ||
652 !memcmp (e->data, "Nikon", 5)))
653 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
655 em = exif_data_get_entry (d, EXIF_TAG_MAKE);
656 if (!em) return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
659 if (!strcmp (exif_entry_get_value (em, value, sizeof (value)), "Canon"))
660 return EXIF_DATA_TYPE_MAKER_NOTE_CANON;
662 /* Pentax & some variant of Nikon */
663 if ((e->size >= 2) && (e->data[0] == 0x00) && (e->data[1] == 0x1b)) {
665 exif_entry_get_value (em, value, sizeof(value)),
667 return EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS;
669 return EXIF_DATA_TYPE_MAKER_NOTE_PENTAX;
672 return EXIF_DATA_TYPE_MAKER_NOTE_NONE;
675 #define LOG_TOO_SMALL \
676 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData", \
677 _("Size of data too small to allow for EXIF data."));
680 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
681 unsigned int ds_orig)
686 const unsigned char *d = d_orig;
687 unsigned int ds = ds_orig, len;
689 if (!data || !data->priv || !d || !ds) return;
691 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
692 "Parsing %i byte(s) EXIF data...\n", ds);
695 * It can be that the data starts with the EXIF header. If it does
696 * not, search the EXIF marker.
702 if (!memcmp (d, ExifHeader, 6)) {
703 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
704 "Found EXIF header.");
707 while ((d[0] == 0xff) && ds) {
712 /* JPEG_MARKER_SOI */
713 if (d[0] == JPEG_MARKER_SOI) {
719 /* JPEG_MARKER_APP0 */
720 if (d[0] == JPEG_MARKER_APP0) {
723 l = (d[0] << 8) | d[1];
731 /* JPEG_MARKER_APP1 */
732 if (d[0] == JPEG_MARKER_APP1)
735 /* Unknown marker or data. Give up. */
736 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
737 "ExifData", _("EXIF marker not found."));
746 len = (d[0] << 8) | d[1];
747 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
748 "We have to deal with %i byte(s) of EXIF data.",
755 * Verify the exif header
756 * (offset 2, length 6).
762 if (memcmp (d, ExifHeader, 6)) {
763 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
764 "ExifData", _("EXIF header not found."));
768 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
769 "Found EXIF header.");
771 /* Byte order (offset 6, length 2) */
774 if (!memcmp (d + 6, "II", 2))
775 data->priv->order = EXIF_BYTE_ORDER_INTEL;
776 else if (!memcmp (d + 6, "MM", 2))
777 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
779 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
780 "ExifData", _("Unknown encoding."));
785 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
789 offset = exif_get_long (d + 10, data->priv->order);
790 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
791 "IFD 0 at %i.", (int) offset);
793 /* Parse the actual exif data (usually offset 14 from start) */
794 exif_data_load_data_content (data, EXIF_IFD_0, d + 6, ds - 6, offset, 0);
797 if (offset + 6 + 2 > ds) {
800 n = exif_get_short (d + 6 + offset, data->priv->order);
801 if (offset + 6 + 2 + 12 * n + 4 > ds) {
804 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
806 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
807 "IFD 1 at %i.", (int) offset);
810 if (offset > ds - 6) {
811 exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA,
812 "ExifData", "Bogus offset.");
816 exif_data_load_data_content (data, EXIF_IFD_1, d + 6, ds - 6, offset, 0);
820 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
821 * cameras use pointers in the maker note tag that point to the
822 * space between IFDs. Here is the only place where we have access
825 switch (exif_data_get_type_maker_note (data)) {
826 case EXIF_DATA_TYPE_MAKER_NOTE_OLYMPUS:
827 data->priv->md = exif_mnote_data_olympus_new (data->priv->mem);
829 case EXIF_DATA_TYPE_MAKER_NOTE_PENTAX:
830 data->priv->md = exif_mnote_data_pentax_new (data->priv->mem);
832 case EXIF_DATA_TYPE_MAKER_NOTE_CANON:
833 data->priv->md = exif_mnote_data_canon_new (data->priv->mem);
840 * If we are able to interpret the maker note, do so.
842 if (data->priv->md) {
843 exif_mnote_data_log (data->priv->md, data->priv->log);
844 exif_mnote_data_set_byte_order (data->priv->md,
846 exif_mnote_data_set_offset (data->priv->md,
847 data->priv->offset_mnote);
848 exif_mnote_data_load (data->priv->md, d, ds);
851 if (data->priv->options & EXIF_DATA_OPTION_FOLLOW_SPECIFICATION)
852 exif_data_fix (data);
856 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
858 if (!data || !d || !ds)
863 *d = exif_data_alloc (data, *ds);
865 memcpy (*d, ExifHeader, 6);
867 /* Order (offset 6) */
868 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
869 memcpy (*d + 6, "II", 2);
871 memcpy (*d + 6, "MM", 2);
874 /* Fixed value (2 bytes, offset 8) */
875 exif_set_short (*d + 8, data->priv->order, 0x002a);
878 * IFD 0 offset (4 bytes, offset 10).
879 * We will start 8 bytes after the
880 * EXIF header (2 bytes for order, another 2 for the test, and
881 * 4 bytes for the IFD 0 offset make 8 bytes together).
883 exif_set_long (*d + 10, data->priv->order, 8);
885 /* Now save IFD 0. IFD 1 will be saved automatically. */
886 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
888 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
890 exif_log (data->priv->log, EXIF_LOG_CODE_DEBUG, "ExifData",
891 "Saved %i byte(s) EXIF data.", *ds);
895 exif_data_new_from_file (const char *path)
900 loader = exif_loader_new ();
901 exif_loader_write_file (loader, path);
902 edata = exif_loader_get_data (loader);
903 exif_loader_unref (loader);
909 exif_data_ref (ExifData *data)
914 data->priv->ref_count++;
918 exif_data_unref (ExifData *data)
922 data->priv->ref_count--;
923 if (!data->priv->ref_count) exif_data_free (data);
927 exif_data_free (ExifData *data)
930 ExifMem *mem = (data && data->priv) ? data->priv->mem : NULL;
934 for (i = 0; i < EXIF_IFD_COUNT; i++) {
936 exif_content_unref (data->ifd[i]);
942 exif_mem_free (mem, data->data);
947 if (data->priv->log) {
948 exif_log_unref (data->priv->log);
949 data->priv->log = NULL;
951 if (data->priv->md) {
952 exif_mnote_data_unref (data->priv->md);
953 data->priv->md = NULL;
955 exif_mem_free (mem, data->priv);
956 exif_mem_free (mem, data);
959 exif_mem_unref (mem);
963 exif_data_dump (ExifData *data)
970 for (i = 0; i < EXIF_IFD_COUNT; i++) {
971 if (data->ifd[i] && data->ifd[i]->count) {
972 printf ("Dumping IFD '%s'...\n",
973 exif_ifd_get_name (i));
974 exif_content_dump (data->ifd[i], 0);
979 printf ("%i byte(s) thumbnail data available.", data->size);
980 if (data->size >= 4) {
981 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
982 data->data[0], data->data[1],
983 data->data[data->size - 2],
984 data->data[data->size - 1]);
990 exif_data_get_byte_order (ExifData *data)
995 return (data->priv->order);
999 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
1007 for (i = 0; i < EXIF_IFD_COUNT; i++)
1008 func (data->ifd[i], user_data);
1011 typedef struct _ByteOrderChangeData ByteOrderChangeData;
1012 struct _ByteOrderChangeData {
1013 ExifByteOrder old, new;
1017 entry_set_byte_order (ExifEntry *e, void *data)
1019 ByteOrderChangeData *d = data;
1024 exif_array_set_byte_order (e->format, e->data, e->components, d->old, d->new);
1028 content_set_byte_order (ExifContent *content, void *data)
1030 exif_content_foreach_entry (content, entry_set_byte_order, data);
1034 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
1036 ByteOrderChangeData d;
1038 if (!data || (order == data->priv->order))
1041 d.old = data->priv->order;
1043 exif_data_foreach_content (data, content_set_byte_order, &d);
1044 data->priv->order = order;
1046 exif_mnote_data_set_byte_order (data->priv->md, order);
1050 exif_data_log (ExifData *data, ExifLog *log)
1054 if (!data || !data->priv) return;
1055 exif_log_unref (data->priv->log);
1056 data->priv->log = log;
1059 for (i = 0; i < EXIF_IFD_COUNT; i++)
1060 exif_content_log (data->ifd[i], log);
1063 /* Used internally within libexif */
1064 ExifLog *exif_data_get_log (ExifData *);
1066 exif_data_get_log (ExifData *data)
1068 if (!data || !data->priv) return NULL;
1069 return data->priv->log;
1073 ExifDataOption option;
1075 const char *description;
1076 } exif_data_option[] = {
1077 {EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS, N_("Ignore unknown tags"),
1078 N_("Ignore unknown tags when loading EXIF data.")},
1079 {EXIF_DATA_OPTION_FOLLOW_SPECIFICATION, N_("Follow specification"),
1080 N_("Add, correct and remove entries to get EXIF data that follows "
1081 "the specification.")},
1086 exif_data_option_get_name (ExifDataOption o)
1090 for (i = 0; exif_data_option[i].name; i++)
1091 if (exif_data_option[i].option == o) break;
1092 return _(exif_data_option[i].name);
1096 exif_data_option_get_description (ExifDataOption o)
1100 for (i = 0; exif_data_option[i].description; i++)
1101 if (exif_data_option[i].option == o) break;
1102 return _(exif_data_option[i].description);
1106 exif_data_set_option (ExifData *d, ExifDataOption o)
1110 d->priv->options |= o;
1114 exif_data_unset_option (ExifData *d, ExifDataOption o)
1118 d->priv->options &= ~o;
1122 fix_func (ExifContent *c, void *data)
1124 switch (exif_content_get_ifd (c)) {
1126 if (c->parent->data)
1127 exif_content_fix (c);
1129 exif_log (c->parent->priv->log, EXIF_LOG_CODE_DEBUG, "exif-data",
1130 "No thumbnail but entries on thumbnail. These entries have been "
1133 exif_content_remove_entry (c, c->entries[c->count - 1]);
1137 exif_content_fix (c);
1142 exif_data_fix (ExifData *d)
1144 exif_data_foreach_content (d, fix_func, NULL);
1148 exif_data_set_data_type (ExifData *d, ExifDataType dt)
1150 if (!d || !d->priv) return;
1152 d->priv->data_type = dt;
1156 exif_data_get_data_type (ExifData *d)
1158 return (d && d->priv) ? d->priv->data_type : EXIF_DATA_TYPE_COUNT;