test_grfmt.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. typedef tuple<string, int> File_Mode;
  45. typedef testing::TestWithParam<File_Mode> Imgcodecs_FileMode;
  46. TEST_P(Imgcodecs_FileMode, regression)
  47. {
  48. const string root = cvtest::TS::ptr()->get_data_path();
  49. const string filename = root + get<0>(GetParam());
  50. const int mode = get<1>(GetParam());
  51. const Mat single = imread(filename, mode);
  52. ASSERT_FALSE(single.empty());
  53. vector<Mat> pages;
  54. ASSERT_TRUE(imreadmulti(filename, pages, mode));
  55. ASSERT_FALSE(pages.empty());
  56. const Mat page = pages[0];
  57. ASSERT_FALSE(page.empty());
  58. EXPECT_EQ(page.channels(), single.channels());
  59. EXPECT_EQ(page.depth(), single.depth());
  60. EXPECT_EQ(page.size().height, single.size().height);
  61. EXPECT_EQ(page.size().width, single.size().width);
  62. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), page, single);
  63. }
  64. const string all_images[] =
  65. {
  66. #if (defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)) \
  67. || defined(HAVE_OPENJPEG)
  68. "readwrite/Rome.jp2",
  69. "readwrite/Bretagne2.jp2",
  70. "readwrite/Bretagne2.jp2",
  71. "readwrite/Grey.jp2",
  72. "readwrite/Grey.jp2",
  73. "readwrite/balloon.j2c",
  74. #endif
  75. #ifdef HAVE_GDCM
  76. "readwrite/int16-mono1.dcm",
  77. "readwrite/uint8-mono2.dcm",
  78. "readwrite/uint16-mono2.dcm",
  79. "readwrite/uint8-rgb.dcm",
  80. #endif
  81. #if defined(HAVE_PNG) || defined(HAVE_SPNG)
  82. "readwrite/color_palette_alpha.png",
  83. #endif
  84. #ifdef HAVE_TIFF
  85. "readwrite/multipage.tif",
  86. #endif
  87. "readwrite/ordinary.bmp",
  88. "readwrite/rle8.bmp",
  89. #ifdef HAVE_JPEG
  90. "readwrite/test_1_c1.jpg",
  91. #endif
  92. #ifdef HAVE_IMGCODEC_HDR
  93. "readwrite/rle.hdr"
  94. #endif
  95. };
  96. const int basic_modes[] =
  97. {
  98. IMREAD_UNCHANGED,
  99. IMREAD_GRAYSCALE,
  100. IMREAD_COLOR,
  101. IMREAD_COLOR_RGB,
  102. IMREAD_ANYDEPTH,
  103. IMREAD_ANYCOLOR
  104. };
  105. INSTANTIATE_TEST_CASE_P(All, Imgcodecs_FileMode,
  106. testing::Combine(
  107. testing::ValuesIn(all_images),
  108. testing::ValuesIn(basic_modes)));
  109. // GDAL does not support "hdr", "dcm" and has problems with JPEG2000 files (jp2, j2c)
  110. struct notForGDAL {
  111. bool operator()(const string &name) const {
  112. const string &ext = name.substr(name.size() - 3, 3);
  113. return ext == "hdr" || ext == "dcm" || ext == "jp2" || ext == "j2c" ||
  114. name.find("rle8.bmp") != std::string::npos;
  115. }
  116. };
  117. inline vector<string> gdal_images()
  118. {
  119. vector<string> res;
  120. std::back_insert_iterator< vector<string> > it(res);
  121. std::remove_copy_if(all_images, all_images + sizeof(all_images)/sizeof(all_images[0]), it, notForGDAL());
  122. return res;
  123. }
  124. INSTANTIATE_TEST_CASE_P(GDAL, Imgcodecs_FileMode,
  125. testing::Combine(
  126. testing::ValuesIn(gdal_images()),
  127. testing::Values(IMREAD_LOAD_GDAL)));
  128. //==================================================================================================
  129. typedef tuple<string, Size> Ext_Size;
  130. typedef testing::TestWithParam<Ext_Size> Imgcodecs_ExtSize;
  131. TEST_P(Imgcodecs_ExtSize, write_imageseq)
  132. {
  133. const string ext = get<0>(GetParam());
  134. const Size size = get<1>(GetParam());
  135. const Point2i center = Point2i(size.width / 2, size.height / 2);
  136. const int radius = std::min(size.height, size.width / 4);
  137. for (int cn = 1; cn <= 4; cn++)
  138. {
  139. SCOPED_TRACE(format("channels %d", cn));
  140. std::vector<int> parameters;
  141. if (cn == 2)
  142. continue;
  143. if (cn == 4 && ext != ".tiff")
  144. continue;
  145. if (cn > 1 && (ext == ".pbm" || ext == ".pgm"))
  146. continue;
  147. if (cn != 3 && ext == ".ppm")
  148. continue;
  149. if (cn == 1 && ext == ".gif")
  150. continue;
  151. if (cn == 1 && ext == ".webp")
  152. continue;
  153. string filename = cv::tempfile(format("%d%s", cn, ext.c_str()).c_str());
  154. Mat img_gt(size, CV_MAKETYPE(CV_8U, cn), Scalar::all(0));
  155. circle(img_gt, center, radius, Scalar::all(255));
  156. #if 1
  157. if (ext == ".pbm" || ext == ".pgm" || ext == ".ppm")
  158. {
  159. parameters.push_back(IMWRITE_PXM_BINARY);
  160. parameters.push_back(0);
  161. }
  162. #endif
  163. ASSERT_TRUE(imwrite(filename, img_gt, parameters));
  164. Mat img = imread(filename, IMREAD_UNCHANGED);
  165. ASSERT_FALSE(img.empty());
  166. EXPECT_EQ(img_gt.size(), img.size());
  167. EXPECT_EQ(img_gt.channels(), img.channels());
  168. if (ext == ".pfm") {
  169. EXPECT_EQ(img_gt.depth(), CV_8U);
  170. EXPECT_EQ(img.depth(), CV_32F);
  171. } else {
  172. EXPECT_EQ(img_gt.depth(), img.depth());
  173. }
  174. EXPECT_EQ(cn, img.channels());
  175. if (ext == ".jpg")
  176. {
  177. // JPEG format does not provide 100% accuracy
  178. // using fuzzy image comparison
  179. double n = cvtest::norm(img, img_gt, NORM_L1);
  180. double expected = 0.07 * img.size().area();
  181. EXPECT_LT(n, expected);
  182. EXPECT_PRED_FORMAT2(cvtest::MatComparator(10, 0), img, img_gt);
  183. }
  184. else if (ext == ".pfm")
  185. {
  186. img_gt.convertTo(img_gt, CV_MAKETYPE(CV_32F, img.channels()));
  187. double n = cvtest::norm(img, img_gt, NORM_L2);
  188. EXPECT_LT(n, 1.);
  189. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
  190. }
  191. else if (ext == ".gif")
  192. {
  193. // GIF encoder will reduce the number of colors to 256.
  194. // It is hard to compare image comparison by pixel unit.
  195. double n = cvtest::norm(img, img_gt, NORM_L1);
  196. double expected = 0.03 * img.size().area();
  197. EXPECT_LT(n, expected);
  198. }
  199. else
  200. {
  201. double n = cvtest::norm(img, img_gt, NORM_L2);
  202. EXPECT_LT(n, 1.);
  203. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
  204. }
  205. #if 0
  206. imshow("loaded", img);
  207. waitKey(0);
  208. #else
  209. EXPECT_EQ(0, remove(filename.c_str()));
  210. #endif
  211. }
  212. }
  213. const string all_exts[] =
  214. {
  215. #if defined(HAVE_PNG) || defined(HAVE_SPNG)
  216. ".png",
  217. #endif
  218. #ifdef HAVE_TIFF
  219. ".tiff",
  220. #endif
  221. #ifdef HAVE_JPEG
  222. ".jpg",
  223. #endif
  224. ".bmp",
  225. #ifdef HAVE_IMGCODEC_PXM
  226. ".pam",
  227. ".ppm",
  228. ".pgm",
  229. ".pbm",
  230. ".pnm",
  231. #endif
  232. #ifdef HAVE_IMGCODEC_PFM
  233. ".pfm",
  234. #endif
  235. #ifdef HAVE_IMGCODEC_GIF
  236. ".gif",
  237. #endif
  238. #ifdef HAVE_WEBP
  239. ".webp",
  240. #endif
  241. };
  242. vector<Size> all_sizes()
  243. {
  244. vector<Size> res;
  245. for (int k = 1; k <= 5; ++k)
  246. res.push_back(Size(640 * k, 480 * k));
  247. return res;
  248. }
  249. INSTANTIATE_TEST_CASE_P(All, Imgcodecs_ExtSize,
  250. testing::Combine(
  251. testing::ValuesIn(all_exts),
  252. testing::ValuesIn(all_sizes())));
  253. #ifdef HAVE_IMGCODEC_PXM
  254. typedef testing::TestWithParam<bool> Imgcodecs_pbm;
  255. TEST_P(Imgcodecs_pbm, write_read)
  256. {
  257. bool binary = GetParam();
  258. const String ext = "pbm";
  259. const string full_name = cv::tempfile(ext.c_str());
  260. Size size(640, 480);
  261. const Point2i center = Point2i(size.width / 2, size.height / 2);
  262. const int radius = std::min(size.height, size.width / 4);
  263. Mat image(size, CV_8UC1, Scalar::all(0));
  264. circle(image, center, radius, Scalar::all(255));
  265. vector<int> pbm_params;
  266. pbm_params.push_back(IMWRITE_PXM_BINARY);
  267. pbm_params.push_back(binary);
  268. imwrite( full_name, image, pbm_params );
  269. Mat loaded = imread(full_name, IMREAD_UNCHANGED);
  270. ASSERT_FALSE(loaded.empty());
  271. EXPECT_EQ(0, cvtest::norm(loaded, image, NORM_INF));
  272. FILE *f = fopen(full_name.c_str(), "rb");
  273. ASSERT_TRUE(f != NULL);
  274. ASSERT_EQ('P', getc(f));
  275. ASSERT_EQ('1' + (binary ? 3 : 0), getc(f));
  276. fclose(f);
  277. EXPECT_EQ(0, remove(full_name.c_str()));
  278. }
  279. INSTANTIATE_TEST_CASE_P(All, Imgcodecs_pbm, testing::Bool());
  280. #endif
  281. // See https://github.com/opencv/opencv/issues/27557
  282. typedef testing::TestWithParam<string> Imgcodecs_invalid_key;
  283. TEST_P(Imgcodecs_invalid_key, encode_regression27557)
  284. {
  285. const string ext = GetParam();
  286. const int matType = ((ext == ".pbm") || (ext == ".pgm"))? CV_8UC1 : CV_8UC3;
  287. Mat src(100, 100, matType, Scalar(0, 255, 0));
  288. std::vector<uchar> buf;
  289. bool status = false;
  290. EXPECT_NO_THROW(status = imencode(ext, src, buf, { -1, -1 }));
  291. EXPECT_TRUE(status);
  292. }
  293. TEST_P(Imgcodecs_invalid_key, write_regression27557)
  294. {
  295. const string ext = GetParam();
  296. string fname = tempfile(ext.c_str());
  297. const int matType = ((ext == ".pbm") || (ext == ".pgm"))? CV_8UC1 : CV_8UC3;
  298. Mat src(100, 100, matType, Scalar(0, 255, 0));
  299. std::vector<uchar> buf;
  300. bool status = false;
  301. EXPECT_NO_THROW(status = imwrite(fname, src, { -1, -1 }));
  302. EXPECT_TRUE(status);
  303. remove(fname.c_str());
  304. }
  305. INSTANTIATE_TEST_CASE_P(All, Imgcodecs_invalid_key, testing::ValuesIn(all_exts));
  306. //==================================================================================================
  307. TEST(Imgcodecs_Bmp, read_rle8)
  308. {
  309. const string root = cvtest::TS::ptr()->get_data_path();
  310. Mat rle = imread(root + "readwrite/rle8.bmp");
  311. ASSERT_FALSE(rle.empty());
  312. Mat ord = imread(root + "readwrite/ordinary.bmp");
  313. ASSERT_FALSE(ord.empty());
  314. EXPECT_LE(cvtest::norm(rle, ord, NORM_L2), 1.e-10);
  315. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), rle, ord);
  316. }
  317. TEST(Imgcodecs_Bmp, read_32bit_rgb)
  318. {
  319. const string root = cvtest::TS::ptr()->get_data_path();
  320. const string filenameInput = root + "readwrite/test_32bit_rgb.bmp";
  321. Mat img;
  322. ASSERT_NO_THROW(img = cv::imread(filenameInput));
  323. ASSERT_FALSE(img.empty());
  324. ASSERT_EQ(CV_8UC3, img.type());
  325. ASSERT_NO_THROW(img = cv::imread(filenameInput, IMREAD_UNCHANGED));
  326. ASSERT_FALSE(img.empty());
  327. ASSERT_EQ(CV_8UC3, img.type());
  328. ASSERT_NO_THROW(img = cv::imread(filenameInput, IMREAD_COLOR | IMREAD_ANYCOLOR | IMREAD_ANYDEPTH));
  329. ASSERT_FALSE(img.empty());
  330. ASSERT_EQ(CV_8UC3, img.type());
  331. }
  332. TEST(Imgcodecs_Bmp, rgba_bit_mask)
  333. {
  334. const string root = cvtest::TS::ptr()->get_data_path();
  335. const string filenameInput = root + "readwrite/test_rgba_mask.bmp";
  336. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  337. ASSERT_FALSE(img.empty());
  338. ASSERT_EQ(CV_8UC4, img.type());
  339. const uchar* data = img.ptr();
  340. ASSERT_EQ(data[3], 255);
  341. }
  342. TEST(Imgcodecs_Bmp, read_32bit_xrgb)
  343. {
  344. const string root = cvtest::TS::ptr()->get_data_path();
  345. const string filenameInput = root + "readwrite/test_32bit_xrgb.bmp";
  346. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  347. ASSERT_FALSE(img.empty());
  348. ASSERT_EQ(CV_8UC4, img.type());
  349. const uchar* data = img.ptr();
  350. ASSERT_EQ(data[3], 255);
  351. }
  352. TEST(Imgcodecs_Bmp, rgba_scale)
  353. {
  354. const string root = cvtest::TS::ptr()->get_data_path();
  355. const string filenameInput = root + "readwrite/test_rgba_scale.bmp";
  356. Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  357. ASSERT_FALSE(img.empty());
  358. ASSERT_EQ(CV_8UC4, img.type());
  359. uchar* data = img.ptr();
  360. ASSERT_EQ(data[0], 255);
  361. ASSERT_EQ(data[1], 255);
  362. ASSERT_EQ(data[2], 255);
  363. ASSERT_EQ(data[3], 255);
  364. img = cv::imread(filenameInput, IMREAD_COLOR);
  365. ASSERT_FALSE(img.empty());
  366. ASSERT_EQ(CV_8UC3, img.type());
  367. img = cv::imread(filenameInput, IMREAD_COLOR_RGB);
  368. ASSERT_FALSE(img.empty());
  369. ASSERT_EQ(CV_8UC3, img.type());
  370. data = img.ptr();
  371. ASSERT_EQ(data[0], 255);
  372. ASSERT_EQ(data[1], 255);
  373. ASSERT_EQ(data[2], 255);
  374. img = cv::imread(filenameInput, IMREAD_GRAYSCALE);
  375. ASSERT_FALSE(img.empty());
  376. ASSERT_EQ(CV_8UC1, img.type());
  377. data = img.ptr();
  378. ASSERT_EQ(data[0], 255);
  379. }
  380. typedef testing::TestWithParam<ImwriteBMPCompressionFlags> Imgcodecs_bmp_compress;
  381. TEST_P(Imgcodecs_bmp_compress, rgba32bpp)
  382. {
  383. const ImwriteBMPCompressionFlags comp = GetParam();
  384. RNG rng = theRNG();
  385. Mat src(256, 256, CV_8UC4);
  386. rng.fill(src, RNG::UNIFORM, Scalar(0,0,0,0), Scalar(255,255,255,255));
  387. vector<uint8_t> buf;
  388. bool ret = false;
  389. ASSERT_NO_THROW(ret = cv::imencode(".bmp", src, buf, {IMWRITE_BMP_COMPRESSION, static_cast<int>(comp)}));
  390. ASSERT_TRUE(ret);
  391. ASSERT_EQ(buf[0x0e], comp == IMWRITE_BMP_COMPRESSION_RGB ? 40 : 124 ); // the size of header
  392. ASSERT_EQ(buf[0x0f], 0);
  393. ASSERT_EQ(buf[0x1c], 32); // the number of bits per pixel = 32
  394. ASSERT_EQ(buf[0x1d], 0);
  395. ASSERT_EQ(buf[0x1e], static_cast<int>(comp)); // the compression method
  396. ASSERT_EQ(buf[0x1f], 0);
  397. ASSERT_EQ(buf[0x20], 0);
  398. ASSERT_EQ(buf[0x21], 0);
  399. Mat dst;
  400. ASSERT_NO_THROW(dst = cv::imdecode(buf, IMREAD_UNCHANGED));
  401. ASSERT_FALSE(dst.empty());
  402. if(comp == IMWRITE_BMP_COMPRESSION_RGB)
  403. {
  404. // If BI_RGB is used, output BMP file stores RGB image.
  405. ASSERT_EQ(CV_8UC3, dst.type());
  406. Mat srcBGR;
  407. cv::cvtColor(src, srcBGR, cv::COLOR_BGRA2BGR);
  408. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), srcBGR, dst);
  409. }
  410. else
  411. {
  412. // If BI_BITFIELDS is used, output BMP file stores RGBA image.
  413. ASSERT_EQ(CV_8UC4, dst.type());
  414. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), src, dst);
  415. }
  416. }
  417. INSTANTIATE_TEST_CASE_P(All,
  418. Imgcodecs_bmp_compress,
  419. testing::Values(
  420. IMWRITE_BMP_COMPRESSION_RGB,
  421. IMWRITE_BMP_COMPRESSION_BITFIELDS));
  422. #ifdef HAVE_IMGCODEC_HDR
  423. TEST(Imgcodecs_Hdr, regression)
  424. {
  425. string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";
  426. string name_rle = folder + "rle.hdr";
  427. string name_no_rle = folder + "no_rle.hdr";
  428. Mat img_rle = imread(name_rle, -1);
  429. ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle;
  430. Mat img_no_rle = imread(name_no_rle, -1);
  431. ASSERT_FALSE(img_no_rle.empty()) << "Could not open " << name_no_rle;
  432. EXPECT_EQ(cvtest::norm(img_rle, img_no_rle, NORM_INF), 0.0);
  433. string tmp_file_name = tempfile(".hdr");
  434. vector<int> param(2);
  435. param[0] = IMWRITE_HDR_COMPRESSION;
  436. for(int i = 0; i < 2; i++) {
  437. param[1] = i;
  438. imwrite(tmp_file_name, img_rle, param);
  439. Mat written_img = imread(tmp_file_name, -1);
  440. EXPECT_EQ(cvtest::norm(written_img, img_rle, NORM_INF), 0.0);
  441. }
  442. remove(tmp_file_name.c_str());
  443. }
  444. TEST(Imgcodecs_Hdr, regression_imencode)
  445. {
  446. string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";
  447. string name = folder + "rle.hdr";
  448. Mat img_ref = imread(name, -1);
  449. ASSERT_FALSE(img_ref.empty()) << "Could not open " << name;
  450. vector<int> params(2);
  451. params[0] = IMWRITE_HDR_COMPRESSION;
  452. {
  453. vector<uchar> buf;
  454. params[1] = IMWRITE_HDR_COMPRESSION_NONE;
  455. imencode(".hdr", img_ref, buf, params);
  456. Mat img = imdecode(buf, -1);
  457. EXPECT_EQ(cvtest::norm(img_ref, img, NORM_INF), 0.0);
  458. }
  459. {
  460. vector<uchar> buf;
  461. params[1] = IMWRITE_HDR_COMPRESSION_RLE;
  462. imencode(".hdr", img_ref, buf, params);
  463. Mat img = imdecode(buf, -1);
  464. EXPECT_EQ(cvtest::norm(img_ref, img, NORM_INF), 0.0);
  465. }
  466. }
  467. #endif
  468. #ifdef HAVE_IMGCODEC_PXM
  469. TEST(Imgcodecs_Pam, read_write)
  470. {
  471. string folder = string(cvtest::TS::ptr()->get_data_path()) + "readwrite/";
  472. string filepath = folder + "lena.pam";
  473. cv::Mat img = cv::imread(filepath);
  474. ASSERT_FALSE(img.empty());
  475. std::vector<int> params;
  476. params.push_back(IMWRITE_PAM_TUPLETYPE);
  477. params.push_back(IMWRITE_PAM_FORMAT_RGB);
  478. string writefile = cv::tempfile(".pam");
  479. EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
  480. cv::Mat reread = cv::imread(writefile);
  481. string writefile_no_param = cv::tempfile(".pam");
  482. EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
  483. cv::Mat reread_no_param = cv::imread(writefile_no_param);
  484. EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
  485. EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
  486. remove(writefile.c_str());
  487. remove(writefile_no_param.c_str());
  488. }
  489. #endif
  490. #ifdef HAVE_IMGCODEC_PFM
  491. TEST(Imgcodecs_Pfm, read_write)
  492. {
  493. Mat img = imread(findDataFile("readwrite/lena.pam"));
  494. ASSERT_FALSE(img.empty());
  495. img.convertTo(img, CV_32F, 1/255.0f);
  496. std::vector<int> params;
  497. string writefile = cv::tempfile(".pfm");
  498. EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
  499. cv::Mat reread = cv::imread(writefile, IMREAD_UNCHANGED);
  500. string writefile_no_param = cv::tempfile(".pfm");
  501. EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
  502. cv::Mat reread_no_param = cv::imread(writefile_no_param, IMREAD_UNCHANGED);
  503. EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
  504. EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
  505. EXPECT_EQ(0, remove(writefile.c_str()));
  506. EXPECT_EQ(0, remove(writefile_no_param.c_str()));
  507. }
  508. #endif
  509. TEST(Imgcodecs, write_parameter_type)
  510. {
  511. cv::Mat m(10, 10, CV_8UC1, cv::Scalar::all(0));
  512. cv::Mat1b m_type = cv::Mat1b::zeros(10, 10);
  513. string tmp_file = cv::tempfile(".bmp");
  514. EXPECT_NO_THROW(cv::imwrite(tmp_file, cv::Mat(m * 2))) << "* Failed with cv::Mat";
  515. EXPECT_NO_THROW(cv::imwrite(tmp_file, m * 2)) << "* Failed with cv::MatExpr";
  516. EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type)) << "* Failed with cv::Mat_";
  517. EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type * 2)) << "* Failed with cv::MatExpr(Mat_)";
  518. cv::Matx<uchar, 10, 10> matx;
  519. EXPECT_NO_THROW(cv::imwrite(tmp_file, matx)) << "* Failed with cv::Matx";
  520. EXPECT_EQ(0, remove(tmp_file.c_str()));
  521. }
  522. TEST(Imgcodecs, imdecode_user_buffer)
  523. {
  524. cv::Mat encoded = cv::Mat::zeros(1, 1024, CV_8UC1);
  525. cv::Mat user_buffer(1, 1024, CV_8UC1);
  526. cv::Mat result = cv::imdecode(encoded, IMREAD_ANYCOLOR, &user_buffer);
  527. EXPECT_TRUE(result.empty());
  528. // the function does not release user-provided buffer
  529. EXPECT_FALSE(user_buffer.empty());
  530. result = cv::imdecode(encoded, IMREAD_ANYCOLOR);
  531. EXPECT_TRUE(result.empty());
  532. }
  533. }} // namespace
  534. #if defined(HAVE_OPENEXR) && defined(OPENCV_IMGCODECS_ENABLE_OPENEXR_TESTS)
  535. #include "test_exr.impl.hpp"
  536. #endif