Skip to content

Commit 24ee7b5

Browse files
committed
Fix for vertically flipped tiff files reading
1 parent cb66f82 commit 24ee7b5

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

modules/imgcodecs/src/grfmt_tiff.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ bool TiffDecoder::readData( Mat& img )
300300
}
301301
bool result = false;
302302
bool color = img.channels() > 1;
303-
uchar* data = img.ptr();
304303

305304
if( img.depth() != CV_8U && img.depth() != CV_16U && img.depth() != CV_32F && img.depth() != CV_64F )
306305
return false;
@@ -316,6 +315,10 @@ bool TiffDecoder::readData( Mat& img )
316315
uint16 bpp = 8, ncn = photometric > 1 ? 3 : 1;
317316
TIFFGetField( tif, TIFFTAG_BITSPERSAMPLE, &bpp );
318317
TIFFGetField( tif, TIFFTAG_SAMPLESPERPIXEL, &ncn );
318+
uint16 img_orientation = ORIENTATION_TOPLEFT;
319+
TIFFGetField( tif, TIFFTAG_ORIENTATION, &img_orientation);
320+
bool vert_flip = (img_orientation == ORIENTATION_BOTRIGHT) || (img_orientation == ORIENTATION_RIGHTBOT) ||
321+
(img_orientation == ORIENTATION_BOTLEFT) || (img_orientation == ORIENTATION_LEFTBOT);
319322
const int bitsPerByte = 8;
320323
int dst_bpp = (int)(img.elemSize1() * bitsPerByte);
321324
int wanted_channels = normalizeChannelsNumber(img.channels());
@@ -358,13 +361,15 @@ bool TiffDecoder::readData( Mat& img )
358361
double* buffer64 = (double*)buffer;
359362
int tileidx = 0;
360363

361-
for( y = 0; y < m_height; y += tile_height0, data += img.step*tile_height0 )
364+
for( y = 0; y < m_height; y += tile_height0 )
362365
{
363366
int tile_height = tile_height0;
364367

365368
if( y + tile_height > m_height )
366369
tile_height = m_height - y;
367370

371+
uchar* data = img.ptr(vert_flip ? m_height - y - tile_height : y);
372+
368373
for( x = 0; x < m_width; x += tile_width0, tileidx++ )
369374
{
370375
int tile_width = tile_width0, ok;

0 commit comments

Comments
 (0)