/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/414243#ifndef _OPENCV_EXIF_HPP_44#define _OPENCV_EXIF_HPP_4546#include <cstdio>47#include <map>48#include <utility>49#include <algorithm>50#include <string>51#include <vector>52#include <iostream>5354namespace cv55{56/**57* @brief Jpeg markers that can encounter in Jpeg file58*/59enum AppMarkerTypes60{61SOI = 0xD8, SOF0 = 0xC0, SOF2 = 0xC2, DHT = 0xC4,62DQT = 0xDB, DRI = 0xDD, SOS = 0xDA,6364RST0 = 0xD0, RST1 = 0xD1, RST2 = 0xD2, RST3 = 0xD3,65RST4 = 0xD4, RST5 = 0xD5, RST6 = 0xD6, RST7 = 0xD7,6667APP0 = 0xE0, APP1 = 0xE1, APP2 = 0xE2, APP3 = 0xE3,68APP4 = 0xE4, APP5 = 0xE5, APP6 = 0xE6, APP7 = 0xE7,69APP8 = 0xE8, APP9 = 0xE9, APP10 = 0xEA, APP11 = 0xEB,70APP12 = 0xEC, APP13 = 0xED, APP14 = 0xEE, APP15 = 0xEF,7172COM = 0xFE, EOI = 0xD973};7475/**76* @brief Base Exif tags used by IFD0 (main image)77*/78enum ExifTagName79{80IMAGE_DESCRIPTION = 0x010E, ///< Image Description: ASCII string81MAKE = 0x010F, ///< Description of manufacturer: ASCII string82MODEL = 0x0110, ///< Description of camera model: ASCII string83ORIENTATION = 0x0112, ///< Orientation of the image: unsigned short84XRESOLUTION = 0x011A, ///< Resolution of the image across X axis: unsigned rational85YRESOLUTION = 0x011B, ///< Resolution of the image across Y axis: unsigned rational86RESOLUTION_UNIT = 0x0128, ///< Resolution units. '1' no-unit, '2' inch, '3' centimeter87SOFTWARE = 0x0131, ///< Shows firmware(internal software of digicam) version number88DATE_TIME = 0x0132, ///< Date/Time of image was last modified89WHITE_POINT = 0x013E, ///< Chromaticity of white point of the image90PRIMARY_CHROMATICIES = 0x013F, ///< Chromaticity of the primaries of the image91Y_CB_CR_COEFFICIENTS = 0x0211, ///< constant to translate an image from YCbCr to RGB format92Y_CB_CR_POSITIONING = 0x0213, ///< Chroma sample point of subsampling pixel array93REFERENCE_BLACK_WHITE = 0x0214, ///< Reference value of black point/white point94COPYRIGHT = 0x8298, ///< Copyright information95EXIF_OFFSET = 0x8769, ///< Offset to Exif Sub IFD96INVALID_TAG = 0xFFFF ///< Shows that the tag was not recognized97};9899enum Endianess_t100{101INTEL = 0x49,102MOTO = 0x4D,103NONE = 0x00104};105106typedef std::pair<uint32_t, uint32_t> u_rational_t;107108/**109* @brief Entry which contains possible values for different exif tags110*/111struct ExifEntry_t112{113ExifEntry_t();114115std::vector<u_rational_t> field_u_rational; ///< vector of rational fields116std::string field_str; ///< any kind of textual information117118float field_float; ///< Currently is not used119double field_double; ///< Currently is not used120121uint32_t field_u32; ///< Unsigned 32-bit value122int32_t field_s32; ///< Signed 32-bit value123124uint16_t tag; ///< Tag number125126uint16_t field_u16; ///< Unsigned 16-bit value127int16_t field_s16; ///< Signed 16-bit value128uint8_t field_u8; ///< Unsigned 8-bit value129int8_t field_s8; ///< Signed 8-bit value130};131132/**133* @brief Picture orientation which may be taken from EXIF134* Orientation usually matters when the picture is taken by135* smartphone or other camera with orientation sensor support136* Corresponds to EXIF 2.3 Specification137*/138enum ImageOrientation139{140IMAGE_ORIENTATION_TL = 1, ///< Horizontal (normal)141IMAGE_ORIENTATION_TR = 2, ///< Mirrored horizontal142IMAGE_ORIENTATION_BR = 3, ///< Rotate 180143IMAGE_ORIENTATION_BL = 4, ///< Mirrored vertical144IMAGE_ORIENTATION_LT = 5, ///< Mirrored horizontal & rotate 270 CW145IMAGE_ORIENTATION_RT = 6, ///< Rotate 90 CW146IMAGE_ORIENTATION_RB = 7, ///< Mirrored horizontal & rotate 90 CW147IMAGE_ORIENTATION_LB = 8 ///< Rotate 270 CW148};149150/**151* @brief Reading exif information from Jpeg file152*153* Usage example for getting the orientation of the image:154*155* @code156* ExifReader reader(fileName);157* if( reader.parse() )158* {159* int orientation = reader.getTag(Orientation).field_u16;160* }161* @endcode162*163*/164class ExifReader165{166public:167/**168* @brief ExifReader constructor. Constructs an object of exif reader169*170* @param [in]stream An istream to look for EXIF bytes from171*/172explicit ExifReader( std::istream& stream );173~ExifReader();174175176/**177* @brief Parse the file with exif info178*179* @return true if parsing was successful and exif information exists in JpegReader object180*/181bool parse();182183/**184* @brief Get tag info by tag number185*186* @param [in] tag The tag number187* @return ExifEntru_t structure. Caller has to know what tag it calls in order to extract proper field from the structure ExifEntry_t188*/189ExifEntry_t getTag( const ExifTagName tag );190191private:192std::istream& m_stream;193std::vector<unsigned char> m_data;194std::map<int, ExifEntry_t > m_exif;195Endianess_t m_format;196197void parseExif();198bool checkTagMark() const;199200size_t getFieldSize ();201size_t getNumDirEntry() const;202uint32_t getStartOffset() const;203uint16_t getExifTag( const size_t offset ) const;204uint16_t getU16( const size_t offset ) const;205uint32_t getU32( const size_t offset ) const;206uint16_t getOrientation( const size_t offset ) const;207uint16_t getResolutionUnit( const size_t offset ) const;208uint16_t getYCbCrPos( const size_t offset ) const;209210Endianess_t getFormat() const;211212ExifEntry_t parseExifEntry( const size_t offset );213214u_rational_t getURational( const size_t offset ) const;215216std::map<int, ExifEntry_t > getExif();217std::string getString( const size_t offset ) const;218std::vector<u_rational_t> getResolution( const size_t offset ) const;219std::vector<u_rational_t> getWhitePoint( const size_t offset ) const;220std::vector<u_rational_t> getPrimaryChromaticies( const size_t offset ) const;221std::vector<u_rational_t> getYCbCrCoeffs( const size_t offset ) const;222std::vector<u_rational_t> getRefBW( const size_t offset ) const;223224private:225static const uint16_t tagMarkRequired = 0x2A;226227//offset to the _number-of-directory-entry_ field228static const size_t offsetNumDir = 8;229230//max size of data in tag.231//'DDDDDDDD' contains the value of that Tag. If its size is over 4bytes,232//'DDDDDDDD' contains the offset to data stored address.233static const size_t maxDataSize = 4;234235//bytes per tag field236static const size_t tiffFieldSize = 12;237238//number of primary chromaticies components239static const size_t primaryChromaticiesComponents = 6;240241//number of YCbCr coefficients in field242static const size_t ycbcrCoeffs = 3;243244//number of Reference Black&White components245static const size_t refBWComponents = 6;246};247248249}250251#endif /* _OPENCV_EXIF_HPP_ */252253254