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 *e,
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, (ExifShort) e->tag);
162 exif_set_short (*d + 6 + offset + 2,
163 data->priv->order, (ExifShort) e->format);
164 exif_set_long (*d + 6 + offset + 4,
165 data->priv->order, e->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 (e->format) * e->components;
176 *d = realloc (*d, sizeof (char) * *ds);
178 exif_set_long (*d + 6 + offset + 8,
179 data->priv->order, doff);
183 /* If this is the maker note tag, update it. */
184 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
188 exif_mnote_data_set_offset (data->priv->md, doff);
189 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
192 /* Write the data. Fill unneeded bytes with 0. */
193 memcpy (*d + 6 + doff, e->data, e->size);
194 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
198 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
199 unsigned int ds, ExifLong offset, ExifLong size)
201 if (ds < offset + size) {
203 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
204 (int) ds, (int) offset, (int) size);
211 data->data = malloc (sizeof (char) * data->size);
212 memcpy (data->data, d + offset, data->size);
216 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
217 const unsigned char *d,
218 unsigned int ds, unsigned int offset)
220 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
226 /* Read the number of entries */
227 if (offset >= ds - 1) return;
228 n = exif_get_short (d + offset, data->priv->order);
230 printf ("Loading %i entries...\n", n);
234 /* Check if we have enough data. */
235 if (offset + 12 * n > ds) n = (ds - offset) / 12;
237 for (i = 0; i < n; i++) {
239 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
241 printf ("Loading entry '%s' (%i of %i)...\n",
242 exif_tag_get_name (tag), i + 1, n);
245 case EXIF_TAG_EXIF_IFD_POINTER:
246 case EXIF_TAG_GPS_INFO_IFD_POINTER:
247 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
248 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
249 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
250 o = exif_get_long (d + offset + 12 * i + 8,
253 case EXIF_TAG_EXIF_IFD_POINTER:
254 exif_data_load_data_content (data,
255 data->ifd[EXIF_IFD_EXIF], d, ds, o);
257 case EXIF_TAG_GPS_INFO_IFD_POINTER:
258 exif_data_load_data_content (data,
259 data->ifd[EXIF_IFD_GPS], d, ds, o);
261 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
262 exif_data_load_data_content (data,
263 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
265 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
267 printf ("Thumbnail at %i.\n", (int) o);
269 thumbnail_offset = o;
270 if (thumbnail_offset && thumbnail_length)
271 exif_data_load_data_thumbnail (data, d,
272 ds, thumbnail_offset,
275 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
277 printf ("Thumbnail size: %i.\n", (int) o);
279 thumbnail_length = o;
280 if (thumbnail_offset && thumbnail_length)
281 exif_data_load_data_thumbnail (data, d,
282 ds, thumbnail_offset,
292 * If we don't know the tag, changes are high
293 * that the EXIF data does not follow the standard.
295 if (!exif_tag_get_name (tag)) return;
296 entry = exif_entry_new ();
297 exif_content_add_entry (ifd, entry);
298 exif_data_load_data_entry (data, entry, d, ds,
300 exif_entry_unref (entry);
307 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
308 unsigned char **d, unsigned int *ds,
311 unsigned int j, n_ptr = 0, n_thumb = 0;
314 if (!data || !ifd || !d || !ds)
317 for (i = 0; i < EXIF_IFD_COUNT; i++)
318 if (ifd == data->ifd[i])
320 if (i == EXIF_IFD_COUNT)
324 * Check if we need some extra entries for pointers or the thumbnail.
330 * The pointer to IFD_EXIF is in IFD_0. The pointer to
331 * IFD_INTEROPERABILITY is in IFD_EXIF.
333 if (data->ifd[EXIF_IFD_EXIF]->count ||
334 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
337 /* The pointer to IFD_GPS is in IFD_0. */
338 if (data->ifd[EXIF_IFD_GPS]->count)
347 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
354 * Allocate enough memory for all entries
355 * and the number of entries.
357 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
358 *d = realloc (*d, sizeof (char) * *ds);
360 /* Save the number of entries */
361 exif_set_short (*d + 6 + offset, data->priv->order,
362 (ExifShort) (ifd->count + n_ptr + n_thumb));
366 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
367 ifd->count, exif_ifd_get_name (i), offset);
370 /* Save each entry */
371 for (j = 0; j < ifd->count; j++)
372 exif_data_save_data_entry (data, ifd->entries[j],
373 d, ds, offset + 12 * j);
374 offset += 12 * ifd->count;
376 /* Now save special entries. */
381 * The pointer to IFD_EXIF is in IFD_0.
382 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
383 * therefore, if IFD_INTEROPERABILITY is not empty, we need
384 * IFD_EXIF even if latter is empty.
386 if (data->ifd[EXIF_IFD_EXIF]->count ||
387 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
388 exif_set_short (*d + 6 + offset + 0, data->priv->order,
389 EXIF_TAG_EXIF_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_EXIF], d, ds, *ds - 6);
401 /* The pointer to IFD_GPS is in IFD_0, too. */
402 if (data->ifd[EXIF_IFD_GPS]->count) {
403 exif_set_short (*d + 6 + offset + 0, data->priv->order,
404 EXIF_TAG_GPS_INFO_IFD_POINTER);
405 exif_set_short (*d + 6 + offset + 2, data->priv->order,
407 exif_set_long (*d + 6 + offset + 4, data->priv->order,
409 exif_set_long (*d + 6 + offset + 8, data->priv->order,
411 exif_data_save_data_content (data,
412 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
420 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
423 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
424 exif_set_short (*d + 6 + offset + 0, data->priv->order,
425 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
426 exif_set_short (*d + 6 + offset + 2, data->priv->order,
428 exif_set_long (*d + 6 + offset + 4, data->priv->order,
430 exif_set_long (*d + 6 + offset + 8, data->priv->order,
432 exif_data_save_data_content (data,
433 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
442 * Information about the thumbnail (if any) is saved in
447 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
448 exif_set_short (*d + 6 + offset + 0, data->priv->order,
449 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
450 exif_set_short (*d + 6 + offset + 2, data->priv->order,
452 exif_set_long (*d + 6 + offset + 4, data->priv->order,
454 exif_set_long (*d + 6 + offset + 8, data->priv->order,
457 *d = realloc (*d, sizeof (char) * *ds);
458 memcpy (*d + *ds - data->size, data->data, data->size);
461 printf ("Wrote %i bytes of thumbnail data at offset "
462 "%i.\n", data->size, *ds - data->size);
463 printf ("We currently have %i bytes EXIF data.\n", *ds);
466 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
467 exif_set_short (*d + 6 + offset + 0, data->priv->order,
468 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
469 exif_set_short (*d + 6 + offset + 2, data->priv->order,
471 exif_set_long (*d + 6 + offset + 4, data->priv->order,
473 exif_set_long (*d + 6 + offset + 8, data->priv->order,
483 /* Correctly terminate the directory */
484 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
488 * We are saving IFD 0. Tell where IFD 1 starts and save
491 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
492 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
495 exif_set_long (*d + 6 + offset, data->priv->order, 0);
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 !memcmp (e->data, "Nikon", 5)))
665 data->priv->md = exif_mnote_data_olympus_new ();
668 else if ((e->size >= 2) && (e->data[0] == 0x00)
669 && (e->data[1] == 0x1b))
670 data->priv->md = exif_mnote_data_pentax_new ();
673 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
678 if (!strcmp (exif_entry_get_value (em, value, sizeof(value)), "Canon"))
679 data->priv->md = exif_mnote_data_canon_new ();
684 * If we are able to interpret the maker note, do so.
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);
697 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
706 *d = malloc (sizeof (char) * *ds);
707 memcpy (*d, ExifHeader, 6);
709 /* Order (offset 6) */
711 *d = realloc (*d, sizeof (char) * *ds);
712 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
713 memcpy (*d + 6, "II", 2);
715 memcpy (*d + 6, "MM", 2);
718 /* Fixed value (2 bytes, offset 8) */
720 *d = realloc (*d, sizeof (char) * *ds);
721 exif_set_short (*d + 8, data->priv->order, 0x002a);
724 * IFD 0 offset (4 bytes, offset 10).
725 * We will start 8 bytes after the
726 * EXIF header (2 bytes for order, another 2 for the test, and
727 * 4 bytes for the IFD 0 offset make 8 bytes together).
730 *d = realloc (*d, sizeof (char) * *ds);
731 exif_set_long (*d + 10, data->priv->order, 8);
733 /* Now save IFD 0. IFD 1 will be saved automatically. */
735 printf ("Saving IFDs...\n");
737 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
741 printf ("Saved %i byte(s) EXIF data.\n", *ds);
746 exif_data_new_from_file (const char *path)
752 unsigned char data[1024];
754 f = fopen (path, "rb");
758 loader = exif_loader_new ();
760 size = fread (data, 1, sizeof (data), f);
761 if (size <= 0) break;
762 if (!exif_loader_write (loader, data, size)) break;
766 edata = exif_loader_get_data (loader);
767 exif_loader_unref (loader);
773 exif_data_ref (ExifData *data)
778 data->priv->ref_count++;
782 exif_data_unref (ExifData *data)
787 data->priv->ref_count--;
788 if (!data->priv->ref_count)
789 exif_data_free (data);
793 exif_data_free (ExifData *data)
800 for (i = 0; i < EXIF_IFD_COUNT; i++) {
802 exif_content_unref (data->ifd[i]);
811 if (data->priv->md) {
812 exif_mnote_data_unref (data->priv->md);
813 data->priv->md = NULL;
822 exif_data_dump (ExifData *data)
829 for (i = 0; i < EXIF_IFD_COUNT; i++) {
830 if (data->ifd[i] && data->ifd[i]->count) {
831 printf ("Dumping IFD '%s'...\n",
832 exif_ifd_get_name (i));
833 exif_content_dump (data->ifd[i], 0);
838 printf ("%i byte(s) thumbnail data available.", data->size);
839 if (data->size >= 4) {
840 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
841 data->data[0], data->data[1],
842 data->data[data->size - 2],
843 data->data[data->size - 1]);
849 exif_data_get_byte_order (ExifData *data)
854 return (data->priv->order);
858 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
866 for (i = 0; i < EXIF_IFD_COUNT; i++)
867 func (data->ifd[i], user_data);
870 typedef struct _ByteOrderChangeData ByteOrderChangeData;
871 struct _ByteOrderChangeData {
872 ExifByteOrder old, new;
876 entry_set_byte_order (ExifEntry *e, void *data)
878 ByteOrderChangeData *d = data;
890 case EXIF_FORMAT_SHORT:
891 for (i = 0; i < e->components; i++) {
892 s = exif_get_short (e->data +
893 (i * exif_format_get_size (e->format)),
895 exif_set_short (e->data +
896 (i * exif_format_get_size (e->format)),
900 case EXIF_FORMAT_LONG:
901 for (i = 0; i < e->components; i++) {
902 l = exif_get_long (e->data +
903 (i * exif_format_get_size (e->format)),
905 exif_set_long (e->data +
906 (i * exif_format_get_size (e->format)),
910 case EXIF_FORMAT_RATIONAL:
911 for (i = 0; i < e->components; i++) {
912 r = exif_get_rational (e->data +
913 (i * exif_format_get_size (e->format)),
915 exif_set_rational (e->data +
916 (i * exif_format_get_size (e->format)),
920 case EXIF_FORMAT_SLONG:
921 for (i = 0; i < e->components; i++) {
922 sl = exif_get_slong (e->data +
923 (i * exif_format_get_size (e->format)),
925 exif_set_slong (e->data +
926 (i * exif_format_get_size (e->format)),
930 case EXIF_FORMAT_SRATIONAL:
931 for (i = 0; i < e->components; i++) {
932 sr = exif_get_srational (e->data +
933 (i * exif_format_get_size (e->format)),
935 exif_set_srational (e->data +
936 (i * exif_format_get_size (e->format)),
940 case EXIF_FORMAT_UNDEFINED:
941 case EXIF_FORMAT_BYTE:
942 case EXIF_FORMAT_SBYTE:
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);