Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/src/exif.hpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
44
#ifndef _OPENCV_EXIF_HPP_
45
#define _OPENCV_EXIF_HPP_
46
47
#include <cstdio>
48
#include <map>
49
#include <utility>
50
#include <algorithm>
51
#include <string>
52
#include <vector>
53
#include <iostream>
54
55
namespace cv
56
{
57
/**
58
* @brief Jpeg markers that can encounter in Jpeg file
59
*/
60
enum AppMarkerTypes
61
{
62
SOI = 0xD8, SOF0 = 0xC0, SOF2 = 0xC2, DHT = 0xC4,
63
DQT = 0xDB, DRI = 0xDD, SOS = 0xDA,
64
65
RST0 = 0xD0, RST1 = 0xD1, RST2 = 0xD2, RST3 = 0xD3,
66
RST4 = 0xD4, RST5 = 0xD5, RST6 = 0xD6, RST7 = 0xD7,
67
68
APP0 = 0xE0, APP1 = 0xE1, APP2 = 0xE2, APP3 = 0xE3,
69
APP4 = 0xE4, APP5 = 0xE5, APP6 = 0xE6, APP7 = 0xE7,
70
APP8 = 0xE8, APP9 = 0xE9, APP10 = 0xEA, APP11 = 0xEB,
71
APP12 = 0xEC, APP13 = 0xED, APP14 = 0xEE, APP15 = 0xEF,
72
73
COM = 0xFE, EOI = 0xD9
74
};
75
76
/**
77
* @brief Base Exif tags used by IFD0 (main image)
78
*/
79
enum ExifTagName
80
{
81
IMAGE_DESCRIPTION = 0x010E, ///< Image Description: ASCII string
82
MAKE = 0x010F, ///< Description of manufacturer: ASCII string
83
MODEL = 0x0110, ///< Description of camera model: ASCII string
84
ORIENTATION = 0x0112, ///< Orientation of the image: unsigned short
85
XRESOLUTION = 0x011A, ///< Resolution of the image across X axis: unsigned rational
86
YRESOLUTION = 0x011B, ///< Resolution of the image across Y axis: unsigned rational
87
RESOLUTION_UNIT = 0x0128, ///< Resolution units. '1' no-unit, '2' inch, '3' centimeter
88
SOFTWARE = 0x0131, ///< Shows firmware(internal software of digicam) version number
89
DATE_TIME = 0x0132, ///< Date/Time of image was last modified
90
WHITE_POINT = 0x013E, ///< Chromaticity of white point of the image
91
PRIMARY_CHROMATICIES = 0x013F, ///< Chromaticity of the primaries of the image
92
Y_CB_CR_COEFFICIENTS = 0x0211, ///< constant to translate an image from YCbCr to RGB format
93
Y_CB_CR_POSITIONING = 0x0213, ///< Chroma sample point of subsampling pixel array
94
REFERENCE_BLACK_WHITE = 0x0214, ///< Reference value of black point/white point
95
COPYRIGHT = 0x8298, ///< Copyright information
96
EXIF_OFFSET = 0x8769, ///< Offset to Exif Sub IFD
97
INVALID_TAG = 0xFFFF ///< Shows that the tag was not recognized
98
};
99
100
enum Endianess_t
101
{
102
INTEL = 0x49,
103
MOTO = 0x4D,
104
NONE = 0x00
105
};
106
107
typedef std::pair<uint32_t, uint32_t> u_rational_t;
108
109
/**
110
* @brief Entry which contains possible values for different exif tags
111
*/
112
struct ExifEntry_t
113
{
114
ExifEntry_t();
115
116
std::vector<u_rational_t> field_u_rational; ///< vector of rational fields
117
std::string field_str; ///< any kind of textual information
118
119
float field_float; ///< Currently is not used
120
double field_double; ///< Currently is not used
121
122
uint32_t field_u32; ///< Unsigned 32-bit value
123
int32_t field_s32; ///< Signed 32-bit value
124
125
uint16_t tag; ///< Tag number
126
127
uint16_t field_u16; ///< Unsigned 16-bit value
128
int16_t field_s16; ///< Signed 16-bit value
129
uint8_t field_u8; ///< Unsigned 8-bit value
130
int8_t field_s8; ///< Signed 8-bit value
131
};
132
133
/**
134
* @brief Picture orientation which may be taken from EXIF
135
* Orientation usually matters when the picture is taken by
136
* smartphone or other camera with orientation sensor support
137
* Corresponds to EXIF 2.3 Specification
138
*/
139
enum ImageOrientation
140
{
141
IMAGE_ORIENTATION_TL = 1, ///< Horizontal (normal)
142
IMAGE_ORIENTATION_TR = 2, ///< Mirrored horizontal
143
IMAGE_ORIENTATION_BR = 3, ///< Rotate 180
144
IMAGE_ORIENTATION_BL = 4, ///< Mirrored vertical
145
IMAGE_ORIENTATION_LT = 5, ///< Mirrored horizontal & rotate 270 CW
146
IMAGE_ORIENTATION_RT = 6, ///< Rotate 90 CW
147
IMAGE_ORIENTATION_RB = 7, ///< Mirrored horizontal & rotate 90 CW
148
IMAGE_ORIENTATION_LB = 8 ///< Rotate 270 CW
149
};
150
151
/**
152
* @brief Reading exif information from Jpeg file
153
*
154
* Usage example for getting the orientation of the image:
155
*
156
* @code
157
* ExifReader reader(fileName);
158
* if( reader.parse() )
159
* {
160
* int orientation = reader.getTag(Orientation).field_u16;
161
* }
162
* @endcode
163
*
164
*/
165
class ExifReader
166
{
167
public:
168
/**
169
* @brief ExifReader constructor. Constructs an object of exif reader
170
*
171
* @param [in]stream An istream to look for EXIF bytes from
172
*/
173
explicit ExifReader( std::istream& stream );
174
~ExifReader();
175
176
177
/**
178
* @brief Parse the file with exif info
179
*
180
* @return true if parsing was successful and exif information exists in JpegReader object
181
*/
182
bool parse();
183
184
/**
185
* @brief Get tag info by tag number
186
*
187
* @param [in] tag The tag number
188
* @return ExifEntru_t structure. Caller has to know what tag it calls in order to extract proper field from the structure ExifEntry_t
189
*/
190
ExifEntry_t getTag( const ExifTagName tag );
191
192
private:
193
std::istream& m_stream;
194
std::vector<unsigned char> m_data;
195
std::map<int, ExifEntry_t > m_exif;
196
Endianess_t m_format;
197
198
void parseExif();
199
bool checkTagMark() const;
200
201
size_t getFieldSize ();
202
size_t getNumDirEntry() const;
203
uint32_t getStartOffset() const;
204
uint16_t getExifTag( const size_t offset ) const;
205
uint16_t getU16( const size_t offset ) const;
206
uint32_t getU32( const size_t offset ) const;
207
uint16_t getOrientation( const size_t offset ) const;
208
uint16_t getResolutionUnit( const size_t offset ) const;
209
uint16_t getYCbCrPos( const size_t offset ) const;
210
211
Endianess_t getFormat() const;
212
213
ExifEntry_t parseExifEntry( const size_t offset );
214
215
u_rational_t getURational( const size_t offset ) const;
216
217
std::map<int, ExifEntry_t > getExif();
218
std::string getString( const size_t offset ) const;
219
std::vector<u_rational_t> getResolution( const size_t offset ) const;
220
std::vector<u_rational_t> getWhitePoint( const size_t offset ) const;
221
std::vector<u_rational_t> getPrimaryChromaticies( const size_t offset ) const;
222
std::vector<u_rational_t> getYCbCrCoeffs( const size_t offset ) const;
223
std::vector<u_rational_t> getRefBW( const size_t offset ) const;
224
225
private:
226
static const uint16_t tagMarkRequired = 0x2A;
227
228
//offset to the _number-of-directory-entry_ field
229
static const size_t offsetNumDir = 8;
230
231
//max size of data in tag.
232
//'DDDDDDDD' contains the value of that Tag. If its size is over 4bytes,
233
//'DDDDDDDD' contains the offset to data stored address.
234
static const size_t maxDataSize = 4;
235
236
//bytes per tag field
237
static const size_t tiffFieldSize = 12;
238
239
//number of primary chromaticies components
240
static const size_t primaryChromaticiesComponents = 6;
241
242
//number of YCbCr coefficients in field
243
static const size_t ycbcrCoeffs = 3;
244
245
//number of Reference Black&White components
246
static const size_t refBWComponents = 6;
247
};
248
249
250
}
251
252
#endif /* _OPENCV_EXIF_HPP_ */
253
254