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-content.h"
27 #include <libjpeg/jpeg-marker.h>
31 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
33 typedef struct _ExifContentNotifyData ExifContentNotifyData;
34 struct _ExifContentNotifyData {
35 ExifContentEvent events;
36 ExifContentNotifyFunc func;
40 struct _ExifContentPrivate
42 ExifContentNotifyData *notifications;
45 unsigned int ref_count;
49 exif_content_add_notify (ExifContent *content, ExifContentEvent events,
50 ExifContentNotifyFunc func, void *data)
55 if (!content->priv->notifications)
56 content->priv->notifications =
57 malloc (sizeof (ExifContentNotifyData));
59 content->priv->notifications =
60 realloc (content->priv->notifications,
61 sizeof (ExifContentNotifyData) *
62 (content->priv->count + 1));
63 content->priv->notifications[content->priv->count].events = events;
64 content->priv->notifications[content->priv->count].func = func;
65 content->priv->notifications[content->priv->count].data = data;
66 content->priv->count++;
68 return (content->priv->count);
72 exif_content_remove_notify (ExifContent *content, unsigned int id)
76 if (id > content->priv->count)
79 memmove (content->priv->notifications + id - 1,
80 content->priv->notifications + id,
81 sizeof (ExifContentNotifyData) *
82 (content->priv->count - id));
83 content->priv->count--;
87 exif_content_new (void)
91 content = malloc (sizeof (ExifContent));
94 memset (content, 0, sizeof (ExifContent));
95 content->priv = malloc (sizeof (ExifContentPrivate));
100 memset (content->priv, 0, sizeof (ExifContentPrivate));
101 content->priv->ref_count = 1;
107 exif_content_ref (ExifContent *content)
109 content->priv->ref_count++;
113 exif_content_unref (ExifContent *content)
115 content->priv->ref_count--;
116 if (!content->priv->ref_count)
117 exif_content_free (content);
121 exif_content_free (ExifContent *content)
125 for (i = 0; i < content->count; i++)
126 exif_entry_unref (content->entries[i]);
127 free (content->entries);
128 free (content->priv);
133 exif_content_parse (ExifContent *content, const unsigned char *data,
134 unsigned int size, unsigned int offset,
144 content->order = order;
146 /* Read number of entries */
147 if (size < offset + 2)
149 n = exif_get_short (data + offset, order);
151 printf ("Parsing directory with %i entries...\n", n);
154 for (i = 0; i < n; i++) {
155 entry = exif_entry_new ();
156 exif_content_add_entry (content, entry);
157 exif_entry_parse (content->entries[i], data, size,
158 offset + 2 + 12 * i, order);
159 exif_entry_unref (entry);
164 exif_content_dump (ExifContent *content, unsigned int indent)
169 for (i = 0; i < 2 * indent; i++)
176 printf ("%sDumping exif content (%i entries)...\n", buf,
178 for (i = 0; i < content->count; i++)
179 exif_entry_dump (content->entries[i], indent + 1);
183 exif_content_notify (ExifContent *content, ExifEntry *entry,
184 ExifContentEvent event)
188 for (i = 0; i < content->priv->count; i++)
189 if (content->priv->notifications[i].events & event)
190 content->priv->notifications[i].func (content, entry,
191 content->priv->notifications[i].data);
195 exif_content_add_entry (ExifContent *content, ExifEntry *entry)
200 entry->parent = content;
201 content->entries = realloc (content->entries,
202 sizeof (ExifEntry) * (content->count + 1));
203 content->entries[content->count] = entry;
204 exif_entry_ref (entry);
207 exif_content_notify (content, entry, EXIF_CONTENT_EVENT_ADD);
211 exif_content_remove_entry (ExifContent *content, ExifEntry *entry)
215 if (entry->parent != content)
218 for (i = 0; i < content->count; i++)
219 if (content->entries[i] == entry)
221 if (i == content->count)
224 memmove (&content->entries[i], &content->entries[i + 1],
225 sizeof (ExifEntry) * (content->count - i - 1));
228 exif_content_notify (content, entry, EXIF_CONTENT_EVENT_REMOVE);
230 entry->parent = NULL;
231 exif_entry_unref (entry);
235 exif_content_get_entry (ExifContent *content, ExifTag tag)
242 for (i = 0; i < content->count; i++)
243 if (content->entries[i]->tag == tag)
244 return (content->entries[i]);