Skip to content

Commit

Permalink
Make the following changes:
Browse files Browse the repository at this point in the history
 - Separate histogram fill tests from utilities

 - Add helper impl tuple limit
  • Loading branch information
codejaeger committed Jul 30, 2020
1 parent 6789232 commit 27c9351
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 205 deletions.
4 changes: 4 additions & 0 deletions example/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,9 @@ int main() {
}
cmyk8_image_t g;
std::vector<std::vector<bool>> d = {{1}};

std::cout<<"\n";
std::cout<<int(std::get<0>(boost::gil::detail::tuple_limit<std::tuple<unsigned char>>::min()))<<std::endl;
std::cout<<int(std::get<1>(boost::gil::detail::tuple_limit<std::tuple<unsigned char, int>>::min()));
return 0;
}
35 changes: 31 additions & 4 deletions include/boost/gil/histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ bool tuple_compare(Tuple const& t1, Tuple const& t2)
return tuple_compare(t1, t2, index_list);
}

template<typename Tuple>
struct tuple_limit
{
static constexpr Tuple min()
{
return min_impl(boost::mp11::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
static constexpr Tuple max()
{
return max_impl(boost::mp11::make_index_sequence<std::tuple_size<Tuple>::value>{});
}

private:

template<std::size_t... I>
static constexpr Tuple min_impl(boost::mp11::index_sequence<I...>)
{
return std::make_tuple(std::numeric_limits<typename std::tuple_element<I, Tuple>::type>::min()...);
}

template<std::size_t... I>
static constexpr Tuple max_impl(boost::mp11::index_sequence<I...>)
{
return std::make_tuple(std::numeric_limits<typename std::tuple_element<I, Tuple>::type>::max()...);
}
};

template<std::size_t Dimension>
struct filler
{
Expand All @@ -121,7 +148,7 @@ struct filler<1>
template<typename Container, typename Tuple>
void operator()(Container& hist, Tuple& lower, Tuple& upper)
{
for(auto i = std::get<0>(lower); i < std::get<0>(upper); ++i)
for(int i = std::get<0>(lower); i < std::get<0>(upper); ++i)
{
hist[i] = 0;
}
Expand Down Expand Up @@ -418,7 +445,7 @@ class histogram : public std::unordered_map<std::tuple<T...>, double, detail::ha
std::for_each(base_t::begin(), base_t::end(), [&](value_t const& v) {
sum += v.second;
});
std::cout<<(long int)sum<<"asfe";
// std::cout<<(long int)sum<<"asfe";
std::for_each(base_t::begin(), base_t::end(), [&](value_t const& v) {
base_t::operator[](v.first) = v.second / sum;
});
Expand Down Expand Up @@ -490,8 +517,8 @@ void fill_histogram(
bool sparsefill = true,
bool applymask = false,
std::vector<std::vector<bool>> mask = {},
typename histogram<T...>::key_type lower = {},
typename histogram<T...>::key_type upper = {},
typename histogram<T...>::key_type lower = detail::tuple_limit<typename histogram<T...>::key_type>::min(),
typename histogram<T...>::key_type upper = detail::tuple_limit<typename histogram<T...>::key_type>::max(),
bool setlimits = false)
{
if (!accumulate)
Expand Down
1 change: 1 addition & 0 deletions test/core/histogram/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ foreach(_name
constructor
cumulative
dimension
fill
hash_tuple
helpers
is_compatible
Expand Down
1 change: 1 addition & 0 deletions test/core/histogram/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ compile constructor.cpp ;
compile dimension.cpp ;
run access.cpp ;
run cumulative.cpp ;
run fill.cpp ;
run hash_tuple.cpp ;
run helpers.cpp ;
run is_compatible.cpp ;
Expand Down
237 changes: 237 additions & 0 deletions test/core/histogram/fill.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
//
// Copyright 2020 Debabrata Mandal <mandaldebabrata123@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//

#include <boost/gil/histogram.hpp>
#include <boost/gil/image.hpp>
#include <boost/gil/image_view.hpp>
#include <boost/gil/typedefs.hpp>

#include <boost/core/lightweight_test.hpp>
#include <iostream>

namespace gil = boost::gil;
namespace mp11 = boost::mp11;

gil::gray8_image_t img1(4, 4, gil::gray8_pixel_t(1));
gil::gray8_view_t v1 = view(img1);

gil::rgb8_image_t img2(4, 4, gil::rgb8_pixel_t(1));
gil::rgb8_view_t v2 = view(img2);

std::uint8_t sparse_matrix[] =
{
1, 1, 1, 1,
3, 3, 3, 3,
5, 5, 5, 5,
7, 7, 7, 7
};

std::uint8_t big_matrix[] =
{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 1, 2, 1, 2, 1, 2,
1, 2, 3, 4, 5, 6, 7, 8,
3, 4, 3, 4, 3, 4, 3, 4,
1, 2, 3, 4, 5, 6, 7, 8,
5, 6, 5, 6, 5, 6, 5, 6,
1, 2, 3, 4, 5, 6, 7, 8,
7, 8, 7, 8, 7, 8, 7, 8
};

std::uint8_t big_rgb_matrix[] =
{
1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8, 7, 8, 9, 8, 9, 10,
1, 2, 3, 2, 3, 4, 1, 2, 3, 2, 3, 4, 1, 2, 3, 2, 3, 4, 1, 2, 3, 2, 3, 4,
1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8, 7, 8, 9, 8, 9, 10,
3, 4, 5, 4, 5, 6, 3, 4, 5, 4, 5, 6, 3, 4, 5, 4, 5, 6, 3, 4, 5, 4, 5, 6,
1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8, 7, 8, 9, 8, 9, 10,
5, 6, 7, 6, 7, 8, 5, 6, 7, 6, 7, 8, 5, 6, 7, 6, 7, 8, 5, 6, 7, 6, 7, 8,
1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8, 7, 8, 9, 8, 9, 10,
7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10, 7, 8, 9, 8, 9, 10,
};

std::vector<std::vector<bool>> mask =
{
{1, 0, 0, 1},
{0, 0, 1, 1},
{0, 1, 0, 1},
{1, 1, 0, 0},
};

gil::gray8c_view_t sparse_gray_view = gil::interleaved_view(4, 4, reinterpret_cast<gil::gray8c_pixel_t*>(sparse_matrix), 4);

gil::gray8c_view_t big_gray_view = gil::interleaved_view(8, 8, reinterpret_cast<gil::gray8c_pixel_t*>(big_matrix), 8);

gil::rgb8c_view_t big_rgb_view = gil::interleaved_view(8, 8, reinterpret_cast<gil::rgb8c_pixel_t*>(big_rgb_matrix), 24);

void check_histogram_fill()
{
gil::histogram<int> h1, h2;
gil::histogram<int, int, int> h3;

h1.fill(big_gray_view);

bool check_gray_fill = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h1(i) != 8)
{
check_gray_fill = false;
}
}
BOOST_TEST(check_gray_fill);

h3.fill(big_rgb_view);

bool check_rgb_fill = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h3(i, i+1, i+2) != 8)
{
check_rgb_fill = false;
}
}
BOOST_TEST(check_rgb_fill);

h2.fill<1>(big_rgb_view);
bool check_gray_fill2 = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h2(i+1) != 8)
{
check_gray_fill2 = false;
}
}
BOOST_TEST(check_gray_fill2);

// Check with limits
std::tuple<int> lower{2}, higher{6};
h1.clear();
h1.fill(big_gray_view, false, {{}}, lower, higher, true);
check_gray_fill = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(i<2 || i>6)
{
check_gray_fill = check_gray_fill & (h1(i)==0);continue;
}
if(h1(i) != 8)
{
check_gray_fill = false;
}
}
BOOST_TEST(check_gray_fill);

std::tuple<int, int ,int> lower1{2,2,2}, higher1{6,6,6};
h3.clear();
h3.fill(big_rgb_view, false, {{}}, lower1, higher1, true);

check_rgb_fill = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(!(i >= 2 && (i+2) <= 6))
{
check_rgb_fill = check_rgb_fill & (h3(i, i+1, i+2)==0);continue;
}
if(h3(i, i+1, i+2) != 8)
{
check_rgb_fill = false;
}
}
BOOST_TEST(check_rgb_fill);

h2.clear();
// std::tuple<int> lower{2}, higher{6};
h2.fill<1>(big_rgb_view, false, {{}}, lower, higher, true);
check_gray_fill2 = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(i+1 < 2 || i+1 > 6)
{
check_gray_fill = check_gray_fill & (h2(i+1)==0);continue;
}
if(h2(i+1) != 8)
{
check_gray_fill2 = false;
}
}
BOOST_TEST(check_gray_fill2);

//Check mask
gil::histogram<int> h4;
std::tuple<int> low{1}, high{8};
gil::fill_histogram(sparse_gray_view, h4, false, false, true, mask, low, high, true);

bool check_1d = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(i%2==1)
{
check_1d = check_1d & (h4(i)==2);
}
}
BOOST_TEST(check_1d);
}

void check_histogram_fill_algorithm()
{
gil::histogram<int> h1;

gil::fill_histogram<1>(big_rgb_view, h1);

bool check_1d = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h1(i+1) != 8)
{
check_1d = false;
}
}
BOOST_TEST(check_1d);

gil::histogram<int, int> h2;

gil::fill_histogram<2, 1>(big_rgb_view, h2);

bool check_2d = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h2(i+2, i+1) != 8)
{
check_2d = false;
}
}
BOOST_TEST(check_2d);

gil::histogram<int> h3;

std::tuple<int> low(1), high(8);
gil::fill_histogram(sparse_gray_view, h3, false, false, false, {{}}, low, high, true);

check_1d = true;
for (std::size_t i = 1; i <= 8; ++i)
{
if(h3.find(i) == h3.end())
{
check_1d = false;
}
else
{
check_1d = check_1d & (i % 2 == 1 ? (h3(i) == 4) : (h3(i) == 0));
}
}
BOOST_TEST(check_1d);
}

int main() {

check_histogram_fill();
check_histogram_fill_algorithm();

return boost::report_errors();
}
17 changes: 17 additions & 0 deletions test/core/histogram/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,27 @@ void check_helper_fn_tuple_to_tuple()
std::get<2>(r1) == std::get<1>(r3));
}

void check_helper_tuple_limit()
{
using type1 = std::tuple<int, short>;
using type2 = std::tuple<unsigned int, unsigned char>;
type1 t1_min(std::numeric_limits<int>::min(), std::numeric_limits<short>::min());
type1 t1_max(std::numeric_limits<int>::max(), std::numeric_limits<short>::max());
type2 t2_min(std::numeric_limits<unsigned int>::min(), std::numeric_limits<unsigned char>::min());
type2 t2_max(std::numeric_limits<unsigned int>::max(), std::numeric_limits<unsigned char>::max());

BOOST_TEST(t1_min == gil::detail::tuple_limit<type1>::min());
BOOST_TEST(t1_max == gil::detail::tuple_limit<type1>::max());
BOOST_TEST(t2_min == gil::detail::tuple_limit<type2>::min());
BOOST_TEST(t2_max == gil::detail::tuple_limit<type2>::max());

}

int main() {

check_helper_fn_pixel_to_tuple();
check_helper_fn_tuple_to_tuple();
check_helper_tuple_limit();

return boost::report_errors();
}
Loading

0 comments on commit 27c9351

Please sign in to comment.