/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "exif.hpp"
namespace {
class ExifParsingError {
};
}
namespace cv
{
ExifEntry_t::ExifEntry_t() :
field_float(0), field_double(0), field_u32(0), field_s32(0),
tag(INVALID_TAG), field_u16(0), field_s16(0), field_u8(0), field_s8(0)
{
}
/**
* @brief ExifReader constructor
*/
ExifReader::ExifReader(std::string filename) : m_filename(filename), m_format(NONE)
{
}
/**
* @brief ExifReader destructor
*/
ExifReader::~ExifReader()
{
}
/**
* @brief Parsing the file and prepare (internally) exif directory structure
* @return true if parsing was successful and exif information exists in JpegReader object
* false in case of unsuccessful parsing
*/
bool ExifReader::parse()
{
try {
m_exif = getExif();
if( !m_exif.empty() )
{
return true;
}
return false;
} catch (ExifParsingError&) {
return false;
}
}
/**
* @brief Get tag value by tag number
*
* @param [in] tag The tag number
*
* @return ExifEntru_t structure. Caller has to know what tag it calls in order to extract proper field from the structure ExifEntry_t
*
*/
ExifEntry_t ExifReader::getTag(const ExifTagName tag)
{
ExifEntry_t entry;
std::map<int, ExifEntry_t>::iterator it = m_exif.find(tag);
if( it != m_exif.end() )
{
entry = it->second;
}
return entry;
}
/**
* @brief Get exif directory structure contained in file (if any)
* This is internal function and is not exposed to client
*
* @return Map where key is tag number and value is ExifEntry_t structure
*/
std::map<int, ExifEntry_t > ExifReader::getExif()
{
const size_t markerSize = 2;
const size_t offsetToTiffHeader = 6; //bytes from Exif size field to the first TIFF header
unsigned char appMarker[markerSize];
m_exif.erase( m_exif.begin(), m_exif.end() );
size_t count;
if (m_filename.size() == 0)
{
return m_exif;
}
FILE* f = fopen( m_filename.c_str(), "rb" );
if( !f )
{
return m_exif; //Until this moment the map is empty
}
bool exifFound = false, stopSearch = false;
while( ( !feof( f ) ) && !exifFound && !stopSearch )
{
count = fread( appMarker, sizeof(unsigned char), markerSize, f );
if( count < markerSize )
{
break;
}
unsigned char marker = appMarker[1];
size_t bytesToSkip;
size_t exifSize;
switch( marker )
{
//For all the markers just skip bytes in file pointed by followed two bytes (field size)
case SOF0: case SOF2: case DHT: case DQT: case DRI: case SOS:
case RST0: case RST1: case RST2: case RST3: case RST4: case RST5: case RST6: case RST7:
case APP0: case APP2: case APP3: case APP4: case APP5: case APP6: case APP7: case APP8:
case APP9: case APP10: case APP11: case APP12: case APP13: case APP14: case APP15:
case COM:
bytesToSkip = getFieldSize( f );
if (bytesToSkip < markerSize) {
fclose(f);
throw ExifParsingError();
}
fseek( f, static_cast<long>( bytesToSkip - markerSize ), SEEK_CUR );
break;
//SOI and EOI don't have the size field after the marker
case SOI: case EOI:
break;
case APP1: //actual Exif Marker
exifSize = getFieldSize(f);
if (exifSize <= offsetToTiffHeader) {
fclose(f);
throw ExifParsingError();
}
m_data.resize( exifSize - offsetToTiffHeader );
fseek(f, static_cast<long>( offsetToTiffHeader ), SEEK_CUR);
count = fread( &m_data[0], sizeof( unsigned char ), exifSize - offsetToTiffHeader, f );
exifFound = true;
break;
default: //No other markers are expected according to standard. May be a signal of error
stopSearch = true;
break;
}
}
fclose(f);
if( !exifFound )
{
return m_exif;
}
parseExif();
return m_exif;
}
/**
* @brief Get the size of exif field (required to properly ready whole exif from the file)
* This is internal function and is not exposed to client
*
* @return size of exif field in the file
*/
size_t ExifReader::getFieldSize (FILE* f) const
{
unsigned char fieldSize[2];
size_t count = fread ( fieldSize, sizeof( char ), 2, f );
if (count < 2)
{
return 0;
}
return ( fieldSize[0] << 8 ) + fieldSize[1];
}
/**
* @brief Filling m_exif member with exif directory elements
* This is internal function and is not exposed to client
*
* @return The function doesn't return any value. In case of unsiccessful parsing
* the m_exif member is not filled up
*/
void ExifReader::parseExif()
{
m_format = getFormat();
if( !checkTagMark() )
{
return;
}
uint32_t offset = getStartOffset();
size_t numEntry = getNumDirEntry();
offset += 2; //go to start of tag fields
for( size_t entry = 0; entry < numEntry; entry++ )
{
ExifEntry_t exifEntry = parseExifEntry( offset );
m_exif.insert( std::make_pair( exifEntry.tag, exifEntry ) );
offset += tiffFieldSize;
}
}
/**
* @brief Get endianness of exif information
* This is internal function and is not exposed to client
*
* @return INTEL, MOTO or NONE
*/
Endianess_t ExifReader::getFormat() const
{
if (m_data.size() < 1)
return NONE;
if( m_data.size() > 1 && m_data[0] != m_data[1] )
{
return NONE;
}
if( m_data[0] == 'I' )
{
return INTEL;
}
if( m_data[0] =
没有合适的资源?快使用搜索试试~ 我知道了~
处理图像的exif中的orientation信息
共2个文件
hpp:1个
cpp:1个
3星 · 超过75%的资源 需积分: 50 55 下载量 178 浏览量
2017-06-30
09:05:40
上传
评论 3
收藏 9KB ZIP 举报
温馨提示
解决用opencv imread读取图像时方向存在翻转的问题,使用参考http://blog.csdn.net/computerme/article/details/73929406
资源推荐
资源详情
资源评论
收起资源包目录
exif.zip (2个子文件)
exif
exif.cpp 18KB
exif.hpp 9KB
共 2 条
- 1
资源评论
- sangky2017-09-24感谢一下,虽然实际检测无效,不知道原因,后续使用exiftool进行处理
- cannycsy2018-09-03东西很不错,可以用
IT修道者
- 粉丝: 318
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功