ISAAC  0.2.11
Flight software for the ISAAC project, adding functionality to the Astrobee robot, operating inside the International Space Station.
All Classes Functions Variables Pages
TinyEXIF.h
1 /*
2  TinyEXIF.h -- A simple ISO C++ library to parse basic EXIF and XMP
3  information from a JPEG file.
4 
5  Copyright (c) 2015-2017 Seacave
6  cdc.seacave@gmail.com
7  All rights reserved.
8 
9  Based on the easyexif library (2013 version)
10  https://github.com/mayanklahiri/easyexif
11  of Mayank Lahiri (mlahiri@gmail.com).
12 
13  Redistribution and use in source and binary forms, with or without
14  modification, are permitted provided that the following conditions are met:
15 
16  - Redistributions of source code must retain the above copyright notice,
17  this list of conditions and the following disclaimer.
18  - Redistributions in binary form must reproduce the above copyright notice,
19  this list of conditions and the following disclaimer in the documentation
20  and/or other materials provided with the distribution.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS
23  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25  NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifndef __TINYEXIF_H__
35 #define __TINYEXIF_H__
36 
37 #include <string>
38 #include <vector>
39 
40 #define TINYEXIF_MAJOR_VERSION 1
41 #define TINYEXIF_MINOR_VERSION 0
42 #define TINYEXIF_PATCH_VERSION 1
43 
44 #ifdef _MSC_VER
45 # ifdef TINYEXIF_EXPORT
46 # define TINYEXIF_LIB __declspec(dllexport)
47 # elif defined(TINYEXIF_IMPORT)
48 # define TINYEXIF_LIB __declspec(dllimport)
49 # else
50 # define TINYEXIF_LIB
51 # endif
52 #elif __GNUC__ >= 4
53 # define TINYEXIF_LIB __attribute__((visibility("default")))
54 #else
55 # define TINYEXIF_LIB
56 #endif
57 
58 namespace TinyEXIF {
59 
60 enum ErrorCode {
61  PARSE_SUCCESS = 0, // Parse EXIF and/or XMP was successful
62  PARSE_INVALID_JPEG = 1, // No JPEG markers found in buffer, possibly invalid JPEG file
63  PARSE_UNKNOWN_BYTEALIGN = 2, // Byte alignment specified in EXIF file was unknown (neither Motorola nor Intel)
64  PARSE_ABSENT_DATA = 3, // No EXIF and/or XMP data found in JPEG file
65  PARSE_CORRUPT_DATA = 4, // EXIF and/or XMP header was found, but data was corrupted
66 };
67 
68 enum FieldCode {
69  FIELD_NA = 0, // No EXIF or XMP data
70  FIELD_EXIF = (1 << 0), // EXIF data available
71  FIELD_XMP = (1 << 1), // XMP data available
72  FIELD_ALL = FIELD_EXIF|FIELD_XMP
73 };
74 
75 class EntryParser;
76 
77 //
78 // Interface class responsible for fetching stream data to be parsed
79 //
80 class TINYEXIF_LIB EXIFStream {
81 public:
82  virtual ~EXIFStream() {}
83 
84  // Check the state of the stream.
85  virtual bool IsValid() const = 0;
86 
87  // Return the pointer to the beginning of the desired size buffer
88  // following current buffer position.
89  virtual const uint8_t* GetBuffer(unsigned desiredLength) = 0;
90 
91  // Advance current buffer position with the desired size;
92  // return false if stream ends in less than the desired size.
93  virtual bool SkipBuffer(unsigned desiredLength) = 0;
94 };
95 
96 //
97 // Class responsible for storing and parsing EXIF & XMP metadata from a JPEG stream
98 //
99 class TINYEXIF_LIB EXIFInfo {
100 public:
101  EXIFInfo();
102  EXIFInfo(EXIFStream& stream);
103  EXIFInfo(const uint8_t* data, unsigned length);
104 
105  // Parsing function for an entire JPEG image stream.
106  //
107  // PARAM 'stream': Interface to fetch JPEG image stream.
108  // PARAM 'data': A pointer to a JPEG image.
109  // PARAM 'length': The length of the JPEG image.
110  // RETURN: PARSE_SUCCESS (0) on success with 'result' filled out
111  // error code otherwise, as defined by the PARSE_* macros
112  int parseFrom(EXIFStream& stream);
113  int parseFrom(const uint8_t* data, unsigned length);
114 
115  // Parsing function for an EXIF segment. This is used internally by parseFrom()
116  // but can be called for special cases where only the EXIF section is
117  // available (i.e., a blob starting with the bytes "Exif\0\0").
118  int parseFromEXIFSegment(const uint8_t* buf, unsigned len);
119 
120  // Parsing function for an XMP segment. This is used internally by parseFrom()
121  // but can be called for special cases where only the XMP section is
122  // available (i.e., a blob starting with the bytes "http://ns.adobe.com/xap/1.0/\0").
123  int parseFromXMPSegment(const uint8_t* buf, unsigned len);
124  int parseFromXMPSegmentXML(const char* szXML, unsigned len);
125 
126  // Set all data members to default values.
127  // Should be called before parsing a new stream.
128  void clear();
129 
130 private:
131  // Parse tag as Image IFD.
132  void parseIFDImage(EntryParser&, unsigned&, unsigned&);
133  // Parse tag as Exif IFD.
134  void parseIFDExif(EntryParser&);
135  // Parse tag as GPS IFD.
136  void parseIFDGPS(EntryParser&);
137  // Parse tag as MakerNote IFD.
138  void parseIFDMakerNote(EntryParser&);
139 
140 public:
141  // Data fields
142  uint32_t Fields; // Store if EXIF and/or XMP data fields are available
143  uint32_t ImageWidth; // Image width reported in EXIF data
144  uint32_t ImageHeight; // Image height reported in EXIF data
145  uint32_t RelatedImageWidth; // Original image width reported in EXIF data
146  uint32_t RelatedImageHeight; // Original image height reported in EXIF data
147  std::string ImageDescription; // Image description
148  std::string Make; // Camera manufacturer's name
149  std::string Model; // Camera model
150  std::string SerialNumber; // Serial number of the body of the camera
151  uint16_t Orientation; // Image orientation, start of data corresponds to
152  // 0: unspecified in EXIF data
153  // 1: upper left of image
154  // 3: lower right of image
155  // 6: upper right of image
156  // 8: lower left of image
157  // 9: undefined
158  double XResolution; // Number of pixels per ResolutionUnit in the ImageWidth direction
159  double YResolution; // Number of pixels per ResolutionUnit in the ImageLength direction
160  uint16_t ResolutionUnit; // Unit of measurement for XResolution and YResolution
161  // 1: no absolute unit of measurement. Used for images that may have a non-square aspect ratio, but no meaningful absolute dimensions
162  // 2: inch
163  // 3: centimeter
164  uint16_t BitsPerSample; // Number of bits per component
165  std::string Software; // Software used
166  std::string DateTime; // File change date and time
167  std::string DateTimeOriginal; // Original file date and time (may not exist)
168  std::string DateTimeDigitized; // Digitization date and time (may not exist)
169  std::string SubSecTimeOriginal; // Sub-second time that original picture was taken
170  std::string Copyright; // File copyright information
171  double ExposureTime; // Exposure time in seconds
172  double FNumber; // F/stop
173  uint16_t ExposureProgram; // Exposure program
174  // 0: not defined
175  // 1: manual
176  // 2: normal program
177  // 3: aperture priority
178  // 4: shutter priority
179  // 5: creative program
180  // 6: action program
181  // 7: portrait mode
182  // 8: landscape mode
183  uint16_t ISOSpeedRatings; // ISO speed
184  double ShutterSpeedValue; // Shutter speed (reciprocal of exposure time)
185  double ApertureValue; // The lens aperture
186  double BrightnessValue; // The value of brightness
187  double ExposureBiasValue; // Exposure bias value in EV
188  double SubjectDistance; // Distance to focus point in meters
189  double FocalLength; // Focal length of lens in millimeters
190  uint16_t Flash; // Flash info
191  // Flash used (Flash&1)
192  // 0: no flash, >0: flash used
193  // Flash returned light status ((Flash & 6) >> 1)
194  // 0: no strobe return detection function
195  // 1: reserved
196  // 2: strobe return light not detected
197  // 3: strobe return light detected
198  // Flash mode ((Flash & 24) >> 3)
199  // 0: unknown
200  // 1: compulsory flash firing
201  // 2: compulsory flash suppression
202  // 3: auto mode
203  // Flash function ((Flash & 32) >> 5)
204  // 0: flash function present, >0: no flash function
205  // Flash red-eye ((Flash & 64) >> 6)
206  // 0: no red-eye reduction mode or unknown, >0: red-eye reduction supported
207  uint16_t MeteringMode; // Metering mode
208  // 0: unknown
209  // 1: average
210  // 2: center weighted average
211  // 3: spot
212  // 4: multi-spot
213  // 5: pattern
214  // 6: partial
215  uint16_t LightSource; // Kind of light source
216  // 0: unknown
217  // 1: daylight
218  // 2: fluorescent
219  // 3: tungsten (incandescent light)
220  // 4: flash
221  // 9: fine weather
222  // 10: cloudy weather
223  // 11: shade
224  // 12: daylight fluorescent (D 5700 - 7100K)
225  // 13: day white fluorescent (N 4600 - 5400K)
226  // 14: cool white fluorescent (W 3900 - 4500K)
227  // 15: white fluorescent (WW 3200 - 3700K)
228  // 17: standard light A
229  // 18: standard light B
230  // 19: standard light C
231  // 20: D55
232  // 21: D65
233  // 22: D75
234  // 23: D50
235  // 24: ISO studio tungsten
236  uint16_t ProjectionType; // Projection type
237  // 0: unknown projection
238  // 1: perspective projection
239  // 2: equirectangular/spherical projection
240  std::vector<uint16_t> SubjectArea; // Location and area of the main subject in the overall scene expressed in relation to the upper left as origin, prior to rotation
241  // 0: unknown
242  // 2: location of the main subject as coordinates (first value is the X coordinate and second is the Y coordinate)
243  // 3: area of the main subject as a circle (first value is the center X coordinate, second is the center Y coordinate, and third is the diameter)
244  // 4: area of the main subject as a rectangle (first value is the center X coordinate, second is the center Y coordinate, third is the width of the area, and fourth is the height of the area)
245  struct TINYEXIF_LIB Calibration_t { // Camera calibration information
246  double FocalLength; // Focal length (pixels)
247  double OpticalCenterX; // Principal point X (pixels)
248  double OpticalCenterY; // Principal point Y (pixels)
249  } Calibration;
250  struct TINYEXIF_LIB LensInfo_t { // Lens information
251  double FStopMin; // Min aperture (f-stop)
252  double FStopMax; // Max aperture (f-stop)
253  double FocalLengthMin; // Min focal length (mm)
254  double FocalLengthMax; // Max focal length (mm)
255  double DigitalZoomRatio; // Digital zoom ratio when the image was shot
256  double FocalLengthIn35mm; // Focal length in 35mm film
257  double FocalPlaneXResolution; // Number of pixels in the image width (X) direction per FocalPlaneResolutionUnit on the camera focal plane (may not exist)
258  double FocalPlaneYResolution; // Number of pixels in the image width (Y) direction per FocalPlaneResolutionUnit on the camera focal plane (may not exist)
259  uint16_t FocalPlaneResolutionUnit;// Unit for measuring FocalPlaneXResolution and FocalPlaneYResolution (may not exist)
260  // 0: unspecified in EXIF data
261  // 1: no absolute unit of measurement
262  // 2: inch
263  // 3: centimeter
264  std::string Make; // Lens manufacturer
265  std::string Model; // Lens model
266  } LensInfo;
267  struct TINYEXIF_LIB Geolocation_t { // GPS information embedded in file
268  double Latitude; // Image latitude expressed as decimal
269  double Longitude; // Image longitude expressed as decimal
270  double Altitude; // Altitude in meters, relative to sea level
271  int8_t AltitudeRef; // 0: above sea level, -1: below sea level
272  double RelativeAltitude; // Relative altitude in meters
273  double RollDegree; // Flight roll in degrees
274  double PitchDegree; // Flight pitch in degrees
275  double YawDegree; // Flight yaw in degrees
276  double SpeedX; // Flight speed on X in meters/second
277  double SpeedY; // Flight speed on Y in meters/second
278  double SpeedZ; // Flight speed on Z in meters/second
279  double AccuracyXY; // GPS accuracy on XY in meters
280  double AccuracyZ; // GPS accuracy on Z in meters
281  double GPSDOP; // GPS DOP (data degree of precision)
282  uint16_t GPSDifferential; // Differential correction applied to the GPS receiver (may not exist)
283  // 0: measurement without differential correction
284  // 1: differential correction applied
285  std::string GPSMapDatum; // Geodetic survey data (may not exist)
286  std::string GPSTimeStamp; // Time as UTC (Coordinated Universal Time) (may not exist)
287  std::string GPSDateStamp; // A character string recording date and time information relative to UTC (Coordinated Universal Time) YYYY:MM:DD (may not exist)
288  struct Coord_t {
289  double degrees;
290  double minutes;
291  double seconds;
292  uint8_t direction;
293  } LatComponents, LonComponents; // Latitude/Longitude expressed in deg/min/sec
294  void parseCoords(); // Convert Latitude/Longitude from deg/min/sec to decimal
295  bool hasLatLon() const; // Return true if (lat,lon) is available
296  bool hasAltitude() const; // Return true if (alt) is available
297  bool hasRelativeAltitude()const;// Return true if (rel_alt) is available
298  bool hasOrientation() const; // Return true if (roll,yaw,pitch) is available
299  bool hasSpeed() const; // Return true if (speedX,speedY,speedZ) is available
300  } GeoLocation;
301 };
302 
303 } // namespace TinyEXIF
304 
305 #endif // __TINYEXIF_H__
TinyEXIF::EXIFInfo
Definition: TinyEXIF.h:99
TinyEXIF::EXIFInfo::Calibration_t
Definition: TinyEXIF.h:245
TinyEXIF::EXIFStream
Definition: TinyEXIF.h:80
TinyEXIF::EXIFInfo::Geolocation_t
Definition: TinyEXIF.h:267
TinyEXIF::EXIFInfo::LensInfo_t
Definition: TinyEXIF.h:250
TinyEXIF::EXIFInfo::Geolocation_t::Coord_t
Definition: TinyEXIF.h:288
TinyEXIF::EntryParser
Definition: TinyEXIF.cc:161