00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00030
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 (%u 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 ExifEntry **entries;
00135 if (!c || !c->priv || !entry || entry->parent) return;
00136
00137
00138 if (exif_content_get_entry (c, entry->tag)) {
00139 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
00140 "An attempt has been made to add "
00141 "the tag '%s' twice to an IFD. This is against "
00142 "specification.", exif_tag_get_name (entry->tag));
00143 return;
00144 }
00145
00146 entries = exif_mem_realloc (c->priv->mem,
00147 c->entries, sizeof (ExifEntry*) * (c->count + 1));
00148 if (!entries) return;
00149 entry->parent = c;
00150 entries[c->count++] = entry;
00151 c->entries = entries;
00152 exif_entry_ref (entry);
00153 }
00154
00155 void
00156 exif_content_remove_entry (ExifContent *c, ExifEntry *e)
00157 {
00158 unsigned int i;
00159
00160 if (!c || !c->priv || !e || (e->parent != c)) return;
00161
00162
00163 for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
00164 if (i == c->count) return;
00165
00166
00167 memmove (&c->entries[i], &c->entries[i + 1],
00168 sizeof (ExifEntry*) * (c->count - i - 1));
00169 c->count--;
00170 e->parent = NULL;
00171 exif_entry_unref (e);
00172 c->entries = exif_mem_realloc (c->priv->mem, c->entries,
00173 sizeof(ExifEntry*) * c->count);
00174 }
00175
00176 ExifEntry *
00177 exif_content_get_entry (ExifContent *content, ExifTag tag)
00178 {
00179 unsigned int i;
00180
00181 if (!content)
00182 return (NULL);
00183
00184 for (i = 0; i < content->count; i++)
00185 if (content->entries[i]->tag == tag)
00186 return (content->entries[i]);
00187 return (NULL);
00188 }
00189
00190 void
00191 exif_content_foreach_entry (ExifContent *content,
00192 ExifContentForeachEntryFunc func, void *data)
00193 {
00194 unsigned int i;
00195
00196 if (!content || !func)
00197 return;
00198
00199 for (i = 0; i < content->count; i++)
00200 func (content->entries[i], data);
00201 }
00202
00203 void
00204 exif_content_log (ExifContent *content, ExifLog *log)
00205 {
00206 if (!content || !content->priv || !log || content->priv->log == log)
00207 return;
00208
00209 if (content->priv->log) exif_log_unref (content->priv->log);
00210 content->priv->log = log;
00211 exif_log_ref (log);
00212 }
00213
00214 ExifIfd
00215 exif_content_get_ifd (ExifContent *c)
00216 {
00217 if (!c || !c->parent) return EXIF_IFD_COUNT;
00218
00219 return
00220 ((c)->parent->ifd[EXIF_IFD_0] == (c)) ? EXIF_IFD_0 :
00221 ((c)->parent->ifd[EXIF_IFD_1] == (c)) ? EXIF_IFD_1 :
00222 ((c)->parent->ifd[EXIF_IFD_EXIF] == (c)) ? EXIF_IFD_EXIF :
00223 ((c)->parent->ifd[EXIF_IFD_GPS] == (c)) ? EXIF_IFD_GPS :
00224 ((c)->parent->ifd[EXIF_IFD_INTEROPERABILITY] == (c)) ? EXIF_IFD_INTEROPERABILITY :
00225 EXIF_IFD_COUNT;
00226 }
00227
00228 static void
00229 fix_func (ExifEntry *e, void *data)
00230 {
00231 exif_entry_fix (e);
00232 }
00233
00234 void
00235 exif_content_fix (ExifContent *c)
00236 {
00237 ExifIfd ifd = exif_content_get_ifd (c);
00238 ExifDataType dt;
00239 ExifTag t;
00240 ExifEntry *e;
00241
00242 if (!c) return;
00243
00244 dt = exif_data_get_data_type (c->parent);
00245
00246
00247 exif_content_foreach_entry (c, fix_func, NULL);
00248
00249
00250
00251
00252
00253 for (t = 0; t <= 0xffff; t++) {
00254 switch (exif_tag_get_support_level_in_ifd (t, ifd, dt)) {
00255 case EXIF_SUPPORT_LEVEL_MANDATORY:
00256 if (exif_content_get_entry (c, t)) break;
00257 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
00258 "Tag '%s' is mandatory in IFD '%s' and has therefore been added.",
00259 exif_tag_get_name_in_ifd (t, ifd), exif_ifd_get_name (ifd));
00260 e = exif_entry_new ();
00261 exif_content_add_entry (c, e);
00262 exif_entry_initialize (e, t);
00263 exif_entry_unref (e);
00264 break;
00265 case EXIF_SUPPORT_LEVEL_NOT_RECORDED:
00266 e = exif_content_get_entry (c, t);
00267 if (!e) break;
00268 exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "exif-content",
00269 "Tag '%s' is not recoreded in IFD '%s' and has therefore been "
00270 "removed.", exif_tag_get_name_in_ifd (t, ifd),
00271 exif_ifd_get_name (ifd));
00272 exif_content_remove_entry (c, e);
00273 break;
00274 case EXIF_SUPPORT_LEVEL_OPTIONAL:
00275 default:
00276 break;
00277 }
00278 }
00279 }