Skip to content

Commit

Permalink
use std::size_t as channel index type in nth_channel_view
Browse files Browse the repository at this point in the history
This unifies the channel index type in nth_channel_view and
image_view::num_channels - the latter one already uses std::size_t.

Fixes #373.
  • Loading branch information
striezel committed May 4, 2022
1 parent 36a45e3 commit 6121765
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/gil/point.hpp>
#include <boost/gil/detail/mp11.hpp>

#include <cstddef>
#include <cstdint>

namespace boost { namespace gil {
Expand Down Expand Up @@ -136,15 +137,15 @@ struct nth_channel_view_fn
{
using result_type = ResultView;

nth_channel_view_fn(int n) : _n(n) {}
nth_channel_view_fn(std::size_t n) : _n(n) {}

template <typename View>
auto operator()(View const& src) const -> result_type
{
return result_type(nth_channel_view(src,_n));
}

int _n;
std::size_t _n;
};

template <typename DstP, typename ResultView, typename CC = default_color_converter>
Expand Down Expand Up @@ -304,7 +305,7 @@ struct nth_channel_view_type<any_image_view<Views...>>
/// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
template <typename ...Views>
inline
auto nth_channel_view(const any_image_view<Views...>& src, int n)
auto nth_channel_view(const any_image_view<Views...>& src, std::size_t n)
-> typename nth_channel_view_type<any_image_view<Views...>>::type
{
using result_view_t = typename nth_channel_view_type<any_image_view<Views...>>::type;
Expand Down
34 changes: 17 additions & 17 deletions include/boost/gil/image_view_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ namespace detail {
struct __nth_channel_view_basic<View,false> {
using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, true, view_is_mutable<View>::value>::type;

static type make(const View& src, int n) {
static type make(const View& src, std::size_t n) {
using locator_t = typename type::xy_locator;
using x_iterator_t = typename type::x_iterator;
using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
Expand All @@ -322,7 +322,7 @@ namespace detail {
template <typename View>
struct __nth_channel_view_basic<View,true> {
using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
static type make(const View& src, int n) {
static type make(const View& src, std::size_t n) {
using x_iterator_t = typename type::x_iterator;
return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
}
Expand All @@ -346,7 +346,7 @@ namespace detail {
public:
using type = typename __nth_channel_view_basic<View,adjacent>::type;

static type make(const View& src, int n) {
static type make(const View& src, std::size_t n) {
return __nth_channel_view_basic<View,adjacent>::make(src,n);
}
};
Expand Down Expand Up @@ -374,10 +374,10 @@ namespace detail {
using reference = mp11::mp_if_c<is_mutable, ref_t, value_type>;
using result_type = reference;

nth_channel_deref_fn(int n=0) : _n(n) {}
nth_channel_deref_fn(std::size_t n=0) : _n(n) {}
template <typename P> nth_channel_deref_fn(const nth_channel_deref_fn<P>& d) : _n(d._n) {}

int _n; // the channel to use
std::size_t _n; // the channel to use

result_type operator()(argument_type srcP) const {
return result_type(srcP[_n]);
Expand All @@ -390,7 +390,7 @@ namespace detail {
using AD = typename View::template add_deref<deref_t>;
public:
using type = typename AD::type;
static type make(const View& src, int n) {
static type make(const View& src, std::size_t n) {
return AD::make(src, deref_t(n));
}
};
Expand All @@ -409,13 +409,13 @@ struct nth_channel_view_type {
using VB = detail::__nth_channel_view<View,view_is_basic<View>::value>;
public:
using type = typename VB::type;
static type make(const View& src, int n) { return VB::make(src,n); }
static type make(const View& src, std::size_t n) { return VB::make(src,n); }
};


/// \ingroup ImageViewTransformationsNthChannel
template <typename View>
typename nth_channel_view_type<View>::type nth_channel_view(const View& src, int n) {
typename nth_channel_view_type<View>::type nth_channel_view(const View& src, std::size_t n) {
return nth_channel_view_type<View>::make(src,n);
}

Expand All @@ -430,11 +430,11 @@ typename nth_channel_view_type<View>::type nth_channel_view(const View& src, int
/// \brief single-channel (grayscale) view of the K-th channel of a given image_view. The channel index is a template parameter

namespace detail {
template <int K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;
template <std::size_t K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;

// kth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
// or images with a step
template <int K, typename View>
template <std::size_t K, typename View>
struct __kth_channel_view_basic<K,View,false> {
private:
using channel_t = typename kth_element_type<typename View::value_type,K>::type;
Expand All @@ -451,7 +451,7 @@ namespace detail {
};

// kth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
template <int K, typename View>
template <std::size_t K, typename View>
struct __kth_channel_view_basic<K,View,true> {
private:
using channel_t = typename kth_element_type<typename View::value_type, K>::type;
Expand All @@ -463,10 +463,10 @@ namespace detail {
}
};

template <int K, typename View, bool IsBasic> struct __kth_channel_view;
template <std::size_t K, typename View, bool IsBasic> struct __kth_channel_view;

// For basic (memory-based) views dispatch to __kth_channel_view_basic
template <int K, typename View> struct __kth_channel_view<K,View,true>
template <std::size_t K, typename View> struct __kth_channel_view<K,View,true>
{
private:
using src_x_iterator = typename View::x_iterator;
Expand All @@ -491,7 +491,7 @@ namespace detail {
/// If the input is a pixel value or constant reference, the function object is immutable. Otherwise it is mutable (and returns non-const reference to the k-th channel)
/// \tparam SrcP reference to PixelConcept (could be pixel value or const/non-const reference)
/// Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
template <int K, typename SrcP>
template <std::size_t K, typename SrcP>
struct kth_channel_deref_fn
{
static constexpr bool is_mutable =
Expand Down Expand Up @@ -519,7 +519,7 @@ namespace detail {
}
};

template <int K, typename View> struct __kth_channel_view<K,View,false> {
template <std::size_t K, typename View> struct __kth_channel_view<K,View,false> {
private:
using deref_t = kth_channel_deref_fn<K,typename View::reference>;
using AD = typename View::template add_deref<deref_t>;
Expand All @@ -537,7 +537,7 @@ namespace detail {
/// If the channels in the source view are adjacent in memory (such as planar non-step view or single-channel view) then the
/// return view is a single-channel non-step view.
/// If the channels are non-adjacent (interleaved and/or step view) then the return view is a single-channel step view.
template <int K, typename View>
template <std::size_t K, typename View>
struct kth_channel_view_type {
private:
BOOST_GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
Expand All @@ -548,7 +548,7 @@ struct kth_channel_view_type {
};

/// \ingroup ImageViewTransformationsKthChannel
template <int K, typename View>
template <std::size_t K, typename View>
typename kth_channel_view_type<K,View>::type kth_channel_view(const View& src) {
return kth_channel_view_type<K,View>::make(src);
}
Expand Down

0 comments on commit 6121765

Please sign in to comment.