// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html #include "test_precomp.hpp" #include "test_common.hpp" namespace opencv_test { namespace { #ifdef HAVE_GDAL static void test_gdal_read(const string filename, bool required = true) { const string path = cvtest::findDataFile(filename); Mat img; ASSERT_NO_THROW(img = imread(path, cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH | cv::IMREAD_ANYCOLOR)); if(!required && img.empty()) { throw SkipTestException("GDAL is built wihout required back-end support"); } ASSERT_FALSE(img.empty()); EXPECT_EQ(3, img.cols); EXPECT_EQ(5, img.rows); EXPECT_EQ(CV_MAKETYPE(CV_32F, 7), img.type()); EXPECT_EQ(101.125, (img.at>(0, 0)[0])); EXPECT_EQ(203.500, (img.at>(2, 1)[3])); EXPECT_EQ(305.875, (img.at>(4, 2)[6])); } TEST(Imgcodecs_gdal, read_envi) { test_gdal_read("../cv/gdal/envi_test.raw"); } TEST(Imgcodecs_gdal, read_fits) { // .fit test is optional because GDAL may be built wihtout CFITSIO library support test_gdal_read("../cv/gdal/fits_test.fit", false); } // See https://github.com/opencv/opencv/pull/29439 // A 3-band GDT_Byte raster (no palette, so the Mat is CV_8UC3) whose third band // is tagged GCI_AlphaBand makes readData pass channel index 3 into the // gdalChannels == 3 && image.channels() == 3 branch of write_pixel, which indexed // a Vec3b out of bounds. The alpha band must be dropped, leaving red and green. TEST(Imgcodecs_gdal, read_3band_alpha_channel_bound) { // 4x4 GeoTIFF, bands Red/Green/Alpha = 11/22/44, band 3 tagged GCI_AlphaBand const uchar tiff_alpha3[] = { 0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x01, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x03, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x11, 0x01, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x9a, 0x01, 0x00, 0x00, 0x15, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x16, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x17, 0x01, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x1c, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x53, 0x01, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x00, 0x80, 0xa4, 0x02, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x3c, 0x47, 0x44, 0x41, 0x4c, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x0a, 0x20, 0x20, 0x3c, 0x49, 0x74, 0x65, 0x6d, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x43, 0x4f, 0x4c, 0x4f, 0x52, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x50, 0x22, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3d, 0x22, 0x30, 0x22, 0x20, 0x72, 0x6f, 0x6c, 0x65, 0x3d, 0x22, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x22, 0x3e, 0x52, 0x65, 0x64, 0x3c, 0x2f, 0x49, 0x74, 0x65, 0x6d, 0x3e, 0x0a, 0x20, 0x20, 0x3c, 0x49, 0x74, 0x65, 0x6d, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x43, 0x4f, 0x4c, 0x4f, 0x52, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x50, 0x22, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3d, 0x22, 0x31, 0x22, 0x20, 0x72, 0x6f, 0x6c, 0x65, 0x3d, 0x22, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x22, 0x3e, 0x47, 0x72, 0x65, 0x65, 0x6e, 0x3c, 0x2f, 0x49, 0x74, 0x65, 0x6d, 0x3e, 0x0a, 0x20, 0x20, 0x3c, 0x49, 0x74, 0x65, 0x6d, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x3d, 0x22, 0x43, 0x4f, 0x4c, 0x4f, 0x52, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x50, 0x22, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3d, 0x22, 0x32, 0x22, 0x20, 0x72, 0x6f, 0x6c, 0x65, 0x3d, 0x22, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x70, 0x22, 0x3e, 0x41, 0x6c, 0x70, 0x68, 0x61, 0x3c, 0x2f, 0x49, 0x74, 0x65, 0x6d, 0x3e, 0x0a, 0x3c, 0x2f, 0x47, 0x44, 0x41, 0x4c, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x0a, 0x00, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, 0x0b, 0x16, 0x2c, }; const string filename = cv::tempfile(".tif"); FILE* fp = fopen(filename.c_str(), "wb"); ASSERT_TRUE(fp != NULL); ASSERT_EQ((size_t)1, fwrite(tiff_alpha3, sizeof(tiff_alpha3), 1, fp)); fclose(fp); Mat img; ASSERT_NO_THROW(img = imread(filename, cv::IMREAD_LOAD_GDAL)); if (img.empty()) { remove(filename.c_str()); throw SkipTestException("GDAL is built without GTiff back-end support"); } EXPECT_EQ(4, img.cols); EXPECT_EQ(4, img.rows); EXPECT_EQ(CV_8UC3, img.type()); // the alpha band maps to channel index 3 and is dropped; red and green stay EXPECT_EQ(11, img.at(0, 0)[2]); EXPECT_EQ(22, img.at(0, 0)[1]); EXPECT_EQ(11, img.at(3, 3)[2]); EXPECT_EQ(22, img.at(3, 3)[1]); EXPECT_EQ(0, remove(filename.c_str())); } #endif // HAVE_GDAL }} // namespace