3 * Copyright (c) 2001 Lutz Mueller <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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
23 #include <libexif/exif-content.h>
24 #include <libexif/exif-system.h>
31 * static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
34 struct _ExifContentPrivate
36 unsigned int ref_count;
43 exif_content_new (void)
45 ExifMem *mem = exif_mem_new_default ();
46 ExifContent *content = exif_content_new_mem (mem);
54 exif_content_new_mem (ExifMem *mem)
58 if (!mem) return NULL;
60 content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
63 content->priv = exif_mem_alloc (mem,
64 (ExifLong) sizeof (ExifContentPrivate));
66 exif_mem_free (mem, content);
70 content->priv->ref_count = 1;
72 content->priv->mem = mem;
79 exif_content_ref (ExifContent *content)
84 content->priv->ref_count++;
88 exif_content_unref (ExifContent *content)
93 content->priv->ref_count--;
94 if (!content->priv->ref_count)
95 exif_content_free (content);
99 exif_content_free (ExifContent *content)
101 ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
104 if (!content) return;
106 for (i = 0; i < content->count; i++)
107 exif_entry_unref (content->entries[i]);
108 exif_mem_free (mem, content->entries);
111 exif_log_unref (content->priv->log);
114 exif_mem_free (mem, content->priv);
115 exif_mem_free (mem, content);
116 exif_mem_unref (mem);
120 exif_content_dump (ExifContent *content, unsigned int indent)
128 l = MIN(sizeof(buf)-1, 2*indent);
132 printf ("%sDumping exif content (%u entries)...\n", buf,
134 for (i = 0; i < content->count; i++)
135 exif_entry_dump (content->entries[i], indent + 1);
139 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
142 if (!c || !c->priv || !entry || entry->parent) return;
144 /* One tag can only be added once to an IFD. */
145 if (exif_content_get_entry (c, entry->tag)) {
146 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
147 "An attempt has been made to add "
148 "the tag '%s' twice to an IFD. This is against "
149 "specification.", exif_tag_get_name (entry->tag));
153 entries = exif_mem_realloc (c->priv->mem,
154 c->entries, sizeof (ExifEntry*) * (c->count + 1));
155 if (!entries) return;
157 entries[c->count++] = entry;
158 c->entries = entries;
159 exif_entry_ref (entry);
163 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
166 ExifEntry **t, *temp;
168 if (!c || !c->priv || !e || (e->parent != c)) return;
170 /* Search the entry */
171 for (i = 0; i < c->count; i++)
172 if (c->entries[i] == e)
178 /* Remove the entry */
179 temp = c->entries[c->count-1];
181 t = exif_mem_realloc (c->priv->mem, c->entries,
182 sizeof(ExifEntry*) * (c->count - 1));
188 if (i != c->count) { /* we deallocated the last slot already */
189 memmove (&t[i], &t[i + 1], sizeof (ExifEntry*) * (c->count - i - 1));
190 t[c->count-1] = temp;
193 exif_mem_free (c->priv->mem, c->entries);
198 exif_entry_unref (e);
202 exif_content_get_entry (ExifContent *content, ExifTag tag)
209 for (i = 0; i < content->count; i++)
210 if (content->entries[i]->tag == tag)
211 return (content->entries[i]);
216 exif_content_foreach_entry (ExifContent *content,
217 ExifContentForeachEntryFunc func, void *data)
221 if (!content || !func)
224 for (i = 0; i < content->count; i++)
225 func (content->entries[i], data);
229 exif_content_log (ExifContent *content, ExifLog *log)
231 if (!content || !content->priv || !log || content->priv->log == log)
234 if (content->priv->log) exif_log_unref (content->priv->log);
235 content->priv->log = log;
240 exif_content_get_ifd (ExifContent *c)
242 if (!c || !c->parent) return EXIF_IFD_COUNT;
245 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
246 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
247 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
248 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
249 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
254 fix_func (ExifEntry *e, void *UNUSED(data))
260 * Check if this entry is unknown and if so, delete it.
261 * \note Be careful calling this function in a loop. Deleting an entry from
262 * an ExifContent changes the index of subsequent entries, as well as the
263 * total size of the entries array.
266 remove_not_recorded (ExifEntry *e, void *UNUSED(data))
268 ExifIfd ifd = exif_entry_get_ifd(e) ;
269 ExifContent *c = e->parent;
270 ExifDataType dt = exif_data_get_data_type (c->parent);
273 if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
274 EXIF_SUPPORT_LEVEL_NOT_RECORDED) {
275 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
276 "Tag 0x%04x is not recorded in IFD '%s' and has therefore been "
277 "removed.", t, exif_ifd_get_name (ifd));
278 exif_content_remove_entry (c, e);
284 exif_content_fix (ExifContent *c)
286 ExifIfd ifd = exif_content_get_ifd (c);
294 dt = exif_data_get_data_type (c->parent);
297 * First of all, fix all existing entries.
299 exif_content_foreach_entry (c, fix_func, NULL);
302 * Go through each tag and if it's not recorded, remove it. If one
303 * is removed, exif_content_foreach_entry() will skip the next entry,
304 * so if this happens do the loop again from the beginning to ensure
305 * they're all checked. This could be avoided if we stop relying on
306 * exif_content_foreach_entry but loop intelligently here.
310 exif_content_foreach_entry (c, remove_not_recorded, NULL);
311 } while (num != c->count);
314 * Then check for non-existing mandatory tags and create them if needed
316 num = exif_tag_table_count();
317 for (i = 0; i < num; ++i) {
318 const ExifTag t = exif_tag_table_get_tag (i);
319 if (exif_tag_get_support_level_in_ifd (t, ifd, dt) ==
320 EXIF_SUPPORT_LEVEL_MANDATORY) {
321 if (exif_content_get_entry (c, t))
322 /* This tag already exists */
324 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
325 "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
326 exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
327 e = exif_entry_new ();
328 exif_content_add_entry (c, e);
329 exif_entry_initialize (e, t);
330 exif_entry_unref (e);