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)
139 data->priv->offset_mnote = doff;
143 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
144 unsigned char **d, unsigned int *ds,
147 unsigned int doff, s;
150 * Each entry is 12 bytes long. The memory for the entry has
151 * already been allocated.
153 exif_set_short (*d + 6 + offset + 0,
154 data->priv->order, entry->tag);
155 exif_set_short (*d + 6 + offset + 2,
156 data->priv->order, entry->format);
157 exif_set_long (*d + 6 + offset + 4,
158 data->priv->order, entry->components);
161 * Size? If bigger than 4 bytes, the actual data is not in
162 * the entry but somewhere else.
164 s = exif_format_get_size (entry->format) * entry->components;
169 *d = realloc (*d, sizeof (char) * *ds);
170 doff = *ds - 6 - entry->size;
171 exif_set_long (*d + 6 + offset + 8,
172 data->priv->order, doff);
176 /* Write the data. Fill unneeded bytes with 0. */
177 memcpy (*d + 6 + doff, entry->data, entry->size);
178 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
182 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
183 unsigned int ds, ExifLong offset, ExifLong size)
185 if (ds < offset + size) {
187 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
188 (int) ds, (int) offset, (int) size);
195 data->data = malloc (sizeof (char) * data->size);
196 memcpy (data->data, d + offset, data->size);
200 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
201 const unsigned char *d,
202 unsigned int ds, unsigned int offset)
204 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
210 /* Read the number of entries */
211 if (offset >= ds - 1) return;
212 n = exif_get_short (d + offset, data->priv->order);
214 printf ("Loading %i entries...\n", n);
218 /* Check if we have enough data. */
219 if (offset + 12 * n > ds) n = (ds - offset) / 12;
221 for (i = 0; i < n; i++) {
223 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
225 printf ("Loading entry '%s' (%i of %i)...\n",
226 exif_tag_get_name (tag), i + 1, n);
229 case EXIF_TAG_EXIF_IFD_POINTER:
230 case EXIF_TAG_GPS_INFO_IFD_POINTER:
231 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
232 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
233 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
234 o = exif_get_long (d + offset + 12 * i + 8,
237 case EXIF_TAG_EXIF_IFD_POINTER:
238 exif_data_load_data_content (data,
239 data->ifd[EXIF_IFD_EXIF], d, ds, o);
241 case EXIF_TAG_GPS_INFO_IFD_POINTER:
242 exif_data_load_data_content (data,
243 data->ifd[EXIF_IFD_GPS], d, ds, o);
245 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
246 exif_data_load_data_content (data,
247 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
249 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
251 printf ("Thumbnail at %i.\n", (int) o);
253 thumbnail_offset = o;
254 if (thumbnail_offset && thumbnail_length)
255 exif_data_load_data_thumbnail (data, d,
256 ds, thumbnail_offset,
259 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
261 printf ("Thumbnail size: %i.\n", (int) o);
263 thumbnail_length = o;
264 if (thumbnail_offset && thumbnail_length)
265 exif_data_load_data_thumbnail (data, d,
266 ds, thumbnail_offset,
274 entry = exif_entry_new ();
275 exif_content_add_entry (ifd, entry);
276 exif_data_load_data_entry (data, entry, d, ds,
278 exif_entry_unref (entry);
285 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
286 unsigned char **d, unsigned int *ds,
289 unsigned int j, n_ptr = 0, n_thumb = 0;
292 if (!data || !ifd || !d || !ds)
295 for (i = 0; i < EXIF_IFD_COUNT; i++)
296 if (ifd == data->ifd[i])
298 if (i == EXIF_IFD_COUNT)
302 * Check if we need some extra entries for pointers or the thumbnail.
308 * The pointer to IFD_EXIF is in IFD_0. The pointer to
309 * IFD_INTEROPERABILITY is in IFD_EXIF.
311 if (data->ifd[EXIF_IFD_EXIF]->count ||
312 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
315 /* The pointer to IFD_GPS is in IFD_0. */
316 if (data->ifd[EXIF_IFD_GPS]->count)
325 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
332 * Allocate enough memory for all entries
333 * and the number of entries.
335 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
336 *d = realloc (*d, sizeof (char) * *ds);
338 /* Save the number of entries */
339 exif_set_short (*d + 6 + offset, data->priv->order,
340 ifd->count + n_ptr + n_thumb);
344 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
345 ifd->count, exif_ifd_get_name (i), offset);
348 /* Save each entry */
349 for (j = 0; j < ifd->count; j++)
350 exif_data_save_data_entry (data, ifd->entries[j],
351 d, ds, offset + 12 * j);
352 offset += 12 * ifd->count;
354 /* Now save special entries. */
359 * The pointer to IFD_EXIF is in IFD_0.
360 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
361 * therefore, if IFD_INTEROPERABILITY is not empty, we need
362 * IFD_EXIF even if latter is empty.
364 if (data->ifd[EXIF_IFD_EXIF]->count ||
365 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
366 exif_set_short (*d + 6 + offset + 0, data->priv->order,
367 EXIF_TAG_EXIF_IFD_POINTER);
368 exif_set_short (*d + 6 + offset + 2, data->priv->order,
370 exif_set_long (*d + 6 + offset + 4, data->priv->order,
372 exif_set_long (*d + 6 + offset + 8, data->priv->order,
374 exif_data_save_data_content (data,
375 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
379 /* The pointer to IFD_GPS is in IFD_0, too. */
380 if (data->ifd[EXIF_IFD_GPS]->count) {
381 exif_set_short (*d + 6 + offset + 0, data->priv->order,
382 EXIF_TAG_GPS_INFO_IFD_POINTER);
383 exif_set_short (*d + 6 + offset + 2, data->priv->order,
385 exif_set_long (*d + 6 + offset + 4, data->priv->order,
387 exif_set_long (*d + 6 + offset + 8, data->priv->order,
389 exif_data_save_data_content (data,
390 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
398 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
401 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
402 exif_set_short (*d + 6 + offset + 0, data->priv->order,
403 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
404 exif_set_short (*d + 6 + offset + 2, data->priv->order,
406 exif_set_long (*d + 6 + offset + 4, data->priv->order,
408 exif_set_long (*d + 6 + offset + 8, data->priv->order,
410 exif_data_save_data_content (data,
411 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
420 * Information about the thumbnail (if any) is saved in
425 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
426 exif_set_short (*d + 6 + offset + 0, data->priv->order,
427 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
428 exif_set_short (*d + 6 + offset + 2, data->priv->order,
430 exif_set_long (*d + 6 + offset + 4, data->priv->order,
432 exif_set_long (*d + 6 + offset + 8, data->priv->order,
435 *d = realloc (*d, sizeof (char) * *ds);
436 memcpy (*d + *ds - data->size, data->data, data->size);
439 printf ("Wrote %i bytes of thumbnail data at offset "
440 "%i.\n", data->size, *ds - data->size);
441 printf ("We currently have %i bytes EXIF data.\n", *ds);
444 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
445 exif_set_short (*d + 6 + offset + 0, data->priv->order,
446 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
447 exif_set_short (*d + 6 + offset + 2, data->priv->order,
449 exif_set_long (*d + 6 + offset + 4, data->priv->order,
451 exif_set_long (*d + 6 + offset + 8, data->priv->order,
461 /* Correctly terminate the directory */
462 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
466 * We are saving IFD 0. Tell where IFD 1 starts and save
469 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
470 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
473 exif_set_long (*d + 6 + offset, data->priv->order, 0);
477 exif_data_remove_entry (ExifData *d, ExifTag t)
479 exif_content_remove_entry (d->ifd[EXIF_IFD_0],
480 exif_content_get_entry (d->ifd[EXIF_IFD_0], t));
481 exif_content_remove_entry (d->ifd[EXIF_IFD_1],
482 exif_content_get_entry (d->ifd[EXIF_IFD_1], t));
483 exif_content_remove_entry (d->ifd[EXIF_IFD_EXIF],
484 exif_content_get_entry (d->ifd[EXIF_IFD_EXIF], t));
485 exif_content_remove_entry (d->ifd[EXIF_IFD_GPS],
486 exif_content_get_entry (d->ifd[EXIF_IFD_GPS], t));
487 exif_content_remove_entry (d->ifd[EXIF_IFD_INTEROPERABILITY],
488 exif_content_get_entry (d->ifd[EXIF_IFD_INTEROPERABILITY], t));
492 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
493 unsigned int ds_orig)
499 const unsigned char *d = d_orig;
500 unsigned int ds = ds_orig, len;
508 printf ("Parsing %i byte(s) EXIF data...\n", ds);
512 * It can be that the data starts with the EXIF header. If it does
513 * not, search the EXIF marker.
517 printf ("Size too small.\n");
521 if (!memcmp (d, ExifHeader, 6)) {
523 printf ("Found EXIF header.\n");
527 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
528 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
531 while ((d[0] == 0xff) && ds) {
536 /* JPEG_MARKER_SOI */
537 if (d[0] == JPEG_MARKER_SOI) {
543 /* JPEG_MARKER_APP0 */
544 if (d[0] == JPEG_MARKER_APP0) {
547 l = (d[0] << 8) | d[1];
555 /* JPEG_MARKER_APP1 */
556 if (d[0] == JPEG_MARKER_APP1)
559 /* Unknown marker or data. Give up. */
561 printf ("EXIF marker not found.\n");
569 printf ("Size too small.\n");
573 len = (d[0] << 8) | d[1];
575 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
582 * Verify the exif header
583 * (offset 2, length 6).
587 printf ("Size too small.\n");
591 if (memcmp (d, ExifHeader, 6)) {
593 printf ("EXIF header not found.\n");
599 printf ("Found EXIF header.\n");
602 /* Byte order (offset 6, length 2) */
605 if (!memcmp (d + 6, "II", 2))
606 data->priv->order = EXIF_BYTE_ORDER_INTEL;
607 else if (!memcmp (d + 6, "MM", 2))
608 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
613 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
617 offset = exif_get_long (d + 10, data->priv->order);
619 printf ("IFD 0 at %i.\n", (int) offset);
622 /* Parse the actual exif data (offset 14) */
623 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
627 n = exif_get_short (d + 6 + offset, data->priv->order);
628 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
631 printf ("IFD 1 at %i.\n", (int) offset);
635 if (offset > ds - 6) {
637 printf ("Bogus offset!\n");
642 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
647 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
648 * cameras use pointers in the maker note tag that point to the
649 * space between IFDs. Here is the only place where we have access
652 e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
656 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5)))
657 data->priv->md = exif_mnote_data_olympus_new ();
660 else if ((e->size >= 2) && (e->data[0] == 0x00)
661 && (e->data[1] == 0x1b))
662 data->priv->md = exif_mnote_data_pentax_new ();
665 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
669 if (!strcmp (exif_entry_get_value (em), "Canon"))
670 data->priv->md = exif_mnote_data_canon_new ();
675 * If we are able to interpret the maker note, interpret it and
676 * remove the corresponding entry as it may contain invalid
677 * pointers after this function here returns.
679 if (data->priv->md) {
680 exif_mnote_data_set_byte_order (data->priv->md,
682 exif_mnote_data_set_offset (data->priv->md,
683 data->priv->offset_mnote);
684 exif_mnote_data_load (data->priv->md, d, ds);
685 exif_data_remove_entry (data, EXIF_TAG_MAKER_NOTE);
691 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
700 *d = malloc (sizeof (char) * *ds);
701 memcpy (*d, ExifHeader, 6);
703 /* Order (offset 6) */
705 *d = realloc (*d, sizeof (char) * *ds);
706 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
707 memcpy (*d + 6, "II", 2);
709 memcpy (*d + 6, "MM", 2);
712 /* Fixed value (2 bytes, offset 8) */
714 *d = realloc (*d, sizeof (char) * *ds);
715 exif_set_short (*d + 8, data->priv->order, 0x002a);
718 * IFD 0 offset (4 bytes, offset 10).
719 * We will start 8 bytes after the
720 * EXIF header (2 bytes for order, another 2 for the test, and
721 * 4 bytes for the IFD 0 offset make 8 bytes together).
724 *d = realloc (*d, sizeof (char) * *ds);
725 exif_set_long (*d + 10, data->priv->order, 8);
727 /* Now save IFD 0. IFD 1 will be saved automatically. */
729 printf ("Saving IFDs...\n");
731 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
735 printf ("Saved %i byte(s) EXIF data.\n", *ds);
740 exif_data_new_from_file (const char *path)
746 unsigned char data[1024];
748 f = fopen (path, "rb");
752 loader = exif_loader_new ();
754 size = fread (data, 1, 1024, f);
755 if (size <= 0) break;
756 if (!exif_loader_write (loader, data, size)) break;
760 edata = exif_loader_get_data (loader);
761 exif_loader_unref (loader);
767 exif_data_ref (ExifData *data)
772 data->priv->ref_count++;
776 exif_data_unref (ExifData *data)
781 data->priv->ref_count--;
782 if (!data->priv->ref_count)
783 exif_data_free (data);
787 exif_data_free (ExifData *data)
794 for (i = 0; i < EXIF_IFD_COUNT; i++) {
796 exif_content_unref (data->ifd[i]);
805 if (data->priv->md) {
806 exif_mnote_data_unref (data->priv->md);
807 data->priv->md = NULL;
816 exif_data_dump (ExifData *data)
823 for (i = 0; i < EXIF_IFD_COUNT; i++) {
824 if (data->ifd[i] && data->ifd[i]->count) {
825 printf ("Dumping IFD '%s'...\n",
826 exif_ifd_get_name (i));
827 exif_content_dump (data->ifd[i], 0);
832 printf ("%i byte(s) thumbnail data available.", data->size);
833 if (data->size >= 4) {
834 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
835 data->data[0], data->data[1],
836 data->data[data->size - 2],
837 data->data[data->size - 1]);
843 exif_data_get_byte_order (ExifData *data)
848 return (data->priv->order);
852 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
860 for (i = 0; i < EXIF_IFD_COUNT; i++)
861 func (data->ifd[i], user_data);
864 typedef struct _ByteOrderChangeData ByteOrderChangeData;
865 struct _ByteOrderChangeData {
866 ExifByteOrder old, new;
870 entry_set_byte_order (ExifEntry *e, void *data)
872 ByteOrderChangeData *d = data;
884 case EXIF_FORMAT_SHORT:
885 for (i = 0; i < e->components; i++) {
886 s = exif_get_short (e->data +
887 (i * exif_format_get_size (e->format)),
889 exif_set_short (e->data +
890 (i * exif_format_get_size (e->format)),
894 case EXIF_FORMAT_LONG:
895 for (i = 0; i < e->components; i++) {
896 l = exif_get_long (e->data +
897 (i * exif_format_get_size (e->format)),
899 exif_set_long (e->data +
900 (i * exif_format_get_size (e->format)),
904 case EXIF_FORMAT_RATIONAL:
905 for (i = 0; i < e->components; i++) {
906 r = exif_get_rational (e->data +
907 (i * exif_format_get_size (e->format)),
909 exif_set_rational (e->data +
910 (i * exif_format_get_size (e->format)),
914 case EXIF_FORMAT_SLONG:
915 for (i = 0; i < e->components; i++) {
916 sl = exif_get_slong (e->data +
917 (i * exif_format_get_size (e->format)),
919 exif_set_slong (e->data +
920 (i * exif_format_get_size (e->format)),
924 case EXIF_FORMAT_SRATIONAL:
925 for (i = 0; i < e->components; i++) {
926 sr = exif_get_srational (e->data +
927 (i * exif_format_get_size (e->format)),
929 exif_set_srational (e->data +
930 (i * exif_format_get_size (e->format)),
934 case EXIF_FORMAT_UNDEFINED:
935 case EXIF_FORMAT_BYTE:
936 case EXIF_FORMAT_ASCII:
944 content_set_byte_order (ExifContent *content, void *data)
946 exif_content_foreach_entry (content, entry_set_byte_order, data);
950 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
952 ByteOrderChangeData d;
954 if (!data || (order == data->priv->order))
957 d.old = data->priv->order;
959 exif_data_foreach_content (data, content_set_byte_order, &d);
960 data->priv->order = order;