exif-content.c

Go to the documentation of this file.
00001 /* exif-content.c
00002  *
00003  * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <libexif/exif-content.h>
00024 
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 
00029 /* unused constant
00030  * static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
00031  */
00032 
00033 struct _ExifContentPrivate
00034 {
00035         unsigned int ref_count;
00036 
00037         ExifMem *mem;
00038         ExifLog *log;
00039 };
00040 
00041 ExifContent *
00042 exif_content_new (void)
00043 {
00044         ExifMem *mem = exif_mem_new_default ();
00045         ExifContent *content = exif_content_new_mem (mem);
00046 
00047         exif_mem_unref (mem);
00048 
00049         return content;
00050 }
00051 
00052 ExifContent *
00053 exif_content_new_mem (ExifMem *mem)
00054 {
00055         ExifContent *content;
00056 
00057         if (!mem) return NULL;
00058 
00059         content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
00060         if (!content)
00061                 return NULL;
00062         content->priv = exif_mem_alloc (mem,
00063                                 (ExifLong) sizeof (ExifContentPrivate));
00064         if (!content->priv) {
00065                 exif_mem_free (mem, content);
00066                 return NULL;
00067         }
00068 
00069         content->priv->ref_count = 1;
00070 
00071         content->priv->mem = mem;
00072         exif_mem_ref (mem);
00073 
00074         return content;
00075 }
00076 
00077 void
00078 exif_content_ref (ExifContent *content)
00079 {
00080         content->priv->ref_count++;
00081 }
00082 
00083 void
00084 exif_content_unref (ExifContent *content)
00085 {
00086         content->priv->ref_count--;
00087         if (!content->priv->ref_count)
00088                 exif_content_free (content);
00089 }
00090 
00091 void
00092 exif_content_free (ExifContent *content)
00093 {
00094         ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
00095         unsigned int i;
00096 
00097         if (!content) return;
00098 
00099         for (i = 0; i < content->count; i++)
00100                 exif_entry_unref (content->entries[i]);
00101         exif_mem_free (mem, content->entries);
00102 
00103         if (content->priv) {
00104                 exif_log_unref (content->priv->log);
00105         }
00106 
00107         exif_mem_free (mem, content->priv);
00108         exif_mem_free (mem, content);
00109         exif_mem_unref (mem);
00110 }
00111 
00112 void
00113 exif_content_dump (ExifContent *content, unsigned int indent)
00114 {
00115         char buf[1024];
00116         unsigned int i;
00117 
00118         for (i = 0; i < 2 * indent; i++)
00119                 buf[i] = ' ';
00120         buf[i] = '\0';
00121 
00122         if (!content)
00123                 return;
00124 
00125         printf ("%sDumping exif content (%i entries)...\n", buf,
00126                 content->count);
00127         for (i = 0; i < content->count; i++)
00128                 exif_entry_dump (content->entries[i], indent + 1);
00129 }
00130 
00131 void
00132 exif_content_add_entry (ExifContent *c, ExifEntry *entry)
00133 {
00134         if (!c || !c->priv || !entry || entry->parent) return;
00135 
00136         /* One tag can only be added once to an IFD. */
00137         if (exif_content_get_entry (c, entry->tag)) {
00138                 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
00139                         "An attempt has been made to add "
00140                         "the tag '%s' twice to an IFD. This is against "
00141                         "specification.", exif_tag_get_name (entry->tag));
00142                 return;
00143         }
00144 
00145         entry->parent = c;
00146         c->entries = exif_mem_realloc (c->priv->mem,
00147                 c->entries, sizeof (ExifEntry) * (c->count + 1));
00148         if (!c->entries) return;
00149         c->entries[c->count] = entry;
00150         exif_entry_ref (entry);
00151         c->count++;
00152 }
00153 
00154 void
00155 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
00156 {
00157         unsigned int i;
00158 
00159         if (!c || !c->priv || !e || (e->parent != c)) return;
00160 
00161         /* Search the entry */
00162         for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
00163         if (i == c->count) return;
00164 
00165         /* Remove the entry */
00166         memmove (&c->entries[i], &c->entries[i + 1],
00167                  sizeof (ExifEntry) * (c->count - i - 1));
00168         c->count--;
00169         e->parent = NULL;
00170         exif_entry_unref (e);
00171         c->entries = exif_mem_realloc (c->priv->mem, c->entries,
00172                                         sizeof(ExifEntry) * c->count);
00173 }
00174 
00175 ExifEntry *
00176 exif_content_get_entry (ExifContent *content, ExifTag tag)
00177 {
00178         unsigned int i;
00179 
00180         if (!content)
00181                 return (NULL);
00182 
00183         for (i = 0; i < content->count; i++)
00184                 if (content->entries[i]->tag == tag)
00185                         return (content->entries[i]);
00186         return (NULL);
00187 }
00188 
00189 void
00190 exif_content_foreach_entry (ExifContent *content,
00191                             ExifContentForeachEntryFunc func, void *data)
00192 {
00193         unsigned int i;
00194 
00195         if (!content || !func)
00196                 return;
00197 
00198         for (i = 0; i < content->count; i++)
00199                 func (content->entries[i], data);
00200 }
00201 
00202 void
00203 exif_content_log (ExifContent *content, ExifLog *log)
00204 {
00205         if (!content || !content->priv || !log || content->priv->log == log)
00206                 return;
00207 
00208         if (content->priv->log) exif_log_unref (content->priv->log);
00209         content->priv->log = log;
00210         exif_log_ref (log);
00211 }
00212 
00213 ExifIfd
00214 exif_content_get_ifd (ExifContent *c)
00215 {
00216         if (!c || !c->parent) return EXIF_IFD_COUNT;
00217 
00218         return 
00219                 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
00220                 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
00221                 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
00222                 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
00223                 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
00224                 EXIF_IFD_COUNT;
00225 }
00226 
00227 static void
00228 fix_func (ExifEntry *e, void *data)
00229 {
00230         exif_entry_fix (e);
00231 }
00232 
00233 void
00234 exif_content_fix (ExifContent *c)
00235 {
00236         ExifIfd ifd = exif_content_get_ifd (c);
00237         ExifDataType dt;
00238         ExifTag t;
00239         ExifEntry *e;
00240 
00241         if (!c) return;
00242 
00243         dt = exif_data_get_data_type (c->parent);
00244 
00245         /* First of all, fix all existing entries. */
00246         exif_content_foreach_entry (c, fix_func, NULL);
00247 
00248         /*
00249          * Then check for existing tags that are not allowed and for
00250          * non-existing mandatory tags.
00251          */
00252         for (t = 0; t <= 0xffff; t++) {
00253                 switch (exif_tag_get_support_level_in_ifd (t, ifd, dt)) {
00254                 case EXIF_SUPPORT_LEVEL_MANDATORY:
00255                         if (exif_content_get_entry (c, t)) break;
00256                         exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
00257                                         "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
00258                                         exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
00259                         e = exif_entry_new ();
00260                         exif_content_add_entry (c, e);
00261                         exif_entry_initialize (e, t);
00262                         exif_entry_unref (e);
00263                         break;
00264                 case EXIF_SUPPORT_LEVEL_NOT_RECORDED:
00265                         e = exif_content_get_entry (c, t);
00266                         if (!e) break;
00267                         exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
00268                                         "Tag '%s' is not recoreded in IFD '%s' and has therefore been "
00269                                         "removed.", exif_tag_get_name_in_ifd (t, ifd),
00270                                         exif_ifd_get_name (ifd));
00271                         exif_content_remove_entry (c, e);
00272                         break;
00273                 case EXIF_SUPPORT_LEVEL_OPTIONAL:
00274                 default:
00275                         break;
00276                 }
00277         }
00278 }

Generated on Tue Dec 19 14:33:53 2006 for EXIF library (libexif) Internals by  doxygen 1.5.1