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"
26 #include <libjpeg/jpeg-marker.h>
28 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
30 struct _ExifDataPrivate
34 unsigned int ref_count;
42 data = malloc (sizeof (ExifData));
45 memset (data, 0, sizeof (ExifData));
46 data->priv = malloc (sizeof (ExifDataPrivate));
51 memset (data->priv, 0, sizeof (ExifDataPrivate));
52 data->priv->ref_count = 1;
54 data->content = exif_content_new ();
56 exif_data_free (data);
64 exif_data_new_from_data (const unsigned char *data, unsigned int size)
73 /* Search the exif marker */
74 for (o = 0; o < size; o++)
75 if (data[o] == JPEG_MARKER_APP1)
83 /* Size of exif data? */
86 len = (data[0] << 8) | data[1];
89 * Verify the exif header
90 * (offset 2, length 6).
94 if (memcmp (data + 2, ExifHeader, 6))
98 * Byte order (offset 8, length 2) plus two values for
99 * correctness check (offset 10, length 2 and 4).
103 if (!memcmp (data + 8, "II", 2))
104 order = EXIF_BYTE_ORDER_INTEL;
105 else if (!memcmp (data + 8, "MM", 2))
106 order = EXIF_BYTE_ORDER_MOTOROLA;
110 edata = exif_data_new ();
111 edata->priv->order = order;
113 /* Parse the actual exif data (offset 16) */
114 exif_content_parse (edata->content, data + 8, size - 8, 8, order);
120 exif_data_new_from_file (const char *path)
127 f = fopen (path, "r");
131 /* For now, we read the data into memory. Patches welcome... */
132 fseek (f, 0, SEEK_END);
134 fseek (f, 0, SEEK_SET);
135 data = malloc (sizeof (char) * size);
138 if (fread (data, 1, size, f) != size) {
143 edata = exif_data_new_from_data (data, size);
152 exif_data_ref (ExifData *data)
157 data->priv->ref_count++;
161 exif_data_unref (ExifData *data)
166 data->priv->ref_count--;
167 if (!data->priv->ref_count)
168 exif_data_free (data);
172 exif_data_free (ExifData *data)
175 exif_content_unref (data->content);
181 exif_data_dump (ExifData *data)
186 printf ("Dumping EXIF data...\n");
187 exif_content_dump (data->content, 0);