comparison src/exiv2.cc @ 184:2507418ab7a2

more exiv2 fixes
author nadvornik
date Thu, 14 Feb 2008 14:04:43 +0000
parents 3962c9d3d6fd
children 354da67a7ca2
comparison
equal deleted inserted replaced
183:3962c9d3d6fd 184:2507418ab7a2
12 extern "C" { 12 extern "C" {
13 13
14 #include <glib.h> 14 #include <glib.h>
15 #include "exif.h" 15 #include "exif.h"
16 16
17 }
17 18
18 struct _ExifData 19 struct _ExifData
19 { 20 {
20 Exiv2::ExifData exifData; 21 Exiv2::ExifData exifData;
21 Exiv2::ExifData::const_iterator iter; 22 Exiv2::ExifData::const_iterator iter;
22 }; 23
23 24 _ExifData(gchar *path, gint parse_color_profile)
24 25 {
25 ExifData *exif_read(gchar *path, gint parse_color_profile)
26 {
27 printf("exif %s\n", path);
28 try {
29 ExifData *exif = new ExifData;
30
31 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path); 26 Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
32 g_assert (image.get() != 0); 27 g_assert (image.get() != 0);
33 image->readMetadata(); 28 image->readMetadata();
34 exif->exifData = image->exifData(); 29 exifData = image->exifData();
35 return exif; 30 }
31
32 };
33
34 extern "C" {
35
36 ExifData *exif_read(gchar *path, gint parse_color_profile)
37 {
38 printf("exif %s\n", path);
39 try {
40 return new ExifData(path, parse_color_profile);
36 } 41 }
37 catch (Exiv2::AnyError& e) { 42 catch (Exiv2::AnyError& e) {
38 std::cout << "Caught Exiv2 exception '" << e << "'\n"; 43 std::cout << "Caught Exiv2 exception '" << e << "'\n";
39 return 0; 44 return 0;
40 } 45 }
45 { 50 {
46 51
47 delete exif; 52 delete exif;
48 } 53 }
49 54
50
51 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
52 {
53 gint key_valid;
54 gchar *text = exif_get_formatted_by_key(exif, key, &key_valid);
55 if (key_valid) return text;
56
57 try {
58 std::stringstream str;
59 str << exif->exifData[key];
60 return g_strdup(str.str().c_str());
61 }
62 catch (Exiv2::AnyError& e) {
63 return NULL;
64 }
65 }
66
67 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
68 {
69 Exiv2::ExifKey ekey(key);
70 Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
71 if (pos == exif->exifData.end()) return 0;
72 *value = pos->getValue()->toLong();
73
74 return 1;
75 }
76
77 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
78 {
79 Exiv2::ExifKey ekey(key);
80 Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
81 if (pos == exif->exifData.end()) return NULL;
82 Exiv2::Rational v = pos->getValue()->toRational();
83 static ExifRational ret;
84 ret.num = v.first;
85 ret.den = v.second;
86 return &ret;
87 }
88
89 ExifItem *exif_get_item(ExifData *exif, const gchar *key) 55 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
90 { 56 {
91 Exiv2::Exifdatum *item = &exif->exifData[key]; 57 try {
92 return (ExifItem *)item; 58 Exiv2::ExifKey ekey(key);
93 } 59 Exiv2::ExifData::iterator pos = exif->exifData.findKey(ekey);
60 if (pos == exif->exifData.end()) return NULL;
61 Exiv2::Exifdatum *item = &*pos;
62 return (ExifItem *)item;
63 }
64 catch (Exiv2::AnyError& e) {
65 std::cout << "Caught Exiv2 exception '" << e << "'\n";
66 return NULL;
67 }
68 }
69
94 70
95 ExifItem *exif_get_first_item(ExifData *exif) 71 ExifItem *exif_get_first_item(ExifData *exif)
96 { 72 {
97 exif->iter = exif->exifData.begin(); 73 try {
98 if (exif->iter == exif->exifData.end()) return NULL; 74 exif->iter = exif->exifData.begin();
99 const Exiv2::Exifdatum *item = &*exif->iter; 75 if (exif->iter == exif->exifData.end()) return NULL;
100 return (ExifItem *)item; 76 const Exiv2::Exifdatum *item = &*exif->iter;
77 return (ExifItem *)item;
78 }
79 catch (Exiv2::AnyError& e) {
80 std::cout << "Caught Exiv2 exception '" << e << "'\n";
81 return NULL;
82 }
101 } 83 }
102 84
103 ExifItem *exif_get_next_item(ExifData *exif) 85 ExifItem *exif_get_next_item(ExifData *exif)
104 { 86 {
105 exif->iter++; 87 try {
106 if (exif->iter == exif->exifData.end()) return NULL; 88 exif->iter++;
107 const Exiv2::Exifdatum *item = &*exif->iter; 89 if (exif->iter == exif->exifData.end()) return NULL;
108 return (ExifItem *)item; 90 const Exiv2::Exifdatum *item = &*exif->iter;
91 return (ExifItem *)item;
92 }
93 catch (Exiv2::AnyError& e) {
94 std::cout << "Caught Exiv2 exception '" << e << "'\n";
95 return NULL;
96 }
109 } 97 }
110 98
111 const char *exif_item_get_tag_name(ExifItem *item) 99 const char *exif_item_get_tag_name(ExifItem *item)
112 { 100 {
113 return ((Exiv2::Exifdatum *)item)->key().c_str(); 101 try {
102 if (!item) return NULL;
103 return ((Exiv2::Exifdatum *)item)->key().c_str();
104 }
105 catch (Exiv2::AnyError& e) {
106 std::cout << "Caught Exiv2 exception '" << e << "'\n";
107 return NULL;
108 }
114 } 109 }
115 110
116 guint exif_item_get_tag_id(ExifItem *item) 111 guint exif_item_get_tag_id(ExifItem *item)
117 { 112 {
118 return ((Exiv2::Exifdatum *)item)->tag(); 113 try {
114 if (!item) return 0;
115 return ((Exiv2::Exifdatum *)item)->tag();
116 }
117 catch (Exiv2::AnyError& e) {
118 std::cout << "Caught Exiv2 exception '" << e << "'\n";
119 return 0;
120 }
119 } 121 }
120 122
121 guint exif_item_get_elements(ExifItem *item) 123 guint exif_item_get_elements(ExifItem *item)
122 { 124 {
123 return ((Exiv2::Exifdatum *)item)->count(); 125 try {
126 if (!item) return 0;
127 return ((Exiv2::Exifdatum *)item)->count();
128 }
129 catch (Exiv2::AnyError& e) {
130 std::cout << "Caught Exiv2 exception '" << e << "'\n";
131 return NULL;
132 }
124 } 133 }
125 134
126 char *exif_item_get_data(ExifItem *item, guint *data_len) 135 char *exif_item_get_data(ExifItem *item, guint *data_len)
127 { 136 {
128 } 137 }
129 138
130 char *exif_item_get_description(ExifItem *item) 139 char *exif_item_get_description(ExifItem *item)
131 { 140 {
132 return g_strdup(((Exiv2::Exifdatum *)item)->tagLabel().c_str()); 141 try {
142 if (!item) return NULL;
143 return g_strdup(((Exiv2::Exifdatum *)item)->tagLabel().c_str());
144 }
145 catch (Exiv2::AnyError& e) {
146 std::cout << "Caught Exiv2 exception '" << e << "'\n";
147 return NULL;
148 }
133 } 149 }
134 150
135 /* 151 /*
136 invalidTypeId, unsignedByte, asciiString, unsignedShort, 152 invalidTypeId, unsignedByte, asciiString, unsignedShort,
137 unsignedLong, unsignedRational, signedByte, undefined, 153 unsignedLong, unsignedRational, signedByte, undefined,
138 signedShort, signedLong, signedRational, string, 154 signedShort, signedLong, signedRational, string,
139 date, time, comment, directory, 155 date, time, comment, directory,
140 xmpText, xmpAlt, xmpBag, xmpSeq, 156 xmpText, xmpAlt, xmpBag, xmpSeq,
141 langAlt, lastTypeId 157 langAlt, lastTypeId
142
143 EXIF_FORMAT_UNKNOWN = 0,
144 EXIF_FORMAT_BYTE_UNSIGNED = 1,
145 EXIF_FORMAT_STRING = 2,
146 EXIF_FORMAT_SHORT_UNSIGNED = 3,
147 EXIF_FORMAT_LONG_UNSIGNED = 4,
148 EXIF_FORMAT_RATIONAL_UNSIGNED = 5,
149 EXIF_FORMAT_BYTE = 6,
150 EXIF_FORMAT_UNDEFINED = 7,
151 EXIF_FORMAT_SHORT = 8,
152 EXIF_FORMAT_LONG = 9,
153 EXIF_FORMAT_RATIONAL = 10,
154 EXIF_FORMAT_FLOAT = 11,
155 EXIF_FORMAT_DOUBLE = 12
156 */ 158 */
157 159
160 static guint format_id_trans_tbl [] = {
161 EXIF_FORMAT_UNKNOWN,
162 EXIF_FORMAT_BYTE_UNSIGNED,
163 EXIF_FORMAT_STRING,
164 EXIF_FORMAT_SHORT_UNSIGNED,
165 EXIF_FORMAT_LONG_UNSIGNED,
166 EXIF_FORMAT_RATIONAL_UNSIGNED,
167 EXIF_FORMAT_BYTE,
168 EXIF_FORMAT_UNDEFINED,
169 EXIF_FORMAT_SHORT,
170 EXIF_FORMAT_LONG,
171 EXIF_FORMAT_RATIONAL,
172 EXIF_FORMAT_STRING,
173 EXIF_FORMAT_STRING,
174 EXIF_FORMAT_STRING,
175 EXIF_FORMAT_UNDEFINED,
176 EXIF_FORMAT_STRING,
177 EXIF_FORMAT_STRING,
178 EXIF_FORMAT_STRING,
179 EXIF_FORMAT_STRING
180 };
181
182
158 183
159 guint exif_item_get_format_id(ExifItem *item) 184 guint exif_item_get_format_id(ExifItem *item)
160 { 185 {
161 return ((Exiv2::Exifdatum *)item)->typeId(); 186 try {
162 } 187 if (!item) return EXIF_FORMAT_UNKNOWN;
188 guint id = ((Exiv2::Exifdatum *)item)->typeId();
189 if (id >= (sizeof(format_id_trans_tbl) / sizeof(format_id_trans_tbl[0])) ) return EXIF_FORMAT_UNKNOWN;
190 return format_id_trans_tbl[id];
191 }
192 catch (Exiv2::AnyError& e) {
193 std::cout << "Caught Exiv2 exception '" << e << "'\n";
194 return EXIF_FORMAT_UNKNOWN;
195 }
196 }
197
163 const char *exif_item_get_format_name(ExifItem *item, gint brief) 198 const char *exif_item_get_format_name(ExifItem *item, gint brief)
164 { 199 {
165 return ((Exiv2::Exifdatum *)item)->typeName(); 200 try {
201 if (!item) return NULL;
202 return ((Exiv2::Exifdatum *)item)->typeName();
203 }
204 catch (Exiv2::AnyError& e) {
205 std::cout << "Caught Exiv2 exception '" << e << "'\n";
206 return NULL;
207 }
166 } 208 }
167 209
168 210
169 gchar *exif_item_get_data_as_text(ExifItem *item) 211 gchar *exif_item_get_data_as_text(ExifItem *item)
170 { 212 {
171 try { 213 try {
214 if (!item) return NULL;
172 std::stringstream str; 215 std::stringstream str;
173 str << *((Exiv2::Exifdatum *)item); 216 str << *((Exiv2::Exifdatum *)item);
174 return g_strdup(str.str().c_str()); 217 return g_strdup(str.str().c_str());
175 } 218 }
176 catch (Exiv2::AnyError& e) { 219 catch (Exiv2::AnyError& e) {
179 } 222 }
180 223
181 224
182 gint exif_item_get_integer(ExifItem *item, gint *value) 225 gint exif_item_get_integer(ExifItem *item, gint *value)
183 { 226 {
184 return ((Exiv2::Exifdatum *)item)->toLong(); 227 try {
228 if (!item) return 0;
229 return ((Exiv2::Exifdatum *)item)->toLong();
230 }
231 catch (Exiv2::AnyError& e) {
232 std::cout << "Caught Exiv2 exception '" << e << "'\n";
233 return 0;
234 }
185 } 235 }
186 236
187 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign) 237 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
188 { 238 {
189 Exiv2::Rational v = ((Exiv2::Exifdatum *)item)->toRational(); 239 try {
190 static ExifRational ret; 240 if (!item) return NULL;
191 ret.num = v.first; 241 Exiv2::Rational v = ((Exiv2::Exifdatum *)item)->toRational();
192 ret.den = v.second; 242 static ExifRational ret;
193 return &ret; 243 ret.num = v.first;
244 ret.den = v.second;
245 return &ret;
246 }
247 catch (Exiv2::AnyError& e) {
248 std::cout << "Caught Exiv2 exception '" << e << "'\n";
249 return NULL;
250 }
194 } 251 }
195 252
196 const gchar *exif_get_tag_description_by_key(const gchar *key) 253 const gchar *exif_get_tag_description_by_key(const gchar *key)
197 { 254 {
198 Exiv2::ExifKey ekey(key); 255 try {
199 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ()); 256 Exiv2::ExifKey ekey(key);
257 return Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ());
258 }
259 catch (Exiv2::AnyError& e) {
260 std::cout << "Caught Exiv2 exception '" << e << "'\n";
261 return NULL;
262 }
200 } 263 }
201 264
202 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path, 265 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
203 unsigned char *header_data, const guint header_len, 266 unsigned char *header_data, const guint header_len,
204 guint *image_offset, guint *exif_offset) 267 guint *image_offset, guint *exif_offset)