test_read_write.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #include "test_precomp.hpp"
  5. #include "test_common.hpp"
  6. namespace opencv_test { namespace {
  7. /* < <file_name, image_size>, <imread mode, scale> > */
  8. typedef tuple< tuple<string, Size>, tuple<ImreadModes, int> > Imgcodecs_Resize_t;
  9. typedef testing::TestWithParam< Imgcodecs_Resize_t > Imgcodecs_Resize;
  10. /* resize_flag_and_dims = <imread_flag, scale>*/
  11. const tuple <ImreadModes, int> resize_flag_and_dims[] =
  12. {
  13. make_tuple(IMREAD_UNCHANGED, 1),
  14. make_tuple(IMREAD_REDUCED_GRAYSCALE_2, 2),
  15. make_tuple(IMREAD_REDUCED_GRAYSCALE_4, 4),
  16. make_tuple(IMREAD_REDUCED_GRAYSCALE_8, 8),
  17. make_tuple(IMREAD_REDUCED_COLOR_2, 2),
  18. make_tuple(IMREAD_REDUCED_COLOR_4, 4),
  19. make_tuple(IMREAD_REDUCED_COLOR_8, 8)
  20. };
  21. const tuple<string, Size> images[] =
  22. {
  23. #ifdef HAVE_JPEG
  24. make_tuple<string, Size>("../cv/imgproc/stuff.jpg", Size(640, 480)),
  25. #endif
  26. #if defined(HAVE_PNG) || defined(HAVE_SPNG)
  27. make_tuple<string, Size>("../cv/shared/pic1.png", Size(400, 300)),
  28. #endif
  29. make_tuple<string, Size>("../highgui/readwrite/ordinary.bmp", Size(480, 272)),
  30. };
  31. TEST_P(Imgcodecs_Resize, imread_reduce_flags)
  32. {
  33. const string file_name = findDataFile(get<0>(get<0>(GetParam())));
  34. const Size imageSize = get<1>(get<0>(GetParam()));
  35. const int imread_flag = get<0>(get<1>(GetParam()));
  36. const int scale = get<1>(get<1>(GetParam()));
  37. const int cols = imageSize.width / scale;
  38. const int rows = imageSize.height / scale;
  39. {
  40. Mat img = imread(file_name, imread_flag);
  41. ASSERT_FALSE(img.empty());
  42. EXPECT_EQ(cols, img.cols);
  43. EXPECT_EQ(rows, img.rows);
  44. }
  45. }
  46. //==================================================================================================
  47. TEST_P(Imgcodecs_Resize, imdecode_reduce_flags)
  48. {
  49. const string file_name = findDataFile(get<0>(get<0>(GetParam())));
  50. const Size imageSize = get<1>(get<0>(GetParam()));
  51. const int imread_flag = get<0>(get<1>(GetParam()));
  52. const int scale = get<1>(get<1>(GetParam()));
  53. const int cols = imageSize.width / scale;
  54. const int rows = imageSize.height / scale;
  55. const std::ios::openmode mode = std::ios::in | std::ios::binary;
  56. std::ifstream ifs(file_name.c_str(), mode);
  57. ASSERT_TRUE(ifs.is_open());
  58. ifs.seekg(0, std::ios::end);
  59. const size_t sz = static_cast<size_t>(ifs.tellg());
  60. ifs.seekg(0, std::ios::beg);
  61. std::vector<char> content(sz);
  62. ifs.read((char*)content.data(), sz);
  63. ASSERT_FALSE(ifs.fail());
  64. {
  65. Mat img = imdecode(Mat(content), imread_flag);
  66. ASSERT_FALSE(img.empty());
  67. EXPECT_EQ(cols, img.cols);
  68. EXPECT_EQ(rows, img.rows);
  69. }
  70. }
  71. //==================================================================================================
  72. INSTANTIATE_TEST_CASE_P(/*nothing*/, Imgcodecs_Resize,
  73. testing::Combine(
  74. testing::ValuesIn(images),
  75. testing::ValuesIn(resize_flag_and_dims)
  76. )
  77. );
  78. //==================================================================================================
  79. TEST(Imgcodecs_Image, read_write_bmp)
  80. {
  81. const size_t IMAGE_COUNT = 10;
  82. const double thresDbell = 32;
  83. for (size_t i = 0; i < IMAGE_COUNT; ++i)
  84. {
  85. stringstream s; s << i;
  86. const string digit = s.str();
  87. const string src_name = TS::ptr()->get_data_path() + "../python/images/QCIF_0" + digit + ".bmp";
  88. const string dst_name = cv::tempfile((digit + ".bmp").c_str());
  89. Mat image = imread(src_name);
  90. ASSERT_FALSE(image.empty());
  91. resize(image, image, Size(968, 757), 0.0, 0.0, INTER_CUBIC);
  92. imwrite(dst_name, image);
  93. Mat loaded = imread(dst_name);
  94. ASSERT_FALSE(loaded.empty());
  95. double psnr = cvtest::PSNR(loaded, image);
  96. EXPECT_GT(psnr, thresDbell);
  97. vector<uchar> from_file;
  98. FILE *f = fopen(dst_name.c_str(), "rb");
  99. fseek(f, 0, SEEK_END);
  100. long len = ftell(f);
  101. from_file.resize((size_t)len);
  102. fseek(f, 0, SEEK_SET);
  103. from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
  104. fclose(f);
  105. vector<uchar> buf;
  106. imencode(".bmp", image, buf);
  107. ASSERT_EQ(buf, from_file);
  108. Mat buf_loaded = imdecode(Mat(buf), 1);
  109. ASSERT_FALSE(buf_loaded.empty());
  110. psnr = cvtest::PSNR(buf_loaded, image);
  111. EXPECT_GT(psnr, thresDbell);
  112. EXPECT_EQ(0, remove(dst_name.c_str()));
  113. }
  114. }
  115. //==================================================================================================
  116. typedef string Ext;
  117. typedef testing::TestWithParam<Ext> Imgcodecs_Image;
  118. const string exts[] = {
  119. #if defined(HAVE_PNG) || defined(HAVE_SPNG)
  120. "png",
  121. #endif
  122. #ifdef HAVE_TIFF
  123. "tiff",
  124. #endif
  125. #ifdef HAVE_JPEG
  126. "jpg",
  127. #endif
  128. #ifdef HAVE_JPEGXL
  129. "jxl",
  130. #endif
  131. #if (defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)) \
  132. || defined(HAVE_OPENJPEG)
  133. "jp2",
  134. #endif
  135. #if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/
  136. "exr",
  137. #endif
  138. "bmp",
  139. #ifdef HAVE_IMGCODEC_PXM
  140. "ppm",
  141. #endif
  142. #ifdef HAVE_IMGCODEC_SUNRASTER
  143. "ras",
  144. #endif
  145. };
  146. static
  147. void test_image_io(const Mat& image, const std::string& fname, const std::string& ext, int imreadFlag, double psnrThreshold)
  148. {
  149. vector<uchar> buf;
  150. ASSERT_NO_THROW(imencode("." + ext, image, buf));
  151. ASSERT_NO_THROW(imwrite(fname, image));
  152. FILE *f = fopen(fname.c_str(), "rb");
  153. fseek(f, 0, SEEK_END);
  154. long len = ftell(f);
  155. cout << "File size: " << len << " bytes" << endl;
  156. EXPECT_GT(len, 1024) << "File is small. Test or implementation is broken";
  157. fseek(f, 0, SEEK_SET);
  158. vector<uchar> file_buf((size_t)len);
  159. EXPECT_EQ(len, (long)fread(&file_buf[0], 1, (size_t)len, f));
  160. fclose(f); f = NULL;
  161. EXPECT_EQ(buf, file_buf) << "imwrite() / imencode() calls must provide the same output (bit-exact)";
  162. Mat buf_loaded = imdecode(Mat(buf), imreadFlag);
  163. EXPECT_FALSE(buf_loaded.empty());
  164. if (imreadFlag & IMREAD_COLOR_RGB && imreadFlag != -1)
  165. {
  166. cvtColor(buf_loaded, buf_loaded, COLOR_RGB2BGR);
  167. }
  168. Mat loaded = imread(fname, imreadFlag);
  169. EXPECT_FALSE(loaded.empty());
  170. if (imreadFlag & IMREAD_COLOR_RGB && imreadFlag != -1)
  171. {
  172. cvtColor(loaded, loaded, COLOR_RGB2BGR);
  173. }
  174. EXPECT_EQ(0, cv::norm(loaded, buf_loaded, NORM_INF)) << "imread() and imdecode() calls must provide the same result (bit-exact)";
  175. double psnr = cvtest::PSNR(loaded, image);
  176. EXPECT_GT(psnr, psnrThreshold);
  177. // not necessary due bitexact check above
  178. //double buf_psnr = cvtest::PSNR(buf_loaded, image);
  179. //EXPECT_GT(buf_psnr, psnrThreshold);
  180. #if 0 // debug
  181. if (psnr <= psnrThreshold /*|| buf_psnr <= thresDbell*/)
  182. {
  183. cout << "File: " << fname << endl;
  184. imshow("origin", image);
  185. imshow("imread", loaded);
  186. imshow("imdecode", buf_loaded);
  187. waitKey();
  188. }
  189. #endif
  190. }
  191. TEST_P(Imgcodecs_Image, read_write_BGR)
  192. {
  193. const string ext = this->GetParam();
  194. const string fname = cv::tempfile(ext.c_str());
  195. double psnrThreshold = 100;
  196. if (ext == "jpg")
  197. psnrThreshold = 32;
  198. if (ext == "jxl")
  199. psnrThreshold = 30;
  200. #if defined(HAVE_JASPER)
  201. if (ext == "jp2")
  202. psnrThreshold = 95;
  203. #elif defined(HAVE_OPENJPEG)
  204. if (ext == "jp2")
  205. psnrThreshold = 35;
  206. #endif
  207. Mat image = generateTestImageBGR();
  208. EXPECT_NO_THROW(test_image_io(image, fname, ext, IMREAD_COLOR, psnrThreshold));
  209. EXPECT_NO_THROW(test_image_io(image, fname, ext, IMREAD_COLOR_RGB, psnrThreshold));
  210. EXPECT_EQ(0, remove(fname.c_str()));
  211. }
  212. TEST_P(Imgcodecs_Image, read_write_GRAYSCALE)
  213. {
  214. const string ext = this->GetParam();
  215. if (false
  216. || ext == "ppm" // grayscale is not implemented
  217. || ext == "ras" // broken (black result)
  218. )
  219. throw SkipTestException("GRAYSCALE mode is not supported");
  220. const string fname = cv::tempfile(ext.c_str());
  221. double psnrThreshold = 100;
  222. if (ext == "jpg")
  223. psnrThreshold = 40;
  224. if (ext == "jxl")
  225. psnrThreshold = 40;
  226. #if defined(HAVE_JASPER)
  227. if (ext == "jp2")
  228. psnrThreshold = 70;
  229. #elif defined(HAVE_OPENJPEG)
  230. if (ext == "jp2")
  231. psnrThreshold = 35;
  232. #endif
  233. Mat image = generateTestImageGrayscale();
  234. EXPECT_NO_THROW(test_image_io(image, fname, ext, IMREAD_GRAYSCALE, psnrThreshold));
  235. EXPECT_EQ(0, remove(fname.c_str()));
  236. }
  237. INSTANTIATE_TEST_CASE_P(imgcodecs, Imgcodecs_Image, testing::ValuesIn(exts));
  238. TEST(Imgcodecs_Image, regression_9376)
  239. {
  240. String path = findDataFile("readwrite/regression_9376.bmp");
  241. Mat m = imread(path);
  242. ASSERT_FALSE(m.empty());
  243. EXPECT_EQ(32, m.cols);
  244. EXPECT_EQ(32, m.rows);
  245. }
  246. TEST(Imgcodecs_Image, imread_overload)
  247. {
  248. const string root = cvtest::TS::ptr()->get_data_path();
  249. const string imgName = findDataFile("../highgui/readwrite/ordinary.bmp");
  250. Mat ref = imread(imgName);
  251. ASSERT_FALSE(ref.empty());
  252. {
  253. Mat img(ref.size(), ref.type(), Scalar::all(0)); // existing image
  254. void * ptr = img.data;
  255. imread(imgName, img);
  256. ASSERT_FALSE(img.empty());
  257. EXPECT_EQ(cv::norm(ref, img, NORM_INF), 0);
  258. EXPECT_EQ(img.data, ptr); // no reallocation
  259. }
  260. {
  261. Mat img; // empty image
  262. imread(imgName, img);
  263. ASSERT_FALSE(img.empty());
  264. EXPECT_EQ(cv::norm(ref, img, NORM_INF), 0);
  265. }
  266. {
  267. UMat img; // empty UMat
  268. imread(imgName, img);
  269. ASSERT_FALSE(img.empty());
  270. EXPECT_EQ(cv::norm(ref, img, NORM_INF), 0);
  271. }
  272. }
  273. //==================================================================================================
  274. TEST(Imgcodecs_Image, write_umat)
  275. {
  276. const string src_name = TS::ptr()->get_data_path() + "../python/images/baboon.bmp";
  277. const string dst_name = cv::tempfile(".bmp");
  278. Mat image1 = imread(src_name);
  279. ASSERT_FALSE(image1.empty());
  280. UMat image1_umat = image1.getUMat(ACCESS_RW);
  281. imwrite(dst_name, image1_umat);
  282. Mat image2 = imread(dst_name);
  283. ASSERT_FALSE(image2.empty());
  284. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), image1, image2);
  285. EXPECT_EQ(0, remove(dst_name.c_str()));
  286. }
  287. #ifdef HAVE_TIFF
  288. TEST(Imgcodecs_Image, multipage_collection_size)
  289. {
  290. const string root = cvtest::TS::ptr()->get_data_path();
  291. const string filename = root + "readwrite/multipage.tif";
  292. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  293. EXPECT_EQ((std::size_t)6, collection.size());
  294. }
  295. TEST(Imgcodecs_Image, multipage_collection_read_pages_iterator)
  296. {
  297. const string root = cvtest::TS::ptr()->get_data_path();
  298. const string filename = root + "readwrite/multipage.tif";
  299. const string page_files[] = {
  300. root + "readwrite/multipage_p1.tif",
  301. root + "readwrite/multipage_p2.tif",
  302. root + "readwrite/multipage_p3.tif",
  303. root + "readwrite/multipage_p4.tif",
  304. root + "readwrite/multipage_p5.tif",
  305. root + "readwrite/multipage_p6.tif"
  306. };
  307. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  308. auto collectionBegin = collection.begin();
  309. for(size_t i = 0; i < collection.size(); ++i, ++collectionBegin)
  310. {
  311. double diff = cv::norm(collectionBegin.operator*(), imread(page_files[i]), NORM_INF);
  312. EXPECT_EQ(0., diff);
  313. }
  314. }
  315. TEST(Imgcodecs_Image, multipage_collection_two_iterator)
  316. {
  317. const string root = cvtest::TS::ptr()->get_data_path();
  318. const string filename = root + "readwrite/multipage.tif";
  319. const string page_files[] = {
  320. root + "readwrite/multipage_p1.tif",
  321. root + "readwrite/multipage_p2.tif",
  322. root + "readwrite/multipage_p3.tif",
  323. root + "readwrite/multipage_p4.tif",
  324. root + "readwrite/multipage_p5.tif",
  325. root + "readwrite/multipage_p6.tif"
  326. };
  327. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  328. auto firstIter = collection.begin();
  329. auto secondIter = collection.begin();
  330. // Decode all odd pages then decode even pages -> 1, 0, 3, 2 ...
  331. firstIter++;
  332. for(size_t i = 1; i < collection.size(); i += 2, ++firstIter, ++firstIter, ++secondIter, ++secondIter) {
  333. Mat mat = *firstIter;
  334. double diff = cv::norm(mat, imread(page_files[i]), NORM_INF);
  335. EXPECT_EQ(0., diff);
  336. Mat evenMat = *secondIter;
  337. diff = cv::norm(evenMat, imread(page_files[i-1]), NORM_INF);
  338. EXPECT_EQ(0., diff);
  339. }
  340. }
  341. TEST(Imgcodecs_Image, multipage_collection_operator_plusplus)
  342. {
  343. const string root = cvtest::TS::ptr()->get_data_path();
  344. const string filename = root + "readwrite/multipage.tif";
  345. // operator++ test
  346. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  347. auto firstIter = collection.begin();
  348. auto secondIter = firstIter++;
  349. // firstIter points to second page, secondIter points to first page
  350. double diff = cv::norm(*firstIter, *secondIter, NORM_INF);
  351. EXPECT_NE(diff, 0.);
  352. }
  353. TEST(Imgcodecs_Image, multipage_collection_backward_decoding)
  354. {
  355. const string root = cvtest::TS::ptr()->get_data_path();
  356. const string filename = root + "readwrite/multipage.tif";
  357. const string page_files[] = {
  358. root + "readwrite/multipage_p1.tif",
  359. root + "readwrite/multipage_p2.tif",
  360. root + "readwrite/multipage_p3.tif",
  361. root + "readwrite/multipage_p4.tif",
  362. root + "readwrite/multipage_p5.tif",
  363. root + "readwrite/multipage_p6.tif"
  364. };
  365. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  366. EXPECT_EQ((size_t)6, collection.size());
  367. // backward decoding -> 5,4,3,2,1,0
  368. for(int i = (int)collection.size() - 1; i >= 0; --i)
  369. {
  370. cv::Mat ithPage = imread(page_files[i]);
  371. EXPECT_FALSE(ithPage.empty());
  372. double diff = cv::norm(collection[i], ithPage, NORM_INF);
  373. EXPECT_EQ(diff, 0.);
  374. }
  375. for(int i = 0; i < (int)collection.size(); ++i)
  376. {
  377. collection.releaseCache(i);
  378. }
  379. double diff = cv::norm(collection[2], imread(page_files[2]), NORM_INF);
  380. EXPECT_EQ(diff, 0.);
  381. }
  382. TEST(ImgCodecs, multipage_collection_decoding_range_based_for_loop_test)
  383. {
  384. const string root = cvtest::TS::ptr()->get_data_path();
  385. const string filename = root + "readwrite/multipage.tif";
  386. const string page_files[] = {
  387. root + "readwrite/multipage_p1.tif",
  388. root + "readwrite/multipage_p2.tif",
  389. root + "readwrite/multipage_p3.tif",
  390. root + "readwrite/multipage_p4.tif",
  391. root + "readwrite/multipage_p5.tif",
  392. root + "readwrite/multipage_p6.tif"
  393. };
  394. ImageCollection collection(filename, IMREAD_ANYCOLOR);
  395. size_t index = 0;
  396. for(auto &i: collection)
  397. {
  398. cv::Mat ithPage = imread(page_files[index]);
  399. EXPECT_FALSE(ithPage.empty());
  400. double diff = cv::norm(i, ithPage, NORM_INF);
  401. EXPECT_EQ(0., diff);
  402. ++index;
  403. }
  404. EXPECT_EQ(index, collection.size());
  405. index = 0;
  406. for(auto &&i: collection)
  407. {
  408. cv::Mat ithPage = imread(page_files[index]);
  409. EXPECT_FALSE(ithPage.empty());
  410. double diff = cv::norm(i, ithPage, NORM_INF);
  411. EXPECT_EQ(0., diff);
  412. ++index;
  413. }
  414. EXPECT_EQ(index, collection.size());
  415. }
  416. TEST(ImgCodecs, multipage_collection_two_iterator_operatorpp)
  417. {
  418. const string root = cvtest::TS::ptr()->get_data_path();
  419. const string filename = root + "readwrite/multipage.tif";
  420. ImageCollection imcol(filename, IMREAD_ANYCOLOR);
  421. auto it0 = imcol.begin(), it1 = it0, it2 = it0;
  422. vector<Mat> img(6);
  423. for (int i = 0; i < 6; i++) {
  424. img[i] = *it0;
  425. it0->release();
  426. ++it0;
  427. }
  428. for (int i = 0; i < 3; i++) {
  429. ++it2;
  430. }
  431. for (int i = 0; i < 3; i++) {
  432. auto img2 = *it2;
  433. auto img1 = *it1;
  434. ++it2;
  435. ++it1;
  436. EXPECT_TRUE(cv::norm(img2, img[i+3], NORM_INF) == 0);
  437. EXPECT_TRUE(cv::norm(img1, img[i], NORM_INF) == 0);
  438. }
  439. }
  440. // See https://github.com/opencv/opencv/issues/26207
  441. TEST(Imgcodecs, imencodemulti_regression_26207)
  442. {
  443. vector<Mat> imgs;
  444. const cv::Mat img(100, 100, CV_8UC1, cv::Scalar::all(0));
  445. imgs.push_back(img);
  446. std::vector<uchar> buf;
  447. bool ret = false;
  448. // Encode single image
  449. EXPECT_NO_THROW(ret = imencode(".tiff", img, buf));
  450. EXPECT_TRUE(ret);
  451. EXPECT_NO_THROW(ret = imencode(".tiff", imgs, buf));
  452. EXPECT_TRUE(ret);
  453. EXPECT_NO_THROW(ret = imencodemulti(".tiff", imgs, buf));
  454. EXPECT_TRUE(ret);
  455. // Encode multiple images
  456. imgs.push_back(img.clone());
  457. EXPECT_NO_THROW(ret = imencode(".tiff", imgs, buf));
  458. EXPECT_TRUE(ret);
  459. EXPECT_NO_THROW(ret = imencodemulti(".tiff", imgs, buf));
  460. EXPECT_TRUE(ret);
  461. // Count stored images from buffer.
  462. // imcount() doesn't support buffer, so encoded buffer outputs to file temporary.
  463. const size_t len = buf.size();
  464. const string filename = cv::tempfile(".tiff");
  465. FILE *f = fopen(filename.c_str(), "wb");
  466. EXPECT_NE(f, nullptr);
  467. EXPECT_EQ(len, fwrite(&buf[0], 1, len, f));
  468. fclose(f);
  469. EXPECT_EQ(2, (int)imcount(filename));
  470. EXPECT_EQ(0, remove(filename.c_str()));
  471. }
  472. #endif
  473. // See https://github.com/opencv/opencv/pull/26211
  474. // ( related with https://github.com/opencv/opencv/issues/26207 )
  475. TEST(Imgcodecs, imencode_regression_26207_extra)
  476. {
  477. // CV_32F is not supported depth for BMP Encoder.
  478. // Encoded buffer contains CV_8U image which is fallbacked.
  479. const cv::Mat src(100, 100, CV_32FC1, cv::Scalar::all(0));
  480. std::vector<uchar> buf;
  481. bool ret = false;
  482. EXPECT_NO_THROW(ret = imencode(".bmp", src, buf));
  483. EXPECT_TRUE(ret);
  484. cv::Mat dst;
  485. EXPECT_NO_THROW(dst = imdecode(buf, IMREAD_GRAYSCALE));
  486. EXPECT_FALSE(dst.empty());
  487. EXPECT_EQ(CV_8UC1, dst.type());
  488. }
  489. TEST(Imgcodecs, imwrite_regression_26207_extra)
  490. {
  491. // CV_32F is not supported depth for BMP Encoder.
  492. // Encoded buffer contains CV_8U image which is fallbacked.
  493. const cv::Mat src(100, 100, CV_32FC1, cv::Scalar::all(0));
  494. const string filename = cv::tempfile(".bmp");
  495. bool ret = false;
  496. EXPECT_NO_THROW(ret = imwrite(filename, src));
  497. EXPECT_TRUE(ret);
  498. cv::Mat dst;
  499. EXPECT_NO_THROW(dst = imread(filename, IMREAD_GRAYSCALE));
  500. EXPECT_FALSE(dst.empty());
  501. EXPECT_EQ(CV_8UC1, dst.type());
  502. EXPECT_EQ(0, remove(filename.c_str()));
  503. }
  504. TEST(Imgcodecs_Params, imwrite_regression_22752)
  505. {
  506. const Mat img(16, 16, CV_8UC3, cv::Scalar::all(0));
  507. vector<int> params;
  508. params.push_back(IMWRITE_JPEG_QUALITY);
  509. // params.push_back(100)); // Forget it.
  510. EXPECT_ANY_THROW(cv::imwrite("test.jpg", img, params)); // parameters size or missing JPEG codec
  511. }
  512. TEST(Imgcodecs_Params, imencode_regression_22752)
  513. {
  514. const Mat img(16, 16, CV_8UC3, cv::Scalar::all(0));
  515. vector<int> params;
  516. params.push_back(IMWRITE_JPEG_QUALITY);
  517. // params.push_back(100)); // Forget it.
  518. vector<uchar> buf;
  519. EXPECT_ANY_THROW(cv::imencode("test.jpg", img, buf, params)); // parameters size or missing JPEG codec
  520. }
  521. TEST(Imgcodecs, decode_over2GB)
  522. {
  523. applyTestTag(CV_TEST_TAG_MEMORY_6GB);
  524. // empty buffer more than 2GB size
  525. std::vector<uint8_t> buf(size_t(INT_MAX) + 4096);
  526. cv::Mat dst;
  527. EXPECT_THROW(dst = cv::imdecode(buf, cv::IMREAD_COLOR), cv::Exception);
  528. }
  529. }} // namespace