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)
271 * If we are to save IFD 1, we can point to the thumbnail if it
274 if ((ifd == data->ifd1) && data->data)
278 * Allocate enough memory for all entries
279 * and the number of entries.
281 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + (n_ptr ? 4 : 0));
282 *d = realloc (*d, sizeof (char) * *ds);
284 /* Save the number of entries */
285 exif_set_short (*d + 6 + offset, data->priv->order,
286 ifd->count + n_ptr + n_thumb);
289 printf ("Saving %i entries (offset: %i)...\n", ifd->count, offset);
292 /* Save each entry */
293 for (i = 0; i < ifd->count; i++)
294 exif_data_save_data_entry (data, ifd->entries[i],
295 d, ds, offset + 12 * i);
296 offset += 12 * ifd->count;
298 /* Save special entries */
301 /* EXIF_TAG_EXIF_IFD_POINTER */
302 exif_set_short (*d + 6 + offset + 0, data->priv->order,
303 EXIF_TAG_EXIF_IFD_POINTER);
304 exif_set_short (*d + 6 + offset + 2, data->priv->order,
306 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
307 exif_set_long (*d + 6 + offset + 8, data->priv->order,
309 exif_data_save_data_content (data, data->ifd_exif, d, ds,
313 /* EXIF_TAG_GPS_INFO_IFD_POINTER */
314 exif_set_short (*d + 6 + offset + 0, data->priv->order,
315 EXIF_TAG_GPS_INFO_IFD_POINTER);
316 exif_set_short (*d + 6 + offset + 2, data->priv->order,
318 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
319 exif_set_long (*d + 6 + offset + 8, data->priv->order,
321 exif_data_save_data_content (data, data->ifd_gps, d, ds,
325 /* EXIF_TAG_INTEROPERABILITY_IFD_POINTER */
326 exif_set_short (*d + 6 + offset + 0, data->priv->order,
327 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
328 exif_set_short (*d + 6 + offset + 2, data->priv->order,
330 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
331 exif_set_long (*d + 6 + offset + 8, data->priv->order,
333 exif_data_save_data_content (data, data->ifd_interoperability,
338 * We are saving IFD 0. Tell where IFD 1 starts and save
341 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
342 exif_data_save_data_content (data, data->ifd1, d, ds,
348 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
349 exif_set_short (*d + 6 + offset + 0, data->priv->order,
350 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
351 exif_set_short (*d + 6 + offset + 2, data->priv->order,
353 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
354 exif_set_long (*d + 6 + offset + 8, data->priv->order,
358 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
359 exif_set_short (*d + 6 + offset + 0, data->priv->order,
360 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
361 exif_set_short (*d + 6 + offset + 2, data->priv->order,
363 exif_set_long (*d + 6 + offset + 4, data->priv->order, 1);
364 exif_set_long (*d + 6 + offset + 8, data->priv->order,
367 *d = realloc (*d, sizeof (char) * *ds);
368 memcpy (*d + *ds - data->size, data->data, data->size);
373 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
375 unsigned int l, len = size;
385 printf ("Parsing %i byte(s) EXIF data...\n", size);
389 * It can be that the data starts with the EXIF header. If it does
390 * not, search the EXIF marker.
394 printf ("Size too small.\n");
398 if (!memcmp (d, ExifHeader, 6)) {
400 printf ("Found EXIF header.\n");
404 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
405 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
408 while ((d[0] == 0xff) && size) {
413 /* JPEG_MARKER_SOI */
414 if (d[0] == JPEG_MARKER_SOI) {
420 /* JPEG_MARKER_APP0 */
421 if (d[0] == JPEG_MARKER_APP0) {
424 l = (d[0] << 8) | d[1];
432 /* JPEG_MARKER_APP1 */
433 if (d[0] == JPEG_MARKER_APP1)
436 /* Unknown marker or data. Give up. */
437 printf ("EXIF marker not found.\n");
444 printf ("Size too small.\n");
448 len = (d[0] << 8) | d[1];
450 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
457 * Verify the exif header
458 * (offset 2, length 6).
462 printf ("Size too small.\n");
466 if (memcmp (d, ExifHeader, 6)) {
468 printf ("EXIF header not found.\n");
474 printf ("Found EXIF header.\n");
477 /* Byte order (offset 6, length 2) */
480 if (!memcmp (d + 6, "II", 2))
481 data->priv->order = EXIF_BYTE_ORDER_INTEL;
482 else if (!memcmp (d + 6, "MM", 2))
483 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
488 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
492 offset = exif_get_long (d + 10, data->priv->order);
494 printf ("IFD 0 at %i.\n", (int) offset);
497 /* Parse the actual exif data (offset 14) */
498 exif_data_load_data_content (data, data->ifd0, d + 6,
502 n = exif_get_short (d + 6 + offset, data->priv->order);
503 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
506 printf ("IFD 1 at %i.\n", (int) offset);
508 exif_data_load_data_content (data, data->ifd1, d + 6,
514 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
523 *d = malloc (sizeof (char) * *ds);
524 memcpy (*d, ExifHeader, 6);
526 /* Order (offset 6) */
528 *d = realloc (*d, sizeof (char) * *ds);
529 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
530 memcpy (*d + 6, "II", 2);
532 memcpy (*d + 6, "MM", 2);
535 /* Fixed value (2 bytes, offset 8) */
537 *d = realloc (*d, sizeof (char) * *ds);
538 exif_set_short (*d + 8, data->priv->order, 0x002a);
541 * IFD 0 offset (4 bytes, offset 10).
542 * We will start 8 bytes after the
543 * EXIF header (2 bytes for order, another 2 for the test, and
544 * 4 bytes for the IFD 0 offset make 8 bytes together).
547 *d = realloc (*d, sizeof (char) * *ds);
548 exif_set_long (*d + 10, data->priv->order, 8);
550 /* Now save IFD 0. IFD 1 will be saved automatically. */
552 printf ("Saving IFDs...\n");
554 exif_data_save_data_content (data, data->ifd0, d, ds, *ds - 6);
557 printf ("Saved %i byte(s) EXIF data.\n", *ds);
562 exif_data_new_from_file (const char *path)
569 f = fopen (path, "r");
573 /* For now, we read the data into memory. Patches welcome... */
574 fseek (f, 0, SEEK_END);
576 fseek (f, 0, SEEK_SET);
577 data = malloc (sizeof (char) * size);
580 if (fread (data, 1, size, f) != size) {
585 edata = exif_data_new_from_data (data, size);
594 exif_data_ref (ExifData *data)
599 data->priv->ref_count++;
603 exif_data_unref (ExifData *data)
608 data->priv->ref_count--;
609 if (!data->priv->ref_count)
610 exif_data_free (data);
614 exif_data_free (ExifData *data)
617 exif_content_unref (data->ifd0);
619 exif_content_unref (data->ifd1);
627 exif_data_dump (ExifData *data)
632 if (data->ifd0->count) {
633 printf ("Dumping IFD 0...\n");
634 exif_content_dump (data->ifd0, 0);
637 if (data->ifd1->count) {
638 printf ("Dumping IFD 1...\n");
639 exif_content_dump (data->ifd1, 0);
642 if (data->ifd_exif->count) {
643 printf ("Dumping IFD EXIF...\n");
644 exif_content_dump (data->ifd_exif, 0);
647 if (data->ifd_gps->count) {
648 printf ("Dumping IFD GPS...\n");
649 exif_content_dump (data->ifd_gps, 0);
652 if (data->ifd_interoperability->count) {
653 printf ("Dumping IFD Interoperability...\n");
654 exif_content_dump (data->ifd_interoperability, 0);
658 printf ("%i byte(s) thumbnail data available.", data->size);
659 if (data->size >= 4) {
660 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
661 data->data[0], data->data[1],
662 data->data[data->size - 2],
663 data->data[data->size - 1]);
669 exif_data_get_byte_order (ExifData *data)
674 return (data->priv->order);
678 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
681 func (data->ifd0, user_data);
682 func (data->ifd1, user_data);
683 func (data->ifd_exif, user_data);
684 func (data->ifd_gps, user_data);
685 func (data->ifd_interoperability, user_data);
688 typedef struct _ByteOrderChangeData ByteOrderChangeData;
689 struct _ByteOrderChangeData {
690 ExifByteOrder old, new;
694 entry_set_byte_order (ExifEntry *e, void *data)
696 ByteOrderChangeData *d = data;
708 case EXIF_FORMAT_SHORT:
709 for (i = 0; i < e->components; i++) {
710 s = exif_get_short (e->data +
711 (i * exif_format_get_size (e->format)),
713 exif_set_short (e->data +
714 (i * exif_format_get_size (e->format)),
718 case EXIF_FORMAT_LONG:
719 for (i = 0; i < e->components; i++) {
720 l = exif_get_long (e->data +
721 (i * exif_format_get_size (e->format)),
723 exif_set_long (e->data +
724 (i * exif_format_get_size (e->format)),
728 case EXIF_FORMAT_RATIONAL:
729 for (i = 0; i < e->components; i++) {
730 r = exif_get_rational (e->data +
731 (i * exif_format_get_size (e->format)),
733 exif_set_rational (e->data +
734 (i * exif_format_get_size (e->format)),
738 case EXIF_FORMAT_SLONG:
739 for (i = 0; i < e->components; i++) {
740 sl = exif_get_slong (e->data +
741 (i * exif_format_get_size (e->format)),
743 exif_set_slong (e->data +
744 (i * exif_format_get_size (e->format)),
748 case EXIF_FORMAT_SRATIONAL:
749 for (i = 0; i < e->components; i++) {
750 sr = exif_get_srational (e->data +
751 (i * exif_format_get_size (e->format)),
753 exif_set_srational (e->data +
754 (i * exif_format_get_size (e->format)),
758 case EXIF_FORMAT_UNDEFINED:
759 case EXIF_FORMAT_BYTE:
760 case EXIF_FORMAT_ASCII:
768 content_set_byte_order (ExifContent *content, void *data)
770 exif_content_foreach_entry (content, entry_set_byte_order, data);
774 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
776 ByteOrderChangeData d;
778 if (!data || (order == data->priv->order))
781 d.old = data->priv->order;
783 exif_data_foreach_content (data, content_set_byte_order, &d);
784 data->priv->order = order;