1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* exif-content.c
*
* Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <libexif/exif-content.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
struct _ExifContentPrivate
{
unsigned int ref_count;
ExifMem *mem;
ExifLog *log;
};
ExifContent *
exif_content_new (void)
{
ExifMem *mem = exif_mem_new_default ();
ExifContent *content = exif_content_new_mem (mem);
exif_mem_unref (mem);
return content;
}
ExifContent *
exif_content_new_mem (ExifMem *mem)
{
ExifContent *content;
if (!mem) return NULL;
content = exif_mem_alloc (mem, (ExifLong) sizeof (ExifContent));
if (!content)
return NULL;
content->priv = exif_mem_alloc (mem,
(ExifLong) sizeof (ExifContentPrivate));
if (!content->priv) {
exif_mem_free (mem, content);
return NULL;
}
content->priv->ref_count = 1;
content->priv->mem = mem;
exif_mem_ref (mem);
return content;
}
void
exif_content_ref (ExifContent *content)
{
content->priv->ref_count++;
}
void
exif_content_unref (ExifContent *content)
{
content->priv->ref_count--;
if (!content->priv->ref_count)
exif_content_free (content);
}
void
exif_content_free (ExifContent *content)
{
ExifMem *mem = (content && content->priv) ? content->priv->mem : NULL;
unsigned int i;
if (!content) return;
for (i = 0; i < content->count; i++)
exif_entry_unref (content->entries[i]);
exif_mem_free (mem, content->entries);
if (content->priv) {
exif_log_unref (content->priv->log);
}
exif_mem_free (mem, content->priv);
exif_mem_free (mem, content);
exif_mem_unref (mem);
}
void
exif_content_dump (ExifContent *content, unsigned int indent)
{
char buf[1024];
unsigned int i;
for (i = 0; i < 2 * indent; i++)
buf[i] = ' ';
buf[i] = '\0';
if (!content)
return;
printf ("%sDumping exif content (%i entries)...\n", buf,
content->count);
for (i = 0; i < content->count; i++)
exif_entry_dump (content->entries[i], indent + 1);
}
void
exif_content_add_entry (ExifContent *c, ExifEntry *entry)
{
if (!c || !c->priv || !entry || entry->parent) return;
/* One tag can only be added once to an IFD. */
if (exif_content_get_entry (c, entry->tag)) {
exif_log (c->priv->log, EXIF_LOG_CODE_DEBUG, "ExifContent",
"An attempt has been made to add "
"the tag '%s' twice to an IFD. This is against "
"specification.", exif_tag_get_name (entry->tag));
return;
}
entry->parent = c;
c->entries = exif_mem_realloc (c->priv->mem,
c->entries, sizeof (ExifEntry) * (c->count + 1));
if (!c->entries) return;
c->entries[c->count] = entry;
exif_entry_ref (entry);
c->count++;
}
void
exif_content_remove_entry (ExifContent *c, ExifEntry *e)
{
unsigned int i;
if (!c || !c->priv || !e || (e->parent != c)) return;
/* Search the entry */
for (i = 0; i < c->count; i++) if (c->entries[i] == e) break;
if (i == c->count) return;
/* Remove the entry */
memmove (&c->entries[i], &c->entries[i + 1],
sizeof (ExifEntry) * (c->count - i - 1));
c->count--;
e->parent = NULL;
exif_entry_unref (e);
c->entries = exif_mem_realloc (c->priv->mem, c->entries,
sizeof(ExifEntry) * c->count);
}
ExifEntry *
exif_content_get_entry (ExifContent *content, ExifTag tag)
{
unsigned int i;
if (!content)
return (NULL);
for (i = 0; i < content->count; i++)
if (content->entries[i]->tag == tag)
return (content->entries[i]);
return (NULL);
}
void
exif_content_foreach_entry (ExifContent *content,
ExifContentForeachEntryFunc func, void *data)
{
unsigned int i;
if (!content || !func)
return;
for (i = 0; i < content->count; i++)
func (content->entries[i], data);
}
void
exif_content_log (ExifContent *content, ExifLog *log)
{
if (!content || !content->priv || !log || content->priv->log == log)
return;
if (content->priv->log) exif_log_unref (content->priv->log);
content->priv->log = log;
exif_log_ref (log);
}
|