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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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)
81 content->priv->ref_count++;
85 exif_content_unref (ExifContent *content)
87 content->priv->ref_count--;
88 if (!content->priv->ref_count)
89 exif_content_free (content);
93 exif_content_free (ExifContent *content)
95 ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
100 for (i = 0; i < content->count; i++)
101 exif_entry_unref (content->entries[i]);
102 exif_mem_free (mem, content->entries);
105 exif_log_unref (content->priv->log);
108 exif_mem_free (mem, content->priv);
109 exif_mem_free (mem, content);
110 exif_mem_unref (mem);
114 exif_content_dump (ExifContent *content, unsigned int indent)
119 for (i = 0; i < 2 * indent; i++)
126 printf ("%sDumping exif content (%u entries)...\n", buf,
128 for (i = 0; i < content->count; i++)
129 exif_entry_dump (content->entries[i], indent + 1);
133 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
136 if (!c || !c->priv || !entry || entry->parent) return;
138 /* One tag can only be added once to an IFD. */
139 if (exif_content_get_entry (c, entry->tag)) {
140 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
141 "An attempt has been made to add "
142 "the tag '%s' twice to an IFD. This is against "
143 "specification.", exif_tag_get_name (entry->tag));
147 entries = exif_mem_realloc (c->priv->mem,
148 c->entries, sizeof (ExifEntry*) * (c->count + 1));
149 if (!entries) return;
151 entries[c->count++] = entry;
152 c->entries = entries;
153 exif_entry_ref (entry);
157 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
160 ExifEntry **t, *temp;
162 if (!c || !c->priv || !e || (e->parent != c)) return;
164 /* Search the entry */
165 for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
166 if (i == c->count) return;
168 /* Remove the entry */
169 temp = c->entries[c->count-1];
171 t = exif_mem_realloc (c->priv->mem, c->entries,
172 sizeof(ExifEntry*) * (c->count - 1));
178 if (i!=c->count) /* we deallocated the last slot already */
179 memmove (&t[i], &t[i + 1], sizeof (ExifEntry*) * (c->count - i - 1));
180 t[c->count-1] = temp;
182 exif_mem_free (c->priv->mem, c->entries);
187 exif_entry_unref (e);
191 exif_content_get_entry (ExifContent *content, ExifTag tag)
198 for (i = 0; i < content->count; i++)
199 if (content->entries[i]->tag == tag)
200 return (content->entries[i]);
205 exif_content_foreach_entry (ExifContent *content,
206 ExifContentForeachEntryFunc func, void *data)
210 if (!content || !func)
213 for (i = 0; i < content->count; i++)
214 func (content->entries[i], data);
218 exif_content_log (ExifContent *content, ExifLog *log)
220 if (!content || !content->priv || !log || content->priv->log == log)
223 if (content->priv->log) exif_log_unref (content->priv->log);
224 content->priv->log = log;
229 exif_content_get_ifd (ExifContent *c)
231 if (!c || !c->parent) return EXIF_IFD_COUNT;
234 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
235 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
236 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
237 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
238 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
243 fix_func (ExifEntry *e, void *UNUSED(data))
249 exif_content_fix (ExifContent *c)
251 ExifIfd ifd = exif_content_get_ifd (c);
258 dt = exif_data_get_data_type (c->parent);
260 /* First of all, fix all existing entries. */
261 exif_content_foreach_entry (c, fix_func, NULL);
264 * Then check for existing tags that are not allowed and for
265 * non-existing mandatory tags.
267 for (t = 0; t <= 0xffff; t++) {
268 switch (exif_tag_get_support_level_in_ifd (t, ifd, dt)) {
269 case EXIF_SUPPORT_LEVEL_MANDATORY:
270 if (exif_content_get_entry (c, t)) break;
271 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
272 "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
273 exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
274 e = exif_entry_new ();
275 exif_content_add_entry (c, e);
276 exif_entry_initialize (e, t);
277 exif_entry_unref (e);
279 case EXIF_SUPPORT_LEVEL_NOT_RECORDED:
280 e = exif_content_get_entry (c, t);
282 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
283 "Tag '%s' is not recorded in IFD '%s' and has therefore been "
284 "removed.", exif_tag_get_name_in_ifd (t, ifd),
285 exif_ifd_get_name (ifd));
286 exif_content_remove_entry (c, e);
288 case EXIF_SUPPORT_LEVEL_OPTIONAL: