3 * Copyright (C) 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.
21 #include "exif-data.h"
27 #include <libjpeg/jpeg-marker.h>
29 #include <libexif/exif-utils.h>
32 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
36 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
38 struct _ExifDataPrivate
42 unsigned int ref_count;
50 data = malloc (sizeof (ExifData));
53 memset (data, 0, sizeof (ExifData));
54 data->priv = malloc (sizeof (ExifDataPrivate));
59 memset (data->priv, 0, sizeof (ExifDataPrivate));
60 data->priv->ref_count = 1;
62 data->ifd0 = exif_content_new ();
63 data->ifd1 = exif_content_new ();
64 data->ifd_exif = exif_content_new ();
65 data->ifd_gps = exif_content_new ();
66 data->ifd_interoperability = exif_content_new ();
67 if (!data->ifd_exif || !data->ifd_gps || !data->ifd_interoperability ||
68 !data->ifd0 || !data->ifd1) {
69 exif_data_free (data);
72 data->ifd0->parent = data;
73 data->ifd1->parent = data;
74 data->ifd_exif->parent = data;
75 data->ifd_gps->parent = data;
76 data->ifd_interoperability->parent = data;
82 exif_data_new_from_data (const unsigned char *data, unsigned int size)
86 edata = exif_data_new ();
87 exif_data_load_data (edata, data, size);
92 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
93 const unsigned char *d,
94 unsigned int size, unsigned int offset)
98 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
99 entry->format = exif_get_short (d + offset + 2, data->priv->order);
100 entry->components = exif_get_long (d + offset + 4, data->priv->order);
103 * Size? If bigger than 4 bytes, the actual data is not
104 * in the entry but somewhere else (offset).
106 s = exif_format_get_size (entry->format) * entry->components;
110 doff = exif_get_long (d + offset + 8, data->priv->order);
118 entry->data = malloc (sizeof (char) * s);
122 memcpy (entry->data, d + doff, s);
126 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
127 unsigned char **d, unsigned int *ds,
130 unsigned int doff, s;
133 * Each entry is 12 bytes long. The memory for the entry has
134 * already been allocated.
136 exif_set_short (*d + 6 + offset + 0,
137 data->priv->order, entry->tag);
138 exif_set_short (*d + 6 + offset + 2,
139 data->priv->order, entry->format);
140 exif_set_long (*d + 6 + offset + 4,
141 data->priv->order, entry->components);
144 * Size? If bigger than 4 bytes, the actual data is not in
145 * the entry but somewhere else.
147 s = exif_format_get_size (entry->format) * entry->components;
152 *d = realloc (*d, sizeof (char) * *ds);
153 doff = *ds - 6 - entry->size;
154 exif_set_long (*d + 6 + offset + 8,
155 data->priv->order, doff);
158 memcpy (*d + 6 + doff, entry->data, entry->size);
162 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
163 unsigned int ds, ExifLong offset, ExifLong size)
165 if (ds < offset + size) {
167 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
168 (int) ds, (int) offset, (int) size);
175 data->data = malloc (sizeof (char) * data->size);
176 memcpy (data->data, d + offset, data->size);
180 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
181 const unsigned char *d,
182 unsigned int ds, unsigned int offset)
184 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
190 /* Read the number of entries */
191 n = exif_get_short (d + offset, data->priv->order);
193 printf ("Loading %i entries...\n", n);
196 for (i = 0; i < n; i++) {
198 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
200 printf ("Loading entry '%s' (%i of %i)...\n",
201 exif_tag_get_name (tag), i + 1, n);
204 case EXIF_TAG_EXIF_IFD_POINTER:
205 case EXIF_TAG_GPS_INFO_IFD_POINTER:
206 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
207 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
208 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
209 o = exif_get_long (d + offset + 12 * i + 8,
212 case EXIF_TAG_EXIF_IFD_POINTER:
213 exif_data_load_data_content (data,
214 data->ifd_exif, d, ds, o);
216 case EXIF_TAG_GPS_INFO_IFD_POINTER:
217 exif_data_load_data_content (data,
218 data->ifd_gps, d, ds, o);
220 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
221 exif_data_load_data_content (data,
222 data->ifd_interoperability, d, ds, o);
224 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
226 printf ("Thumbnail at %i.\n", (int) o);
228 thumbnail_offset = o;
229 if (thumbnail_offset && thumbnail_length)
230 exif_data_load_data_thumbnail (data, d,
231 ds, thumbnail_offset,
234 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
236 printf ("Thumbnail size: %i.\n", (int) o);
238 thumbnail_length = o;
239 if (thumbnail_offset && thumbnail_length)
240 exif_data_load_data_thumbnail (data, d,
241 ds, thumbnail_offset,
249 entry = exif_entry_new ();
250 exif_content_add_entry (ifd, entry);
251 exif_data_load_data_entry (data, entry, d, ds,
253 exif_entry_unref (entry);
260 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
261 unsigned char **d, unsigned int *ds,
264 unsigned int i, n_ptr = 0, n_thumb = 0;
267 * Check if we need some extra entries for pointers or the thumbnail.
269 if (ifd == data->ifd0) {
270 if (data->ifd_exif->count || data->ifd_interoperability->count)
272 if (data->ifd_gps->count)
274 } else if (ifd == data->ifd1) {
277 } else if (ifd == data->ifd_exif) {
278 if (data->ifd_interoperability->count)
283 * Allocate enough memory for all entries
284 * and the number of entries.
286 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
287 *d = realloc (*d, sizeof (char) * *ds);
289 /* Save the number of entries */
290 exif_set_short (*d + 6 + offset, data->priv->order,
291 ifd->count + n_ptr + n_thumb);
294 printf ("Saving %i entries (offset: %i)...\n", ifd->count, offset);
297 /* Save each entry */
298 for (i = 0; i < ifd->count; i++)
299 exif_data_save_data_entry (data, ifd->entries[i],
300 d, ds, offset + 12 * i);
301 offset += 12 * ifd->count;
303 /* Save special entries */
304 if (ifd == data->ifd0 && (data->ifd_exif->count ||
305 data->ifd_interoperability->count)) {
307 /* EXIF_TAG_EXIF_IFD_POINTER */
308 exif_set_short (*d + 6 + offset + 0, data->priv->order,
309 EXIF_TAG_EXIF_IFD_POINTER);
310 exif_set_short (*d + 6 + offset + 2, data->priv->order,
312 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
313 exif_set_long (*d + 6 + offset + 8, data->priv->order,
315 exif_data_save_data_content (data, data->ifd_exif, d, ds,
320 if (ifd == data->ifd0 && data->ifd_gps->count) {
322 /* EXIF_TAG_GPS_INFO_IFD_POINTER */
323 exif_set_short (*d + 6 + offset + 0, data->priv->order,
324 EXIF_TAG_GPS_INFO_IFD_POINTER);
325 exif_set_short (*d + 6 + offset + 2, data->priv->order,
327 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
328 exif_set_long (*d + 6 + offset + 8, data->priv->order,
330 exif_data_save_data_content (data, data->ifd_gps, d, ds,
335 if (ifd == data->ifd_exif && data->ifd_interoperability->count) {
337 /* EXIF_TAG_INTEROPERABILITY_IFD_POINTER */
338 exif_set_short (*d + 6 + offset + 0, data->priv->order,
339 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
340 exif_set_short (*d + 6 + offset + 2, data->priv->order,
342 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
343 exif_set_long (*d + 6 + offset + 8, data->priv->order,
345 exif_data_save_data_content (data, data->ifd_interoperability,
352 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
353 exif_set_short (*d + 6 + offset + 0, data->priv->order,
354 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
355 exif_set_short (*d + 6 + offset + 2, data->priv->order,
357 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
358 exif_set_long (*d + 6 + offset + 8, data->priv->order,
362 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
363 exif_set_short (*d + 6 + offset + 0, data->priv->order,
364 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
365 exif_set_short (*d + 6 + offset + 2, data->priv->order,
367 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
368 exif_set_long (*d + 6 + offset + 8, data->priv->order,
371 *d = realloc (*d, sizeof (char) * *ds);
372 memcpy (*d + *ds - data->size, data->data, data->size);
376 if (ifd == data->ifd0 && data->ifd1->count) {
379 * We are saving IFD 0. Tell where IFD 1 starts and save
382 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
383 exif_data_save_data_content (data, data->ifd1, d, ds,
386 exif_set_long (*d + 6 + offset, data->priv->order, 0);
390 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
392 unsigned int l, len = size;
402 printf ("Parsing %i byte(s) EXIF data...\n", size);
406 * It can be that the data starts with the EXIF header. If it does
407 * not, search the EXIF marker.
411 printf ("Size too small.\n");
415 if (!memcmp (d, ExifHeader, 6)) {
417 printf ("Found EXIF header.\n");
421 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
422 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
425 while ((d[0] == 0xff) && size) {
430 /* JPEG_MARKER_SOI */
431 if (d[0] == JPEG_MARKER_SOI) {
437 /* JPEG_MARKER_APP0 */
438 if (d[0] == JPEG_MARKER_APP0) {
441 l = (d[0] << 8) | d[1];
449 /* JPEG_MARKER_APP1 */
450 if (d[0] == JPEG_MARKER_APP1)
453 /* Unknown marker or data. Give up. */
455 printf ("EXIF marker not found.\n");
463 printf ("Size too small.\n");
467 len = (d[0] << 8) | d[1];
469 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
476 * Verify the exif header
477 * (offset 2, length 6).
481 printf ("Size too small.\n");
485 if (memcmp (d, ExifHeader, 6)) {
487 printf ("EXIF header not found.\n");
493 printf ("Found EXIF header.\n");
496 /* Byte order (offset 6, length 2) */
499 if (!memcmp (d + 6, "II", 2))
500 data->priv->order = EXIF_BYTE_ORDER_INTEL;
501 else if (!memcmp (d + 6, "MM", 2))
502 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
507 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
511 offset = exif_get_long (d + 10, data->priv->order);
513 printf ("IFD 0 at %i.\n", (int) offset);
516 /* Parse the actual exif data (offset 14) */
517 exif_data_load_data_content (data, data->ifd0, d + 6,
521 n = exif_get_short (d + 6 + offset, data->priv->order);
522 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
525 printf ("IFD 1 at %i.\n", (int) offset);
527 exif_data_load_data_content (data, data->ifd1, d + 6,
533 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
542 *d = malloc (sizeof (char) * *ds);
543 memcpy (*d, ExifHeader, 6);
545 /* Order (offset 6) */
547 *d = realloc (*d, sizeof (char) * *ds);
548 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
549 memcpy (*d + 6, "II", 2);
551 memcpy (*d + 6, "MM", 2);
554 /* Fixed value (2 bytes, offset 8) */
556 *d = realloc (*d, sizeof (char) * *ds);
557 exif_set_short (*d + 8, data->priv->order, 0x002a);
560 * IFD 0 offset (4 bytes, offset 10).
561 * We will start 8 bytes after the
562 * EXIF header (2 bytes for order, another 2 for the test, and
563 * 4 bytes for the IFD 0 offset make 8 bytes together).
566 *d = realloc (*d, sizeof (char) * *ds);
567 exif_set_long (*d + 10, data->priv->order, 8);
569 /* Now save IFD 0. IFD 1 will be saved automatically. */
571 printf ("Saving IFDs...\n");
573 exif_data_save_data_content (data, data->ifd0, d, ds, *ds - 6);
576 printf ("Saved %i byte(s) EXIF data.\n", *ds);
581 exif_data_new_from_file (const char *path)
589 f = fopen (path, "rb");
594 while ((marker = fgetc (f)) == 0xff);
596 /* JPEG_MARKER_SOI */
597 if (marker == JPEG_MARKER_SOI)
600 /* JPEG_MARKER_APP0 */
601 if (marker == JPEG_MARKER_APP0) {
604 size = (lh << 8) | ll;
605 if (fseek (f, size - 2, SEEK_CUR) < 0)
610 /* JPEG_MARKER_APP1 */
611 if (marker == JPEG_MARKER_APP1)
614 /* Unknown marker or data. Give up. */
618 /* EXIF data found. Allocate the necessary memory and read the data. */
621 size = (lh << 8) | ll;
622 data = malloc (sizeof (char) * size);
625 if (fread (data, 1, size, f) != size) {
630 edata = exif_data_new_from_data (data, size);
639 exif_data_ref (ExifData *data)
644 data->priv->ref_count++;
648 exif_data_unref (ExifData *data)
653 data->priv->ref_count--;
654 if (!data->priv->ref_count)
655 exif_data_free (data);
659 exif_data_free (ExifData *data)
665 exif_content_unref (data->ifd0);
669 exif_content_unref (data->ifd1);
672 if (data->ifd_exif) {
673 exif_content_unref (data->ifd_exif);
674 data->ifd_exif = NULL;
677 exif_content_unref (data->ifd_gps);
678 data->ifd_gps = NULL;
680 if (data->ifd_interoperability) {
681 exif_content_unref (data->ifd_interoperability);
682 data->ifd_interoperability = NULL;
696 exif_data_dump (ExifData *data)
701 if (data->ifd0->count) {
702 printf ("Dumping IFD 0...\n");
703 exif_content_dump (data->ifd0, 0);
706 if (data->ifd1->count) {
707 printf ("Dumping IFD 1...\n");
708 exif_content_dump (data->ifd1, 0);
711 if (data->ifd_exif->count) {
712 printf ("Dumping IFD EXIF...\n");
713 exif_content_dump (data->ifd_exif, 0);
716 if (data->ifd_gps->count) {
717 printf ("Dumping IFD GPS...\n");
718 exif_content_dump (data->ifd_gps, 0);
721 if (data->ifd_interoperability->count) {
722 printf ("Dumping IFD Interoperability...\n");
723 exif_content_dump (data->ifd_interoperability, 0);
727 printf ("%i byte(s) thumbnail data available.", data->size);
728 if (data->size >= 4) {
729 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
730 data->data[0], data->data[1],
731 data->data[data->size - 2],
732 data->data[data->size - 1]);
738 exif_data_get_byte_order (ExifData *data)
743 return (data->priv->order);
747 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
753 func (data->ifd0, user_data);
754 func (data->ifd1, user_data);
755 func (data->ifd_exif, user_data);
756 func (data->ifd_gps, user_data);
757 func (data->ifd_interoperability, user_data);
760 typedef struct _ByteOrderChangeData ByteOrderChangeData;
761 struct _ByteOrderChangeData {
762 ExifByteOrder old, new;
766 entry_set_byte_order (ExifEntry *e, void *data)
768 ByteOrderChangeData *d = data;
780 case EXIF_FORMAT_SHORT:
781 for (i = 0; i < e->components; i++) {
782 s = exif_get_short (e->data +
783 (i * exif_format_get_size (e->format)),
785 exif_set_short (e->data +
786 (i * exif_format_get_size (e->format)),
790 case EXIF_FORMAT_LONG:
791 for (i = 0; i < e->components; i++) {
792 l = exif_get_long (e->data +
793 (i * exif_format_get_size (e->format)),
795 exif_set_long (e->data +
796 (i * exif_format_get_size (e->format)),
800 case EXIF_FORMAT_RATIONAL:
801 for (i = 0; i < e->components; i++) {
802 r = exif_get_rational (e->data +
803 (i * exif_format_get_size (e->format)),
805 exif_set_rational (e->data +
806 (i * exif_format_get_size (e->format)),
810 case EXIF_FORMAT_SLONG:
811 for (i = 0; i < e->components; i++) {
812 sl = exif_get_slong (e->data +
813 (i * exif_format_get_size (e->format)),
815 exif_set_slong (e->data +
816 (i * exif_format_get_size (e->format)),
820 case EXIF_FORMAT_SRATIONAL:
821 for (i = 0; i < e->components; i++) {
822 sr = exif_get_srational (e->data +
823 (i * exif_format_get_size (e->format)),
825 exif_set_srational (e->data +
826 (i * exif_format_get_size (e->format)),
830 case EXIF_FORMAT_UNDEFINED:
831 case EXIF_FORMAT_BYTE:
832 case EXIF_FORMAT_ASCII:
840 content_set_byte_order (ExifContent *content, void *data)
842 exif_content_foreach_entry (content, entry_set_byte_order, data);
846 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
848 ByteOrderChangeData d;
850 if (!data || (order == data->priv->order))
853 d.old = data->priv->order;
855 exif_data_foreach_content (data, content_set_byte_order, &d);
856 data->priv->order = order;