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 "exif-loader.h"
26 #include "jpeg-marker.h"
33 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
37 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
39 struct _ExifDataPrivate
43 unsigned int ref_count;
52 data = malloc (sizeof (ExifData));
55 memset (data, 0, sizeof (ExifData));
56 data->priv = malloc (sizeof (ExifDataPrivate));
61 memset (data->priv, 0, sizeof (ExifDataPrivate));
62 data->priv->ref_count = 1;
64 for (i = 0; i < EXIF_IFD_COUNT; i++) {
65 data->ifd[i] = exif_content_new ();
67 exif_data_free (data);
70 data->ifd[i]->parent = data;
77 exif_data_new_from_data (const unsigned char *data, unsigned int size)
81 edata = exif_data_new ();
82 exif_data_load_data (edata, data, size);
87 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
88 const unsigned char *d,
89 unsigned int size, unsigned int offset)
93 entry->tag = exif_get_short (d + offset + 0, data->priv->order);
94 entry->format = exif_get_short (d + offset + 2, data->priv->order);
95 entry->components = exif_get_long (d + offset + 4, data->priv->order);
98 * Size? If bigger than 4 bytes, the actual data is not
99 * in the entry but somewhere else (offset).
101 s = exif_format_get_size (entry->format) * entry->components;
105 doff = exif_get_long (d + offset + 8, data->priv->order);
113 entry->data = malloc (sizeof (char) * s);
117 memcpy (entry->data, d + doff, s);
121 exif_data_save_data_entry (ExifData *data, ExifEntry *entry,
122 unsigned char **d, unsigned int *ds,
125 unsigned int doff, s;
128 * Each entry is 12 bytes long. The memory for the entry has
129 * already been allocated.
131 exif_set_short (*d + 6 + offset + 0,
132 data->priv->order, entry->tag);
133 exif_set_short (*d + 6 + offset + 2,
134 data->priv->order, entry->format);
135 exif_set_long (*d + 6 + offset + 4,
136 data->priv->order, entry->components);
139 * Size? If bigger than 4 bytes, the actual data is not in
140 * the entry but somewhere else.
142 s = exif_format_get_size (entry->format) * entry->components;
147 *d = realloc (*d, sizeof (char) * *ds);
148 doff = *ds - 6 - entry->size;
149 exif_set_long (*d + 6 + offset + 8,
150 data->priv->order, doff);
153 memcpy (*d + 6 + doff, entry->data, entry->size);
157 exif_data_load_data_thumbnail (ExifData *data, const unsigned char *d,
158 unsigned int ds, ExifLong offset, ExifLong size)
160 if (ds < offset + size) {
162 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
163 (int) ds, (int) offset, (int) size);
170 data->data = malloc (sizeof (char) * data->size);
171 memcpy (data->data, d + offset, data->size);
175 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
176 const unsigned char *d,
177 unsigned int ds, unsigned int offset)
179 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
185 /* Read the number of entries */
186 n = exif_get_short (d + offset, data->priv->order);
188 printf ("Loading %i entries...\n", n);
191 for (i = 0; i < n; i++) {
193 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
195 printf ("Loading entry '%s' (%i of %i)...\n",
196 exif_tag_get_name (tag), i + 1, n);
199 case EXIF_TAG_EXIF_IFD_POINTER:
200 case EXIF_TAG_GPS_INFO_IFD_POINTER:
201 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
202 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
203 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
204 o = exif_get_long (d + offset + 12 * i + 8,
207 case EXIF_TAG_EXIF_IFD_POINTER:
208 exif_data_load_data_content (data,
209 data->ifd[EXIF_IFD_EXIF], d, ds, o);
211 case EXIF_TAG_GPS_INFO_IFD_POINTER:
212 exif_data_load_data_content (data,
213 data->ifd[EXIF_IFD_GPS], d, ds, o);
215 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
216 exif_data_load_data_content (data,
217 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds, o);
219 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
221 printf ("Thumbnail at %i.\n", (int) o);
223 thumbnail_offset = o;
224 if (thumbnail_offset && thumbnail_length)
225 exif_data_load_data_thumbnail (data, d,
226 ds, thumbnail_offset,
229 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
231 printf ("Thumbnail size: %i.\n", (int) o);
233 thumbnail_length = o;
234 if (thumbnail_offset && thumbnail_length)
235 exif_data_load_data_thumbnail (data, d,
236 ds, thumbnail_offset,
244 entry = exif_entry_new ();
245 exif_content_add_entry (ifd, entry);
246 exif_data_load_data_entry (data, entry, d, ds,
248 exif_entry_unref (entry);
255 exif_data_save_data_content (ExifData *data, ExifContent *ifd,
256 unsigned char **d, unsigned int *ds,
259 unsigned int j, n_ptr = 0, n_thumb = 0;
262 if (!data || !ifd || !d || !ds)
265 for (i = 0; i < EXIF_IFD_COUNT; i++)
266 if (ifd == data->ifd[i])
268 if (i == EXIF_IFD_COUNT)
272 * Check if we need some extra entries for pointers or the thumbnail.
278 * The pointer to IFD_EXIF is in IFD_0. The pointer to
279 * IFD_INTEROPERABILITY is in IFD_EXIF.
281 if (data->ifd[EXIF_IFD_EXIF]->count ||
282 data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
285 /* The pointer to IFD_GPS is in IFD_0. */
286 if (data->ifd[EXIF_IFD_GPS]->count)
295 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count)
302 * Allocate enough memory for all entries
303 * and the number of entries.
305 *ds += (2 + (ifd->count + n_ptr + n_thumb) * 12 + 4);
306 *d = realloc (*d, sizeof (char) * *ds);
308 /* Save the number of entries */
309 exif_set_short (*d + 6 + offset, data->priv->order,
310 ifd->count + n_ptr + n_thumb);
314 printf ("Saving %i entries (IFD '%s', offset: %i)...\n",
315 ifd->count, exif_ifd_get_name (i), offset);
318 /* Save each entry */
319 for (j = 0; j < ifd->count; j++)
320 exif_data_save_data_entry (data, ifd->entries[j],
321 d, ds, offset + 12 * j);
322 offset += 12 * ifd->count;
324 /* Now save special entries. */
329 * The pointer to IFD_EXIF is in IFD_0.
330 * However, the pointer to IFD_INTEROPERABILITY is in IFD_EXIF,
331 * therefore, if IFD_INTEROPERABILITY is not empty, we need
332 * IFD_EXIF even if latter is empty.
334 if (data->ifd[EXIF_IFD_EXIF]->count ||
335 data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
336 exif_set_short (*d + 6 + offset + 0, data->priv->order,
337 EXIF_TAG_EXIF_IFD_POINTER);
338 exif_set_short (*d + 6 + offset + 2, data->priv->order,
340 exif_set_long (*d + 6 + offset + 4, data->priv->order,
342 exif_set_long (*d + 6 + offset + 8, data->priv->order,
344 exif_data_save_data_content (data,
345 data->ifd[EXIF_IFD_EXIF], d, ds, *ds - 6);
349 /* The pointer to IFD_GPS is in IFD_0, too. */
350 if (data->ifd[EXIF_IFD_GPS]->count) {
351 exif_set_short (*d + 6 + offset + 0, data->priv->order,
352 EXIF_TAG_GPS_INFO_IFD_POINTER);
353 exif_set_short (*d + 6 + offset + 2, data->priv->order,
355 exif_set_long (*d + 6 + offset + 4, data->priv->order,
357 exif_set_long (*d + 6 + offset + 8, data->priv->order,
359 exif_data_save_data_content (data,
360 data->ifd[EXIF_IFD_GPS], d, ds, *ds - 6);
368 * The pointer to IFD_INTEROPERABILITY is in IFD_EXIF.
371 if (data->ifd[EXIF_IFD_INTEROPERABILITY]->count) {
372 exif_set_short (*d + 6 + offset + 0, data->priv->order,
373 EXIF_TAG_INTEROPERABILITY_IFD_POINTER);
374 exif_set_short (*d + 6 + offset + 2, data->priv->order,
376 exif_set_long (*d + 6 + offset + 4, data->priv->order,
378 exif_set_long (*d + 6 + offset + 8, data->priv->order,
380 exif_data_save_data_content (data,
381 data->ifd[EXIF_IFD_INTEROPERABILITY], d, ds,
390 * Information about the thumbnail (if any) is saved in
395 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT */
396 exif_set_short (*d + 6 + offset + 0, data->priv->order,
397 EXIF_TAG_JPEG_INTERCHANGE_FORMAT);
398 exif_set_short (*d + 6 + offset + 2, data->priv->order,
400 exif_set_long (*d + 6 + offset + 4, data->priv->order,
402 exif_set_long (*d + 6 + offset + 8, data->priv->order,
405 *d = realloc (*d, sizeof (char) * *ds);
406 memcpy (*d + *ds - data->size, data->data, data->size);
409 printf ("Wrote %i bytes of thumbnail data at offset "
410 "%i.\n", data->size, *ds - data->size);
411 printf ("We currently have %i bytes EXIF data.\n", *ds);
414 /* EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH */
415 exif_set_short (*d + 6 + offset + 0, data->priv->order,
416 EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH);
417 exif_set_short (*d + 6 + offset + 2, data->priv->order,
419 exif_set_long (*d + 6 + offset + 4, data->priv->order,
421 exif_set_long (*d + 6 + offset + 8, data->priv->order,
431 /* Correctly terminate the directory */
432 if (i == EXIF_IFD_0 && (data->ifd[EXIF_IFD_1]->count ||
436 * We are saving IFD 0. Tell where IFD 1 starts and save
439 exif_set_long (*d + 6 + offset, data->priv->order, *ds - 6);
440 exif_data_save_data_content (data, data->ifd[EXIF_IFD_1], d, ds,
443 exif_set_long (*d + 6 + offset, data->priv->order, 0);
447 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
449 unsigned int l, len = size;
459 printf ("Parsing %i byte(s) EXIF data...\n", size);
463 * It can be that the data starts with the EXIF header. If it does
464 * not, search the EXIF marker.
468 printf ("Size too small.\n");
472 if (!memcmp (d, ExifHeader, 6)) {
474 printf ("Found EXIF header.\n");
478 printf ("Data begins with 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x "
479 "0x%x...\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
482 while ((d[0] == 0xff) && size) {
487 /* JPEG_MARKER_SOI */
488 if (d[0] == JPEG_MARKER_SOI) {
494 /* JPEG_MARKER_APP0 */
495 if (d[0] == JPEG_MARKER_APP0) {
498 l = (d[0] << 8) | d[1];
506 /* JPEG_MARKER_APP1 */
507 if (d[0] == JPEG_MARKER_APP1)
510 /* Unknown marker or data. Give up. */
512 printf ("EXIF marker not found.\n");
520 printf ("Size too small.\n");
524 len = (d[0] << 8) | d[1];
526 printf ("We have to deal with %i byte(s) of EXIF data.\n", len);
533 * Verify the exif header
534 * (offset 2, length 6).
538 printf ("Size too small.\n");
542 if (memcmp (d, ExifHeader, 6)) {
544 printf ("EXIF header not found.\n");
550 printf ("Found EXIF header.\n");
553 /* Byte order (offset 6, length 2) */
556 if (!memcmp (d + 6, "II", 2))
557 data->priv->order = EXIF_BYTE_ORDER_INTEL;
558 else if (!memcmp (d + 6, "MM", 2))
559 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
564 if (exif_get_short (d + 8, data->priv->order) != 0x002a)
568 offset = exif_get_long (d + 10, data->priv->order);
570 printf ("IFD 0 at %i.\n", (int) offset);
573 /* Parse the actual exif data (offset 14) */
574 exif_data_load_data_content (data, data->ifd[EXIF_IFD_0], d + 6,
578 n = exif_get_short (d + 6 + offset, data->priv->order);
579 offset = exif_get_long (d + 6 + offset + 2 + 12 * n, data->priv->order);
582 printf ("IFD 1 at %i.\n", (int) offset);
586 if (offset > size - 6) {
588 printf ("Bogus offset!\n");
593 exif_data_load_data_content (data, data->ifd[EXIF_IFD_1], d + 6,
599 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
608 *d = malloc (sizeof (char) * *ds);
609 memcpy (*d, ExifHeader, 6);
611 /* Order (offset 6) */
613 *d = realloc (*d, sizeof (char) * *ds);
614 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
615 memcpy (*d + 6, "II", 2);
617 memcpy (*d + 6, "MM", 2);
620 /* Fixed value (2 bytes, offset 8) */
622 *d = realloc (*d, sizeof (char) * *ds);
623 exif_set_short (*d + 8, data->priv->order, 0x002a);
626 * IFD 0 offset (4 bytes, offset 10).
627 * We will start 8 bytes after the
628 * EXIF header (2 bytes for order, another 2 for the test, and
629 * 4 bytes for the IFD 0 offset make 8 bytes together).
632 *d = realloc (*d, sizeof (char) * *ds);
633 exif_set_long (*d + 10, data->priv->order, 8);
635 /* Now save IFD 0. IFD 1 will be saved automatically. */
637 printf ("Saving IFDs...\n");
639 exif_data_save_data_content (data, data->ifd[EXIF_IFD_0], d, ds,
643 printf ("Saved %i byte(s) EXIF data.\n", *ds);
648 exif_data_new_from_file (const char *path)
654 unsigned char data[1024];
656 f = fopen (path, "rb");
660 loader = exif_loader_new ();
662 size = fread (data, 1, 1024, f);
664 if (!exif_loader_write (loader, data, size)) break;
668 edata = exif_loader_get_data (loader);
669 exif_loader_unref (loader);
675 exif_data_ref (ExifData *data)
680 data->priv->ref_count++;
684 exif_data_unref (ExifData *data)
689 data->priv->ref_count--;
690 if (!data->priv->ref_count)
691 exif_data_free (data);
695 exif_data_free (ExifData *data)
702 for (i = 0; i < EXIF_IFD_COUNT; i++) {
704 exif_content_unref (data->ifd[i]);
720 exif_data_dump (ExifData *data)
727 for (i = 0; i < EXIF_IFD_COUNT; i++) {
728 if (data->ifd[i] && data->ifd[i]->count) {
729 printf ("Dumping IFD '%s'...\n",
730 exif_ifd_get_name (i));
731 exif_content_dump (data->ifd[i], 0);
736 printf ("%i byte(s) thumbnail data available.", data->size);
737 if (data->size >= 4) {
738 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
739 data->data[0], data->data[1],
740 data->data[data->size - 2],
741 data->data[data->size - 1]);
747 exif_data_get_byte_order (ExifData *data)
752 return (data->priv->order);
756 exif_data_foreach_content (ExifData *data, ExifDataForeachContentFunc func,
764 for (i = 0; i < EXIF_IFD_COUNT; i++)
765 func (data->ifd[i], user_data);
768 typedef struct _ByteOrderChangeData ByteOrderChangeData;
769 struct _ByteOrderChangeData {
770 ExifByteOrder old, new;
774 entry_set_byte_order (ExifEntry *e, void *data)
776 ByteOrderChangeData *d = data;
788 case EXIF_FORMAT_SHORT:
789 for (i = 0; i < e->components; i++) {
790 s = exif_get_short (e->data +
791 (i * exif_format_get_size (e->format)),
793 exif_set_short (e->data +
794 (i * exif_format_get_size (e->format)),
798 case EXIF_FORMAT_LONG:
799 for (i = 0; i < e->components; i++) {
800 l = exif_get_long (e->data +
801 (i * exif_format_get_size (e->format)),
803 exif_set_long (e->data +
804 (i * exif_format_get_size (e->format)),
808 case EXIF_FORMAT_RATIONAL:
809 for (i = 0; i < e->components; i++) {
810 r = exif_get_rational (e->data +
811 (i * exif_format_get_size (e->format)),
813 exif_set_rational (e->data +
814 (i * exif_format_get_size (e->format)),
818 case EXIF_FORMAT_SLONG:
819 for (i = 0; i < e->components; i++) {
820 sl = exif_get_slong (e->data +
821 (i * exif_format_get_size (e->format)),
823 exif_set_slong (e->data +
824 (i * exif_format_get_size (e->format)),
828 case EXIF_FORMAT_SRATIONAL:
829 for (i = 0; i < e->components; i++) {
830 sr = exif_get_srational (e->data +
831 (i * exif_format_get_size (e->format)),
833 exif_set_srational (e->data +
834 (i * exif_format_get_size (e->format)),
838 case EXIF_FORMAT_UNDEFINED:
839 case EXIF_FORMAT_BYTE:
840 case EXIF_FORMAT_ASCII:
848 content_set_byte_order (ExifContent *content, void *data)
850 exif_content_foreach_entry (content, entry_set_byte_order, data);
854 exif_data_set_byte_order (ExifData *data, ExifByteOrder order)
856 ByteOrderChangeData d;
858 if (!data || (order == data->priv->order))
861 d.old = data->priv->order;
863 exif_data_foreach_content (data, content_set_byte_order, &d);
864 data->priv->order = order;