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,
290 entry = exif_entry_new ();
291 exif_content_add_entry (ifd, entry);
292 exif_data_load_data_entry (data, entry, d, ds,
294 exif_entry_unref (entry);
301 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
302 unsigned char **d, unsigned int *ds,
305 unsigned int j, n_ptr = 0, n_thumb = 0;
308 if (!data || !ifd || !d || !ds)
311 for (i = 0; i < EXIF_IFD_COUNT; i++)
312 if (ifd == data->ifd[i])
314 if (i == EXIF_IFD_COUNT)
318 * Check if we need some extra entries for pointers or the thumbnail.
324 * The pointer to IFD_EXIF is in IFD_0. The pointer to
325 * IFD_INTEROPERABILITY is in IFD_EXIF.
327 if (data->ifd[EXIF_IFD_EXIF]->count ||
328 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
331 /* The pointer to IFD_GPS is in IFD_0. */
332 if (data->ifd[EXIF_IFD_GPS]->count)
341 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
348 * Allocate enough memory for all entries
349 * and the number of entries.
351 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
352 *d = realloc (*d, sizeof (char) * *ds);
354 /* Save the number of entries */
355 exif_set_short (*d + 6 + offset, data->priv->order,
356 (ExifShort) (ifd->count + n_ptr + n_thumb));
360 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
361 ifd->count, exif_ifd_get_name (i), offset);
364 /* Save each entry */
365 for (j = 0; j < ifd->count; j++)
366 exif_data_save_data_entry (data, ifd->entries[j],
367 d, ds, offset + 12 * j);
368 offset += 12 * ifd->count;
370 /* Now save special entries. */
375 * The pointer to IFD_EXIF is in IFD_0.
376 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
377 * therefore, if IFD_INTEROPERABILITY is not empty, we need
378 * IFD_EXIF even if latter is empty.
380 if (data->ifd[EXIF_IFD_EXIF]->count ||
381 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
382 exif_set_short (*d + 6 + offset + 0, data->priv->order,
383 EXIF_TAG_EXIF_IFD_POINTER);
384 exif_set_short (*d + 6 + offset + 2, data->priv->order,
386 exif_set_long (*d + 6 + offset + 4, data->priv->order,
388 exif_set_long (*d + 6 + offset + 8, data->priv->order,
390 exif_data_save_data_content (data,
391 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
395 /* The pointer to IFD_GPS is in IFD_0, too. */
396 if (data->ifd[EXIF_IFD_GPS]->count) {
397 exif_set_short (*d + 6 + offset + 0, data->priv->order,
398 EXIF_TAG_GPS_INFO_IFD_POINTER);
399 exif_set_short (*d + 6 + offset + 2, data->priv->order,
401 exif_set_long (*d + 6 + offset + 4, data->priv->order,
403 exif_set_long (*d + 6 + offset + 8, data->priv->order,
405 exif_data_save_data_content (data,
406 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
414 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
417 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
418 exif_set_short (*d + 6 + offset + 0, data->priv->order,
419 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
420 exif_set_short (*d + 6 + offset + 2, data->priv->order,
422 exif_set_long (*d + 6 + offset + 4, data->priv->order,
424 exif_set_long (*d + 6 + offset + 8, data->priv->order,
426 exif_data_save_data_content (data,
427 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
436 * Information about the thumbnail (if any) is saved in
441 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
442 exif_set_short (*d + 6 + offset + 0, data->priv->order,
443 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
444 exif_set_short (*d + 6 + offset + 2, data->priv->order,
446 exif_set_long (*d + 6 + offset + 4, data->priv->order,
448 exif_set_long (*d + 6 + offset + 8, data->priv->order,
451 *d = realloc (*d, sizeof (char) * *ds);
452 memcpy (*d + *ds - data->size, data->data, data->size);
455 printf ("Wrote %i bytes of thumbnail data at offset "
456 "%i.\n", data->size, *ds - data->size);
457 printf ("We currently have %i bytes EXIF data.\n", *ds);
460 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
461 exif_set_short (*d + 6 + offset + 0, data->priv->order,
462 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
463 exif_set_short (*d + 6 + offset + 2, data->priv->order,
465 exif_set_long (*d + 6 + offset + 4, data->priv->order,
467 exif_set_long (*d + 6 + offset + 8, data->priv->order,
477 /* Correctly terminate the directory */
478 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
482 * We are saving IFD 0. Tell where IFD 1 starts and save
485 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
486 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
489 exif_set_long (*d + 6 + offset, data->priv->order, 0);
493 exif_data_load_data (ExifData *data, const unsigned char *d_orig,
494 unsigned int ds_orig)
500 const unsigned char *d = d_orig;
501 unsigned int ds = ds_orig, len;
509 printf ("Parsing %i byte(s) EXIF data...\n", ds);
513 * It can be that the data starts with the EXIF header. If it does
514 * not, search the EXIF marker.
518 printf ("Size too small.\n");
522 if (!memcmp (d, ExifHeader, 6)) {
524 printf ("Found EXIF header.\n");
528 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
529 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
532 while ((d[0] == 0xff) && ds) {
537 /* JPEG_MARKER_SOI */
538 if (d[0] == JPEG_MARKER_SOI) {
544 /* JPEG_MARKER_APP0 */
545 if (d[0] == JPEG_MARKER_APP0) {
548 l = (d[0] << 8) | d[1];
556 /* JPEG_MARKER_APP1 */
557 if (d[0] == JPEG_MARKER_APP1)
560 /* Unknown marker or data. Give up. */
562 printf ("EXIF marker not found.\n");
570 printf ("Size too small.\n");
574 len = (d[0] << 8) | d[1];
576 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
583 * Verify the exif header
584 * (offset 2, length 6).
588 printf ("Size too small.\n");
592 if (memcmp (d, ExifHeader, 6)) {
594 printf ("EXIF header not found.\n");
600 printf ("Found EXIF header.\n");
603 /* Byte order (offset 6, length 2) */
606 if (!memcmp (d + 6, "II", 2))
607 data->priv->order = EXIF_BYTE_ORDER_INTEL;
608 else if (!memcmp (d + 6, "MM", 2))
609 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
614 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
618 offset = exif_get_long (d + 10, data->priv->order);
620 printf ("IFD 0 at %i.\n", (int) offset);
623 /* Parse the actual exif data (offset 14) */
624 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
628 n = exif_get_short (d + 6 + offset, data->priv->order);
629 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
632 printf ("IFD 1 at %i.\n", (int) offset);
636 if (offset > ds - 6) {
638 printf ("Bogus offset!\n");
643 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
648 * If we got an EXIF_TAG_MAKER_NOTE, try to interpret it. Some
649 * cameras use pointers in the maker note tag that point to the
650 * space between IFDs. Here is the only place where we have access
653 e = exif_data_get_entry (data, EXIF_TAG_MAKER_NOTE);
657 if ((e->size >= 5) && (!memcmp (e->data, "OLYMP", 5)))
658 data->priv->md = exif_mnote_data_olympus_new ();
661 else if ((e->size >= 2) && (e->data[0] == 0x00)
662 && (e->data[1] == 0x1b))
663 data->priv->md = exif_mnote_data_pentax_new ();
666 em = exif_data_get_entry (data, EXIF_TAG_MAKE);
671 if (!strcmp (exif_entry_get_value (em, value, sizeof(value)), "Canon"))
672 data->priv->md = exif_mnote_data_canon_new ();
677 * If we are able to interpret the maker note, do so.
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);
690 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
699 *d = malloc (sizeof (char) * *ds);
700 memcpy (*d, ExifHeader, 6);
702 /* Order (offset 6) */
704 *d = realloc (*d, sizeof (char) * *ds);
705 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
706 memcpy (*d + 6, "II", 2);
708 memcpy (*d + 6, "MM", 2);
711 /* Fixed value (2 bytes, offset 8) */
713 *d = realloc (*d, sizeof (char) * *ds);
714 exif_set_short (*d + 8, data->priv->order, 0x002a);
717 * IFD 0 offset (4 bytes, offset 10).
718 * We will start 8 bytes after the
719 * EXIF header (2 bytes for order, another 2 for the test, and
720 * 4 bytes for the IFD 0 offset make 8 bytes together).
723 *d = realloc (*d, sizeof (char) * *ds);
724 exif_set_long (*d + 10, data->priv->order, 8);
726 /* Now save IFD 0. IFD 1 will be saved automatically. */
728 printf ("Saving IFDs...\n");
730 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
734 printf ("Saved %i byte(s) EXIF data.\n", *ds);
739 exif_data_new_from_file (const char *path)
745 unsigned char data[1024];
747 f = fopen (path, "rb");
751 loader = exif_loader_new ();
753 size = fread (data, 1, sizeof (data), f);
754 if (size <= 0) break;
755 if (!exif_loader_write (loader, data, size)) break;
759 edata = exif_loader_get_data (loader);
760 exif_loader_unref (loader);
766 exif_data_ref (ExifData *data)
771 data->priv->ref_count++;
775 exif_data_unref (ExifData *data)
780 data->priv->ref_count--;
781 if (!data->priv->ref_count)
782 exif_data_free (data);
786 exif_data_free (ExifData *data)
793 for (i = 0; i < EXIF_IFD_COUNT; i++) {
795 exif_content_unref (data->ifd[i]);
804 if (data->priv->md) {
805 exif_mnote_data_unref (data->priv->md);
806 data->priv->md = NULL;
815 exif_data_dump (ExifData *data)
822 for (i = 0; i < EXIF_IFD_COUNT; i++) {
823 if (data->ifd[i] && data->ifd[i]->count) {
824 printf ("Dumping IFD '%s'...\n",
825 exif_ifd_get_name (i));
826 exif_content_dump (data->ifd[i], 0);
831 printf ("%i byte(s) thumbnail data available.", data->size);
832 if (data->size >= 4) {
833 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
834 data->data[0], data->data[1],
835 data->data[data->size - 2],
836 data->data[data->size - 1]);
842 exif_data_get_byte_order (ExifData *data)
847 return (data->priv->order);
851 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
859 for (i = 0; i < EXIF_IFD_COUNT; i++)
860 func (data->ifd[i], user_data);
863 typedef struct _ByteOrderChangeData ByteOrderChangeData;
864 struct _ByteOrderChangeData {
865 ExifByteOrder old, new;
869 entry_set_byte_order (ExifEntry *e, void *data)
871 ByteOrderChangeData *d = data;
883 case EXIF_FORMAT_SHORT:
884 for (i = 0; i < e->components; i++) {
885 s = exif_get_short (e->data +
886 (i * exif_format_get_size (e->format)),
888 exif_set_short (e->data +
889 (i * exif_format_get_size (e->format)),
893 case EXIF_FORMAT_LONG:
894 for (i = 0; i < e->components; i++) {
895 l = exif_get_long (e->data +
896 (i * exif_format_get_size (e->format)),
898 exif_set_long (e->data +
899 (i * exif_format_get_size (e->format)),
903 case EXIF_FORMAT_RATIONAL:
904 for (i = 0; i < e->components; i++) {
905 r = exif_get_rational (e->data +
906 (i * exif_format_get_size (e->format)),
908 exif_set_rational (e->data +
909 (i * exif_format_get_size (e->format)),
913 case EXIF_FORMAT_SLONG:
914 for (i = 0; i < e->components; i++) {
915 sl = exif_get_slong (e->data +
916 (i * exif_format_get_size (e->format)),
918 exif_set_slong (e->data +
919 (i * exif_format_get_size (e->format)),
923 case EXIF_FORMAT_SRATIONAL:
924 for (i = 0; i < e->components; i++) {
925 sr = exif_get_srational (e->data +
926 (i * exif_format_get_size (e->format)),
928 exif_set_srational (e->data +
929 (i * exif_format_get_size (e->format)),
933 case EXIF_FORMAT_UNDEFINED:
934 case EXIF_FORMAT_BYTE:
935 case EXIF_FORMAT_SBYTE:
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;
962 exif_mnote_data_set_byte_order (data->priv->md, order);