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-data.h"
24 #include "exif-utils.h"
25 #include "jpeg-marker.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;
51 data = malloc (sizeof (ExifData));
54 memset (data, 0, sizeof (ExifData));
55 data->priv = malloc (sizeof (ExifDataPrivate));
60 memset (data->priv, 0, sizeof (ExifDataPrivate));
61 data->priv->ref_count = 1;
63 for (i = 0; i < EXIF_IFD_COUNT; i++) {
64 data->ifd[i] = exif_content_new ();
66 exif_data_free (data);
69 data->ifd[i]->parent = data;
76 exif_data_new_from_data (const unsigned char *data, unsigned int size)
80 edata = exif_data_new ();
81 exif_data_load_data (edata, data, size);
86 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
87 const unsigned char *d,
88 unsigned int size, unsigned int offset)
92 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
93 entry->format = exif_get_short (d + offset + 2, data->priv->order);
94 entry->components = exif_get_long (d + offset + 4, data->priv->order);
97 * Size? If bigger than 4 bytes, the actual data is not
98 * in the entry but somewhere else (offset).
100 s = exif_format_get_size (entry->format) * entry->components;
104 doff = exif_get_long (d + offset + 8, data->priv->order);
112 entry->data = malloc (sizeof (char) * s);
116 memcpy (entry->data, d + doff, s);
120 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
121 unsigned char **d, unsigned int *ds,
124 unsigned int doff, s;
127 * Each entry is 12 bytes long. The memory for the entry has
128 * already been allocated.
130 exif_set_short (*d + 6 + offset + 0,
131 data->priv->order, entry->tag);
132 exif_set_short (*d + 6 + offset + 2,
133 data->priv->order, entry->format);
134 exif_set_long (*d + 6 + offset + 4,
135 data->priv->order, entry->components);
138 * Size? If bigger than 4 bytes, the actual data is not in
139 * the entry but somewhere else.
141 s = exif_format_get_size (entry->format) * entry->components;
146 *d = realloc (*d, sizeof (char) * *ds);
147 doff = *ds - 6 - entry->size;
148 exif_set_long (*d + 6 + offset + 8,
149 data->priv->order, doff);
152 memcpy (*d + 6 + doff, entry->data, entry->size);
156 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
157 unsigned int ds, ExifLong offset, ExifLong size)
159 if (ds < offset + size) {
161 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
162 (int) ds, (int) offset, (int) size);
169 data->data = malloc (sizeof (char) * data->size);
170 memcpy (data->data, d + offset, data->size);
174 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
175 const unsigned char *d,
176 unsigned int ds, unsigned int offset)
178 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
184 /* Read the number of entries */
185 n = exif_get_short (d + offset, data->priv->order);
187 printf ("Loading %i entries...\n", n);
190 for (i = 0; i < n; i++) {
192 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
194 printf ("Loading entry '%s' (%i of %i)...\n",
195 exif_tag_get_name (tag), i + 1, n);
198 case EXIF_TAG_EXIF_IFD_POINTER:
199 case EXIF_TAG_GPS_INFO_IFD_POINTER:
200 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
201 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
202 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
203 o = exif_get_long (d + offset + 12 * i + 8,
206 case EXIF_TAG_EXIF_IFD_POINTER:
207 exif_data_load_data_content (data,
208 data->ifd[EXIF_IFD_EXIF], d, ds, o);
210 case EXIF_TAG_GPS_INFO_IFD_POINTER:
211 exif_data_load_data_content (data,
212 data->ifd[EXIF_IFD_GPS], d, ds, o);
214 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
215 exif_data_load_data_content (data,
216 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
218 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
220 printf ("Thumbnail at %i.\n", (int) o);
222 thumbnail_offset = o;
223 if (thumbnail_offset && thumbnail_length)
224 exif_data_load_data_thumbnail (data, d,
225 ds, thumbnail_offset,
228 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
230 printf ("Thumbnail size: %i.\n", (int) o);
232 thumbnail_length = o;
233 if (thumbnail_offset && thumbnail_length)
234 exif_data_load_data_thumbnail (data, d,
235 ds, thumbnail_offset,
243 entry = exif_entry_new ();
244 exif_content_add_entry (ifd, entry);
245 exif_data_load_data_entry (data, entry, d, ds,
247 exif_entry_unref (entry);
254 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
255 unsigned char **d, unsigned int *ds,
258 unsigned int j, n_ptr = 0, n_thumb = 0;
261 if (!data || !ifd || !d || !ds)
264 for (i = 0; i < EXIF_IFD_COUNT; i++)
265 if (ifd == data->ifd[i])
267 if (i == EXIF_IFD_COUNT)
271 * Check if we need some extra entries for pointers or the thumbnail.
277 * The pointer to IFD_EXIF is in IFD_0. The pointer to
278 * IFD_INTEROPERABILITY is in IFD_EXIF.
280 if (data->ifd[EXIF_IFD_EXIF]->count ||
281 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
284 /* The pointer to IFD_GPS is in IFD_0. */
285 if (data->ifd[EXIF_IFD_GPS]->count)
294 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
301 * Allocate enough memory for all entries
302 * and the number of entries.
304 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
305 *d = realloc (*d, sizeof (char) * *ds);
307 /* Save the number of entries */
308 exif_set_short (*d + 6 + offset, data->priv->order,
309 ifd->count + n_ptr + n_thumb);
313 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
314 ifd->count, exif_ifd_get_name (i), offset);
317 /* Save each entry */
318 for (j = 0; j < ifd->count; j++)
319 exif_data_save_data_entry (data, ifd->entries[j],
320 d, ds, offset + 12 * j);
321 offset += 12 * ifd->count;
323 /* Now save special entries. */
328 * The pointer to IFD_EXIF is in IFD_0.
329 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
330 * therefore, if IFD_INTEROPERABILITY is not empty, we need
331 * IFD_EXIF even if latter is empty.
333 if (data->ifd[EXIF_IFD_EXIF]->count ||
334 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
335 exif_set_short (*d + 6 + offset + 0, data->priv->order,
336 EXIF_TAG_EXIF_IFD_POINTER);
337 exif_set_short (*d + 6 + offset + 2, data->priv->order,
339 exif_set_long (*d + 6 + offset + 4, data->priv->order,
341 exif_set_long (*d + 6 + offset + 8, data->priv->order,
343 exif_data_save_data_content (data,
344 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
348 /* The pointer to IFD_GPS is in IFD_0, too. */
349 if (data->ifd[EXIF_IFD_GPS]->count) {
350 exif_set_short (*d + 6 + offset + 0, data->priv->order,
351 EXIF_TAG_GPS_INFO_IFD_POINTER);
352 exif_set_short (*d + 6 + offset + 2, data->priv->order,
354 exif_set_long (*d + 6 + offset + 4, data->priv->order,
356 exif_set_long (*d + 6 + offset + 8, data->priv->order,
358 exif_data_save_data_content (data,
359 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
367 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
370 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
371 exif_set_short (*d + 6 + offset + 0, data->priv->order,
372 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
373 exif_set_short (*d + 6 + offset + 2, data->priv->order,
375 exif_set_long (*d + 6 + offset + 4, data->priv->order,
377 exif_set_long (*d + 6 + offset + 8, data->priv->order,
379 exif_data_save_data_content (data,
380 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
389 * Information about the thumbnail (if any) is saved in
394 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
395 exif_set_short (*d + 6 + offset + 0, data->priv->order,
396 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
397 exif_set_short (*d + 6 + offset + 2, data->priv->order,
399 exif_set_long (*d + 6 + offset + 4, data->priv->order,
401 exif_set_long (*d + 6 + offset + 8, data->priv->order,
404 *d = realloc (*d, sizeof (char) * *ds);
405 memcpy (*d + *ds - data->size, data->data, data->size);
408 printf ("Wrote %i bytes of thumbnail data at offset "
409 "%i.\n", data->size, *ds - data->size);
410 printf ("We currently have %i bytes EXIF data.\n", *ds);
413 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
414 exif_set_short (*d + 6 + offset + 0, data->priv->order,
415 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
416 exif_set_short (*d + 6 + offset + 2, data->priv->order,
418 exif_set_long (*d + 6 + offset + 4, data->priv->order,
420 exif_set_long (*d + 6 + offset + 8, data->priv->order,
430 /* Correctly terminate the directory */
431 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
435 * We are saving IFD 0. Tell where IFD 1 starts and save
438 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
439 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
442 exif_set_long (*d + 6 + offset, data->priv->order, 0);
446 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
448 unsigned int l, len = size;
458 printf ("Parsing %i byte(s) EXIF data...\n", size);
462 * It can be that the data starts with the EXIF header. If it does
463 * not, search the EXIF marker.
467 printf ("Size too small.\n");
471 if (!memcmp (d, ExifHeader, 6)) {
473 printf ("Found EXIF header.\n");
477 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
478 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
481 while ((d[0] == 0xff) && size) {
486 /* JPEG_MARKER_SOI */
487 if (d[0] == JPEG_MARKER_SOI) {
493 /* JPEG_MARKER_APP0 */
494 if (d[0] == JPEG_MARKER_APP0) {
497 l = (d[0] << 8) | d[1];
505 /* JPEG_MARKER_APP1 */
506 if (d[0] == JPEG_MARKER_APP1)
509 /* Unknown marker or data. Give up. */
511 printf ("EXIF marker not found.\n");
519 printf ("Size too small.\n");
523 len = (d[0] << 8) | d[1];
525 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
532 * Verify the exif header
533 * (offset 2, length 6).
537 printf ("Size too small.\n");
541 if (memcmp (d, ExifHeader, 6)) {
543 printf ("EXIF header not found.\n");
549 printf ("Found EXIF header.\n");
552 /* Byte order (offset 6, length 2) */
555 if (!memcmp (d + 6, "II", 2))
556 data->priv->order = EXIF_BYTE_ORDER_INTEL;
557 else if (!memcmp (d + 6, "MM", 2))
558 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
563 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
567 offset = exif_get_long (d + 10, data->priv->order);
569 printf ("IFD 0 at %i.\n", (int) offset);
572 /* Parse the actual exif data (offset 14) */
573 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
577 n = exif_get_short (d + 6 + offset, data->priv->order);
578 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
581 printf ("IFD 1 at %i.\n", (int) offset);
585 if (offset > size - 6) {
587 printf ("Bogus offset!\n");
592 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
598 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
607 *d = malloc (sizeof (char) * *ds);
608 memcpy (*d, ExifHeader, 6);
610 /* Order (offset 6) */
612 *d = realloc (*d, sizeof (char) * *ds);
613 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
614 memcpy (*d + 6, "II", 2);
616 memcpy (*d + 6, "MM", 2);
619 /* Fixed value (2 bytes, offset 8) */
621 *d = realloc (*d, sizeof (char) * *ds);
622 exif_set_short (*d + 8, data->priv->order, 0x002a);
625 * IFD 0 offset (4 bytes, offset 10).
626 * We will start 8 bytes after the
627 * EXIF header (2 bytes for order, another 2 for the test, and
628 * 4 bytes for the IFD 0 offset make 8 bytes together).
631 *d = realloc (*d, sizeof (char) * *ds);
632 exif_set_long (*d + 10, data->priv->order, 8);
634 /* Now save IFD 0. IFD 1 will be saved automatically. */
636 printf ("Saving IFDs...\n");
638 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
642 printf ("Saved %i byte(s) EXIF data.\n", *ds);
647 exif_data_new_from_file (const char *path)
655 f = fopen (path, "rb");
660 while ((marker = fgetc (f)) == 0xff);
662 /* JPEG_MARKER_SOI */
663 if (marker == JPEG_MARKER_SOI)
666 /* JPEG_MARKER_APP0 */
667 if (marker == JPEG_MARKER_APP0) {
670 size = (lh << 8) | ll;
671 if (fseek (f, size - 2, SEEK_CUR) < 0)
676 /* JPEG_MARKER_APP1 */
677 if (marker == JPEG_MARKER_APP1)
680 /* Unknown marker or data. Give up. */
684 /* EXIF data found. Allocate the necessary memory and read the data. */
687 size = (lh << 8) | ll;
688 data = malloc (sizeof (char) * size);
691 if (fread (data, 1, size, f) != size) {
696 edata = exif_data_new_from_data (data, size);
705 exif_data_ref (ExifData *data)
710 data->priv->ref_count++;
714 exif_data_unref (ExifData *data)
719 data->priv->ref_count--;
720 if (!data->priv->ref_count)
721 exif_data_free (data);
725 exif_data_free (ExifData *data)
732 for (i = 0; i < EXIF_IFD_COUNT; i++) {
734 exif_content_unref (data->ifd[i]);
750 exif_data_dump (ExifData *data)
757 for (i = 0; i < EXIF_IFD_COUNT; i++) {
758 if (data->ifd[i] && data->ifd[i]->count) {
759 printf ("Dumping IFD '%s'...\n",
760 exif_ifd_get_name (i));
761 exif_content_dump (data->ifd[i], 0);
766 printf ("%i byte(s) thumbnail data available.", data->size);
767 if (data->size >= 4) {
768 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
769 data->data[0], data->data[1],
770 data->data[data->size - 2],
771 data->data[data->size - 1]);
777 exif_data_get_byte_order (ExifData *data)
782 return (data->priv->order);
786 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
794 for (i = 0; i < EXIF_IFD_COUNT; i++)
795 func (data->ifd[i], user_data);
798 typedef struct _ByteOrderChangeData ByteOrderChangeData;
799 struct _ByteOrderChangeData {
800 ExifByteOrder old, new;
804 entry_set_byte_order (ExifEntry *e, void *data)
806 ByteOrderChangeData *d = data;
818 case EXIF_FORMAT_SHORT:
819 for (i = 0; i < e->components; i++) {
820 s = exif_get_short (e->data +
821 (i * exif_format_get_size (e->format)),
823 exif_set_short (e->data +
824 (i * exif_format_get_size (e->format)),
828 case EXIF_FORMAT_LONG:
829 for (i = 0; i < e->components; i++) {
830 l = exif_get_long (e->data +
831 (i * exif_format_get_size (e->format)),
833 exif_set_long (e->data +
834 (i * exif_format_get_size (e->format)),
838 case EXIF_FORMAT_RATIONAL:
839 for (i = 0; i < e->components; i++) {
840 r = exif_get_rational (e->data +
841 (i * exif_format_get_size (e->format)),
843 exif_set_rational (e->data +
844 (i * exif_format_get_size (e->format)),
848 case EXIF_FORMAT_SLONG:
849 for (i = 0; i < e->components; i++) {
850 sl = exif_get_slong (e->data +
851 (i * exif_format_get_size (e->format)),
853 exif_set_slong (e->data +
854 (i * exif_format_get_size (e->format)),
858 case EXIF_FORMAT_SRATIONAL:
859 for (i = 0; i < e->components; i++) {
860 sr = exif_get_srational (e->data +
861 (i * exif_format_get_size (e->format)),
863 exif_set_srational (e->data +
864 (i * exif_format_get_size (e->format)),
868 case EXIF_FORMAT_UNDEFINED:
869 case EXIF_FORMAT_BYTE:
870 case EXIF_FORMAT_ASCII:
878 content_set_byte_order (ExifContent *content, void *data)
880 exif_content_foreach_entry (content, entry_set_byte_order, data);
884 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
886 ByteOrderChangeData d;
888 if (!data || (order == data->priv->order))
891 d.old = data->priv->order;
893 exif_data_foreach_content (data, content_set_byte_order, &d);
894 data->priv->order = order;