From 93d469956d594fad7c0c9d7c7d4784288171426f Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Sun, 6 Jun 2010 16:44:38 +0000 Subject: [PATCH] fixed reading of certain BMP files --- modules/highgui/src/grfmt_bmp.cpp | 17 +++++++++++------ modules/highgui/src/grfmt_bmp.hpp | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/highgui/src/grfmt_bmp.cpp b/modules/highgui/src/grfmt_bmp.cpp index ba157dae87..ece953ca16 100644 --- a/modules/highgui/src/grfmt_bmp.cpp +++ b/modules/highgui/src/grfmt_bmp.cpp @@ -103,7 +103,7 @@ bool BmpDecoder::readHeader() int clrused = m_strm.getDWord(); m_strm.skip( size - 36 ); - if( m_width > 0 && m_height > 0 && + if( m_width > 0 && m_height != 0 && (((m_bpp == 1 || m_bpp == 4 || m_bpp == 8 || m_bpp == 24 || m_bpp == 32 ) && m_rle_code == BMP_RGB) || (m_bpp == 16 && (m_rle_code == BMP_RGB || m_rle_code == BMP_BITFIELDS)) || @@ -143,7 +143,7 @@ bool BmpDecoder::readHeader() m_bpp = m_strm.getDWord() >> 16; m_rle_code = BMP_RGB; - if( m_width > 0 && m_height > 0 && + if( m_width > 0 && m_height != 0 && (m_bpp == 1 || m_bpp == 4 || m_bpp == 8 || m_bpp == 24 || m_bpp == 32 )) { @@ -168,6 +168,9 @@ bool BmpDecoder::readHeader() } m_type = iscolor ? CV_8UC3 : CV_8UC1; + m_origin = m_height > 0 ? IPL_ORIGIN_BL : IPL_ORIGIN_TL; + m_height = std::abs(m_height); + if( !result ) { m_offset = -1; @@ -192,12 +195,14 @@ bool BmpDecoder::readData( Mat& img ) if( m_offset < 0 || !m_strm.isOpened()) return false; - data += (m_height - 1)*step; - step = -step; + if( m_origin == IPL_ORIGIN_BL ) + { + data += (m_height - 1)*step; + step = -step; + } AutoBuffer _src, _bgr; - if( (m_bpp != 24 || !color) ) - _src.allocate(src_pitch + 32); + _src.allocate(src_pitch + 32); if( !color ) { diff --git a/modules/highgui/src/grfmt_bmp.hpp b/modules/highgui/src/grfmt_bmp.hpp index fc38ae0616..f3841e8460 100644 --- a/modules/highgui/src/grfmt_bmp.hpp +++ b/modules/highgui/src/grfmt_bmp.hpp @@ -75,6 +75,7 @@ protected: RLByteStream m_strm; PaletteEntry m_palette[256]; + int m_origin; int m_bpp; int m_offset; BmpCompression m_rle_code;