Camera orientation handling is added for jpeg files

This commit is contained in:
micalan
2015-10-11 10:08:32 +03:00
parent d430e802f2
commit 0a306f8804
6 changed files with 1042 additions and 0 deletions

View File

@@ -41,6 +41,7 @@
#include "precomp.hpp"
#include "grfmt_jpeg.hpp"
#include "jpeg_exif.hpp"
#ifdef HAVE_JPEG
@@ -177,6 +178,7 @@ JpegDecoder::JpegDecoder()
m_state = 0;
m_f = 0;
m_buf_supported = true;
m_orientation = JPEG_ORIENTATION_TL;
}
@@ -253,12 +255,64 @@ bool JpegDecoder::readHeader()
}
}
m_orientation = getOrientation();
if( !result )
close();
return result;
}
int JpegDecoder::getOrientation()
{
int orientation = JPEG_ORIENTATION_TL;
ExifReader reader( m_filename );
if( reader.parse() )
{
orientation = reader.getTag( ORIENTATION ).field_u16;//orientation is unsigned short, so check field_u16
}
return orientation;
}
void JpegDecoder::setOrientation(Mat& img)
{
switch( m_orientation )
{
case JPEG_ORIENTATION_TL: //0th row == visual top, 0th column == visual left-hand side
//do nothing, the image already has proper orientation
break;
case JPEG_ORIENTATION_TR: //0th row == visual top, 0th column == visual right-hand side
flip(img, img, 1); //flip horizontally
break;
case JPEG_ORIENTATION_BR: //0th row == visual bottom, 0th column == visual right-hand side
flip(img, img, -1);//flip both horizontally and vertically
break;
case JPEG_ORIENTATION_BL: //0th row == visual bottom, 0th column == visual left-hand side
flip(img, img, 0); //flip vertically
break;
case JPEG_ORIENTATION_LT: //0th row == visual left-hand side, 0th column == visual top
transpose(img, img);
break;
case JPEG_ORIENTATION_RT: //0th row == visual right-hand side, 0th column == visual top
transpose(img, img);
flip(img, img, 1); //flip horizontally
break;
case JPEG_ORIENTATION_RB: //0th row == visual right-hand side, 0th column == visual bottom
transpose(img, img);
flip(img, img, -1); //flip both horizontally and vertically
break;
case JPEG_ORIENTATION_LB: //0th row == visual left-hand side, 0th column == visual bottom
transpose(img, img);
flip(img, img, 0); //flip vertically
break;
default:
//by default the image read has normal (JPEG_ORIENTATION_TL) orientation
break;
}
}
/***************************************************************************
* following code is for supporting MJPEG image files
* based on a message of Laurent Pinchart on the video4linux mailing list
@@ -472,8 +526,10 @@ bool JpegDecoder::readData( Mat& img )
icvCvt_CMYK2Gray_8u_C4C1R( buffer[0], 0, data, 0, cvSize(m_width,1) );
}
}
result = true;
jpeg_finish_decompress( cinfo );
setOrientation( img );
}
}