Path: blob/master/modules/imgcodecs/test/test_grfmt.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546typedef tuple<string, int> File_Mode;47typedef testing::TestWithParam<File_Mode> Imgcodecs_FileMode;4849TEST_P(Imgcodecs_FileMode, regression)50{51const string root = cvtest::TS::ptr()->get_data_path();52const string filename = root + get<0>(GetParam());53const int mode = get<1>(GetParam());5455const Mat single = imread(filename, mode);56ASSERT_FALSE(single.empty());5758vector<Mat> pages;59ASSERT_TRUE(imreadmulti(filename, pages, mode));60ASSERT_FALSE(pages.empty());61const Mat page = pages[0];62ASSERT_FALSE(page.empty());6364EXPECT_EQ(page.channels(), single.channels());65EXPECT_EQ(page.depth(), single.depth());66EXPECT_EQ(page.size().height, single.size().height);67EXPECT_EQ(page.size().width, single.size().width);68EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), page, single);69}7071const string all_images[] =72{73#ifdef HAVE_JASPER74"readwrite/Rome.jp2",75"readwrite/Bretagne2.jp2",76"readwrite/Bretagne2.jp2",77"readwrite/Grey.jp2",78"readwrite/Grey.jp2",79#endif80#ifdef HAVE_GDCM81"readwrite/int16-mono1.dcm",82"readwrite/uint8-mono2.dcm",83"readwrite/uint16-mono2.dcm",84"readwrite/uint8-rgb.dcm",85#endif86"readwrite/color_palette_alpha.png",87"readwrite/multipage.tif",88"readwrite/ordinary.bmp",89"readwrite/rle8.bmp",90"readwrite/test_1_c1.jpg",91#ifdef HAVE_IMGCODEC_HDR92"readwrite/rle.hdr"93#endif94};9596const int basic_modes[] =97{98IMREAD_UNCHANGED,99IMREAD_GRAYSCALE,100IMREAD_COLOR,101IMREAD_ANYDEPTH,102IMREAD_ANYCOLOR103};104105INSTANTIATE_TEST_CASE_P(All, Imgcodecs_FileMode,106testing::Combine(107testing::ValuesIn(all_images),108testing::ValuesIn(basic_modes)));109110// GDAL does not support "hdr", "dcm" and have problems with "jp2"111struct notForGDAL {112bool operator()(const string &name) const {113const string &ext = name.substr(name.size() - 3, 3);114return ext == "hdr" || ext == "dcm" || ext == "jp2" ||115name.find("rle8.bmp") != std::string::npos;116}117};118119inline vector<string> gdal_images()120{121vector<string> res;122std::back_insert_iterator< vector<string> > it(res);123std::remove_copy_if(all_images, all_images + sizeof(all_images)/sizeof(all_images[0]), it, notForGDAL());124return res;125}126127INSTANTIATE_TEST_CASE_P(GDAL, Imgcodecs_FileMode,128testing::Combine(129testing::ValuesIn(gdal_images()),130testing::Values(IMREAD_LOAD_GDAL)));131132//==================================================================================================133134typedef tuple<string, Size> Ext_Size;135typedef testing::TestWithParam<Ext_Size> Imgcodecs_ExtSize;136137TEST_P(Imgcodecs_ExtSize, write_imageseq)138{139const string ext = get<0>(GetParam());140const Size size = get<1>(GetParam());141const Point2i center = Point2i(size.width / 2, size.height / 2);142const int radius = std::min(size.height, size.width / 4);143144for (int cn = 1; cn <= 4; cn++)145{146SCOPED_TRACE(format("channels %d", cn));147std::vector<int> parameters;148if (cn == 2)149continue;150if (cn == 4 && ext != ".tiff")151continue;152if (cn > 1 && (ext == ".pbm" || ext == ".pgm"))153continue;154if (cn != 3 && ext == ".ppm")155continue;156string filename = cv::tempfile(format("%d%s", cn, ext.c_str()).c_str());157158Mat img_gt(size, CV_MAKETYPE(CV_8U, cn), Scalar::all(0));159circle(img_gt, center, radius, Scalar::all(255));160161#if 1162if (ext == ".pbm" || ext == ".pgm" || ext == ".ppm")163{164parameters.push_back(IMWRITE_PXM_BINARY);165parameters.push_back(0);166}167#endif168ASSERT_TRUE(imwrite(filename, img_gt, parameters));169Mat img = imread(filename, IMREAD_UNCHANGED);170ASSERT_FALSE(img.empty());171EXPECT_EQ(img.size(), img.size());172EXPECT_EQ(img.type(), img.type());173EXPECT_EQ(cn, img.channels());174175176if (ext == ".jpg")177{178// JPEG format does not provide 100% accuracy179// using fuzzy image comparison180double n = cvtest::norm(img, img_gt, NORM_L1);181double expected = 0.07 * img.size().area();182EXPECT_LT(n, expected);183EXPECT_PRED_FORMAT2(cvtest::MatComparator(10, 0), img, img_gt);184}185else if (ext == ".pfm")186{187img_gt.convertTo(img_gt, CV_MAKETYPE(CV_32F, img.channels()));188double n = cvtest::norm(img, img_gt, NORM_L2);189EXPECT_LT(n, 1.);190EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);191}192else193{194double n = cvtest::norm(img, img_gt, NORM_L2);195EXPECT_LT(n, 1.);196EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);197}198199#if 0200imshow("loaded", img);201waitKey(0);202#else203EXPECT_EQ(0, remove(filename.c_str()));204#endif205}206}207208const string all_exts[] =209{210#ifdef HAVE_PNG211".png",212#endif213#ifdef HAVE_TIFF214".tiff",215#endif216#ifdef HAVE_JPEG217".jpg",218#endif219".bmp",220#ifdef HAVE_IMGCODEC_PXM221".pam",222".ppm",223".pgm",224".pbm",225".pnm",226#endif227#ifdef HAVE_IMGCODEC_PFM228".pfm",229#endif230};231232vector<Size> all_sizes()233{234vector<Size> res;235for (int k = 1; k <= 5; ++k)236res.push_back(Size(640 * k, 480 * k));237return res;238}239240INSTANTIATE_TEST_CASE_P(All, Imgcodecs_ExtSize,241testing::Combine(242testing::ValuesIn(all_exts),243testing::ValuesIn(all_sizes())));244245#ifdef HAVE_IMGCODEC_PXM246typedef testing::TestWithParam<bool> Imgcodecs_pbm;247TEST_P(Imgcodecs_pbm, write_read)248{249bool binary = GetParam();250const String ext = "pbm";251const string full_name = cv::tempfile(ext.c_str());252253Size size(640, 480);254const Point2i center = Point2i(size.width / 2, size.height / 2);255const int radius = std::min(size.height, size.width / 4);256Mat image(size, CV_8UC1, Scalar::all(0));257circle(image, center, radius, Scalar::all(255));258259vector<int> pbm_params;260pbm_params.push_back(IMWRITE_PXM_BINARY);261pbm_params.push_back(binary);262263imwrite( full_name, image, pbm_params );264Mat loaded = imread(full_name, IMREAD_UNCHANGED);265ASSERT_FALSE(loaded.empty());266267EXPECT_EQ(0, cvtest::norm(loaded, image, NORM_INF));268269FILE *f = fopen(full_name.c_str(), "rb");270ASSERT_TRUE(f != NULL);271ASSERT_EQ('P', getc(f));272ASSERT_EQ('1' + (binary ? 3 : 0), getc(f));273fclose(f);274EXPECT_EQ(0, remove(full_name.c_str()));275}276277INSTANTIATE_TEST_CASE_P(All, Imgcodecs_pbm, testing::Bool());278#endif279280281//==================================================================================================282283TEST(Imgcodecs_Bmp, read_rle8)284{285const string root = cvtest::TS::ptr()->get_data_path();286Mat rle = imread(root + "readwrite/rle8.bmp");287ASSERT_FALSE(rle.empty());288Mat ord = imread(root + "readwrite/ordinary.bmp");289ASSERT_FALSE(ord.empty());290EXPECT_LE(cvtest::norm(rle, ord, NORM_L2), 1.e-10);291EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), rle, ord);292}293294#ifdef HAVE_IMGCODEC_HDR295TEST(Imgcodecs_Hdr, regression)296{297string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";298string name_rle = folder + "rle.hdr";299string name_no_rle = folder + "no_rle.hdr";300Mat img_rle = imread(name_rle, -1);301ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle;302Mat img_no_rle = imread(name_no_rle, -1);303ASSERT_FALSE(img_no_rle.empty()) << "Could not open " << name_no_rle;304305double min = 0.0, max = 1.0;306minMaxLoc(abs(img_rle - img_no_rle), &min, &max);307ASSERT_FALSE(max > DBL_EPSILON);308string tmp_file_name = tempfile(".hdr");309vector<int>param(1);310for(int i = 0; i < 2; i++) {311param[0] = i;312imwrite(tmp_file_name, img_rle, param);313Mat written_img = imread(tmp_file_name, -1);314ASSERT_FALSE(written_img.empty()) << "Could not open " << tmp_file_name;315minMaxLoc(abs(img_rle - written_img), &min, &max);316ASSERT_FALSE(max > DBL_EPSILON);317}318remove(tmp_file_name.c_str());319}320#endif321322#ifdef HAVE_IMGCODEC_PXM323TEST(Imgcodecs_Pam, read_write)324{325string folder = string(cvtest::TS::ptr()->get_data_path()) + "readwrite/";326string filepath = folder + "lena.pam";327328cv::Mat img = cv::imread(filepath);329ASSERT_FALSE(img.empty());330331std::vector<int> params;332params.push_back(IMWRITE_PAM_TUPLETYPE);333params.push_back(IMWRITE_PAM_FORMAT_RGB);334335string writefile = cv::tempfile(".pam");336EXPECT_NO_THROW(cv::imwrite(writefile, img, params));337cv::Mat reread = cv::imread(writefile);338339string writefile_no_param = cv::tempfile(".pam");340EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));341cv::Mat reread_no_param = cv::imread(writefile_no_param);342343EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));344EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));345346remove(writefile.c_str());347remove(writefile_no_param.c_str());348}349#endif350351#ifdef HAVE_IMGCODEC_PFM352TEST(Imgcodecs_Pfm, read_write)353{354Mat img = imread(findDataFile("readwrite/lena.pam"));355ASSERT_FALSE(img.empty());356img.convertTo(img, CV_32F, 1/255.0f);357358std::vector<int> params;359string writefile = cv::tempfile(".pfm");360EXPECT_NO_THROW(cv::imwrite(writefile, img, params));361cv::Mat reread = cv::imread(writefile, IMREAD_UNCHANGED);362363string writefile_no_param = cv::tempfile(".pfm");364EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));365cv::Mat reread_no_param = cv::imread(writefile_no_param, IMREAD_UNCHANGED);366367EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));368EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));369370EXPECT_EQ(0, remove(writefile.c_str()));371EXPECT_EQ(0, remove(writefile_no_param.c_str()));372}373#endif374375TEST(Imgcodecs, write_parameter_type)376{377cv::Mat m(10, 10, CV_8UC1, cv::Scalar::all(0));378cv::Mat1b m_type = cv::Mat1b::zeros(10, 10);379string tmp_file = cv::tempfile(".bmp");380EXPECT_NO_THROW(cv::imwrite(tmp_file, cv::Mat(m * 2))) << "* Failed with cv::Mat";381EXPECT_NO_THROW(cv::imwrite(tmp_file, m * 2)) << "* Failed with cv::MatExpr";382EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type)) << "* Failed with cv::Mat_";383EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type * 2)) << "* Failed with cv::MatExpr(Mat_)";384cv::Matx<uchar, 10, 10> matx;385EXPECT_NO_THROW(cv::imwrite(tmp_file, matx)) << "* Failed with cv::Matx";386EXPECT_EQ(0, remove(tmp_file.c_str()));387}388389}} // namespace390391392