Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use std::size_t as channel index type in nth_channel_view #659

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include <boost/gil/dynamic_step.hpp>
#include <boost/gil/image_view_factory.hpp>
#include <boost/gil/point.hpp>
#include <boost/gil/typedefs.hpp>
#include <boost/gil/detail/mp11.hpp>

#include <cstddef>
#include <cstdint>

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

nth_channel_view_fn(int n) : _n(n) {}
nth_channel_view_fn(index_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;
index_t _n;
};

template <typename DstP, typename ResultView, typename CC = default_color_converter>
Expand Down Expand Up @@ -305,7 +307,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(any_image_view<Views...> const& src, int n)
auto nth_channel_view(any_image_view<Views...> const& src, index_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
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ void non_overlapping_interpolated_clahe(
using dst_channel_t = typename channel_type<DstView>::type;
using coord_t = typename SrcView::x_coord_t;

std::size_t const channels = num_channels<SrcView>::value;
coord_t const width = src_view.width();
coord_t const height = src_view.height();
index_t const channels = num_channels<SrcView>::value;
coord_t const width = src_view.width();
coord_t const height = src_view.height();

// Find control points

Expand All @@ -184,7 +184,7 @@ void non_overlapping_interpolated_clahe(

copy_pixels(src_view, subimage_view(view(padded_img), top_left_x, top_left_y, width, height));

for (std::size_t k = 0; k < channels; k++)
for (index_t k = 0; k < channels; k++)
{
std::vector<histogram<source_channel_t>> prev_row(new_width / tile_width_x),
next_row((new_width / tile_width_x));
Expand Down
2 changes: 1 addition & 1 deletion include/boost/gil/image_processing/convolve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ void convolve_2d(SrcView const& src_view, Kernel const& kernel, DstView const& d
typename color_space_type<DstView>::type
>::value, "Source and destination views must have pixels with the same color space");

for (std::size_t i = 0; i < src_view.num_channels(); i++)
for (index_t i = 0; i < src_view.num_channels(); i++)
{
detail::convolve_2d_impl(
nth_channel_view(src_view, i),
Expand Down
2 changes: 1 addition & 1 deletion include/boost/gil/image_processing/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void median_filter(SrcView const& src_view, DstView const& dst_view, std::size_t
src_view.height()
);

for (std::size_t channel = 0; channel < extended_view.num_channels(); channel++)
for (index_t channel = 0; channel < extended_view.num_channels(); channel++)
{
detail::filter_median_impl(
nth_channel_view(extended_view, channel),
Expand Down
12 changes: 6 additions & 6 deletions include/boost/gil/image_processing/histogram_equalization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ void histogram_equalization(
using dst_channel_t = typename channel_type<DstView>::type;
using coord_t = typename SrcView::x_coord_t;

std::size_t const channels = num_channels<SrcView>::value;
coord_t const width = src_view.width();
coord_t const height = src_view.height();
std::size_t pixel_max = (std::numeric_limits<dst_channel_t>::max)();
std::size_t pixel_min = (std::numeric_limits<dst_channel_t>::min)();
index_t const channels = num_channels<SrcView>::value;
coord_t const width = src_view.width();
coord_t const height = src_view.height();
std::size_t pixel_max = (std::numeric_limits<dst_channel_t>::max)();
std::size_t pixel_min = (std::numeric_limits<dst_channel_t>::min)();

for (std::size_t i = 0; i < channels; i++)
for (index_t i = 0; i < channels; i++)
{
histogram<source_channel_t> h;
fill_histogram(nth_channel_view(src_view, i), h, bin_width, false, false, mask, src_mask);
Expand Down
4 changes: 2 additions & 2 deletions include/boost/gil/image_processing/histogram_matching.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ void histogram_matching(
using dst_channel_t = typename channel_type<DstView>::type;
using coord_t = typename SrcView::x_coord_t;

std::size_t const channels = num_channels<SrcView>::value;
index_t const channels = num_channels<SrcView>::value;
coord_t const width = src_view.width();
coord_t const height = src_view.height();
source_channel_t src_pixel_min = (std::numeric_limits<source_channel_t>::min)();
source_channel_t src_pixel_max = (std::numeric_limits<source_channel_t>::max)();
ref_channel_t ref_pixel_min = (std::numeric_limits<ref_channel_t>::min)();
ref_channel_t ref_pixel_max = (std::numeric_limits<ref_channel_t>::max)();

for (std::size_t i = 0; i < channels; i++)
for (index_t i = 0; i < channels; i++)
{
histogram<source_channel_t> src_histogram;
histogram<ref_channel_t> ref_histogram;
Expand Down
4 changes: 2 additions & 2 deletions include/boost/gil/image_processing/morphology.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void morph(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_m

gil::image<typename DstView::value_type> intermediate_img(src_view.dimensions());

for (std::size_t i = 0; i < src_view.num_channels(); i++)
for (index_t i = 0; i < src_view.num_channels(); i++)
{
morph_impl(nth_channel_view(src_view, i), nth_channel_view(view(intermediate_img), i),
ker_mat, identifier);
Expand Down Expand Up @@ -153,7 +153,7 @@ void difference(SrcView const& src_view1, SrcView const& src_view2, DiffView con
gil_function_requires<ColorSpacesCompatibleConcept<
typename color_space_type<SrcView>::type, typename color_space_type<DiffView>::type>>();

for (std::size_t i = 0; i < src_view1.num_channels(); i++)
for (index_t i = 0; i < src_view1.num_channels(); i++)
{
difference_impl(nth_channel_view(src_view1, i), nth_channel_view(src_view2, i),
nth_channel_view(diff_view, i));
Expand Down
2 changes: 1 addition & 1 deletion include/boost/gil/image_processing/threshold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void threshold_optimal
{
if (mode == threshold_optimal_value::otsu)
{
for (std::size_t i = 0; i < src_view.num_channels(); i++)
for (index_t i = 0; i < src_view.num_channels(); i++)
{
detail::otsu_impl
(nth_channel_view(src_view, i), nth_channel_view(dst_view, i), direction);
Expand Down
35 changes: 18 additions & 17 deletions include/boost/gil/image_view_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/gil/image_view.hpp>
#include <boost/gil/metafunctions.hpp>
#include <boost/gil/point.hpp>
#include <boost/gil/typedefs.hpp>
#include <boost/gil/detail/mp11.hpp>

#include <boost/assert.hpp>
Expand Down Expand Up @@ -338,7 +339,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(View const& src, int n) {
static type make(View const& src, index_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 @@ -351,7 +352,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(View const& src, int n) {
static type make(View const& src, index_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 @@ -375,7 +376,7 @@ namespace detail {
public:
using type = typename __nth_channel_view_basic<View,adjacent>::type;

static type make(View const& src, int n) {
static type make(View const& src, index_t n) {
return __nth_channel_view_basic<View,adjacent>::make(src,n);
}
};
Expand Down Expand Up @@ -403,11 +404,11 @@ 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(index_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
index_t _n; // the channel to use

auto operator()(argument_type srcP) const -> result_type
{
Expand All @@ -421,7 +422,7 @@ namespace detail {
using AD = typename View::template add_deref<deref_t>;
public:
using type = typename AD::type;
static type make(View const& src, int n) {
static type make(View const& src, index_t n) {
return AD::make(src, deref_t(n));
}
};
Expand All @@ -440,12 +441,12 @@ 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(View const& src, int n) { return VB::make(src,n); }
static type make(View const& src, index_t n) { return VB::make(src,n); }
};

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

Expand All @@ -454,11 +455,11 @@ typename nth_channel_view_type<View>::type nth_channel_view(View const& 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 <index_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 <index_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 @@ -475,7 +476,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 <index_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 @@ -487,10 +488,10 @@ namespace detail {
}
};

template <int K, typename View, bool IsBasic> struct __kth_channel_view;
template <index_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 <index_t K, typename View> struct __kth_channel_view<K,View,true>
{
private:
using src_x_iterator = typename View::x_iterator;
Expand All @@ -515,7 +516,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 <index_t K, typename SrcP>
struct kth_channel_deref_fn
{
static constexpr bool is_mutable =
Expand Down Expand Up @@ -544,7 +545,7 @@ namespace detail {
}
};

template <int K, typename View> struct __kth_channel_view<K,View,false> {
template <index_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 @@ -562,7 +563,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 <index_t K, typename View>
struct kth_channel_view_type {
private:
BOOST_GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
Expand All @@ -573,7 +574,7 @@ struct kth_channel_view_type {
};

/// \ingroup ImageViewTransformationsKthChannel
template <int K, typename View>
template <index_t K, typename View>
auto kth_channel_view(View const& src)
-> typename kth_channel_view_type<K,View>::type
{
Expand Down
5 changes: 5 additions & 0 deletions include/boost/gil/typedefs.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// Copyright 2005-2007 Adobe Systems Incorporated
// Copyright 2018 Mateusz Loskot <[email protected]>
// Copyright 2022 Dirk Stolle
//
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -16,6 +17,7 @@
#include <boost/gil/rgb.hpp>
#include <boost/gil/rgba.hpp>

#include <cstddef>
#include <cstdint>
#include <memory>
#if !defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Expand Down Expand Up @@ -120,6 +122,9 @@ template <typename B, typename Mn, typename Mx> struct scoped_channel_value;
template <typename T> struct float_point_zero;
template <typename T> struct float_point_one;

// general-purpose index type
using index_t = std::ptrdiff_t;

//////////////////////////////////////////////////////////////////////////////////////////
/// Built-in channel models
//////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading