test_fixture.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #include <boost/gil.hpp>
  9. #include <boost/assert.hpp>
  10. #include <cstdint>
  11. #include <initializer_list>
  12. #include <limits>
  13. #include <random>
  14. #include <tuple>
  15. #include <type_traits>
  16. namespace boost { namespace gil {
  17. namespace test { namespace fixture {
  18. using image_types = std::tuple
  19. <
  20. gil::gray8_image_t,
  21. gil::gray16_image_t,
  22. gil::gray32_image_t,
  23. gil::bgr8_image_t,
  24. gil::bgr16_image_t,
  25. gil::bgr32_image_t,
  26. gil::rgb8_image_t,
  27. gil::rgb16_image_t,
  28. gil::rgb32_image_t,
  29. gil::rgba8_image_t,
  30. gil::rgba16_image_t,
  31. gil::rgba32_image_t
  32. >;
  33. template <typename T>
  34. struct consecutive_value
  35. {
  36. consecutive_value(T start) : current_(start)
  37. {
  38. BOOST_TEST(static_cast<int>(current_) >= 0);
  39. }
  40. T operator()()
  41. {
  42. BOOST_ASSERT(static_cast<int>(current_) + 1 > 0);
  43. return current_++;
  44. }
  45. T current_;
  46. };
  47. template <typename T>
  48. struct reverse_consecutive_value
  49. {
  50. reverse_consecutive_value(T start) : current_(start)
  51. {
  52. BOOST_ASSERT(static_cast<int>(current_) > 0);
  53. }
  54. T operator()()
  55. {
  56. BOOST_ASSERT(static_cast<int>(current_) + 1 >= 0);
  57. return current_--;
  58. }
  59. T current_;
  60. };
  61. template <typename T>
  62. struct random_value
  63. {
  64. static_assert(std::is_integral<T>::value, "T must be integral type");
  65. static constexpr auto range_min = std::numeric_limits<T>::min();
  66. static constexpr auto range_max = std::numeric_limits<T>::max();
  67. random_value() : rng_(rd_()), uid_(range_min, range_max) {}
  68. T operator()()
  69. {
  70. auto value = uid_(rng_);
  71. BOOST_ASSERT(range_min <= value && value <= range_max);
  72. return static_cast<T>(value);
  73. }
  74. std::random_device rd_;
  75. std::mt19937 rng_;
  76. std::uniform_int_distribution<typename gil::promote_integral<T>::type> uid_;
  77. };
  78. template <typename Image, typename Generator>
  79. auto generate_image(std::ptrdiff_t size_x, std::ptrdiff_t size_y, Generator&& generate) -> Image
  80. {
  81. using pixel_t = typename Image::value_type;
  82. Image out(size_x, size_y);
  83. gil::for_each_pixel(view(out), [&generate](pixel_t& p) {
  84. gil::static_generate(p, [&generate]() { return generate(); });
  85. });
  86. return out;
  87. }
  88. template <typename Image>
  89. auto create_image(std::ptrdiff_t size_x, std::ptrdiff_t size_y, int channel_value) -> Image
  90. {
  91. using pixel_t = typename Image::value_type;
  92. using channel_t = typename gil::channel_type<pixel_t>::type;
  93. static_assert(std::is_integral<channel_t>::value, "channel must be integral type");
  94. Image out(size_x, size_y);
  95. gil::for_each_pixel(view(out), [&channel_value](pixel_t& p) {
  96. gil::static_fill(p, static_cast<channel_t>(channel_value));
  97. });
  98. return out;
  99. }
  100. }}}} // namespace boost::gil::test::fixture