Commit e6c41f0d authored by Ilari Venäläinen's avatar Ilari Venäläinen Committed by Alexander Alekhin

Merge pull request #12989 from venalil:fix_thresholded_tiff_read

* Fix reading of black-and-white (thresholded) TIFF images

I recently updated my local OpenCV version to 3.4.3 and found out that
I could not read my TIFF images related to my project. After debugging I
found out that there has been some static analysis fixes made
that accidentally have broken reading those black-and-white TIFF images.

Commit hash in which reading of mentioned TIFF images has been broken:
cbb1e867

Basically the fix is to revert back to the same functionality that has been there before,
when black-and-white images are read bpp (bitspersample) is 1.
Without the case 1: this TiffDecoder::readHeader() function always return false.

* Added type and default error message

* Added stdexcept include

* Use CV_Error instead of throw std::runtime_error

* imgcodecs(test): add TIFF B/W decoding tests
parent 733fec02
......@@ -253,6 +253,10 @@ bool TiffDecoder::readHeader()
int wanted_channels = normalizeChannelsNumber(ncn);
switch(bpp)
{
case 1:
m_type = CV_MAKETYPE(CV_8U, photometric > 1 ? wanted_channels : 1);
result = true;
break;
case 8:
m_type = CV_MAKETYPE(CV_8U, photometric > 1 ? wanted_channels : 1);
result = true;
......@@ -269,6 +273,8 @@ bool TiffDecoder::readHeader()
m_type = CV_MAKETYPE(CV_64F, photometric > 1 ? 3 : 1);
result = true;
break;
default:
CV_Error(cv::Error::StsError, "Invalid bitsperpixel value read from TIFF header! Must be 1, 8, 16, 32 or 64.");
}
}
}
......
......@@ -251,6 +251,32 @@ TEST(Imgcodecs_Tiff, imdecode_no_exception_temporary_file_removed)
EXPECT_NO_THROW(cv::imdecode(buf, IMREAD_UNCHANGED));
}
TEST(Imgcodecs_Tiff, decode_black_and_write_image_pr12989)
{
const string filename = cvtest::findDataFile("readwrite/bitsperpixel1.tiff");
cv::Mat img;
ASSERT_NO_THROW(img = cv::imread(filename, IMREAD_GRAYSCALE));
ASSERT_FALSE(img.empty());
EXPECT_EQ(64, img.cols);
EXPECT_EQ(64, img.rows);
EXPECT_EQ(CV_8UC1, img.type()) << cv::typeToString(img.type());
// Check for 0/255 values only: 267 + 3829 = 64*64
EXPECT_EQ(267, countNonZero(img == 0));
EXPECT_EQ(3829, countNonZero(img == 255));
}
TEST(Imgcodecs_Tiff, decode_black_and_write_image_pr12989_default)
{
const string filename = cvtest::findDataFile("readwrite/bitsperpixel1.tiff");
cv::Mat img;
ASSERT_NO_THROW(img = cv::imread(filename)); // by default image type is CV_8UC3
ASSERT_FALSE(img.empty());
EXPECT_EQ(64, img.cols);
EXPECT_EQ(64, img.rows);
EXPECT_EQ(CV_8UC3, img.type()) << cv::typeToString(img.type());
}
#endif
}} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment