1 /**************************************************************************
2   exif.h  -- A simple ISO C++ library to parse basic EXIF
3              information from a JPEG file.
4 
5   Based on the description of the EXIF file format at:
6   -- http://park2.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
7   -- http://www.media.mit.edu/pia/Research/deepview/exif.html
8   -- http://www.exif.org/Exif2-2.PDF
9 
10   Copyright (c) 2010-2015 Mayank Lahiri
11   mlahiri@gmail.com
12   All rights reserved.
13 
14   VERSION HISTORY:
15   ================
16 
17   2.2: Release December 2014
18        --
19 
20   2.1: Released July 2013
21        -- fixed a bug where JPEGs without an EXIF SubIFD would not be parsed
22        -- fixed a bug in parsing GPS coordinate seconds
23        -- fixed makefile bug
24        -- added two pathological test images from Matt Galloway
25           http://www.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/
26        -- split main parsing routine for easier integration into Firefox
27 
28   2.0: Released February 2013
29        -- complete rewrite
30        -- no new/delete
31        -- added GPS support
32 
33   1.0: Released 2010
34 
35   Redistribution and use in source and binary forms, with or without
36   modification, are permitted provided that the following conditions are met:
37 
38   -- Redistributions of source code must retain the above copyright notice,
39      this list of conditions and the following disclaimer.
40   -- Redistributions in binary form must reproduce the above copyright notice,
41      this list of conditions and the following disclaimer in the documentation
42    and/or other materials provided with the distribution.
43 
44    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
45    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
47    NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
48    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
51    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
53    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55 #ifndef __EXIF_H
56 #define __EXIF_H
57 
58 #include <string>
59 
60 namespace easyexif {
61 
62 //
63 // Class responsible for storing and parsing EXIF information from a JPEG blob
64 //
65 class EXIFInfo {
66  public:
67   // Parsing function for an entire JPEG image buffer.
68   //
69   // PARAM 'data': A pointer to a JPEG image.
70   // PARAM 'length': The length of the JPEG image.
71   // RETURN:  PARSE_EXIF_SUCCESS (0) on succes with 'result' filled out
72   //          error code otherwise, as defined by the PARSE_EXIF_ERROR_* macros
73   int parseFrom(const unsigned char *data, unsigned length);
74   int parseFrom(const std::string &data);
75 
76   // Parsing function for an EXIF segment. This is used internally by parseFrom()
77   // but can be called for special cases where only the EXIF section is
78   // available (i.e., a blob starting with the bytes "Exif\0\0").
79   int parseFromEXIFSegment(const unsigned char *buf, unsigned len);
80 
81   // Set all data members to default values.
82   void clear();
83 
84   // Data fields filled out by parseFrom()
85   char ByteAlign;                   // 0 = Motorola byte alignment, 1 = Intel
86   std::string ImageDescription;     // Image description
87   std::string Make;                 // Camera manufacturer's name
88   std::string Model;                // Camera model
89   unsigned short Orientation;       // Image orientation, start of data corresponds to
90                                     // 0: unspecified in EXIF data
91                                     // 1: upper left of image
92                                     // 3: lower right of image
93                                     // 6: upper right of image
94                                     // 8: lower left of image
95                                     // 9: undefined
96   unsigned short BitsPerSample;     // Number of bits per component
97   std::string Software;             // Software used
98   std::string DateTime;             // File change date and time
99   std::string DateTimeOriginal;     // Original file date and time (may not exist)
100   std::string DateTimeDigitized;    // Digitization date and time (may not exist)
101   std::string SubSecTimeOriginal;   // Sub-second time that original picture was taken
102   std::string Copyright;            // File copyright information
103   double ExposureTime;              // Exposure time in seconds
104   double FNumber;                   // F/stop
105   unsigned short ISOSpeedRatings;   // ISO speed
106   double ShutterSpeedValue;         // Shutter speed (reciprocal of exposure time)
107   double ExposureBiasValue;         // Exposure bias value in EV
108   double SubjectDistance;           // Distance to focus point in meters
109   double FocalLength;               // Focal length of lens in millimeters
110   unsigned short FocalLengthIn35mm; // Focal length in 35mm film
111   char Flash;                       // 0 = no flash, 1 = flash used
112   unsigned short MeteringMode;      // Metering mode
113                                     // 1: average
114                                     // 2: center weighted average
115                                     // 3: spot
116                                     // 4: multi-spot
117                                     // 5: multi-segment
118   unsigned ImageWidth;              // Image width reported in EXIF data
119   unsigned ImageHeight;             // Image height reported in EXIF data
120   struct Geolocation_t {            // GPS information embedded in file
121     double Latitude;                  // Image latitude expressed as decimal
122     double Longitude;                 // Image longitude expressed as decimal
123     double Altitude;                  // Altitude in meters, relative to sea level
124     char AltitudeRef;                 // 0 = above sea level, -1 = below sea level
125     double DOP;                       // GPS degree of precision (DOP)
126     struct Coord_t {
127       double degrees;
128       double minutes;
129       double seconds;
130       char direction;
131     } LatComponents, LonComponents;   // Latitude, Longitude expressed in deg/min/sec
132   } GeoLocation;
133   struct LensInfo_t {               // Lens information
134     double FStopMin;                // Min aperture (f-stop)
135     double FStopMax;                // Max aperture (f-stop)
136     double FocalLengthMin;          // Min focal length (mm)
137     double FocalLengthMax;          // Max focal length (mm)
138     double FocalPlaneXResolution;   // Focal plane X-resolution
139     double FocalPlaneYResolution;   // Focal plane Y-resolution
140     std::string Make;               // Lens manufacturer
141     std::string Model;              // Lens model
142   } LensInfo;
143 
144 
EXIFInfo()145   EXIFInfo() {
146     clear();
147   }
148 };
149 
150 }
151 
152 // Parse was successful
153 #define PARSE_EXIF_SUCCESS                    0
154 // No JPEG markers found in buffer, possibly invalid JPEG file
155 #define PARSE_EXIF_ERROR_NO_JPEG              1982
156 // No EXIF header found in JPEG file.
157 #define PARSE_EXIF_ERROR_NO_EXIF              1983
158 // Byte alignment specified in EXIF file was unknown (not Motorola or Intel).
159 #define PARSE_EXIF_ERROR_UNKNOWN_BYTEALIGN    1984
160 // EXIF header was found, but data was corrupted.
161 #define PARSE_EXIF_ERROR_CORRUPT              1985
162 
163 #endif
164