3 * Copyright © 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.
23 #include <libexif/exif-content.h>
30 * static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
33 struct _ExifContentPrivate
35 unsigned int ref_count;
42 exif_content_new (void)
44 ExifMem *mem = exif_mem_new_default ();
45 ExifContent *content = exif_content_new_mem (mem);
53 exif_content_new_mem (ExifMem *mem)
57 if (!mem) return NULL;
59 content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
62 content->priv = exif_mem_alloc (mem,
63 (ExifLong) sizeof (ExifContentPrivate));
65 exif_mem_free (mem, content);
69 content->priv->ref_count = 1;
71 content->priv->mem = mem;
78 exif_content_ref (ExifContent *content)
80 content->priv->ref_count++;
84 exif_content_unref (ExifContent *content)
86 content->priv->ref_count--;
87 if (!content->priv->ref_count)
88 exif_content_free (content);
92 exif_content_free (ExifContent *content)
94 ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
99 for (i = 0; i < content->count; i++)
100 exif_entry_unref (content->entries[i]);
101 exif_mem_free (mem, content->entries);
104 exif_log_unref (content->priv->log);
107 exif_mem_free (mem, content->priv);
108 exif_mem_free (mem, content);
109 exif_mem_unref (mem);
113 exif_content_dump (ExifContent *content, unsigned int indent)
118 for (i = 0; i < 2 * indent; i++)
125 printf ("%sDumping exif content (%u entries)...\n", buf,
127 for (i = 0; i < content->count; i++)
128 exif_entry_dump (content->entries[i], indent + 1);
132 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
134 if (!c || !c->priv || !entry || entry->parent) return;
136 /* One tag can only be added once to an IFD. */
137 if (exif_content_get_entry (c, entry->tag)) {
138 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
139 "An attempt has been made to add "
140 "the tag '%s' twice to an IFD. This is against "
141 "specification.", exif_tag_get_name (entry->tag));
146 c->entries = exif_mem_realloc (c->priv->mem,
147 c->entries, sizeof (ExifEntry) * (c->count + 1));
148 if (!c->entries) return;
149 c->entries[c->count] = entry;
150 exif_entry_ref (entry);
155 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
159 if (!c || !c->priv || !e || (e->parent != c)) return;
161 /* Search the entry */
162 for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
163 if (i == c->count) return;
165 /* Remove the entry */
166 memmove (&c->entries[i], &c->entries[i + 1],
167 sizeof (ExifEntry) * (c->count - i - 1));
170 exif_entry_unref (e);
171 c->entries = exif_mem_realloc (c->priv->mem, c->entries,
172 sizeof(ExifEntry) * c->count);
176 exif_content_get_entry (ExifContent *content, ExifTag tag)
183 for (i = 0; i < content->count; i++)
184 if (content->entries[i]->tag == tag)
185 return (content->entries[i]);
190 exif_content_foreach_entry (ExifContent *content,
191 ExifContentForeachEntryFunc func, void *data)
195 if (!content || !func)
198 for (i = 0; i < content->count; i++)
199 func (content->entries[i], data);
203 exif_content_log (ExifContent *content, ExifLog *log)
205 if (!content || !content->priv || !log || content->priv->log == log)
208 if (content->priv->log) exif_log_unref (content->priv->log);
209 content->priv->log = log;
214 exif_content_get_ifd (ExifContent *c)
216 if (!c || !c->parent) return EXIF_IFD_COUNT;
219 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
220 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
221 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
222 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
223 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
228 fix_func (ExifEntry *e, void *data)
234 exif_content_fix (ExifContent *c)
236 ExifIfd ifd = exif_content_get_ifd (c);
243 dt = exif_data_get_data_type (c->parent);
245 /* First of all, fix all existing entries. */
246 exif_content_foreach_entry (c, fix_func, NULL);
249 * Then check for existing tags that are not allowed and for
250 * non-existing mandatory tags.
252 for (t = 0; t <= 0xffff; t++) {
253 switch (exif_tag_get_support_level_in_ifd (t, ifd, dt)) {
254 case EXIF_SUPPORT_LEVEL_MANDATORY:
255 if (exif_content_get_entry (c, t)) break;
256 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
257 "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
258 exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
259 e = exif_entry_new ();
260 exif_content_add_entry (c, e);
261 exif_entry_initialize (e, t);
262 exif_entry_unref (e);
264 case EXIF_SUPPORT_LEVEL_NOT_RECORDED:
265 e = exif_content_get_entry (c, t);
267 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
268 "Tag '%s' is not recoreded in IFD '%s' and has therefore been "
269 "removed.", exif_tag_get_name_in_ifd (t, ifd),
270 exif_ifd_get_name (ifd));
271 exif_content_remove_entry (c, e);
273 case EXIF_SUPPORT_LEVEL_OPTIONAL: