indexed_image_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright 2013 Christian Henning
  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/gil/extension/toolbox/image_types/indexed_image.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <cstdint>
  13. namespace bg = boost::gil;
  14. BOOST_AUTO_TEST_SUITE(toolbox_tests)
  15. BOOST_AUTO_TEST_CASE(index_image_test)
  16. {
  17. auto const pixel_generator = []() -> bg::rgb8_pixel_t {
  18. static int i = 0;
  19. i = (i > 255) ? 0 : (i + 1);
  20. auto const i8 = static_cast<std::uint8_t>(i);
  21. return bg::rgb8_pixel_t(i8, i8, i8);
  22. };
  23. {
  24. bg::indexed_image<std::uint8_t, bg::rgb8_pixel_t> img(640, 480);
  25. bg::fill_pixels(bg::view(img), bg::rgb8_pixel_t(255, 0, 0));
  26. bg::rgb8_pixel_t const p = *bg::view(img).xy_at(10, 10);
  27. BOOST_TEST(p[0] == 255);
  28. }
  29. {
  30. using image_t = bg::indexed_image<bg::gray8_pixel_t, bg::rgb8_pixel_t>;
  31. image_t img(640, 480, 256);
  32. generate_pixels(img.get_indices_view(), []() -> bg::gray8_pixel_t
  33. {
  34. static int i = 0;
  35. i = (i > 255) ? 0 : (i + 1);
  36. auto const i8 = static_cast<std::uint8_t>(i);
  37. return bg::gray8_pixel_t(i8);
  38. });
  39. generate_pixels(img.get_palette_view(), pixel_generator);
  40. bg::gray8_pixel_t index{0};
  41. index = *img.get_indices_view().xy_at(0, 0); // verify values along first row
  42. BOOST_TEST(static_cast<int>(index) == (0 + 1));
  43. index = *img.get_indices_view().xy_at(128, 0);
  44. BOOST_TEST(static_cast<int>(index) == (128 + 1));
  45. // verify wrapping of value by the pixels generator above
  46. index = *img.get_indices_view().xy_at(255, 0);
  47. BOOST_TEST(static_cast<int>(index) == 0);
  48. // access via member function
  49. bg::rgb8_pixel_t const pixel1 = *img.get_palette_view().xy_at(index, 0);
  50. BOOST_TEST(pixel1[0] == pixel1[1]);
  51. BOOST_TEST(pixel1[1] == pixel1[2]);
  52. // access via free function
  53. bg::rgb8_pixel_t const pixel2 = *bg::view(img).xy_at(10, 1);
  54. BOOST_TEST(pixel2[0] == pixel2[1]);
  55. BOOST_TEST(pixel2[1] == pixel2[2]);
  56. }
  57. {
  58. using image_t = bg::indexed_image<bg::gray8_pixel_t, bg::rgb8_pixel_t>;
  59. image_t img(640, 480, 256);
  60. generate_pixels(img.get_indices_view(), []() -> uint8_t
  61. {
  62. static int i = 0;
  63. i = (i > 255) ? 0 : (i + 1);
  64. return static_cast<std::uint8_t>(i);
  65. });
  66. generate_pixels(img.get_palette_view(), pixel_generator);
  67. std::uint8_t index = *img.get_indices_view().xy_at(128, 0);
  68. BOOST_TEST(static_cast<int>(index) == (128 + 1));
  69. bg::rgb8_pixel_t const pixel1 = *img.get_palette_view().xy_at(index, 0);
  70. BOOST_TEST(pixel1[0] == pixel1[1]);
  71. BOOST_TEST(pixel1[1] == pixel1[2]);
  72. bg::rgb8_pixel_t const pixel2 = *view(img).xy_at(10, 1);
  73. BOOST_TEST(pixel2[0] == pixel2[1]);
  74. BOOST_TEST(pixel2[1] == pixel2[2]);
  75. }
  76. {
  77. using image_t = bg::indexed_image<std::uint8_t, bg::rgb8_pixel_t>;
  78. image_t img(640, 480, 256);
  79. for (image_t::y_coord_t y = 0; y < bg::view(img).height(); ++y)
  80. {
  81. image_t::view_t::x_iterator it = bg::view(img).row_begin(y);
  82. for (image_t::x_coord_t x = 0; x < bg::view(img).width(); ++x)
  83. {
  84. bg::rgb8_pixel_t p = *it;
  85. boost::ignore_unused(p);
  86. it++;
  87. }
  88. }
  89. // TODO: No checks? ~mloskot
  90. }
  91. }
  92. BOOST_AUTO_TEST_CASE(index_image_view_test)
  93. {
  94. // generate some data
  95. std::size_t const width = 640;
  96. std::size_t const height = 480;
  97. std::size_t const num_colors = 3;
  98. std::uint8_t const index = 2;
  99. // indices
  100. std::vector<std::uint8_t> indices(width * height, index);
  101. // colors
  102. std::vector<bg::rgb8_pixel_t> palette(num_colors);
  103. palette[0] = bg::rgb8_pixel_t(10, 20, 30);
  104. palette[1] = bg::rgb8_pixel_t(40, 50, 60);
  105. palette[2] = bg::rgb8_pixel_t(70, 80, 90);
  106. // create image views from raw memory
  107. auto indices_view = bg::interleaved_view(width, height,
  108. (bg::gray8_image_t::view_t::x_iterator) indices.data(),
  109. width); // row size in bytes
  110. auto palette_view = bg::interleaved_view(100, 1,
  111. (bg::rgb8_image_t::view_t::x_iterator) palette.data(),
  112. num_colors * 3); // row size in bytes
  113. auto ii_view = bg::view(indices_view, palette_view);
  114. auto p = ii_view(bg::point_t(0, 0));
  115. auto q = *ii_view.at(bg::point_t(0, 0));
  116. BOOST_ASSERT(bg::get_color(p, bg::red_t()) == 70);
  117. BOOST_ASSERT(bg::get_color(p, bg::green_t()) == 80);
  118. BOOST_ASSERT(bg::get_color(p, bg::blue_t()) == 90);
  119. BOOST_ASSERT(bg::get_color(q, bg::red_t()) == 70);
  120. BOOST_ASSERT(bg::get_color(q, bg::green_t()) == 80);
  121. BOOST_ASSERT(bg::get_color(q, bg::blue_t()) == 90);
  122. }
  123. BOOST_AUTO_TEST_SUITE_END()