aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/exiv2_extractor.cc
blob: c3bd91ae3503e099437775629e4ece5bad1593af (plain) (blame)
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
// ***************************************************************** -*- C++ -*-
/*
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/*!
  @file    exiv2_extractor.cc
  @brief   libextractor plugin for Exif using exiv2
  @version $Rev$
  @author  Andreas Huggel (ahu)
  <a href="mailto:ahuggel@gmx.net">ahuggel@gmx.net</a>
  @date    30-Jun-05, ahu: created; 15-Dec-09, cg: updated
*/

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <math.h>

#include "platform.h"
#include "extractor.h"

#include <exiv2/exif.hpp>
#include <exiv2/image.hpp>
#include <exiv2/futils.hpp>

#define SUPPRESS_WARNINGS 1

#define ADD(s, type) do { if (0!=proc(proc_cls, "exiv2", type, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1)) return 1; } while (0)

static int
addExiv2Tag(const Exiv2::ExifData& exifData,
	    const std::string& key,
	    enum EXTRACTOR_MetaType type,
	    EXTRACTOR_MetaDataProcessor proc,
	    void *proc_cls)
{
  const char * str;
  
  Exiv2::ExifKey ek(key);
  Exiv2::ExifData::const_iterator md = exifData.findKey(ek);
  if (md != exifData.end()) {
    std::string ccstr = Exiv2::toString(*md);
    str = ccstr.c_str();
    while ( (strlen(str) > 0) && isspace((unsigned char) str[0])) str++;
    if (strlen(str) > 0)
      ADD (str, type);
    md++;
  }
  return 0;
}


static int
addIptcData(const Exiv2::IptcData& iptcData,
	    const std::string& key,
	    enum EXTRACTOR_MetaType type,
	    EXTRACTOR_MetaDataProcessor proc,
	    void *proc_cls)
{
  const char * str;
  
  Exiv2::IptcKey ek(key);
  Exiv2::IptcData::const_iterator md = iptcData.findKey(ek);
  while (md != iptcData.end()) 
    {
      if (0 != strcmp (Exiv2::toString(md->key()).c_str(), key.c_str()))
	  break;
      std::string ccstr = Exiv2::toString(*md);
      str = ccstr.c_str();
      while ( (strlen(str) > 0) && isspace( (unsigned char) str[0])) str++;
      if (strlen(str) > 0)
	ADD (str, type);
      md++;
    }
  return 0;
}


static int
addXmpData(const Exiv2::XmpData& xmpData,
	   const std::string& key,
	   enum EXTRACTOR_MetaType type,
	   EXTRACTOR_MetaDataProcessor proc,
	   void *proc_cls)
{
  const char * str;
  
  Exiv2::XmpKey ek(key);
  Exiv2::XmpData::const_iterator md = xmpData.findKey(ek);
  while (md != xmpData.end()) 
    {
      if (0 != strcmp (Exiv2::toString(md->key()).c_str(), key.c_str()))
	break;
      std::string ccstr = Exiv2::toString(*md);
      str = ccstr.c_str();
      while ( (strlen(str) > 0) && isspace( (unsigned char) str[0])) str++;
      if (strlen(str) > 0)
	ADD (str, type);
      md++;
    }
  return 0;
}

#define ADDEXIV(s,t) do { if (0 != addExiv2Tag (exifData, s, t, proc, proc_cls)) return 1; } while (0)
#define ADDIPTC(s,t) do { if (0 != addIptcData (iptcData, s, t, proc, proc_cls)) return 1; } while (0)
#define ADDXMP(s,t)  do { if (0 != addXmpData  (xmpData,  s, t, proc, proc_cls)) return 1; } while (0)


