perf_png.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "perf_precomp.hpp"
  5. namespace opencv_test
  6. {
  7. #if defined(HAVE_PNG) || defined(HAVE_SPNG)
  8. using namespace perf;
  9. CV_ENUM(PNGStrategy, IMWRITE_PNG_STRATEGY_DEFAULT, IMWRITE_PNG_STRATEGY_FILTERED, IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY, IMWRITE_PNG_STRATEGY_RLE, IMWRITE_PNG_STRATEGY_FIXED);
  10. CV_ENUM(PNGFilters, IMWRITE_PNG_FILTER_NONE, IMWRITE_PNG_FILTER_SUB, IMWRITE_PNG_FILTER_UP, IMWRITE_PNG_FILTER_AVG, IMWRITE_PNG_FILTER_PAETH, IMWRITE_PNG_FAST_FILTERS, IMWRITE_PNG_ALL_FILTERS);
  11. typedef perf::TestBaseWithParam<testing::tuple<PNGStrategy, PNGFilters, int>> PNG;
  12. PERF_TEST(PNG, decode)
  13. {
  14. String filename = getDataPath("perf/2560x1600.png");
  15. FILE *f = fopen(filename.c_str(), "rb");
  16. fseek(f, 0, SEEK_END);
  17. long len = ftell(f);
  18. fseek(f, 0, SEEK_SET);
  19. vector<uchar> file_buf((size_t)len);
  20. EXPECT_EQ(len, (long)fread(&file_buf[0], 1, (size_t)len, f));
  21. fclose(f); f = NULL;
  22. TEST_CYCLE() imdecode(file_buf, IMREAD_UNCHANGED);
  23. SANITY_CHECK_NOTHING();
  24. }
  25. PERF_TEST(PNG, decode_rgb)
  26. {
  27. String filename = getDataPath("perf/2560x1600.png");
  28. FILE *f = fopen(filename.c_str(), "rb");
  29. fseek(f, 0, SEEK_END);
  30. long len = ftell(f);
  31. fseek(f, 0, SEEK_SET);
  32. vector<uchar> file_buf((size_t)len);
  33. EXPECT_EQ(len, (long)fread(&file_buf[0], 1, (size_t)len, f));
  34. fclose(f); f = NULL;
  35. TEST_CYCLE() imdecode(file_buf, IMREAD_COLOR_RGB);
  36. SANITY_CHECK_NOTHING();
  37. }
  38. PERF_TEST(PNG, encode)
  39. {
  40. String filename = getDataPath("perf/2560x1600.png");
  41. cv::Mat src = imread(filename);
  42. vector<uchar> buf;
  43. TEST_CYCLE() imencode(".png", src, buf);
  44. SANITY_CHECK_NOTHING();
  45. }
  46. PERF_TEST_P(PNG, params,
  47. testing::Combine(
  48. testing::Values(IMWRITE_PNG_STRATEGY_DEFAULT, IMWRITE_PNG_STRATEGY_FILTERED, IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY, IMWRITE_PNG_STRATEGY_RLE, IMWRITE_PNG_STRATEGY_FIXED),
  49. testing::Values(IMWRITE_PNG_FILTER_NONE, IMWRITE_PNG_FILTER_SUB, IMWRITE_PNG_FILTER_UP, IMWRITE_PNG_FILTER_AVG, IMWRITE_PNG_FILTER_PAETH, IMWRITE_PNG_FAST_FILTERS, IMWRITE_PNG_ALL_FILTERS),
  50. testing::Values(1, 6)))
  51. {
  52. String filename = getDataPath("perf/1920x1080.png");
  53. const int strategy = get<0>(GetParam());
  54. const int filter = get<1>(GetParam());
  55. const int level = get<2>(GetParam());
  56. Mat src = imread(filename);
  57. EXPECT_FALSE(src.empty()) << "Cannot open test image perf/1920x1080.png";
  58. vector<uchar> buf;
  59. TEST_CYCLE() imencode(".png", src, buf, { IMWRITE_PNG_COMPRESSION, level, IMWRITE_PNG_STRATEGY, strategy, IMWRITE_PNG_FILTER, filter });
  60. std::cout << " Encoded buffer size: " << buf.size()
  61. << " bytes, Compression ratio: " << std::fixed << std::setprecision(2)
  62. << (static_cast<double>(buf.size()) / (src.total() * src.channels())) * 100.0 << "%" << std::endl;
  63. SANITY_CHECK_NOTHING();
  64. }
  65. #endif // HAVE_PNG
  66. } // namespace