11 #ifndef BOOST_GIL_IMAGE_PROCESSING_KERNEL_HPP
12 #define BOOST_GIL_IMAGE_PROCESSING_KERNEL_HPP
14 #include <boost/gil/utilities.hpp>
15 #include <boost/gil/point.hpp>
17 #include <boost/assert.hpp>
27 namespace boost {
namespace gil {
36 template <
typename Core>
45 BOOST_ASSERT(center_ < this->
size());
49 : Core(
size) , center_(center)
51 BOOST_ASSERT(this->size() > 0);
52 BOOST_ASSERT(center_ < this->
size());
56 : Core(other), center_(other.center_)
58 BOOST_ASSERT(this->size() > 0);
59 BOOST_ASSERT(center_ < this->
size());
64 Core::operator=(other);
65 center_ = other.center_;
69 std::size_t left_size()
const
71 BOOST_ASSERT(center_ < this->
size());
75 std::size_t right_size()
const
77 BOOST_ASSERT(center_ < this->
size());
78 return this->size() - center_ - 1;
81 auto center() -> std::size_t&
83 BOOST_ASSERT(center_ < this->
size());
87 auto center()
const -> std::size_t
const&
89 BOOST_ASSERT(center_ < this->
size());
94 std::size_t center_{0};
100 template <
typename T,
typename Allocator = std::allocator<T> >
109 template <
typename FwdIterator>
110 kernel_1d(FwdIterator elements, std::size_t
size, std::size_t center)
113 detail::copy_n(elements,
size, this->begin());
121 template <
typename T,std::
size_t Size>
126 static constexpr std::size_t static_size = Size;
127 static_assert(static_size > 0,
"kernel must have size greater than 0");
128 static_assert(static_size % 2 == 1,
"kernel size must be odd to ensure validity at the center");
133 template <
typename FwdIterator>
137 detail::copy_n(elements, Size, this->begin());
146 template <
typename T,std::
size_t Size>
150 template <
typename Kernel>
151 inline Kernel reverse_kernel(Kernel
const& kernel)
153 Kernel result(kernel);
154 result.center() = kernel.right_size();
155 std::reverse(result.begin(), result.end());
162 template <
typename Core>
163 class kernel_2d_adaptor :
public Core
166 kernel_2d_adaptor() =
default;
168 explicit kernel_2d_adaptor(std::size_t center_y, std::size_t center_x)
169 : center_(center_x, center_y)
171 BOOST_ASSERT(center_.y < this->size() && center_.x < this->size());
174 kernel_2d_adaptor(std::size_t size, std::size_t center_y, std::size_t center_x)
175 : Core(size * size), square_size(size), center_(center_x, center_y)
177 BOOST_ASSERT(this->size() > 0);
178 BOOST_ASSERT(center_.y < this->size() && center_.x < this->size());
181 kernel_2d_adaptor(kernel_2d_adaptor
const& other)
182 : Core(other), square_size(other.square_size), center_(other.center_.x, other.center_.y)
184 BOOST_ASSERT(this->size() > 0);
185 BOOST_ASSERT(center_.y < this->size() && center_.x < this->size());
188 kernel_2d_adaptor& operator=(kernel_2d_adaptor
const& other)
190 Core::operator=(other);
191 center_.y = other.center_.y;
192 center_.x = other.center_.x;
193 square_size = other.square_size;
197 std::size_t upper_size()
const
199 BOOST_ASSERT(center_.y < this->size());
203 std::size_t lower_size()
const
205 BOOST_ASSERT(center_.y < this->size());
206 return this->size() - center_.y - 1;
209 std::size_t left_size()
const
211 BOOST_ASSERT(center_.x < this->size());
215 std::size_t right_size()
const
217 BOOST_ASSERT(center_.x < this->size());
218 return this->size() - center_.x - 1;
221 auto center_y() -> std::size_t&
223 BOOST_ASSERT(center_.y < this->size());
227 auto center_y() const -> std::
size_t const&
229 BOOST_ASSERT(center_.y < this->size());
233 auto center_x() -> std::size_t&
235 BOOST_ASSERT(center_.x < this->size());
239 auto center_x() const -> std::
size_t const&
241 BOOST_ASSERT(center_.x < this->size());
245 std::size_t size()
const
250 typename Core::value_type at(std::size_t x, std::size_t y)
const
252 if (x >= this->size() || y >= this->size())
254 throw std::out_of_range(
"Index out of range");
256 return this->begin()[y * this->size() + x];
260 std::size_t square_size{0};
263 point<std::size_t> center_{0, 0};
270 typename Allocator = std::allocator<T>
272 class kernel_2d :
public detail::kernel_2d_adaptor<std::vector<T, Allocator>>
274 using parent_t = detail::kernel_2d_adaptor<std::vector<T, Allocator>>;
279 kernel_2d(std::size_t
size,std::size_t center_y, std::size_t center_x)
280 : parent_t(
size, center_y, center_x)
283 template <
typename FwdIterator>
284 kernel_2d(FwdIterator elements, std::size_t
size, std::size_t center_y, std::size_t center_x)
285 : parent_t(
static_cast<int>(std::sqrt(
size)), center_y, center_x)
287 detail::copy_n(elements,
size, this->begin());
295 template <
typename T, std::
size_t Size>
297 public detail::kernel_2d_adaptor<std::array<T, Size * Size>>
299 using parent_t = detail::kernel_2d_adaptor<std::array<T, Size * Size>>;
301 static constexpr std::size_t static_size = Size;
302 static_assert(static_size > 0,
"kernel must have size greater than 0");
303 static_assert(static_size % 2 == 1,
"kernel size must be odd to ensure validity at the center");
307 this->square_size = Size;
311 parent_t(center_y, center_x)
313 this->square_size = Size;
316 template <
typename FwdIterator>
317 explicit kernel_2d_fixed(FwdIterator elements, std::size_t center_y, std::size_t center_x)
318 : parent_t(center_y, center_x)
320 this->square_size = Size;
321 detail::copy_n(elements, Size * Size, this->begin());
330 template <
typename T, std::
size_t Size>
333 template <
typename Kernel>
334 inline Kernel reverse_kernel_2d(Kernel
const& kernel)
336 Kernel result(kernel);
337 result.center_x() = kernel.lower_size();
338 result.center_y() = kernel.right_size();
339 std::reverse(result.begin(), result.end());
345 template<
typename T,
typename Allocator>
346 inline kernel_2d<T, Allocator> reverse_kernel(kernel_2d<T, Allocator>
const& kernel)
348 return reverse_kernel_2d(kernel);
352 template<
typename T, std::
size_t Size>
353 inline kernel_2d_fixed<T, Size> reverse_kernel(kernel_2d_fixed<T, Size>
const& kernel)
355 return reverse_kernel_2d(kernel);
kernel adaptor for one-dimensional cores Core needs to provide size(),begin(),end(),...
Definition: kernel.hpp:38
static-size kernel
Definition: kernel.hpp:298
variable-size kernel
Definition: kernel.hpp:273
static-size kernel
Definition: kernel.hpp:123
variable-size kernel
Definition: kernel.hpp:102
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Returns an integral constant type specifying the number of elements in a color base.
Definition: color_base_algorithm.hpp:42