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);
165 /* If this is the maker note tag, update it. */
166 if ((e->tag == EXIF_TAG_MAKER_NOTE) && data->priv->md) {
170 exif_mnote_data_set_offset (data->priv->md, *ds - 6);
171 exif_mnote_data_save (data->priv->md, &e->data, &e->size);
172 e->components = e->size;
175 exif_set_long (*d + 6 + offset + 4,
176 data->priv->order, e->components);
179 * Size? If bigger than 4 bytes, the actual data is not in
180 * the entry but somewhere else.
182 s = exif_format_get_size (e->format) * e->components;
186 *d = realloc (*d, *ds);
189 exif_set_long (*d + 6 + offset + 8,
190 data->priv->order, doff);
194 /* Write the data. Fill unneeded bytes with 0. */
195 memcpy (*d + 6 + doff, e->data, s);
196 if (s < 4) memset (*d + 6 + doff + s, 0, (4 - s));
200 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
201 unsigned int ds, ExifLong offset, ExifLong size)
203 if (ds < offset + size) {
205 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
206 (int) ds, (int) offset, (int) size);
213 data->data = malloc (data->size);
216 memcpy (data->data, d + offset, data->size);
220 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
221 const unsigned char *d,
222 unsigned int ds, unsigned int offset)
224 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
230 /* Read the number of entries */
231 if (offset >= ds - 1) return;
232 n = exif_get_short (d + offset, data->priv->order);
234 printf ("Loading %i entries...\n", n);
238 /* Check if we have enough data. */
239 if (offset + 12 * n > ds) n = (ds - offset) / 12;
241 for (i = 0; i < n; i++) {
243 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
245 printf ("Loading entry '%s' (%i of %i)...\n",
246 exif_tag_get_name (tag), i + 1, n);
249 case EXIF_TAG_EXIF_IFD_POINTER:
250 case EXIF_TAG_GPS_INFO_IFD_POINTER:
251 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
252 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
253 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
254 o = exif_get_long (d + offset + 12 * i + 8,
257 case EXIF_TAG_EXIF_IFD_POINTER:
258 exif_data_load_data_content (data,
259 data->ifd[EXIF_IFD_EXIF], d, ds, o);
261 case EXIF_TAG_GPS_INFO_IFD_POINTER:
262 exif_data_load_data_content (data,
263 data->ifd[EXIF_IFD_GPS], d, ds, o);
265 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
266 exif_data_load_data_content (data,
267 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
269 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
271 printf ("Thumbnail at %i.\n", (int) o);
273 thumbnail_offset = o;
274 if (thumbnail_offset && thumbnail_length)
275 exif_data_load_data_thumbnail (data, d,
276 ds, thumbnail_offset,
279 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
281 printf ("Thumbnail size: %i.\n", (int) o);
283 thumbnail_length = o;
284 if (thumbnail_offset && thumbnail_length)
285 exif_data_load_data_thumbnail (data, d,
286 ds, thumbnail_offset,
296 * If we don't know the tag, changes are high
297 * that the EXIF data does not follow the standard.
299 if (!exif_tag_get_name (tag)) return;
300 entry = exif_entry_new ();
301 exif_content_add_entry (ifd, entry);
302 exif_data_load_data_entry (data, entry, d, ds,
304 exif_entry_unref (entry);
311 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
312 unsigned char **d, unsigned int *ds,
315 unsigned int j, n_ptr = 0, n_thumb = 0;
318 if (!data || !ifd || !d || !ds)
321 for (i = 0; i < EXIF_IFD_COUNT; i++)
322 if (ifd == data->ifd[i])
324 if (i == EXIF_IFD_COUNT)
328 * Check if we need some extra entries for pointers or the thumbnail.
334 * The pointer to IFD_EXIF is in IFD_0. The pointer to
335 * IFD_INTEROPERABILITY is in IFD_EXIF.
337 if (data->ifd[EXIF_IFD_EXIF]->count ||
338 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
341 /* The pointer to IFD_GPS is in IFD_0. */
342 if (data->ifd[EXIF_IFD_GPS]->count)
351 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
358 * Allocate enough memory for all entries
359 * and the number of entries.
361 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
362 *d = realloc (*d, *ds);
366 /* Save the number of entries */
367 exif_set_short (*d + 6 + offset, data->priv->order,
368 (ExifShort) (ifd->count + n_ptr + n_thumb));
372 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
373 ifd->count, exif_ifd_get_name (i), offset);
376 /* Save each entry */
377 for (j = 0; j < ifd->count; j++)
378 exif_data_save_data_entry (data, ifd->entries[j],
379 d, ds, offset + 12 * j);
380 offset += 12 * ifd->count;
382 /* Now save special entries. */
387 * The pointer to IFD_EXIF is in IFD_0.
388 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
389 * therefore, if IFD_INTEROPERABILITY is not empty, we need
390 * IFD_EXIF even if latter is empty.
392 if (data->ifd[EXIF_IFD_EXIF]->count ||
393 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
394 exif_set_short (*d + 6 + offset + 0, data->priv->order,
395 EXIF_TAG_EXIF_IFD_POINTER);
396 exif_set_short (*d + 6 + offset + 2, data->priv->order,
398 exif_set_long (*d + 6 + offset + 4, data->priv->order,
400 exif_set_long (*d + 6 + offset + 8, data->priv->order,
402 exif_data_save_data_content (data,
403 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
407 /* The pointer to IFD_GPS is in IFD_0, too. */
408 if (data->ifd[EXIF_IFD_GPS]->count) {
409 exif_set_short (*d + 6 + offset + 0, data->priv->order,
410 EXIF_TAG_GPS_INFO_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_GPS], d, ds, *ds - 6);
426 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
429 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
430 exif_set_short (*d + 6 + offset + 0, data->priv->order,
431 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
432 exif_set_short (*d + 6 + offset + 2, data->priv->order,
434 exif_set_long (*d + 6 + offset + 4, data->priv->order,
436 exif_set_long (*d + 6 + offset + 8, data->priv->order,
438 exif_data_save_data_content (data,
439 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
448 * Information about the thumbnail (if any) is saved in
453 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
454 exif_set_short (*d + 6 + offset + 0, data->priv->order,
455 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
456 exif_set_short (*d + 6 + offset + 2, data->priv->order,
458 exif_set_long (*d + 6 + offset + 4, data->priv->order,
460 exif_set_long (*d + 6 + offset + 8, data->priv->order,
463 *d = realloc (*d, *ds);
466 memcpy (*d + *ds - data->size, data->data, data->size);
469 printf ("Wrote %i bytes of thumbnail data at offset "
470 "%i.\n", data->size, *ds - data->size);
471 printf ("We currently have %i bytes EXIF data.\n", *ds);
474 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
475 exif_set_short (*d + 6 + offset + 0, data->priv->order,
476 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
477 exif_set_short (*d + 6 + offset + 2, data->priv->order,
479 exif_set_long (*d + 6 + offset + 4, data->priv->order,
481 exif_set_long (*d + 6 + offset + 8, data->priv->order,
491 /* Correctly terminate the directory */
492 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
496 * We are saving IFD 0. Tell where IFD 1 starts and save
499 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
500 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
503 exif_set_long (*d + 6 + offset, data->priv->order, 0);
507 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
508 unsigned int ds_orig)
514 const unsigned char *d = d_orig;
515 unsigned int ds = ds_orig, len;
523 printf ("Parsing %i byte(s) EXIF data...\n", ds);
527 * It can be that the data starts with the EXIF header. If it does
528 * not, search the EXIF marker.
532 printf ("Size too small.\n");
536 if (!memcmp (d, ExifHeader, 6)) {
538 printf ("Found EXIF header.\n");
542 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
543 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
546 while ((d[0] == 0xff) && ds) {
551 /* JPEG_MARKER_SOI */
552 if (d[0] == JPEG_MARKER_SOI) {
558 /* JPEG_MARKER_APP0 */
559 if (d[0] == JPEG_MARKER_APP0) {
562 l = (d[0] << 8) | d[1];
570 /* JPEG_MARKER_APP1 */
571 if (d[0] == JPEG_MARKER_APP1)
574 /* Unknown marker or data. Give up. */
576 printf ("EXIF marker not found.\n");
584 printf ("Size too small.\n");
588 len = (d[0] << 8) | d[1];
590 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
597 * Verify the exif header
598 * (offset 2, length 6).
602 printf ("Size too small.\n");
606 if (memcmp (d, ExifHeader, 6)) {
608 printf ("EXIF header not found.\n");
614 printf ("Found EXIF header.\n");
617 /* Byte order (offset 6, length 2) */
620 if (!memcmp (d + 6, "II", 2))
621 data->priv->order = EXIF_BYTE_ORDER_INTEL;
622 else if (!memcmp (d + 6, "MM", 2))
623 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
628 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
632 offset = exif_get_long (d + 10, data->priv->order);
634 printf ("IFD 0 at %i.\n", (int) offset);
637 /* Parse the actual exif data (offset 14) */
638 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
642 n = exif_get_short (d + 6 + offset, data->priv->order);
643 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
646 printf ("IFD 1 at %i.\n", (int) offset);
650 if (offset > ds - 6) {
652 printf ("Bogus offset!\n");
657 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
662 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
663 * cameras use pointers in the maker note tag that point to the
664 * space between IFDs. Here is the only place where we have access
667 e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
670 /* Olympus & Nikon */
671 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5) ||
672 !memcmp (e->data, "Nikon", 5))) {
673 data->priv->md = exif_mnote_data_olympus_new ();
676 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
677 /* Pentax & some variant of Nikon */
678 if ((e->size >= 2) && (e->data[0] == 0x00)
679 && (e->data[1] == 0x1b)) {
680 if (em && !strncmp (exif_entry_get_value (em, value, sizeof(value)), "Nikon", 5)) {
681 data->priv->md = exif_mnote_data_olympus_new ();
683 data->priv->md = exif_mnote_data_pentax_new ();
690 if (!strcmp (exif_entry_get_value (em, value, sizeof(value)), "Canon"))
691 data->priv->md = exif_mnote_data_canon_new ();
697 * If we are able to interpret the maker note, do so.
699 if (data->priv->md) {
700 exif_mnote_data_set_byte_order (data->priv->md,
702 exif_mnote_data_set_offset (data->priv->md,
703 data->priv->offset_mnote);
704 exif_mnote_data_load (data->priv->md, d, ds);
710 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
712 if (!data || !d || !ds)
720 memcpy (*d, ExifHeader, 6);
722 /* Order (offset 6) */
723 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
724 memcpy (*d + 6, "II", 2);
726 memcpy (*d + 6, "MM", 2);
729 /* Fixed value (2 bytes, offset 8) */
730 exif_set_short (*d + 8, data->priv->order, 0x002a);
733 * IFD 0 offset (4 bytes, offset 10).
734 * We will start 8 bytes after the
735 * EXIF header (2 bytes for order, another 2 for the test, and
736 * 4 bytes for the IFD 0 offset make 8 bytes together).
738 exif_set_long (*d + 10, data->priv->order, 8);
740 /* Now save IFD 0. IFD 1 will be saved automatically. */
742 printf ("Saving IFDs...\n");
744 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
748 printf ("Saved %i byte(s) EXIF data.\n", *ds);
753 exif_data_new_from_file (const char *path)
759 unsigned char data[1024];
761 f = fopen (path, "rb");
765 loader = exif_loader_new ();
767 size = fread (data, 1, sizeof (data), f);
768 if (size <= 0) break;
769 if (!exif_loader_write (loader, data, size)) break;
773 edata = exif_loader_get_data (loader);
774 exif_loader_unref (loader);
780 exif_data_ref (ExifData *data)
785 data->priv->ref_count++;
789 exif_data_unref (ExifData *data)
794 data->priv->ref_count--;
795 if (!data->priv->ref_count)
796 exif_data_free (data);
800 exif_data_free (ExifData *data)
807 for (i = 0; i < EXIF_IFD_COUNT; i++) {
809 exif_content_unref (data->ifd[i]);
818 if (data->priv->md) {
819 exif_mnote_data_unref (data->priv->md);
820 data->priv->md = NULL;
829 exif_data_dump (ExifData *data)
836 for (i = 0; i < EXIF_IFD_COUNT; i++) {
837 if (data->ifd[i] && data->ifd[i]->count) {
838 printf ("Dumping IFD '%s'...\n",
839 exif_ifd_get_name (i));
840 exif_content_dump (data->ifd[i], 0);
845 printf ("%i byte(s) thumbnail data available.", data->size);
846 if (data->size >= 4) {
847 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
848 data->data[0], data->data[1],
849 data->data[data->size - 2],
850 data->data[data->size - 1]);
856 exif_data_get_byte_order (ExifData *data)
861 return (data->priv->order);
865 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
873 for (i = 0; i < EXIF_IFD_COUNT; i++)
874 func (data->ifd[i], user_data);
877 typedef struct _ByteOrderChangeData ByteOrderChangeData;
878 struct _ByteOrderChangeData {
879 ExifByteOrder old, new;
883 entry_set_byte_order (ExifEntry *e, void *data)
885 ByteOrderChangeData *d = data;
897 case EXIF_FORMAT_SHORT:
898 for (i = 0; i < e->components; i++) {
899 s = exif_get_short (e->data +
900 (i * exif_format_get_size (e->format)),
902 exif_set_short (e->data +
903 (i * exif_format_get_size (e->format)),
907 case EXIF_FORMAT_LONG:
908 for (i = 0; i < e->components; i++) {
909 l = exif_get_long (e->data +
910 (i * exif_format_get_size (e->format)),
912 exif_set_long (e->data +
913 (i * exif_format_get_size (e->format)),
917 case EXIF_FORMAT_RATIONAL:
918 for (i = 0; i < e->components; i++) {
919 r = exif_get_rational (e->data +
920 (i * exif_format_get_size (e->format)),
922 exif_set_rational (e->data +
923 (i * exif_format_get_size (e->format)),
927 case EXIF_FORMAT_SLONG:
928 for (i = 0; i < e->components; i++) {
929 sl = exif_get_slong (e->data +
930 (i * exif_format_get_size (e->format)),
932 exif_set_slong (e->data +
933 (i * exif_format_get_size (e->format)),
937 case EXIF_FORMAT_SRATIONAL:
938 for (i = 0; i < e->components; i++) {
939 sr = exif_get_srational (e->data +
940 (i * exif_format_get_size (e->format)),
942 exif_set_srational (e->data +
943 (i * exif_format_get_size (e->format)),
947 case EXIF_FORMAT_UNDEFINED:
948 case EXIF_FORMAT_BYTE:
949 case EXIF_FORMAT_SBYTE:
950 case EXIF_FORMAT_ASCII:
958 content_set_byte_order (ExifContent *content, void *data)
960 exif_content_foreach_entry (content, entry_set_byte_order, data);
964 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
966 ByteOrderChangeData d;
968 if (!data || (order == data->priv->order))
971 d.old = data->priv->order;
973 exif_data_foreach_content (data, content_set_byte_order, &d);
974 data->priv->order = order;
976 exif_mnote_data_set_byte_order (data->priv->md, order);