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;
61 data = malloc (sizeof (ExifData));
64 memset (data, 0, sizeof (ExifData));
65 data->priv = malloc (sizeof (ExifDataPrivate));
70 memset (data->priv, 0, sizeof (ExifDataPrivate));
71 data->priv->ref_count = 1;
73 for (i = 0; i < EXIF_IFD_COUNT; i++) {
74 data->ifd[i] = exif_content_new ();
76 exif_data_free (data);
79 data->ifd[i]->parent = data;
86 exif_data_new_from_data (const unsigned char *data, unsigned int size)
90 edata = exif_data_new ();
91 exif_data_load_data (edata, data, size);
96 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
97 const unsigned char *d,
98 unsigned int size, unsigned int offset)
100 unsigned int s, doff;
102 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
103 entry->format = exif_get_short (d + offset + 2, data->priv->order);
104 entry->components = exif_get_long (d + offset + 4, data->priv->order);
107 * Size? If bigger than 4 bytes, the actual data is not
108 * in the entry but somewhere else (offset).
110 s = exif_format_get_size (entry->format) * entry->components;
114 doff = exif_get_long (d + offset + 8, data->priv->order);
122 entry->data = malloc (sizeof (char) * s);
126 memcpy (entry->data, d + doff, s);
130 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
131 unsigned char **d, unsigned int *ds,
134 unsigned int doff, s;
137 * Each entry is 12 bytes long. The memory for the entry has
138 * already been allocated.
140 exif_set_short (*d + 6 + offset + 0,
141 data->priv->order, entry->tag);
142 exif_set_short (*d + 6 + offset + 2,
143 data->priv->order, entry->format);
144 exif_set_long (*d + 6 + offset + 4,
145 data->priv->order, entry->components);
148 * Size? If bigger than 4 bytes, the actual data is not in
149 * the entry but somewhere else.
151 s = exif_format_get_size (entry->format) * entry->components;
156 *d = realloc (*d, sizeof (char) * *ds);
157 doff = *ds - 6 - entry->size;
158 exif_set_long (*d + 6 + offset + 8,
159 data->priv->order, doff);
163 /* Write the data. Fill unneeded bytes with 0. */
164 memcpy (*d + 6 + doff, entry->data, entry->size);
165 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
169 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
170 unsigned int ds, ExifLong offset, ExifLong size)
172 if (ds < offset + size) {
174 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
175 (int) ds, (int) offset, (int) size);
182 data->data = malloc (sizeof (char) * data->size);
183 memcpy (data->data, d + offset, data->size);
187 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
188 const unsigned char *d,
189 unsigned int ds, unsigned int offset)
191 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
197 /* Read the number of entries */
198 if (offset >= ds - 1) return;
199 n = exif_get_short (d + offset, data->priv->order);
201 printf ("Loading %i entries...\n", n);
205 /* Check if we have enough data. */
206 if (offset + 12 * n > ds) n = (ds - offset) / 12;
208 for (i = 0; i < n; i++) {
210 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
212 printf ("Loading entry '%s' (%i of %i)...\n",
213 exif_tag_get_name (tag), i + 1, n);
216 case EXIF_TAG_EXIF_IFD_POINTER:
217 case EXIF_TAG_GPS_INFO_IFD_POINTER:
218 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
219 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
220 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
221 o = exif_get_long (d + offset + 12 * i + 8,
224 case EXIF_TAG_EXIF_IFD_POINTER:
225 exif_data_load_data_content (data,
226 data->ifd[EXIF_IFD_EXIF], d, ds, o);
228 case EXIF_TAG_GPS_INFO_IFD_POINTER:
229 exif_data_load_data_content (data,
230 data->ifd[EXIF_IFD_GPS], d, ds, o);
232 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
233 exif_data_load_data_content (data,
234 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
236 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
238 printf ("Thumbnail at %i.\n", (int) o);
240 thumbnail_offset = o;
241 if (thumbnail_offset && thumbnail_length)
242 exif_data_load_data_thumbnail (data, d,
243 ds, thumbnail_offset,
246 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
248 printf ("Thumbnail size: %i.\n", (int) o);
250 thumbnail_length = o;
251 if (thumbnail_offset && thumbnail_length)
252 exif_data_load_data_thumbnail (data, d,
253 ds, thumbnail_offset,
261 entry = exif_entry_new ();
262 exif_content_add_entry (ifd, entry);
263 exif_data_load_data_entry (data, entry, d, ds,
265 exif_entry_unref (entry);
272 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
273 unsigned char **d, unsigned int *ds,
276 unsigned int j, n_ptr = 0, n_thumb = 0;
279 if (!data || !ifd || !d || !ds)
282 for (i = 0; i < EXIF_IFD_COUNT; i++)
283 if (ifd == data->ifd[i])
285 if (i == EXIF_IFD_COUNT)
289 * Check if we need some extra entries for pointers or the thumbnail.
295 * The pointer to IFD_EXIF is in IFD_0. The pointer to
296 * IFD_INTEROPERABILITY is in IFD_EXIF.
298 if (data->ifd[EXIF_IFD_EXIF]->count ||
299 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
302 /* The pointer to IFD_GPS is in IFD_0. */
303 if (data->ifd[EXIF_IFD_GPS]->count)
312 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
319 * Allocate enough memory for all entries
320 * and the number of entries.
322 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
323 *d = realloc (*d, sizeof (char) * *ds);
325 /* Save the number of entries */
326 exif_set_short (*d + 6 + offset, data->priv->order,
327 ifd->count + n_ptr + n_thumb);
331 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
332 ifd->count, exif_ifd_get_name (i), offset);
335 /* Save each entry */
336 for (j = 0; j < ifd->count; j++)
337 exif_data_save_data_entry (data, ifd->entries[j],
338 d, ds, offset + 12 * j);
339 offset += 12 * ifd->count;
341 /* Now save special entries. */
346 * The pointer to IFD_EXIF is in IFD_0.
347 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
348 * therefore, if IFD_INTEROPERABILITY is not empty, we need
349 * IFD_EXIF even if latter is empty.
351 if (data->ifd[EXIF_IFD_EXIF]->count ||
352 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
353 exif_set_short (*d + 6 + offset + 0, data->priv->order,
354 EXIF_TAG_EXIF_IFD_POINTER);
355 exif_set_short (*d + 6 + offset + 2, data->priv->order,
357 exif_set_long (*d + 6 + offset + 4, data->priv->order,
359 exif_set_long (*d + 6 + offset + 8, data->priv->order,
361 exif_data_save_data_content (data,
362 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
366 /* The pointer to IFD_GPS is in IFD_0, too. */
367 if (data->ifd[EXIF_IFD_GPS]->count) {
368 exif_set_short (*d + 6 + offset + 0, data->priv->order,
369 EXIF_TAG_GPS_INFO_IFD_POINTER);
370 exif_set_short (*d + 6 + offset + 2, data->priv->order,
372 exif_set_long (*d + 6 + offset + 4, data->priv->order,
374 exif_set_long (*d + 6 + offset + 8, data->priv->order,
376 exif_data_save_data_content (data,
377 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
385 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
388 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
389 exif_set_short (*d + 6 + offset + 0, data->priv->order,
390 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
391 exif_set_short (*d + 6 + offset + 2, data->priv->order,
393 exif_set_long (*d + 6 + offset + 4, data->priv->order,
395 exif_set_long (*d + 6 + offset + 8, data->priv->order,
397 exif_data_save_data_content (data,
398 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
407 * Information about the thumbnail (if any) is saved in
412 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
413 exif_set_short (*d + 6 + offset + 0, data->priv->order,
414 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
415 exif_set_short (*d + 6 + offset + 2, data->priv->order,
417 exif_set_long (*d + 6 + offset + 4, data->priv->order,
419 exif_set_long (*d + 6 + offset + 8, data->priv->order,
422 *d = realloc (*d, sizeof (char) * *ds);
423 memcpy (*d + *ds - data->size, data->data, data->size);
426 printf ("Wrote %i bytes of thumbnail data at offset "
427 "%i.\n", data->size, *ds - data->size);
428 printf ("We currently have %i bytes EXIF data.\n", *ds);
431 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
432 exif_set_short (*d + 6 + offset + 0, data->priv->order,
433 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
434 exif_set_short (*d + 6 + offset + 2, data->priv->order,
436 exif_set_long (*d + 6 + offset + 4, data->priv->order,
438 exif_set_long (*d + 6 + offset + 8, data->priv->order,
448 /* Correctly terminate the directory */
449 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
453 * We are saving IFD 0. Tell where IFD 1 starts and save
456 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
457 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
460 exif_set_long (*d + 6 + offset, data->priv->order, 0);
464 exif_data_remove_entry (ExifData *d, ExifTag t)
466 exif_content_remove_entry (d->ifd[EXIF_IFD_0],
467 exif_content_get_entry (d->ifd[EXIF_IFD_0], t));
468 exif_content_remove_entry (d->ifd[EXIF_IFD_1],
469 exif_content_get_entry (d->ifd[EXIF_IFD_1], t));
470 exif_content_remove_entry (d->ifd[EXIF_IFD_EXIF],
471 exif_content_get_entry (d->ifd[EXIF_IFD_EXIF], t));
472 exif_content_remove_entry (d->ifd[EXIF_IFD_GPS],
473 exif_content_get_entry (d->ifd[EXIF_IFD_GPS], t));
474 exif_content_remove_entry (d->ifd[EXIF_IFD_INTEROPERABILITY],
475 exif_content_get_entry (d->ifd[EXIF_IFD_INTEROPERABILITY], t));
479 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
480 unsigned int ds_orig)
486 const unsigned char *d = d_orig;
487 unsigned int size = ds_orig, len;
495 printf ("Parsing %i byte(s) EXIF data...\n", size);
499 * It can be that the data starts with the EXIF header. If it does
500 * not, search the EXIF marker.
504 printf ("Size too small.\n");
508 if (!memcmp (d, ExifHeader, 6)) {
510 printf ("Found EXIF header.\n");
514 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
515 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
518 while ((d[0] == 0xff) && size) {
523 /* JPEG_MARKER_SOI */
524 if (d[0] == JPEG_MARKER_SOI) {
530 /* JPEG_MARKER_APP0 */
531 if (d[0] == JPEG_MARKER_APP0) {
534 l = (d[0] << 8) | d[1];
542 /* JPEG_MARKER_APP1 */
543 if (d[0] == JPEG_MARKER_APP1)
546 /* Unknown marker or data. Give up. */
548 printf ("EXIF marker not found.\n");
556 printf ("Size too small.\n");
560 len = (d[0] << 8) | d[1];
562 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
569 * Verify the exif header
570 * (offset 2, length 6).
574 printf ("Size too small.\n");
578 if (memcmp (d, ExifHeader, 6)) {
580 printf ("EXIF header not found.\n");
586 printf ("Found EXIF header.\n");
589 /* Byte order (offset 6, length 2) */
592 if (!memcmp (d + 6, "II", 2))
593 data->priv->order = EXIF_BYTE_ORDER_INTEL;
594 else if (!memcmp (d + 6, "MM", 2))
595 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
600 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
604 offset = exif_get_long (d + 10, data->priv->order);
606 printf ("IFD 0 at %i.\n", (int) offset);
609 /* Parse the actual exif data (offset 14) */
610 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
614 n = exif_get_short (d + 6 + offset, data->priv->order);
615 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
618 printf ("IFD 1 at %i.\n", (int) offset);
622 if (offset > size - 6) {
624 printf ("Bogus offset!\n");
629 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
634 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
635 * cameras use pointers in the maker note tag that point to the
636 * space between IFDs. Here is the only place where we have access
639 e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
643 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5)))
644 data->priv->md = exif_mnote_data_olympus_new (
648 else if ((e->size >= 2) && (e->data[0] == 0x00)
649 && (e->data[1] == 0x1b))
650 data->priv->md = exif_mnote_data_pentax_new (
654 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
658 if (!strcmp (exif_entry_get_value (em), "Canon"))
659 data->priv->md = exif_mnote_data_canon_new (
665 * If we are able to interpret the maker note, interpret it and
666 * remove the corresponding entry as it may contain invalid
667 * pointers after this function here returns.
669 if (data->priv->md) {
670 exif_mnote_data_load (data->priv->md, d_orig, ds_orig);
671 exif_data_remove_entry (data, EXIF_TAG_MAKER_NOTE);
677 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
686 *d = malloc (sizeof (char) * *ds);
687 memcpy (*d, ExifHeader, 6);
689 /* Order (offset 6) */
691 *d = realloc (*d, sizeof (char) * *ds);
692 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
693 memcpy (*d + 6, "II", 2);
695 memcpy (*d + 6, "MM", 2);
698 /* Fixed value (2 bytes, offset 8) */
700 *d = realloc (*d, sizeof (char) * *ds);
701 exif_set_short (*d + 8, data->priv->order, 0x002a);
704 * IFD 0 offset (4 bytes, offset 10).
705 * We will start 8 bytes after the
706 * EXIF header (2 bytes for order, another 2 for the test, and
707 * 4 bytes for the IFD 0 offset make 8 bytes together).
710 *d = realloc (*d, sizeof (char) * *ds);
711 exif_set_long (*d + 10, data->priv->order, 8);
713 /* Now save IFD 0. IFD 1 will be saved automatically. */
715 printf ("Saving IFDs...\n");
717 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
721 printf ("Saved %i byte(s) EXIF data.\n", *ds);
726 exif_data_new_from_file (const char *path)
732 unsigned char data[1024];
734 f = fopen (path, "rb");
738 loader = exif_loader_new ();
740 size = fread (data, 1, 1024, f);
741 if (size <= 0) break;
742 if (!exif_loader_write (loader, data, size)) break;
746 edata = exif_loader_get_data (loader);
747 exif_loader_unref (loader);
753 exif_data_ref (ExifData *data)
758 data->priv->ref_count++;
762 exif_data_unref (ExifData *data)
767 data->priv->ref_count--;
768 if (!data->priv->ref_count)
769 exif_data_free (data);
773 exif_data_free (ExifData *data)
780 for (i = 0; i < EXIF_IFD_COUNT; i++) {
782 exif_content_unref (data->ifd[i]);
791 if (data->priv->md) {
792 exif_mnote_data_unref (data->priv->md);
793 data->priv->md = NULL;
802 exif_data_dump (ExifData *data)
809 for (i = 0; i < EXIF_IFD_COUNT; i++) {
810 if (data->ifd[i] && data->ifd[i]->count) {
811 printf ("Dumping IFD '%s'...\n",
812 exif_ifd_get_name (i));
813 exif_content_dump (data->ifd[i], 0);
818 printf ("%i byte(s) thumbnail data available.", data->size);
819 if (data->size >= 4) {
820 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
821 data->data[0], data->data[1],
822 data->data[data->size - 2],
823 data->data[data->size - 1]);
829 exif_data_get_byte_order (ExifData *data)
834 return (data->priv->order);
838 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
846 for (i = 0; i < EXIF_IFD_COUNT; i++)
847 func (data->ifd[i], user_data);
850 typedef struct _ByteOrderChangeData ByteOrderChangeData;
851 struct _ByteOrderChangeData {
852 ExifByteOrder old, new;
856 entry_set_byte_order (ExifEntry *e, void *data)
858 ByteOrderChangeData *d = data;
870 case EXIF_FORMAT_SHORT:
871 for (i = 0; i < e->components; i++) {
872 s = exif_get_short (e->data +
873 (i * exif_format_get_size (e->format)),
875 exif_set_short (e->data +
876 (i * exif_format_get_size (e->format)),
880 case EXIF_FORMAT_LONG:
881 for (i = 0; i < e->components; i++) {
882 l = exif_get_long (e->data +
883 (i * exif_format_get_size (e->format)),
885 exif_set_long (e->data +
886 (i * exif_format_get_size (e->format)),
890 case EXIF_FORMAT_RATIONAL:
891 for (i = 0; i < e->components; i++) {
892 r = exif_get_rational (e->data +
893 (i * exif_format_get_size (e->format)),
895 exif_set_rational (e->data +
896 (i * exif_format_get_size (e->format)),
900 case EXIF_FORMAT_SLONG:
901 for (i = 0; i < e->components; i++) {
902 sl = exif_get_slong (e->data +
903 (i * exif_format_get_size (e->format)),
905 exif_set_slong (e->data +
906 (i * exif_format_get_size (e->format)),
910 case EXIF_FORMAT_SRATIONAL:
911 for (i = 0; i < e->components; i++) {
912 sr = exif_get_srational (e->data +
913 (i * exif_format_get_size (e->format)),
915 exif_set_srational (e->data +
916 (i * exif_format_get_size (e->format)),
920 case EXIF_FORMAT_UNDEFINED:
921 case EXIF_FORMAT_BYTE:
922 case EXIF_FORMAT_ASCII:
930 content_set_byte_order (ExifContent *content, void *data)
932 exif_content_foreach_entry (content, entry_set_byte_order, data);
936 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
938 ByteOrderChangeData d;
940 if (!data || (order == data->priv->order))
943 d.old = data->priv->order;
945 exif_data_foreach_content (data, content_set_byte_order, &d);
946 data->priv->order = order;