-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif.cpp
109 lines (97 loc) · 2.41 KB
/
exif.cpp
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
/*
* Copyright 2015 by Philip N. Garner
*
* See the file COPYING for the licence associated with this software.
*
* Author(s):
* Phil Garner, September 2015
*/
#include "exif.h"
EXIF::EXIF(var iPath)
{
mData = 0;
load(iPath);
}
EXIF::EXIF(const unsigned char *iData, unsigned int iSize)
{
mData = 0;
load(iData, iSize);
}
EXIF::~EXIF()
{
if (mData)
exif_data_free(mData);
mData = 0;
}
void EXIF::load(var iPath)
{
if (mData)
exif_data_free(mData);
mData = exif_data_new_from_file(iPath.str());
if (mData)
// I'm not sure this is necessary, but it feels good
exif_data_fix(mData);
}
void EXIF::load(const unsigned char *iData, unsigned int iSize)
{
if (mData)
exif_data_free(mData);
mData = exif_data_new_from_data(iData, iSize);
if (mData)
// Ditto...
exif_data_fix(mData);
}
void EXIF::dump()
{
if (valid())
exif_data_dump(mData);
}
var EXIF::entry(ExifTag iTag)
{
if (!valid())
throw lube::error("EXIF class invalid");
var r;
ExifEntry* e = exif_data_get_entry(mData, iTag);
if (!e)
return r;
// Debuggin'
//exif_entry_dump(e, 2);
switch (e->format)
{
case EXIF_FORMAT_ASCII:
r = var(e->components, (const char*)e->data);
break;
default:
throw lube::error("EXIF::entry: Unknown format");
}
return r;
}
/**
* Helper function to return the date as an array of strings
*/
var EXIF::date()
{
if (!valid())
throw lube::error("EXIF class invalid");
// Try for the DATE_TIME EXIF tag, then DATE_TIME_ORIGINAL
var r = entry(EXIF_TAG_DATE_TIME);
if (!r)
r = entry(EXIF_TAG_DATE_TIME_ORIGINAL);
if (!r)
return lube::nil;
// Parse it to get at least the ISO date and the month
// iPhone has format 2015:01:01 17:31:46
// Old Nokia has 2010:06:09 12:22:02
// Old Samsung has 2012-12-02 04:08:18
// Dropbox files are 2012-12-02 04.08.18
// ISO format is 2012-12-02T04:08:18Z
// or 20121202T040818Z
// So we need to get rid of at least ':', '-' and '.', and the ISO tags.
// Actually, geeqie fails on that Samsung format
r.replace("[-:.TZ]", " ");
var s = r.strip().split(" ");
if (s[0].size() != 4) return lube::nil;
if (s[1].size() != 2) return lube::nil;
if (s[2].size() != 2) return lube::nil;
return s;
}