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.
22 #include "exif-mnote-data.h"
24 #include "exif-data.h"
26 #include "exif-mnote-data-priv.h"
27 #include "exif-utils.h"
28 #include "exif-loader.h"
29 #include "jpeg-marker.h"
31 #include <libexif/olympus/exif-mnote-data-olympus.h>
32 #include <libexif/canon/exif-mnote-data-canon.h>
33 #include <libexif/pentax/exif-mnote-data-pentax.h>
40 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
44 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
46 struct _ExifDataPrivate
52 unsigned int ref_count;
54 /* Temporarily used while loading data */
55 unsigned int offset_mnote;
59 exif_data_get_mnote_data (ExifData *d)
61 return (d && d->priv) ? d->priv->md : NULL;
70 data = malloc (sizeof (ExifData));
73 memset (data, 0, sizeof (ExifData));
74 data->priv = malloc (sizeof (ExifDataPrivate));
79 memset (data->priv, 0, sizeof (ExifDataPrivate));
80 data->priv->ref_count = 1;
82 for (i = 0; i < EXIF_IFD_COUNT; i++) {
83 data->ifd[i] = exif_content_new ();
85 exif_data_free (data);
88 data->ifd[i]->parent = data;
95 exif_data_new_from_data (const unsigned char *data, unsigned int size)
99 edata = exif_data_new ();
100 exif_data_load_data (edata, data, size);
105 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
106 const unsigned char *d,
107 unsigned int size, unsigned int offset)
109 unsigned int s, doff;
111 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
112 entry->format = exif_get_short (d + offset + 2, data->priv->order);
113 entry->components = exif_get_long (d + offset + 4, data->priv->order);
116 * Size? If bigger than 4 bytes, the actual data is not
117 * in the entry but somewhere else (offset).
119 s = exif_format_get_size (entry->format) * entry->components;
123 doff = exif_get_long (d + offset + 8, data->priv->order);
131 entry->data = malloc (sizeof (char) * s);
135 memcpy (entry->data, d + doff, s);
137 /* If this is the MakerNote, remember the offset */
138 if (entry->tag == EXIF_TAG_MAKER_NOTE) {
140 printf ("%02x %02x %02x %02x %02x %02x %02x\n",
141 entry->data[0], entry->data[1], entry->data[2],
142 entry->data[3], entry->data[4], entry->data[5],
145 data->priv->offset_mnote = doff;
150 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
151 unsigned char **d, unsigned int *ds,
154 unsigned int doff, s;
157 * Each entry is 12 bytes long. The memory for the entry has
158 * already been allocated.
160 exif_set_short (*d + 6 + offset + 0,
161 data->priv->order, entry->tag);
162 exif_set_short (*d + 6 + offset + 2,
163 data->priv->order, entry->format);
164 exif_set_long (*d + 6 + offset + 4,
165 data->priv->order, entry->components);
168 * Size? If bigger than 4 bytes, the actual data is not in
169 * the entry but somewhere else.
171 s = exif_format_get_size (entry->format) * entry->components;
176 *d = realloc (*d, sizeof (char) * *ds);
177 doff = *ds - 6 - entry->size;
178 exif_set_long (*d + 6 + offset + 8,
179 data->priv->order, doff);
183 /* Write the data. Fill unneeded bytes with 0. */
184 memcpy (*d + 6 + doff, entry->data, entry->size);
185 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
189 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
190 unsigned int ds, ExifLong offset, ExifLong size)
192 if (ds < offset + size) {
194 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
195 (int) ds, (int) offset, (int) size);
202 data->data = malloc (sizeof (char) * data->size);
203 memcpy (data->data, d + offset, data->size);
207 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
208 const unsigned char *d,
209 unsigned int ds, unsigned int offset)
211 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
217 /* Read the number of entries */
218 if (offset >= ds - 1) return;
219 n = exif_get_short (d + offset, data->priv->order);
221 printf ("Loading %i entries...\n", n);
225 /* Check if we have enough data. */
226 if (offset + 12 * n > ds) n = (ds - offset) / 12;
228 for (i = 0; i < n; i++) {
230 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
232 printf ("Loading entry '%s' (%i of %i)...\n",
233 exif_tag_get_name (tag), i + 1, n);
236 case EXIF_TAG_EXIF_IFD_POINTER:
237 case EXIF_TAG_GPS_INFO_IFD_POINTER:
238 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
239 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
240 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
241 o = exif_get_long (d + offset + 12 * i + 8,
244 case EXIF_TAG_EXIF_IFD_POINTER:
245 exif_data_load_data_content (data,
246 data->ifd[EXIF_IFD_EXIF], d, ds, o);
248 case EXIF_TAG_GPS_INFO_IFD_POINTER:
249 exif_data_load_data_content (data,
250 data->ifd[EXIF_IFD_GPS], d, ds, o);
252 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
253 exif_data_load_data_content (data,
254 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
256 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
258 printf ("Thumbnail at %i.\n", (int) o);
260 thumbnail_offset = o;
261 if (thumbnail_offset && thumbnail_length)
262 exif_data_load_data_thumbnail (data, d,
263 ds, thumbnail_offset,
266 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
268 printf ("Thumbnail size: %i.\n", (int) o);
270 thumbnail_length = o;
271 if (thumbnail_offset && thumbnail_length)
272 exif_data_load_data_thumbnail (data, d,
273 ds, thumbnail_offset,
281 entry = exif_entry_new ();
282 exif_content_add_entry (ifd, entry);
283 exif_data_load_data_entry (data, entry, d, ds,
285 exif_entry_unref (entry);
292 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
293 unsigned char **d, unsigned int *ds,
296 unsigned int j, n_ptr = 0, n_thumb = 0;
299 if (!data || !ifd || !d || !ds)
302 for (i = 0; i < EXIF_IFD_COUNT; i++)
303 if (ifd == data->ifd[i])
305 if (i == EXIF_IFD_COUNT)
309 * Check if we need some extra entries for pointers or the thumbnail.
315 * The pointer to IFD_EXIF is in IFD_0. The pointer to
316 * IFD_INTEROPERABILITY is in IFD_EXIF.
318 if (data->ifd[EXIF_IFD_EXIF]->count ||
319 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
322 /* The pointer to IFD_GPS is in IFD_0. */
323 if (data->ifd[EXIF_IFD_GPS]->count)
332 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
339 * Allocate enough memory for all entries
340 * and the number of entries.
342 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
343 *d = realloc (*d, sizeof (char) * *ds);
345 /* Save the number of entries */
346 exif_set_short (*d + 6 + offset, data->priv->order,
347 ifd->count + n_ptr + n_thumb);
351 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
352 ifd->count, exif_ifd_get_name (i), offset);
355 /* Save each entry */
356 for (j = 0; j < ifd->count; j++)
357 exif_data_save_data_entry (data, ifd->entries[j],
358 d, ds, offset + 12 * j);
359 offset += 12 * ifd->count;
361 /* Now save special entries. */
366 * The pointer to IFD_EXIF is in IFD_0.
367 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
368 * therefore, if IFD_INTEROPERABILITY is not empty, we need
369 * IFD_EXIF even if latter is empty.
371 if (data->ifd[EXIF_IFD_EXIF]->count ||
372 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
373 exif_set_short (*d + 6 + offset + 0, data->priv->order,
374 EXIF_TAG_EXIF_IFD_POINTER);
375 exif_set_short (*d + 6 + offset + 2, data->priv->order,
377 exif_set_long (*d + 6 + offset + 4, data->priv->order,
379 exif_set_long (*d + 6 + offset + 8, data->priv->order,
381 exif_data_save_data_content (data,
382 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
386 /* The pointer to IFD_GPS is in IFD_0, too. */
387 if (data->ifd[EXIF_IFD_GPS]->count) {
388 exif_set_short (*d + 6 + offset + 0, data->priv->order,
389 EXIF_TAG_GPS_INFO_IFD_POINTER);
390 exif_set_short (*d + 6 + offset + 2, data->priv->order,
392 exif_set_long (*d + 6 + offset + 4, data->priv->order,
394 exif_set_long (*d + 6 + offset + 8, data->priv->order,
396 exif_data_save_data_content (data,
397 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
405 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
408 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
409 exif_set_short (*d + 6 + offset + 0, data->priv->order,
410 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
411 exif_set_short (*d + 6 + offset + 2, data->priv->order,
413 exif_set_long (*d + 6 + offset + 4, data->priv->order,
415 exif_set_long (*d + 6 + offset + 8, data->priv->order,
417 exif_data_save_data_content (data,
418 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
427 * Information about the thumbnail (if any) is saved in
432 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
433 exif_set_short (*d + 6 + offset + 0, data->priv->order,
434 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
435 exif_set_short (*d + 6 + offset + 2, data->priv->order,
437 exif_set_long (*d + 6 + offset + 4, data->priv->order,
439 exif_set_long (*d + 6 + offset + 8, data->priv->order,
442 *d = realloc (*d, sizeof (char) * *ds);
443 memcpy (*d + *ds - data->size, data->data, data->size);
446 printf ("Wrote %i bytes of thumbnail data at offset "
447 "%i.\n", data->size, *ds - data->size);
448 printf ("We currently have %i bytes EXIF data.\n", *ds);
451 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
452 exif_set_short (*d + 6 + offset + 0, data->priv->order,
453 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
454 exif_set_short (*d + 6 + offset + 2, data->priv->order,
456 exif_set_long (*d + 6 + offset + 4, data->priv->order,
458 exif_set_long (*d + 6 + offset + 8, data->priv->order,
468 /* Correctly terminate the directory */
469 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
473 * We are saving IFD 0. Tell where IFD 1 starts and save
476 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
477 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
480 exif_set_long (*d + 6 + offset, data->priv->order, 0);
484 exif_data_remove_entry (ExifData *d, ExifTag t)
486 exif_content_remove_entry (d->ifd[EXIF_IFD_0],
487 exif_content_get_entry (d->ifd[EXIF_IFD_0], t));
488 exif_content_remove_entry (d->ifd[EXIF_IFD_1],
489 exif_content_get_entry (d->ifd[EXIF_IFD_1], t));
490 exif_content_remove_entry (d->ifd[EXIF_IFD_EXIF],
491 exif_content_get_entry (d->ifd[EXIF_IFD_EXIF], t));
492 exif_content_remove_entry (d->ifd[EXIF_IFD_GPS],
493 exif_content_get_entry (d->ifd[EXIF_IFD_GPS], t));
494 exif_content_remove_entry (d->ifd[EXIF_IFD_INTEROPERABILITY],
495 exif_content_get_entry (d->ifd[EXIF_IFD_INTEROPERABILITY], t));
499 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
500 unsigned int ds_orig)
506 const unsigned char *d = d_orig;
507 unsigned int ds = ds_orig, len;
515 printf ("Parsing %i byte(s) EXIF data...\n", ds);
519 * It can be that the data starts with the EXIF header. If it does
520 * not, search the EXIF marker.
524 printf ("Size too small.\n");
528 if (!memcmp (d, ExifHeader, 6)) {
530 printf ("Found EXIF header.\n");
534 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
535 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
538 while ((d[0] == 0xff) && ds) {
543 /* JPEG_MARKER_SOI */
544 if (d[0] == JPEG_MARKER_SOI) {
550 /* JPEG_MARKER_APP0 */
551 if (d[0] == JPEG_MARKER_APP0) {
554 l = (d[0] << 8) | d[1];
562 /* JPEG_MARKER_APP1 */
563 if (d[0] == JPEG_MARKER_APP1)
566 /* Unknown marker or data. Give up. */
568 printf ("EXIF marker not found.\n");
576 printf ("Size too small.\n");
580 len = (d[0] << 8) | d[1];
582 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
589 * Verify the exif header
590 * (offset 2, length 6).
594 printf ("Size too small.\n");
598 if (memcmp (d, ExifHeader, 6)) {
600 printf ("EXIF header not found.\n");
606 printf ("Found EXIF header.\n");
609 /* Byte order (offset 6, length 2) */
612 if (!memcmp (d + 6, "II", 2))
613 data->priv->order = EXIF_BYTE_ORDER_INTEL;
614 else if (!memcmp (d + 6, "MM", 2))
615 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
620 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
624 offset = exif_get_long (d + 10, data->priv->order);
626 printf ("IFD 0 at %i.\n", (int) offset);
629 /* Parse the actual exif data (offset 14) */
630 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
634 n = exif_get_short (d + 6 + offset, data->priv->order);
635 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
638 printf ("IFD 1 at %i.\n", (int) offset);
642 if (offset > ds - 6) {
644 printf ("Bogus offset!\n");
649 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
654 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
655 * cameras use pointers in the maker note tag that point to the
656 * space between IFDs. Here is the only place where we have access
659 e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
663 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5)))
664 data->priv->md = exif_mnote_data_olympus_new ();
667 else if ((e->size >= 2) && (e->data[0] == 0x00)
668 && (e->data[1] == 0x1b))
669 data->priv->md = exif_mnote_data_pentax_new ();
672 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
676 if (!strcmp (exif_entry_get_value (em), "Canon"))
677 data->priv->md = exif_mnote_data_canon_new ();
682 * If we are able to interpret the maker note, interpret it and
683 * remove the corresponding entry as it may contain invalid
684 * pointers after this function here returns.
686 if (data->priv->md) {
687 exif_mnote_data_set_byte_order (data->priv->md,
689 exif_mnote_data_set_offset (data->priv->md,
690 data->priv->offset_mnote);
691 exif_mnote_data_load (data->priv->md, d, ds);
692 exif_data_remove_entry (data, EXIF_TAG_MAKER_NOTE);
698 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
707 *d = malloc (sizeof (char) * *ds);
708 memcpy (*d, ExifHeader, 6);
710 /* Order (offset 6) */
712 *d = realloc (*d, sizeof (char) * *ds);
713 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
714 memcpy (*d + 6, "II", 2);
716 memcpy (*d + 6, "MM", 2);
719 /* Fixed value (2 bytes, offset 8) */
721 *d = realloc (*d, sizeof (char) * *ds);
722 exif_set_short (*d + 8, data->priv->order, 0x002a);
725 * IFD 0 offset (4 bytes, offset 10).
726 * We will start 8 bytes after the
727 * EXIF header (2 bytes for order, another 2 for the test, and
728 * 4 bytes for the IFD 0 offset make 8 bytes together).
731 *d = realloc (*d, sizeof (char) * *ds);
732 exif_set_long (*d + 10, data->priv->order, 8);
734 /* Now save IFD 0. IFD 1 will be saved automatically. */
736 printf ("Saving IFDs...\n");
738 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
742 printf ("Saved %i byte(s) EXIF data.\n", *ds);
747 exif_data_new_from_file (const char *path)
753 unsigned char data[1024];
755 f = fopen (path, "rb");
759 loader = exif_loader_new ();
761 size = fread (data, 1, 1024, f);
762 if (size <= 0) break;
763 if (!exif_loader_write (loader, data, size)) break;
767 edata = exif_loader_get_data (loader);
768 exif_loader_unref (loader);
774 exif_data_ref (ExifData *data)
779 data->priv->ref_count++;
783 exif_data_unref (ExifData *data)
788 data->priv->ref_count--;
789 if (!data->priv->ref_count)
790 exif_data_free (data);
794 exif_data_free (ExifData *data)
801 for (i = 0; i < EXIF_IFD_COUNT; i++) {
803 exif_content_unref (data->ifd[i]);
812 if (data->priv->md) {
813 exif_mnote_data_unref (data->priv->md);
814 data->priv->md = NULL;
823 exif_data_dump (ExifData *data)
830 for (i = 0; i < EXIF_IFD_COUNT; i++) {
831 if (data->ifd[i] && data->ifd[i]->count) {
832 printf ("Dumping IFD '%s'...\n",
833 exif_ifd_get_name (i));
834 exif_content_dump (data->ifd[i], 0);
839 printf ("%i byte(s) thumbnail data available.", data->size);
840 if (data->size >= 4) {
841 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
842 data->data[0], data->data[1],
843 data->data[data->size - 2],
844 data->data[data->size - 1]);
850 exif_data_get_byte_order (ExifData *data)
855 return (data->priv->order);
859 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
867 for (i = 0; i < EXIF_IFD_COUNT; i++)
868 func (data->ifd[i], user_data);
871 typedef struct _ByteOrderChangeData ByteOrderChangeData;
872 struct _ByteOrderChangeData {
873 ExifByteOrder old, new;
877 entry_set_byte_order (ExifEntry *e, void *data)
879 ByteOrderChangeData *d = data;
891 case EXIF_FORMAT_SHORT:
892 for (i = 0; i < e->components; i++) {
893 s = exif_get_short (e->data +
894 (i * exif_format_get_size (e->format)),
896 exif_set_short (e->data +
897 (i * exif_format_get_size (e->format)),
901 case EXIF_FORMAT_LONG:
902 for (i = 0; i < e->components; i++) {
903 l = exif_get_long (e->data +
904 (i * exif_format_get_size (e->format)),
906 exif_set_long (e->data +
907 (i * exif_format_get_size (e->format)),
911 case EXIF_FORMAT_RATIONAL:
912 for (i = 0; i < e->components; i++) {
913 r = exif_get_rational (e->data +
914 (i * exif_format_get_size (e->format)),
916 exif_set_rational (e->data +
917 (i * exif_format_get_size (e->format)),
921 case EXIF_FORMAT_SLONG:
922 for (i = 0; i < e->components; i++) {
923 sl = exif_get_slong (e->data +
924 (i * exif_format_get_size (e->format)),
926 exif_set_slong (e->data +
927 (i * exif_format_get_size (e->format)),
931 case EXIF_FORMAT_SRATIONAL:
932 for (i = 0; i < e->components; i++) {
933 sr = exif_get_srational (e->data +
934 (i * exif_format_get_size (e->format)),
936 exif_set_srational (e->data +
937 (i * exif_format_get_size (e->format)),
941 case EXIF_FORMAT_UNDEFINED:
942 case EXIF_FORMAT_BYTE:
943 case EXIF_FORMAT_ASCII:
951 content_set_byte_order (ExifContent *content, void *data)
953 exif_content_foreach_entry (content, entry_set_byte_order, data);
957 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
959 ByteOrderChangeData d;
961 if (!data || (order == data->priv->order))
964 d.old = data->priv->order;
966 exif_data_foreach_content (data, content_set_byte_order, &d);
967 data->priv->order = order;
969 exif_mnote_data_set_byte_order (data->priv->md, order);