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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* Eye Of Mate - EXIF Utilities
*
* Copyright (C) 2006-2007 The Free Software Foundation
*
* Author: Lucas Rocha <lucasr@gnome.org>
* Author: Claudio Saavedra <csaavedra@alumnos.utalca.cl>
* Author: Felix Riemann <felix@hsgheli.de>
*
* Based on code by:
* - Jens Finke <jens@gnome.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STRPTIME
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE_EXTENDED 1
#endif
#include <time.h>
#include "eom-exif-util.h"
#include "eom-util.h"
#include <string.h>
#include <glib/gi18n.h>
#define DATE_BUF_SIZE 200
/* gboolean <-> gpointer conversion macros taken from gedit */
#ifndef GBOOLEAN_TO_POINTER
#define GBOOLEAN_TO_POINTER(i) (GINT_TO_POINTER ((i) ? 2 : 1))
#endif
#ifndef GPOINTER_TO_BOOLEAN
#define GPOINTER_TO_BOOLEAN(i) ((gboolean) ((GPOINTER_TO_INT (i) == 2) ? TRUE : FALSE))
#endif
typedef ExifData EomExifData;
/* Define EomExifData type */
G_DEFINE_BOXED_TYPE(EomExifData, eom_exif_data, eom_exif_data_copy, eom_exif_data_free)
#ifdef HAVE_STRPTIME
static gpointer
_check_strptime_updates_wday (gpointer data)
{
struct tm tm;
memset (&tm, '\0', sizeof (tm));
strptime ("2008:12:24 20:30:45", "%Y:%m:%d %T", &tm);
/* Check if tm.tm_wday is set to Wednesday (3) now */
return GBOOLEAN_TO_POINTER (tm.tm_wday == 3);
}
#endif
/**
* _calculate_wday_yday:
* @tm: A struct tm that should be updated.
*
* Ensure tm_wday and tm_yday are set correctly in a struct tm.
* The other date (dmy) values must have been set already.
**/
static void
_calculate_wday_yday (struct tm *tm)
{
GDate *exif_date;
struct tm tmp_tm;
exif_date = g_date_new_dmy (tm->tm_mday,
tm->tm_mon+1,
tm->tm_year+1900);
g_return_if_fail (exif_date != NULL && g_date_valid (exif_date));
// Use this to get GLib <-> libc corrected values
g_date_to_struct_tm (exif_date, &tmp_tm);
g_date_free (exif_date);
tm->tm_wday = tmp_tm.tm_wday;
tm->tm_yday = tmp_tm.tm_yday;
}
/* Older GCCs don't support pragma diagnostic inside functions.
* Put these here to avoid problems with the strftime format strings
* without breaking the build for these older GCCs */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#ifdef HAVE_STRPTIME
static gchar *
eom_exif_util_format_date_with_strptime (const gchar *date, const gchar* format)
{
static GOnce strptime_updates_wday = G_ONCE_INIT;
gchar *new_date = NULL;
gchar tmp_date[DATE_BUF_SIZE];
gchar *p;
gsize dlen;
struct tm tm;
memset (&tm, '\0', sizeof (tm));
p = strptime (date, "%Y:%m:%d %T", &tm);
if (p == date + strlen (date)) {
g_once (&strptime_updates_wday,
_check_strptime_updates_wday,
NULL);
// Ensure tm.tm_wday and tm.tm_yday are set
if (!GPOINTER_TO_BOOLEAN (strptime_updates_wday.retval))
_calculate_wday_yday (&tm);
/* A strftime-formatted string, to display the date the image was taken. */
dlen = strftime (tmp_date, DATE_BUF_SIZE * sizeof(gchar), format, &tm);
new_date = g_strndup (tmp_date, dlen);
}
return new_date;
}
#else
static gchar *
eom_exif_util_format_date_by_hand (const gchar *date, const gchar* format)
{
int year, month, day, hour, minutes, seconds;
int result;
gchar *new_date = NULL;
result = sscanf (date, "%d:%d:%d %d:%d:%d",
&year, &month, &day, &hour, &minutes, &seconds);
if (result < 3 || !g_date_valid_dmy (day, month, year)) {
return NULL;
} else {
gchar tmp_date[DATE_BUF_SIZE];
gsize dlen;
struct tm tm;
memset (&tm, '\0', sizeof (tm));
tm.tm_mday = day;
tm.tm_mon = month-1;
tm.tm_year = year-1900;
// Calculate tm.tm_wday
_calculate_wday_yday (&tm);
tm.tm_sec = result < 6 ? 0 : seconds;
tm.tm_min = result < 5 ? 0 : minutes;
tm.tm_hour = result < 4 ? 0 : hour;
dlen = strftime (tmp_date, DATE_BUF_SIZE * sizeof(gchar), format, &tm);
if (dlen == 0)
return NULL;
else
new_date = g_strndup (tmp_date, dlen);
}
return new_date;
}
#endif /* HAVE_STRPTIME */
#pragma GCC diagnostic pop
/**
* eom_exif_util_format_date:
* @date: a date string following Exif specifications
*
* Takes a date string formatted after Exif specifications and generates a
* more readable, possibly localized, string out of it.
*
* Returns: a newly allocated date string formatted according to the
* current locale.
*/
gchar *
eom_exif_util_format_date (const gchar *date)
{
gchar *new_date;
#ifdef HAVE_STRPTIME
/* A strftime-formatted string, to display the date the image was taken. */
new_date = eom_exif_util_format_date_with_strptime (date, _("%a, %d %B %Y %X"));
#else
new_date = eom_exif_util_format_date_by_hand (date, _("%a, %d %B %Y %X"));
#endif /* HAVE_STRPTIME */
return new_date;
}
void
eom_exif_util_set_label_text (GtkLabel *label,
EomExifData *exif_data,
gint tag_id)
{
gchar exif_buffer[512];
const gchar *buf_ptr;
gchar *label_text = NULL;
g_return_if_fail (GTK_IS_LABEL (label));
if (exif_data) {
buf_ptr = eom_exif_data_get_value (exif_data, tag_id,
exif_buffer, 512);
if (tag_id == EXIF_TAG_DATE_TIME_ORIGINAL && buf_ptr)
label_text = eom_exif_util_format_date (buf_ptr);
else
label_text = eom_util_make_valid_utf8 (buf_ptr);
}
gtk_label_set_text (label, label_text);
g_free (label_text);
}
void
eom_exif_util_format_datetime_label (GtkLabel *label, EomExifData *exif_data,
gint tag_id, const gchar *format)
{
gchar exif_buffer[512];
const gchar *buf_ptr;
gchar *label_text = NULL;
g_return_if_fail (GTK_IS_LABEL (label));
g_warn_if_fail (tag_id == EXIF_TAG_DATE_TIME_ORIGINAL);
if (exif_data) {
buf_ptr = eom_exif_data_get_value (exif_data, tag_id,
exif_buffer, 512);
if (tag_id == EXIF_TAG_DATE_TIME_ORIGINAL && buf_ptr)
#ifdef HAVE_STRPTIME
label_text = eom_exif_util_format_date_with_strptime (buf_ptr, format);
#else
label_text = eom_exif_util_format_date_by_hand (buf_ptr, format);
#endif /* HAVE_STRPTIME */
}
gtk_label_set_text (label, label_text);
g_free (label_text);
}
void
eom_exif_util_set_focal_length_label_text (GtkLabel *label,
EomExifData *exif_data)
{
ExifEntry *entry = NULL, *entry35mm = NULL;
ExifByteOrder byte_order;
gfloat f_val = 0.0;
gchar *fl_text = NULL,*fl35_text = NULL;
/* If no ExifData is supplied the label will be
* cleared later as fl35_text is NULL. */
if (exif_data != NULL) {
entry = exif_data_get_entry (exif_data, EXIF_TAG_FOCAL_LENGTH);
entry35mm = exif_data_get_entry (exif_data,
EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM);
byte_order = exif_data_get_byte_order (exif_data);
}
if (entry && G_LIKELY (entry->format == EXIF_FORMAT_RATIONAL)) {
ExifRational value;
/* Decode value by hand as libexif is not necessarily returning
* it in the format we want it to be.
*/
value = exif_get_rational (entry->data, byte_order);
/* Guard against div by zero */
if (G_LIKELY(value.denominator != 0))
f_val = (gfloat)value.numerator/
(gfloat)value.denominator;
/* TRANSLATORS: This is the actual focal length used when
the image was taken.*/
fl_text = g_strdup_printf (_("%.1f (lens)"), f_val);
}
if (entry35mm && G_LIKELY (entry35mm->format == EXIF_FORMAT_SHORT)) {
ExifShort s_val;
s_val = exif_get_short (entry35mm->data, byte_order);
/* Print as float to get a similar look as above. */
/* TRANSLATORS: This is the equivalent focal length assuming
a 35mm film camera. */
fl35_text = g_strdup_printf(_("%.1f (35mm film)"),(float)s_val);
}
if (fl_text) {
if (fl35_text) {
gchar *merged_txt;
merged_txt = g_strconcat (fl35_text,", ", fl_text, NULL);
gtk_label_set_text (label, merged_txt);
g_free (merged_txt);
} else {
gtk_label_set_text (label, fl_text);
}
} else {
/* This will also clear the label if no ExifData was supplied */
gtk_label_set_text (label, fl35_text);
}
g_free (fl35_text);
g_free (fl_text);
}
/**
* eom_exif_data_get_value:
* @exif_data: pointer to an <structname>ExifData</structname> struct
* @tag_id: the requested tag's id. See <filename>exif-tag.h</filename>
* from the libexif package for possible values (e.g. %EXIF_TAG_EXPOSURE_MODE).
* @buffer: a pre-allocated output buffer
* @buf_size: size of @buffer
*
* Convenience function to extract a string representation of an Exif tag
* directly from an <structname>ExifData</structname> struct. The string is
* written into @buffer as far as @buf_size permits.
*
* Returns: a pointer to @buffer.
*/
const gchar *
eom_exif_data_get_value (EomExifData *exif_data, gint tag_id, gchar *buffer, guint buf_size)
{
ExifEntry *exif_entry;
const gchar *exif_value;
exif_entry = exif_data_get_entry (exif_data, tag_id);
buffer[0] = 0;
exif_value = exif_entry_get_value (exif_entry, buffer, buf_size);
return exif_value;
}
EomExifData *
eom_exif_data_copy (EomExifData *data)
{
exif_data_ref (data);
return data;
}
void
eom_exif_data_free (EomExifData *data)
{
exif_data_unref (data);
}
|