Path: blob/master/modules/imgcodecs/test/test_read_write.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html3#include "test_precomp.hpp"45namespace opencv_test { namespace {67TEST(Imgcodecs_Image, read_write_bmp)8{9const size_t IMAGE_COUNT = 10;10const double thresDbell = 32;1112for (size_t i = 0; i < IMAGE_COUNT; ++i)13{14stringstream s; s << i;15const string digit = s.str();16const string src_name = TS::ptr()->get_data_path() + "../python/images/QCIF_0" + digit + ".bmp";17const string dst_name = cv::tempfile((digit + ".bmp").c_str());18Mat image = imread(src_name);19ASSERT_FALSE(image.empty());2021resize(image, image, Size(968, 757), 0.0, 0.0, INTER_CUBIC);22imwrite(dst_name, image);23Mat loaded = imread(dst_name);24ASSERT_FALSE(loaded.empty());2526double psnr = cvtest::PSNR(loaded, image);27EXPECT_GT(psnr, thresDbell);2829vector<uchar> from_file;3031FILE *f = fopen(dst_name.c_str(), "rb");32fseek(f, 0, SEEK_END);33long len = ftell(f);34from_file.resize((size_t)len);35fseek(f, 0, SEEK_SET);36from_file.resize(fread(&from_file[0], 1, from_file.size(), f));37fclose(f);3839vector<uchar> buf;40imencode(".bmp", image, buf);41ASSERT_EQ(buf, from_file);4243Mat buf_loaded = imdecode(Mat(buf), 1);44ASSERT_FALSE(buf_loaded.empty());4546psnr = cvtest::PSNR(buf_loaded, image);47EXPECT_GT(psnr, thresDbell);4849EXPECT_EQ(0, remove(dst_name.c_str()));50}51}5253//==================================================================================================5455typedef string Ext;56typedef testing::TestWithParam<Ext> Imgcodecs_Image;5758TEST_P(Imgcodecs_Image, read_write)59{60const string ext = this->GetParam();61const string full_name = cv::tempfile(ext.c_str());62const string _name = TS::ptr()->get_data_path() + "../cv/shared/baboon.png";63const double thresDbell = 32;6465Mat image = imread(_name);66image.convertTo(image, CV_8UC3);67ASSERT_FALSE(image.empty());6869imwrite(full_name, image);70Mat loaded = imread(full_name);71ASSERT_FALSE(loaded.empty());7273double psnr = cvtest::PSNR(loaded, image);74EXPECT_GT(psnr, thresDbell);7576vector<uchar> from_file;77FILE *f = fopen(full_name.c_str(), "rb");78fseek(f, 0, SEEK_END);79long len = ftell(f);80from_file.resize((size_t)len);81fseek(f, 0, SEEK_SET);82from_file.resize(fread(&from_file[0], 1, from_file.size(), f));83fclose(f);84vector<uchar> buf;85imencode("." + ext, image, buf);86ASSERT_EQ(buf, from_file);8788Mat buf_loaded = imdecode(Mat(buf), 1);89ASSERT_FALSE(buf_loaded.empty());9091psnr = cvtest::PSNR(buf_loaded, image);92EXPECT_GT(psnr, thresDbell);9394EXPECT_EQ(0, remove(full_name.c_str()));95}9697const string exts[] = {98#ifdef HAVE_PNG99"png",100#endif101#ifdef HAVE_TIFF102"tiff",103#endif104#ifdef HAVE_JPEG105"jpg",106#endif107#ifdef HAVE_JASPER108"jp2",109#endif110#if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/111"exr",112#endif113"bmp",114#ifdef HAVE_IMGCODEC_PXM115"ppm",116#endif117#ifdef HAVE_IMGCODEC_SUNRASTER118"ras",119#endif120};121122INSTANTIATE_TEST_CASE_P(imgcodecs, Imgcodecs_Image, testing::ValuesIn(exts));123124TEST(Imgcodecs_Image, regression_9376)125{126String path = findDataFile("readwrite/regression_9376.bmp");127Mat m = imread(path);128ASSERT_FALSE(m.empty());129EXPECT_EQ(32, m.cols);130EXPECT_EQ(32, m.rows);131}132133//==================================================================================================134135TEST(Imgcodecs_Image, write_umat)136{137const string src_name = TS::ptr()->get_data_path() + "../python/images/baboon.bmp";138const string dst_name = cv::tempfile(".bmp");139140Mat image1 = imread(src_name);141ASSERT_FALSE(image1.empty());142143UMat image1_umat = image1.getUMat(ACCESS_RW);144145imwrite(dst_name, image1_umat);146147Mat image2 = imread(dst_name);148ASSERT_FALSE(image2.empty());149150EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), image1, image2);151EXPECT_EQ(0, remove(dst_name.c_str()));152}153154}} // namespace155156157