kernel.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. // Copyright 2019 Miral Shah <miralshah2211@gmail.com>
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_EXTENSION_NUMERIC_KERNEL_HPP
  10. #define BOOST_GIL_EXTENSION_NUMERIC_KERNEL_HPP
  11. #include <boost/gil/utilities.hpp>
  12. #include <boost/gil/point.hpp>
  13. #include <boost/assert.hpp>
  14. #include <algorithm>
  15. #include <array>
  16. #include <cstddef>
  17. #include <memory>
  18. #include <vector>
  19. #include <cmath>
  20. #include <stdexcept>
  21. namespace boost { namespace gil {
  22. // Definitions of 1D fixed-size and variable-size kernels and related operations
  23. namespace detail {
  24. /// \brief kernel adaptor for one-dimensional cores
  25. /// Core needs to provide size(),begin(),end(),operator[],
  26. /// value_type,iterator,const_iterator,reference,const_reference
  27. template <typename Core>
  28. class kernel_1d_adaptor : public Core
  29. {
  30. public:
  31. kernel_1d_adaptor() = default;
  32. explicit kernel_1d_adaptor(std::size_t center)
  33. : center_(center)
  34. {
  35. BOOST_ASSERT(center_ < this->size());
  36. }
  37. kernel_1d_adaptor(std::size_t size, std::size_t center)
  38. : Core(size) , center_(center)
  39. {
  40. BOOST_ASSERT(this->size() > 0);
  41. BOOST_ASSERT(center_ < this->size()); // also implies `size() > 0`
  42. }
  43. kernel_1d_adaptor(kernel_1d_adaptor const& other)
  44. : Core(other), center_(other.center_)
  45. {
  46. BOOST_ASSERT(this->size() > 0);
  47. BOOST_ASSERT(center_ < this->size()); // also implies `size() > 0`
  48. }
  49. kernel_1d_adaptor& operator=(kernel_1d_adaptor const& other)
  50. {
  51. Core::operator=(other);
  52. center_ = other.center_;
  53. return *this;
  54. }
  55. std::size_t left_size() const
  56. {
  57. BOOST_ASSERT(center_ < this->size());
  58. return center_;
  59. }
  60. std::size_t right_size() const
  61. {
  62. BOOST_ASSERT(center_ < this->size());
  63. return this->size() - center_ - 1;
  64. }
  65. auto center() -> std::size_t&
  66. {
  67. BOOST_ASSERT(center_ < this->size());
  68. return center_;
  69. }
  70. auto center() const -> std::size_t const&
  71. {
  72. BOOST_ASSERT(center_ < this->size());
  73. return center_;
  74. }
  75. private:
  76. std::size_t center_{0};
  77. };
  78. } // namespace detail
  79. /// \brief variable-size kernel
  80. template <typename T, typename Allocator = std::allocator<T> >
  81. class kernel_1d : public detail::kernel_1d_adaptor<std::vector<T, Allocator>>
  82. {
  83. using parent_t = detail::kernel_1d_adaptor<std::vector<T, Allocator>>;
  84. public:
  85. kernel_1d() = default;
  86. kernel_1d(std::size_t size, std::size_t center) : parent_t(size, center) {}
  87. template <typename FwdIterator>
  88. kernel_1d(FwdIterator elements, std::size_t size, std::size_t center)
  89. : parent_t(size, center)
  90. {
  91. detail::copy_n(elements, size, this->begin());
  92. }
  93. kernel_1d(kernel_1d const& other) : parent_t(other) {}
  94. kernel_1d& operator=(kernel_1d const& other) = default;
  95. };
  96. /// \brief static-size kernel
  97. template <typename T,std::size_t Size>
  98. class kernel_1d_fixed : public detail::kernel_1d_adaptor<std::array<T, Size>>
  99. {
  100. using parent_t = detail::kernel_1d_adaptor<std::array<T, Size>>;
  101. public:
  102. static constexpr std::size_t static_size = Size;
  103. static_assert(static_size > 0, "kernel must have size greater than 0");
  104. static_assert(static_size % 2 == 1, "kernel size must be odd to ensure validity at the center");
  105. kernel_1d_fixed() = default;
  106. explicit kernel_1d_fixed(std::size_t center) : parent_t(center) {}
  107. template <typename FwdIterator>
  108. explicit kernel_1d_fixed(FwdIterator elements, std::size_t center)
  109. : parent_t(center)
  110. {
  111. detail::copy_n(elements, Size, this->begin());
  112. }
  113. kernel_1d_fixed(kernel_1d_fixed const& other) : parent_t(other) {}
  114. kernel_1d_fixed& operator=(kernel_1d_fixed const& other) = default;
  115. };
  116. // TODO: This data member is odr-used and definition at namespace scope
  117. // is required by C++11. Redundant and deprecated in C++17.
  118. template <typename T,std::size_t Size>
  119. constexpr std::size_t kernel_1d_fixed<T, Size>::static_size;
  120. /// \brief reverse a kernel
  121. template <typename Kernel>
  122. inline Kernel reverse_kernel(Kernel const& kernel)
  123. {
  124. Kernel result(kernel);
  125. result.center() = kernel.right_size();
  126. std::reverse(result.begin(), result.end());
  127. return result;
  128. }
  129. namespace detail {
  130. template <typename Core>
  131. class kernel_2d_adaptor : public Core
  132. {
  133. public:
  134. kernel_2d_adaptor() = default;
  135. explicit kernel_2d_adaptor(std::size_t center_y, std::size_t center_x)
  136. : center_(center_x, center_y)
  137. {
  138. BOOST_ASSERT(center_.y < this->size() && center_.x < this->size());
  139. }
  140. kernel_2d_adaptor(std::size_t size, std::size_t center_y, std::size_t center_x)
  141. : Core(size * size), square_size(size), center_(center_x, center_y)
  142. {
  143. BOOST_ASSERT(this->size() > 0);
  144. BOOST_ASSERT(center_.y < this->size() && center_.x < this->size()); // implies `size() > 0`
  145. }
  146. kernel_2d_adaptor(kernel_2d_adaptor const& other)
  147. : Core(other), square_size(other.square_size), center_(other.center_.x, other.center_.y)
  148. {
  149. BOOST_ASSERT(this->size() > 0);
  150. BOOST_ASSERT(center_.y < this->size() && center_.x < this->size()); // implies `size() > 0`
  151. }
  152. kernel_2d_adaptor& operator=(kernel_2d_adaptor const& other)
  153. {
  154. Core::operator=(other);
  155. center_.y = other.center_.y;
  156. center_.x = other.center_.x;
  157. square_size = other.square_size;
  158. return *this;
  159. }
  160. std::size_t upper_size() const
  161. {
  162. BOOST_ASSERT(center_.y < this->size());
  163. return center_.y;
  164. }
  165. std::size_t lower_size() const
  166. {
  167. BOOST_ASSERT(center_.y < this->size());
  168. return this->size() - center_.y - 1;
  169. }
  170. std::size_t left_size() const
  171. {
  172. BOOST_ASSERT(center_.x < this->size());
  173. return center_.x;
  174. }
  175. std::size_t right_size() const
  176. {
  177. BOOST_ASSERT(center_.x < this->size());
  178. return this->size() - center_.x - 1;
  179. }
  180. auto center_y() -> std::size_t&
  181. {
  182. BOOST_ASSERT(center_.y < this->size());
  183. return center_.y;
  184. }
  185. auto center_y() const -> std::size_t const&
  186. {
  187. BOOST_ASSERT(center_.y < this->size());
  188. return center_.y;
  189. }
  190. auto center_x() -> std::size_t&
  191. {
  192. BOOST_ASSERT(center_.x < this->size());
  193. return center_.x;
  194. }
  195. auto center_x() const -> std::size_t const&
  196. {
  197. BOOST_ASSERT(center_.x < this->size());
  198. return center_.x;
  199. }
  200. std::size_t size() const
  201. {
  202. return square_size;
  203. }
  204. typename Core::value_type at(std::size_t x, std::size_t y) const
  205. {
  206. if (x >= this->size() || y >= this->size())
  207. {
  208. throw std::out_of_range("Index out of range");
  209. }
  210. return this->begin()[y * this->size() + x];
  211. }
  212. protected:
  213. std::size_t square_size{0};
  214. private:
  215. point<std::size_t> center_{0, 0};
  216. };
  217. /// \brief variable-size kernel
  218. template
  219. <
  220. typename T,
  221. typename Allocator = std::allocator<T>
  222. >
  223. class kernel_2d : public detail::kernel_2d_adaptor<std::vector<T, Allocator>>
  224. {
  225. using parent_t = detail::kernel_2d_adaptor<std::vector<T, Allocator>>;
  226. public:
  227. kernel_2d() = default;
  228. kernel_2d(std::size_t size,std::size_t center_y, std::size_t center_x)
  229. : parent_t(size, center_y, center_x)
  230. {}
  231. template <typename FwdIterator>
  232. kernel_2d(FwdIterator elements, std::size_t size, std::size_t center_y, std::size_t center_x)
  233. : parent_t(static_cast<int>(std::sqrt(size)), center_y, center_x)
  234. {
  235. detail::copy_n(elements, size, this->begin());
  236. }
  237. kernel_2d(kernel_2d const& other) : parent_t(other) {}
  238. kernel_2d& operator=(kernel_2d const& other) = default;
  239. };
  240. /// \brief static-size kernel
  241. template <typename T, std::size_t Size>
  242. class kernel_2d_fixed :
  243. public detail::kernel_2d_adaptor<std::array<T, Size * Size>>
  244. {
  245. using parent_t = detail::kernel_2d_adaptor<std::array<T, Size * Size>>;
  246. public:
  247. static constexpr std::size_t static_size = Size;
  248. static_assert(static_size > 0, "kernel must have size greater than 0");
  249. static_assert(static_size % 2 == 1, "kernel size must be odd to ensure validity at the center");
  250. kernel_2d_fixed()
  251. {
  252. this->square_size = Size;
  253. }
  254. explicit kernel_2d_fixed(std::size_t center_y, std::size_t center_x) :
  255. parent_t(center_y, center_x)
  256. {
  257. this->square_size = Size;
  258. }
  259. template <typename FwdIterator>
  260. explicit kernel_2d_fixed(FwdIterator elements, std::size_t center_y, std::size_t center_x)
  261. : parent_t(center_y, center_x)
  262. {
  263. this->square_size = Size;
  264. detail::copy_n(elements, Size * Size, this->begin());
  265. }
  266. kernel_2d_fixed(kernel_2d_fixed const& other) : parent_t(other) {}
  267. kernel_2d_fixed& operator=(kernel_2d_fixed const& other) = default;
  268. };
  269. // TODO: This data member is odr-used and definition at namespace scope
  270. // is required by C++11. Redundant and deprecated in C++17.
  271. template <typename T, std::size_t Size>
  272. constexpr std::size_t kernel_2d_fixed<T, Size>::static_size;
  273. template <typename Kernel>
  274. inline Kernel reverse_kernel_2d(Kernel const& kernel)
  275. {
  276. Kernel result(kernel);
  277. result.center_x() = kernel.lower_size();
  278. result.center_y() = kernel.right_size();
  279. std::reverse(result.begin(), result.end());
  280. return result;
  281. }
  282. /// \brief reverse a kernel_2d
  283. template<typename T, typename Allocator>
  284. inline kernel_2d<T, Allocator> reverse_kernel(kernel_2d<T, Allocator> const& kernel)
  285. {
  286. return reverse_kernel_2d(kernel);
  287. }
  288. /// \brief reverse a kernel_2d
  289. template<typename T, std::size_t Size>
  290. inline kernel_2d_fixed<T, Size> reverse_kernel(kernel_2d_fixed<T, Size> const& kernel)
  291. {
  292. return reverse_kernel_2d(kernel);
  293. }
  294. } //namespace detail
  295. }} // namespace boost::gil
  296. #endif