annotate src/exif-common.c @ 191:19df9953a5d1

crw preview support with exiv2
author nadvornik
date Sat, 22 Mar 2008 16:43:59 +0000
parents c2923efebfdc
children d662d680250d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
182
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
1 /*
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
2 * GQView
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
3 * (C) 2006 John Ellis
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
4 *
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
5 */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
6
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
7 #ifdef HAVE_CONFIG_H
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
8 # include "config.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
9 #endif
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
10
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
11 #include <stdio.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
12 #include <string.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
13 #include <fcntl.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
14 #include <unistd.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
15 #include <sys/types.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
16 #include <sys/stat.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
17 #include <sys/mman.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
18 #include <math.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
19
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
20 #include <glib.h>
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
21
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
22 #include "intl.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
23
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
24 #include "gqview.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
25 #include "exif.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
26
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
27 #include "format_raw.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
28 #include "ui_fileops.h"
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
29
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
30
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
31 /* human readable key list */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
32
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
33 ExifFormattedText ExifFormattedList[] = {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
34 { "fCamera", N_("Camera") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
35 { "fDateTime", N_("Date") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
36 { "fShutterSpeed", N_("Shutter speed") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
37 { "fAperture", N_("Aperture") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
38 { "fExposureBias", N_("Exposure bias") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
39 { "fISOSpeedRating", N_("ISO sensitivity") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
40 { "fFocalLength", N_("Focal length") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
41 { "fSubjectDistance", N_("Subject distance") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
42 { "fFlash", N_("Flash") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
43 { "fResolution", N_("Resolution") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
44 { NULL, NULL }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
45 };
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
46
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
47 static ExifTextList ExifFlashList[] = {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
48 { 0, N_("no") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
49 { 1, N_("yes") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
50 { 5, N_("yes, not detected by strobe") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
51 { 7, N_("yes, detected by strobe") },
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
52 EXIF_TEXT_LIST_END
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
53 };
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
54
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
55
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
56 double exif_rational_to_double(ExifRational *r, gint sign)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
57 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
58 if (!r || r->den == 0.0) return 0.0;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
59
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
60 if (sign) return (double)((int)r->num) / (double)((int)r->den);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
61 return (double)r->num / r->den;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
62 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
63
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
64 double exif_get_rational_as_double(ExifData *exif, const gchar *key)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
65 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
66 ExifRational *r;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
67 gint sign;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
68
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
69 r = exif_get_rational(exif, key, &sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
70 return exif_rational_to_double(r, sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
71 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
72
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
73 static GString *append_comma_text(GString *string, const gchar *text)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
74 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
75 string = g_string_append(string, ", ");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
76 string = g_string_append(string, text);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
77
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
78 return string;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
79 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
80
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
81
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
82 gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
83 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
84 /* must begin with f, else not formatted */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
85 if (key[0] != 'f')
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
86 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
87 if (key_valid) *key_valid = FALSE;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
88 return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
89 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
90
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
91 if (key_valid) *key_valid = TRUE;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
92
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
93 if (strcmp(key, "fCamera") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
94 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
95 gchar *text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
96 gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
97 gchar *model = exif_get_data_as_text(exif, "Exif.Image.Model");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
98 gchar *software = exif_get_data_as_text(exif, "Exif.Image.Software");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
99
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
100 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", ((make) && (model)) ? " " : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
101 (model) ? model : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
102 (software) ? " (" : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
103 (software) ? software : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
104 (software) ? ")" : "");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
105
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
106 g_free(make);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
107 g_free(model);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
108 g_free(software);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
109 return text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
110 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
111 if (strcmp(key, "fDateTime") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
112 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
113 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
114 gchar *subsec = NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
115 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTimeOriginal");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
116 if (!text)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
117 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
118 text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
119 if (text) subsec = exif_get_data_as_text(exif, "Exif.Photo.SubSecTime");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
120 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
121 if (subsec)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
122 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
123 gchar *tmp = text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
124 text = g_strconcat(tmp, ".", subsec, NULL);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
125 g_free(tmp);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
126 g_free(subsec);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
127 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
128 return text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
129 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
130 if (strcmp(key, "fShutterSpeed") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
131 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
132 ExifRational *r;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
133
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
134 r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
135 if (r && r->num && r->den)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
136 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
137 double n = (double)r->den / (double)r->num;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
138 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
139 n > 1.0 ? n : 1.0 / n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
140 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
141 r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
142 if (r && r->num && r->den)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
143 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
144 double n = pow(2.0, exif_rational_to_double(r, TRUE));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
145
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
146 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
147 if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
148
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
149 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
150 n > 1.0 ? floor(n) : 1.0 / n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
151 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
152 return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
153 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
154 if (strcmp(key, "fAperture") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
155 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
156 double n;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
157
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
158 n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
159 if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
160 if (n == 0.0) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
161
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
162 return g_strdup_printf("f/%.1f", n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
163 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
164 if (strcmp(key, "fExposureBias") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
165 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
166 ExifRational *r;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
167 gint sign;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
168 double n;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
169
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
170 r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
171 if (!r) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
172
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
173 n = exif_rational_to_double(r, sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
174 return g_strdup_printf("%+.1f", n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
175 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
176 if (strcmp(key, "fFocalLength") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
177 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
178 double n;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
179
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
180 n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
181 if (n == 0.0) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
182 return g_strdup_printf("%.2f mm", n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
183 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
184 if (strcmp(key, "fISOSpeedRating") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
185 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
186 gchar *text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
187
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
188 text = exif_get_data_as_text(exif, "Exif.Photo.ISOSpeedRatings");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
189 /* kodak may set this instead */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
190 if (!text) text = exif_get_data_as_text(exif, "Exif.Photo.ExposureIndex");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
191 return text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
192 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
193 if (strcmp(key, "fSubjectDistance") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
194 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
195 ExifRational *r;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
196 gint sign;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
197 double n;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
198
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
199 r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
200 if (!r) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
201
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
202 if ((long)r->num == 0xffffffff) return g_strdup(_("infinity"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
203 if ((long)r->num == 0) return g_strdup(_("unknown"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
204
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
205 n = exif_rational_to_double(r, sign);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
206 if (n == 0.0) return _("unknown");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
207 return g_strdup_printf("%.3f m", n);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
208 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
209 if (strcmp(key, "fFlash") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
210 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
211 /* grr, flash is a bitmask... */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
212 GString *string;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
213 gchar *text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
214 gint n;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
215 gint v;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
216
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
217 if (!exif_get_integer(exif, "Exif.Photo.Flash", &n)) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
218
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
219 /* Exif 2.1 only defines first 3 bits */
184
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
220 if (n <= 0x07) return exif_get_data_as_text(exif, "Exif.Photo.Flash");
182
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
221
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
222 /* must be Exif 2.2 */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
223 string = g_string_new("");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
224
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
225 /* flash fired (bit 0) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
226 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
227
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
228 /* flash mode (bits 3, 4) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
229 v = (n >> 3) & 0x03;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
230 if (v) string = append_comma_text(string, _("mode:"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
231 switch (v)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
232 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
233 case 1:
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
234 string = g_string_append(string, _("on"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
235 break;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
236 case 2:
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
237 string = g_string_append(string, _("off"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
238 break;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
239 case 3:
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
240 string = g_string_append(string, _("auto"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
241 break;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
242 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
243
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
244 /* return light (bits 1, 2) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
245 v = (n >> 1) & 0x03;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
246 if (v == 2) string = append_comma_text(string, _("not detected by strobe"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
247 if (v == 3) string = append_comma_text(string, _("detected by strobe"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
248
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
249 /* we ignore flash function (bit 5) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
250
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
251 /* red-eye (bit 6) */
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
252 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
253
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
254 text = string->str;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
255 g_string_free(string, FALSE);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
256 return text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
257 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
258 if (strcmp(key, "fResolution") == 0)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
259 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
260 ExifRational *rx, *ry;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
261 gchar *units;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
262 gchar *text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
263
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
264 rx = exif_get_rational(exif, "Exif.Image.XResolution", NULL);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
265 ry = exif_get_rational(exif, "Exif.Image.YResolution", NULL);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
266 if (!rx || !ry) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
267
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
268 units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit");
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
269 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0,
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
270 ry->den ? (double)ry->num / ry->den : 1.0,
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
271 _("dot"), (units) ? units : _("unknown"));
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
272
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
273 g_free(units);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
274 return text;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
275 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
276
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
277 if (key_valid) *key_valid = FALSE;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
278 return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
279 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
280
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
281 const gchar *exif_get_description_by_key(const gchar *key)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
282 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
283 gint i;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
284
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
285 if (!key) return NULL;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
286
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
287 i = 0;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
288 while (ExifFormattedList[i].key != NULL)
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
289 {
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
290 if (strcmp(key, ExifFormattedList[i].key) == 0) return _(ExifFormattedList[i].description);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
291 i++;
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
292 }
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
293
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
294 return exif_get_tag_description_by_key(key);
8a417f10ba09 more exiv2 fixes
nadvornik
parents:
diff changeset
295 }
184
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
296
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
297 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
298 {
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
299 ExifItem *item;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
300
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
301 item = exif_get_item(exif, key);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
302 return exif_item_get_integer(item, value);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
303 }
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
304
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
305 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
306 {
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
307 ExifItem *item;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
308
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
309 item = exif_get_item(exif, key);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
310 return exif_item_get_rational(item, sign);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
311 }
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
312
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
313 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
314 {
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
315 ExifItem *item;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
316 gchar *text;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
317 gint key_valid;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
318
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
319 if (!key) return NULL;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
320
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
321 text = exif_get_formatted_by_key(exif, key, &key_valid);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
322 if (key_valid) return text;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
323
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
324 item = exif_get_item(exif, key);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
325 if (item) return exif_item_get_data_as_text(item);
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
326
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
327 return NULL;
2507418ab7a2 more exiv2 fixes
nadvornik
parents: 182
diff changeset
328 }
188
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
329
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
330 ExifData *exif_read_fd(FileData *fd, gint parse_color_profile)
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
331 {
190
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
332 GList *work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
188
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
333 gchar *sidecar_path = NULL;
190
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
334
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
335 if (strcasecmp(fd->extension, ".cr2") == 0 || // FIXME: list of formats that can have xmp sidecar, make it configurable
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
336 strcasecmp(fd->extension, ".nef") == 0)
188
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
337 {
190
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
338 while(work)
188
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
339 {
190
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
340 FileData *sfd = work->data;
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
341 work = work->next;
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
342 printf("sfd %s\n", sfd->path);
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
343 if (strcasecmp(sfd->extension, ".xmp") == 0)
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
344 {
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
345 sidecar_path = sfd->path;
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
346 break;
c2923efebfdc whitelist of files that can have an xmp sidecar, sample external command
nadvornik
parents: 188
diff changeset
347 }
188
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
348 }
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
349 }
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
350
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
351
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
352 // FIXME: some caching would be nice
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
353 return exif_read(fd->path, sidecar_path, parse_color_profile);
0584cb78aa14 write comment and keywords to xmp, sidecars are used if exist
nadvornik
parents: 184
diff changeset
354 }