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>
30 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
34 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
36 struct _ExifDataPrivate
40 unsigned int ref_count;
48 data = malloc (sizeof (ExifData));
51 memset (data, 0, sizeof (ExifData));
52 data->priv = malloc (sizeof (ExifDataPrivate));
57 memset (data->priv, 0, sizeof (ExifDataPrivate));
58 data->priv->ref_count = 1;
60 data->ifd0 = exif_content_new ();
61 data->ifd1 = exif_content_new ();
62 data->ifd_exif = exif_content_new ();
63 data->ifd_gps = exif_content_new ();
64 data->ifd_interoperability = exif_content_new ();
65 if (!data->ifd_exif || !data->ifd_gps || !data->ifd_interoperability ||
66 !data->ifd0 || !data->ifd1) {
67 exif_data_free (data);
75 exif_data_new_from_data (const unsigned char *data, unsigned int size)
79 edata = exif_data_new ();
80 exif_data_load_data (edata, data, size);
85 exif_data_load_data_entry (ExifData *data, ExifEntry *entry,
86 const unsigned char *d,
87 unsigned int size, unsigned int offset)
91 entry->order = data->priv->order;
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_load_data_thumbnail (ExifData *data, const unsigned char *d,
121 unsigned int ds, ExifLong offset, ExifLong size)
123 if (ds < offset + size) {
125 printf ("Bogus thumbnail offset and size: %i < %i + %i.\n",
126 (int) ds, (int) offset, (int) size);
133 data->data = malloc (sizeof (char) * data->size);
134 memcpy (data->data, d + offset, data->size);
138 exif_data_load_data_content (ExifData *data, ExifContent *ifd,
139 const unsigned char *d,
140 unsigned int ds, unsigned int offset)
142 ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
148 ifd->order = data->priv->order;
150 /* Read the number of entries */
151 n = exif_get_short (d + offset, data->priv->order);
153 for (i = 0; i < n; i++) {
155 tag = exif_get_short (d + offset + 12 * i, data->priv->order);
157 case EXIF_TAG_EXIF_IFD_POINTER:
158 case EXIF_TAG_GPS_INFO_IFD_POINTER:
159 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
160 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
161 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
162 o = exif_get_long (d + offset + 12 * i + 8,
165 case EXIF_TAG_EXIF_IFD_POINTER:
166 exif_data_load_data_content (data,
167 data->ifd_exif, d, ds, o);
169 case EXIF_TAG_GPS_INFO_IFD_POINTER:
170 exif_data_load_data_content (data,
171 data->ifd_gps, d, ds, o);
173 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
174 exif_data_load_data_content (data,
175 data->ifd_interoperability, d, ds, o);
176 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
178 printf ("Thumbnail at %i.\n", (int) o);
180 thumbnail_offset = o;
181 if (thumbnail_offset && thumbnail_length)
182 exif_data_load_data_thumbnail (data, d,
183 ds, thumbnail_offset,
186 case EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH:
188 printf ("Thumbnail size: %i.\n", (int) o);
190 thumbnail_length = o;
191 if (thumbnail_offset && thumbnail_length)
192 exif_data_load_data_thumbnail (data, d,
193 ds, thumbnail_offset,
201 entry = exif_entry_new ();
202 exif_content_add_entry (ifd, entry);
203 exif_data_load_data_entry (data, entry, d, ds,
205 exif_entry_unref (entry);
211 exif_data_load_data (ExifData *data, const unsigned char *d, unsigned int size)
222 /* Search the exif marker */
223 for (o = 0; o < size; o++)
224 if (d[o] == JPEG_MARKER_APP1)
232 /* Size of exif data? */
235 len = (d[0] << 8) | d[1];
238 * Verify the exif header
239 * (offset 2, length 6).
243 if (memcmp (d + 2, ExifHeader, 6))
246 /* Byte order (offset 8, length 2) */
249 if (!memcmp (d + 8, "II", 2))
250 data->priv->order = EXIF_BYTE_ORDER_INTEL;
251 else if (!memcmp (d + 8, "MM", 2))
252 data->priv->order = EXIF_BYTE_ORDER_MOTOROLA;
257 if (exif_get_short (d + 10, data->priv->order) != 0x002a)
261 offset = exif_get_long (d + 12, data->priv->order);
263 printf ("IFD 0 at %i.\n", (int) offset);
266 /* Parse the actual exif data (offset 16) */
267 exif_data_load_data_content (data, data->ifd0, d + 8,
271 n = exif_get_short (d + 8 + offset, data->priv->order);
272 offset = exif_get_long (d + 8 + offset + 2 + 12 * n, data->priv->order);
275 printf ("IFD 1 at %i.\n", (int) offset);
277 exif_data_load_data_content (data, data->ifd1, d + 8,
283 exif_data_save_data (ExifData *data, unsigned char **d, unsigned int *ds)
294 *d = malloc (sizeof (char) * *ds);
298 /* Header (offset 2) */
300 *d = realloc (*d, sizeof (char) * *ds);
303 memcpy (*d + 2, ExifHeader, 6);
305 /* Order (offset 8) */
307 *d = realloc (*d, sizeof (char) * *ds);
310 if (data->priv->order == EXIF_BYTE_ORDER_INTEL) {
311 memcpy (*d + 8, "II", 2);
313 memcpy (*d + 8, "MM", 2);
318 exif_data_new_from_file (const char *path)
325 f = fopen (path, "r");
329 /* For now, we read the data into memory. Patches welcome... */
330 fseek (f, 0, SEEK_END);
332 fseek (f, 0, SEEK_SET);
333 data = malloc (sizeof (char) * size);
336 if (fread (data, 1, size, f) != size) {
341 edata = exif_data_new_from_data (data, size);
350 exif_data_ref (ExifData *data)
355 data->priv->ref_count++;
359 exif_data_unref (ExifData *data)
364 data->priv->ref_count--;
365 if (!data->priv->ref_count)
366 exif_data_free (data);
370 exif_data_free (ExifData *data)
373 exif_content_unref (data->ifd0);
375 exif_content_unref (data->ifd1);
383 exif_data_dump (ExifData *data)
388 if (data->ifd0->count) {
389 printf ("Dumping IFD 0...\n");
390 exif_content_dump (data->ifd0, 0);
393 if (data->ifd1->count) {
394 printf ("Dumping IFD 1...\n");
395 exif_content_dump (data->ifd1, 0);
398 if (data->ifd_exif->count) {
399 printf ("Dumping IFD EXIF...\n");
400 exif_content_dump (data->ifd_exif, 0);
403 if (data->ifd_gps->count) {
404 printf ("Dumping IFD GPS...\n");
405 exif_content_dump (data->ifd_gps, 0);
408 if (data->ifd_interoperability->count) {
409 printf ("Dumping IFD Interoperability...\n");
410 exif_content_dump (data->ifd_interoperability, 0);
414 printf ("%i byte(s) thumbnail data available.", data->size);
415 if (data->size >= 4) {
416 printf ("0x%02x 0x%02x ... 0x%02x 0x%02x\n",
417 data->data[0], data->data[1],
418 data->data[data->size - 2],
419 data->data[data->size - 1]);