extern "C" {
  
  int 
  EXTRACTOR_exiv2_extract (const char *data,
			   size_t size,
			   EXTRACTOR_MetaDataProcessor proc,
			   void *proc_cls,
			   const char *options)
  {
    try 
      {	    
	Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open( (Exiv2::byte*) data,
								 size);
	if (image.get() == 0)
	  return 0;
	image->readMetadata();
	Exiv2::ExifData &exifData = image->exifData();
	if (!exifData.empty()) 
	  {	   		
	    Exiv2::ExifData::const_iterator md;
	    
	    /* FIXME: this should be a loop over data,
	       not a looooong block of code */
	    ADDEXIV ("Exif.Image.Copyright", EXTRACTOR_METATYPE_COPYRIGHT);
	    ADDEXIV ("Exif.Photo.UserComment", EXTRACTOR_METATYPE_COMMENT);
	    ADDEXIV ("Exif.GPSInfo.GPSLatitudeRef", EXTRACTOR_METATYPE_GPS_LATITUDE_REF);
	    ADDEXIV ("Exif.GPSInfo.GPSLatitude", EXTRACTOR_METATYPE_GPS_LATITUDE);
	    ADDEXIV ("Exif.GPSInfo.GPSLongitudeRef", EXTRACTOR_METATYPE_GPS_LONGITUDE_REF);
	    ADDEXIV ("Exif.GPSInfo.GPSLongitude", EXTRACTOR_METATYPE_GPS_LONGITUDE);
	    ADDEXIV ("Exif.Image.Make", EXTRACTOR_METATYPE_CAMERA_MAKE);    
	    ADDEXIV ("Exif.Image.Model", EXTRACTOR_METATYPE_CAMERA_MODEL);
	    ADDEXIV ("Exif.Image.Orientation", EXTRACTOR_METATYPE_ORIENTATION);
	    ADDEXIV ("Exif.Photo.DateTimeOriginal", EXTRACTOR_METATYPE_CREATION_DATE);
	    ADDEXIV ("Exif.Photo.ExposureBiasValue", EXTRACTOR_METATYPE_EXPOSURE_BIAS);
	    ADDEXIV ("Exif.Photo.Flash", EXTRACTOR_METATYPE_FLASH);
	    ADDEXIV ("Exif.CanonSi.FlashBias", EXTRACTOR_METATYPE_FLASH_BIAS);
	    ADDEXIV ("Exif.Panasonic.FlashBias", EXTRACTOR_METATYPE_FLASH_BIAS);
	    ADDEXIV ("Exif.Olympus.FlashBias", EXTRACTOR_METATYPE_FLASH_BIAS);
	    ADDEXIV ("Exif.Photo.FocalLength", EXTRACTOR_METATYPE_FOCAL_LENGTH);
	    ADDEXIV ("Exif.Photo.FocalLengthIn35mmFilm", EXTRACTOR_METATYPE_FOCAL_LENGTH_35MM);
	    ADDEXIV ("Exif.Photo.ISOSpeedRatings", EXTRACTOR_METATYPE_ISO_SPEED);
	    ADDEXIV ("Exif.CanonSi.ISOSpeed", EXTRACTOR_METATYPE_ISO_SPEED);
	    ADDEXIV ("Exif.Nikon1.ISOSpeed", EXTRACTOR_METATYPE_ISO_SPEED);
	    ADDEXIV ("Exif.Nikon2.ISOSpeed", EXTRACTOR_METATYPE_ISO_SPEED);
	    ADDEXIV ("Exif.Nikon3.ISOSpeed", EXTRACTOR_METATYPE_ISO_SPEED);
	    ADDEXIV ("Exif.Photo.ExposureProgram", EXTRACTOR_METATYPE_EXPOSURE_MODE);
	    ADDEXIV ("Exif.CanonCs.ExposureProgram", EXTRACTOR_METATYPE_EXPOSURE_MODE);
	    ADDEXIV ("Exif.Photo.MeteringMode", EXTRACTOR_METATYPE_METERING_MODE);
	    ADDEXIV ("Exif.CanonCs.Macro", EXTRACTOR_METATYPE_MACRO_MODE);
	    ADDEXIV ("Exif.Fujifilm.Macro", EXTRACTOR_METATYPE_MACRO_MODE);
	    ADDEXIV ("Exif.Olympus.Macro", EXTRACTOR_METATYPE_MACRO_MODE);
	    ADDEXIV ("Exif.Panasonic.Macro", EXTRACTOR_METATYPE_MACRO_MODE);
	    ADDEXIV ("Exif.CanonCs.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Fujifilm.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Sigma.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Nikon1.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Nikon2.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Nikon3.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Olympus.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.Panasonic.Quality", EXTRACTOR_METATYPE_IMAGE_QUALITY);
	    ADDEXIV ("Exif.CanonSi.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Fujifilm.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Sigma.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Nikon1.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Nikon2.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Nikon3.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Olympus.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);
	    ADDEXIV ("Exif.Panasonic.WhiteBalance", EXTRACTOR_METATYPE_WHITE_BALANCE);

	    ADDEXIV ("Exif.Photo.FNumber", EXTRACTOR_METATYPE_APERTURE);
	    md = exifData.findKey(Exiv2::ExifKey("Exif.Photo.ApertureValue"));
	    if (md != exifData.end()) {
	      std::ostringstream os;
	      os << std::fixed << std::setprecision(1)
		 << "F" << exp(log(2.0) * md->toFloat() / 2);
	      ADD (os.str().c_str(), EXTRACTOR_METATYPE_APERTURE);
	    }
    
	    ADDEXIV ("Exif.Photo.ExposureTime", EXTRACTOR_METATYPE_EXPOSURE);
	    md = exifData.findKey(Exiv2::ExifKey("Exif.Photo.ShutterSpeedValue"));
	    if (md != exifData.end()) {
	      double tmp = exp(log(2.0) * md->toFloat()) + 0.5;
	      std::ostringstream os;
	      if (tmp > 1) {
		os << "1/" << static_cast<long>(tmp) << " s";
	      }
	      else {
		os << static_cast<long>(1/tmp) << " s";
	      }
	      ADD (os.str().c_str(), EXTRACTOR_METATYPE_EXPOSURE);
	    }

	    /* this can sometimes be wrong (corrupt exiv2 data?).
	       Either way, we should get the data directly from
	       the specific file format parser (i.e. jpeg, tiff). */
	    // Exif Resolution
	    unsigned long xdim = 0;
	    unsigned long ydim = 0;
	    md = exifData.findKey(Exiv2::ExifKey("Exif.Photo.PixelXDimension"));
	    if (md != exifData.end()) xdim = md->toLong();
	    md = exifData.findKey(Exiv2::ExifKey("Exif.Photo.PixelYDimension"));
	    if (md != exifData.end()) ydim = md->toLong();
	    if ( (xdim != 0) && (ydim != 0))
	      {
		std::ostringstream os;
		os << xdim << "x" << ydim;
		ADD (os.str().c_str(), EXTRACTOR_METATYPE_IMAGE_DIMENSIONS);
	      }	    
	  } 

	Exiv2::IptcData &iptcData = image->iptcData();
	if (! iptcData.empty()) {
	  ADDIPTC ("Iptc.Application2.Keywords", EXTRACTOR_METATYPE_KEYWORDS);
	  ADDIPTC ("Iptc.Application2.City", EXTRACTOR_METATYPE_LOCATION_CITY);
	  ADDIPTC ("Iptc.Application2.SubLocation", EXTRACTOR_METATYPE_LOCATION_SUBLOCATION);
	  ADDIPTC ("Iptc.Application2.CountryName", EXTRACTOR_METATYPE_LOCATION_COUNTRY);
	  ADDIPTC ("Xmp.photoshop.Country", EXTRACTOR_METATYPE_RATING);
	}

	Exiv2::XmpData &xmpData = image->xmpData();
	if (! xmpData.empty()) {
	  ADDXMP ("Xmp.photoshop.City", EXTRACTOR_METATYPE_LOCATION_CITY);
	  ADDXMP ("Xmp.xmp.Rating", EXTRACTOR_METATYPE_RATING);
	  ADDXMP ("Xmp.MicrosoftPhoto.Rating", EXTRACTOR_METATYPE_RATING);
	  ADDXMP ("Xmp.iptc.CountryCode", EXTRACTOR_METATYPE_LOCATION_COUNTRY_CODE);
	  ADDXMP ("Xmp.xmp.CreatorTool", EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE);
	  ADDXMP ("Xmp.lr.hierarchicalSubject", EXTRACTOR_METATYPE_SUBJECT);
	}	
      }
    catch (const Exiv2::AnyError& e) {
#ifndef SUPPRESS_WARNINGS
      std::cout << "Caught Exiv2 exception '" << e << "'\n";
#endif
    }
    
    return 0;
  }


}