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;
266 /* If we are to save IFD 0, we need some extra entries. */
267 if (ifd == data->ifd0) {
268 if (data->ifd_exif->count)
270 if (data->ifd_gps->count)
272 if (data->ifd_interoperability->count)
277 * If we are to save IFD 1, we can point to the thumbnail if it
280 if ((ifd == data->ifd1) && data->data)
284 * Allocate enough memory for all entries
285 * and the number of entries.
287 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
288 *d = realloc (*d, sizeof (char) * *ds);
290 /* Save the number of entries */
291 exif_set_short (*d + 6 + offset, data->priv->order,
292 ifd->count + n_ptr + n_thumb);
295 printf ("Saving %i entries (offset: %i)...\n", ifd->count, offset);
298 /* Save each entry */
299 for (i = 0; i < ifd->count; i++)
300 exif_data_save_data_entry (data, ifd->entries[i],
301 d, ds, offset + 12 * i);
302 offset += 12 * ifd->count;
304 /* Save special entries */
305 if (ifd == data->ifd0 && data->ifd_exif->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->ifd0 && 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);
375 if (ifd == data->ifd0 && data->ifd1->count) {
377 * We are saving IFD 0. Tell where IFD 1 starts and save
380 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
381 exif_data_save_data_content (data, data->ifd1, d, ds,
384 exif_set_long (*d + 6 + offset, data->priv->order, 0);
388 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
390 unsigned int l, len = size;
400 printf ("Parsing %i byte(s) EXIF data...\n", size);
404 * It can be that the data starts with the EXIF header. If it does
405 * not, search the EXIF marker.
409 printf ("Size too small.\n");
413 if (!memcmp (d, ExifHeader, 6)) {
415 printf ("Found EXIF header.\n");
419 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
420 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
423 while ((d[0] == 0xff) && size) {
428 /* JPEG_MARKER_SOI */
429 if (d[0] == JPEG_MARKER_SOI) {
435 /* JPEG_MARKER_APP0 */
436 if (d[0] == JPEG_MARKER_APP0) {
439 l = (d[0] << 8) | d[1];
447 /* JPEG_MARKER_APP1 */
448 if (d[0] == JPEG_MARKER_APP1)
451 /* Unknown marker or data. Give up. */
453 printf ("EXIF marker not found.\n");
461 printf ("Size too small.\n");
465 len = (d[0] << 8) | d[1];
467 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
474 * Verify the exif header
475 * (offset 2, length 6).
479 printf ("Size too small.\n");
483 if (memcmp (d, ExifHeader, 6)) {
485 printf ("EXIF header not found.\n");
491 printf ("Found EXIF header.\n");
494 /* Byte order (offset 6, length 2) */
497 if (!memcmp (d + 6, "II", 2))
498 data->priv->order = EXIF_BYTE_ORDER_INTEL;
499 else if (!memcmp (d + 6, "MM", 2))
500 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
505 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
509 offset = exif_get_long (d + 10, data->priv->order);
511 printf ("IFD 0 at %i.\n", (int) offset);
514 /* Parse the actual exif data (offset 14) */
515 exif_data_load_data_content (data, data->ifd0, d + 6,
519 n = exif_get_short (d + 6 + offset, data->priv->order);
520 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
523 printf ("IFD 1 at %i.\n", (int) offset);
525 exif_data_load_data_content (data, data->ifd1, d + 6,
531 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
540 *d = malloc (sizeof (char) * *ds);
541 memcpy (*d, ExifHeader, 6);
543 /* Order (offset 6) */
545 *d = realloc (*d, sizeof (char) * *ds);
546 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
547 memcpy (*d + 6, "II", 2);
549 memcpy (*d + 6, "MM", 2);
552 /* Fixed value (2 bytes, offset 8) */
554 *d = realloc (*d, sizeof (char) * *ds);
555 exif_set_short (*d + 8, data->priv->order, 0x002a);
558 * IFD 0 offset (4 bytes, offset 10).
559 * We will start 8 bytes after the
560 * EXIF header (2 bytes for order, another 2 for the test, and
561 * 4 bytes for the IFD 0 offset make 8 bytes together).
564 *d = realloc (*d, sizeof (char) * *ds);
565 exif_set_long (*d + 10, data->priv->order, 8);
567 /* Now save IFD 0. IFD 1 will be saved automatically. */
569 printf ("Saving IFDs...\n");
571 exif_data_save_data_content (data, data->ifd0, d, ds, *ds - 6);
574 printf ("Saved %i byte(s) EXIF data.\n", *ds);
579 exif_data_new_from_file (const char *path)
587 f = fopen (path, "rb");
592 while ((marker = fgetc (f)) == 0xff);
594 /* JPEG_MARKER_SOI */
595 if (marker == JPEG_MARKER_SOI)
598 /* JPEG_MARKER_APP0 */
599 if (marker == JPEG_MARKER_APP0) {
602 size = (lh << 8) | ll;
603 if (fseek (f, size - 2, SEEK_CUR) < 0)
608 /* JPEG_MARKER_APP1 */
609 if (marker == JPEG_MARKER_APP1)
612 /* Unknown marker or data. Give up. */
616 /* EXIF data found. Allocate the necessary memory and read the data. */
619 size = (lh << 8) | ll;
620 data = malloc (sizeof (char) * size);
623 if (fread (data, 1, size, f) != size) {
628 edata = exif_data_new_from_data (data, size);
637 exif_data_ref (ExifData *data)
642 data->priv->ref_count++;
646 exif_data_unref (ExifData *data)
651 data->priv->ref_count--;
652 if (!data->priv->ref_count)
653 exif_data_free (data);
657 exif_data_free (ExifData *data)
660 exif_content_unref (data->ifd0);
662 exif_content_unref (data->ifd1);
670 exif_data_dump (ExifData *data)
675 if (data->ifd0->count) {
676 printf ("Dumping IFD 0...\n");
677 exif_content_dump (data->ifd0, 0);
680 if (data->ifd1->count) {
681 printf ("Dumping IFD 1...\n");
682 exif_content_dump (data->ifd1, 0);
685 if (data->ifd_exif->count) {
686 printf ("Dumping IFD EXIF...\n");
687 exif_content_dump (data->ifd_exif, 0);
690 if (data->ifd_gps->count) {
691 printf ("Dumping IFD GPS...\n");
692 exif_content_dump (data->ifd_gps, 0);
695 if (data->ifd_interoperability->count) {
696 printf ("Dumping IFD Interoperability...\n");
697 exif_content_dump (data->ifd_interoperability, 0);
701 printf ("%i byte(s) thumbnail data available.", data->size);
702 if (data->size >= 4) {
703 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
704 data->data[0], data->data[1],
705 data->data[data->size - 2],
706 data->data[data->size - 1]);
712 exif_data_get_byte_order (ExifData *data)
717 return (data->priv->order);
721 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
727 func (data->ifd0, user_data);
728 func (data->ifd1, user_data);
729 func (data->ifd_exif, user_data);
730 func (data->ifd_gps, user_data);
731 func (data->ifd_interoperability, user_data);
734 typedef struct _ByteOrderChangeData ByteOrderChangeData;
735 struct _ByteOrderChangeData {
736 ExifByteOrder old, new;
740 entry_set_byte_order (ExifEntry *e, void *data)
742 ByteOrderChangeData *d = data;
754 case EXIF_FORMAT_SHORT:
755 for (i = 0; i < e->components; i++) {
756 s = exif_get_short (e->data +
757 (i * exif_format_get_size (e->format)),
759 exif_set_short (e->data +
760 (i * exif_format_get_size (e->format)),
764 case EXIF_FORMAT_LONG:
765 for (i = 0; i < e->components; i++) {
766 l = exif_get_long (e->data +
767 (i * exif_format_get_size (e->format)),
769 exif_set_long (e->data +
770 (i * exif_format_get_size (e->format)),
774 case EXIF_FORMAT_RATIONAL:
775 for (i = 0; i < e->components; i++) {
776 r = exif_get_rational (e->data +
777 (i * exif_format_get_size (e->format)),
779 exif_set_rational (e->data +
780 (i * exif_format_get_size (e->format)),
784 case EXIF_FORMAT_SLONG:
785 for (i = 0; i < e->components; i++) {
786 sl = exif_get_slong (e->data +
787 (i * exif_format_get_size (e->format)),
789 exif_set_slong (e->data +
790 (i * exif_format_get_size (e->format)),
794 case EXIF_FORMAT_SRATIONAL:
795 for (i = 0; i < e->components; i++) {
796 sr = exif_get_srational (e->data +
797 (i * exif_format_get_size (e->format)),
799 exif_set_srational (e->data +
800 (i * exif_format_get_size (e->format)),
804 case EXIF_FORMAT_UNDEFINED:
805 case EXIF_FORMAT_BYTE:
806 case EXIF_FORMAT_ASCII:
814 content_set_byte_order (ExifContent *content, void *data)
816 exif_content_foreach_entry (content, entry_set_byte_order, data);
820 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
822 ByteOrderChangeData d;
824 if (!data || (order == data->priv->order))
827 d.old = data->priv->order;
829 exif_data_foreach_content (data, content_set_byte_order, &d);
830 data->priv->order = order;