8 #ifndef BOOST_GIL_IMAGE_VIEW_FACTORY_HPP
9 #define BOOST_GIL_IMAGE_VIEW_FACTORY_HPP
11 #include <boost/gil/color_convert.hpp>
12 #include <boost/gil/dynamic_step.hpp>
13 #include <boost/gil/gray.hpp>
14 #include <boost/gil/image_view.hpp>
15 #include <boost/gil/metafunctions.hpp>
16 #include <boost/gil/point.hpp>
17 #include <boost/gil/detail/mp11.hpp>
19 #include <boost/assert.hpp>
22 #include <type_traits>
36 namespace boost {
namespace gil {
38 struct default_color_converter;
40 template <
typename T>
struct transposed_type;
44 template <
typename View>
50 template <
typename View>
56 template <
typename Iterator>
58 std::size_t width, std::size_t height,
59 Iterator pixels, std::ptrdiff_t rowsize_in_bytes)
63 return RView(width, height,
typename RView::locator(pixels, rowsize_in_bytes));
68 template <
typename Iterator>
71 std::ptrdiff_t rowsize_in_bytes)
75 return RView(dim,
typename RView::locator(pixels, rowsize_in_bytes));
83 template <
typename View,
bool IsMutable>
84 struct channel_pointer_type_impl;
86 template <
typename View>
87 struct channel_pointer_type_impl<View, true>
89 using type =
typename channel_type<View>::type*;
92 template <
typename View>
93 struct channel_pointer_type_impl<View, false>
95 using type =
const typename channel_type<View>::type*;
98 template <
typename View>
99 struct channel_pointer_type
100 :
public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
105 template <
typename HomogeneousView>
107 ->
typename detail::channel_pointer_type<HomogeneousView>::type
110 static_assert(std::is_pointer<typename HomogeneousView::x_iterator>::value,
"");
112 return &gil::at_c<0>(
view(0,0));
117 template <
typename HomogeneousView>
119 ->
typename detail::channel_pointer_type<HomogeneousView>::type
122 return dynamic_at_c(
view.row_begin(0),plane_index);
134 template <
typename SrcConstRefP,
typename DstP,
typename CC=default_color_converter >
142 DstP operator()(SrcConstRefP srcP)
const {
151 template <
typename SrcView,
typename CC,
typename DstP,
typename SrcP>
152 struct _color_converted_view_type {
155 using add_ref_t =
typename SrcView::template add_deref<deref_t>;
157 using type =
typename add_ref_t::type;
158 static type make(
const SrcView& sv,CC cc) {
return add_ref_t::make(sv,deref_t(cc));}
162 template <
typename SrcView,
typename CC,
typename DstP>
163 struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
164 using type = SrcView;
165 static type make(
const SrcView& sv,CC) {
return sv;}
172 template <
typename SrcView,
typename DstP,
typename CC=default_color_converter>
176 typename SrcView::value_type> {
183 template <
typename DstP,
typename View,
typename CC>
185 ->
typename color_converted_view_type<View,DstP,CC>::type
192 template <
typename DstP,
typename View>
194 ->
typename color_converted_view_type<View,DstP>::type
204 template <
typename View>
205 inline auto flipped_up_down_view(View
const& src)
206 ->
typename dynamic_y_step_type<View>::type
208 using RView =
typename dynamic_y_step_type<View>::type;
209 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
217 template <
typename View>
218 inline auto flipped_left_right_view(View
const& src)
219 ->
typename dynamic_x_step_type<View>::type
221 using RView =
typename dynamic_x_step_type<View>::type;
222 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
230 template <
typename View>
231 inline auto transposed_view(View
const& src)
232 ->
typename dynamic_xy_step_transposed_type<View>::type
234 using RView =
typename dynamic_xy_step_transposed_type<View>::type;
235 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(0,0),1,1,
true));
243 template <
typename View>
244 inline auto rotated90cw_view(View
const& src)
245 ->
typename dynamic_xy_step_transposed_type<View>::type
247 using RView =
typename dynamic_xy_step_transposed_type<View>::type;
248 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,
true));
256 template <
typename View>
257 inline auto rotated90ccw_view(View
const& src)
258 ->
typename dynamic_xy_step_transposed_type<View>::type
260 using RView =
typename dynamic_xy_step_transposed_type<View>::type;
261 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,
true));
269 template <
typename View>
270 inline auto rotated180_view(View
const& src)
271 ->
typename dynamic_xy_step_type<View>::type
273 using RView =
typename dynamic_xy_step_type<View>::type;
274 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
282 template <
typename View>
283 inline View subimage_view(
285 typename View::point_t
const& topleft,
286 typename View::point_t
const& dimensions)
288 return View(dimensions, src.xy_at(topleft));
292 template <
typename View>
293 inline View subimage_view(View
const& src,
294 typename View::coord_t x_min,
295 typename View::coord_t y_min,
296 typename View::coord_t width,
297 typename View::coord_t height)
299 return View(width, height, src.xy_at(x_min, y_min));
307 template <
typename View>
309 auto subsampled_view(View
const& src,
typename View::coord_t x_step,
typename View::coord_t y_step)
310 ->
typename dynamic_xy_step_type<View>::type
312 BOOST_ASSERT(x_step > 0 && y_step > 0);
313 using view_t =
typename dynamic_xy_step_type<View>::type;
315 (src.width() + (x_step - 1)) / x_step,
316 (src.height() + (y_step - 1)) / y_step,
317 typename view_t::xy_locator(src.xy_at(0,0), x_step, y_step));
321 template <
typename View>
322 inline auto subsampled_view(View
const& src,
typename View::point_t
const& step)
323 ->
typename dynamic_xy_step_type<View>::type
325 return subsampled_view(src, step.x, step.y);
333 template <
typename View,
bool AreChannelsTogether>
struct __nth_channel_view_basic;
337 template <
typename View>
338 struct __nth_channel_view_basic<View,false> {
339 using type =
typename view_type<typename channel_type<View>::type, gray_layout_t,
false,
true, view_is_mutable<View>::value>::type;
341 static type make(View
const& src,
int n) {
342 using locator_t =
typename type::xy_locator;
343 using x_iterator_t =
typename type::x_iterator;
344 using x_iterator_base_t =
typename iterator_adaptor_get_base<x_iterator_t>::type;
345 x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
346 return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
351 template <
typename View>
352 struct __nth_channel_view_basic<View,true> {
353 using type =
typename view_type<typename channel_type<View>::type, gray_layout_t,
false,
false, view_is_mutable<View>::value>::type;
354 static type make(View
const& src,
int n) {
355 using x_iterator_t =
typename type::x_iterator;
356 return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
360 template <
typename View,
bool IsBasic>
struct __nth_channel_view;
363 template <
typename View>
364 struct __nth_channel_view<View,true>
367 using src_x_iterator =
typename View::x_iterator;
371 static constexpr
bool adjacent =
372 !iterator_is_step<src_x_iterator>::value &&
373 (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
376 using type =
typename __nth_channel_view_basic<View,adjacent>::type;
378 static type make(View
const& src,
int n) {
379 return __nth_channel_view_basic<View,adjacent>::make(src,n);
387 template <
typename SrcP>
391 static constexpr
bool is_mutable =
394 using src_pixel_t =
typename std::remove_reference<SrcP>::type;
396 using const_ref_t =
typename src_pixel_t::const_reference;
402 using argument_type = SrcP;
403 using reference = mp11::mp_if_c<is_mutable, ref_t, value_type>;
404 using result_type = reference;
407 template <
typename P>
412 auto operator()(argument_type srcP)
const -> result_type
414 return result_type(srcP[_n]);
418 template <
typename View>
struct __nth_channel_view<View,false> {
421 using AD =
typename View::template add_deref<deref_t>;
423 using type =
typename AD::type;
424 static type make(View
const& src,
int n) {
425 return AD::make(src, deref_t(n));
436 template <
typename View>
440 using VB = detail::__nth_channel_view<View,view_is_basic<View>::value>;
442 using type =
typename VB::type;
443 static type make(View
const& src,
int n) {
return VB::make(src,n); }
447 template <
typename View>
448 typename nth_channel_view_type<View>::type nth_channel_view(View
const& src,
int n) {
457 template <
int K,
typename View,
bool AreChannelsTogether>
struct __kth_channel_view_basic;
461 template <
int K,
typename View>
462 struct __kth_channel_view_basic<K,View,false> {
464 using channel_t =
typename kth_element_type<typename View::value_type,K>::type;
466 using type =
typename view_type<channel_t, gray_layout_t, false, true, view_is_mutable<View>::value>::type;
468 static type make(View
const& src) {
469 using locator_t =
typename type::xy_locator;
470 using x_iterator_t =
typename type::x_iterator;
471 using x_iterator_base_t =
typename iterator_adaptor_get_base<x_iterator_t>::type;
472 x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
473 return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
478 template <
int K,
typename View>
479 struct __kth_channel_view_basic<K,View,true> {
481 using channel_t =
typename kth_element_type<typename View::value_type, K>::type;
483 using type =
typename view_type<channel_t, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
484 static type make(View
const& src) {
485 using x_iterator_t =
typename type::x_iterator;
486 return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
490 template <
int K,
typename View,
bool IsBasic>
struct __kth_channel_view;
493 template <
int K,
typename View>
struct __kth_channel_view<K,View,true>
496 using src_x_iterator =
typename View::x_iterator;
500 static constexpr
bool adjacent =
501 !iterator_is_step<src_x_iterator>::value &&
502 (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
505 using type =
typename __kth_channel_view_basic<K,View,adjacent>::type;
507 static type make(View
const& src) {
508 return __kth_channel_view_basic<K,View,adjacent>::make(src);
518 template <
int K,
typename SrcP>
521 static constexpr
bool is_mutable =
525 using src_pixel_t =
typename std::remove_reference<SrcP>::type;
526 using channel_t =
typename kth_element_type<src_pixel_t, K>::type;
527 using const_ref_t =
typename src_pixel_t::const_reference;
534 using argument_type = SrcP;
535 using reference = mp11::mp_if_c<is_mutable, ref_t, value_type>;
536 using result_type = reference;
539 template <
typename P>
542 result_type operator()(argument_type srcP)
const {
543 return result_type(gil::at_c<K>(srcP));
547 template <
int K,
typename View>
struct __kth_channel_view<K,View,false> {
550 using AD =
typename View::template add_deref<deref_t>;
552 using type =
typename AD::type;
553 static type make(View
const& src) {
554 return AD::make(src, deref_t());
565 template <
int K,
typename View>
569 using VB = detail::__kth_channel_view<K,View,view_is_basic<View>::value>;
571 using type =
typename VB::type;
572 static type make(View
const& src) {
return VB::make(src); }
576 template <
int K,
typename View>
577 auto kth_channel_view(View
const& src)
578 ->
typename kth_channel_view_type<K,View>::type
Function object that given a source pixel, returns it converted to a given color space and channel de...
Definition: image_view_factory.hpp:135
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,...
Definition: image_view.hpp:54
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
auto interleaved_view_get_raw_data(HomogeneousView const &view) -> typename detail::channel_pointer_type< HomogeneousView >::type
Returns C pointer to the the channels of an interleaved homogeneous view.
Definition: image_view_factory.hpp:106
auto planar_view_get_raw_data(HomogeneousView const &view, int plane_index) -> typename detail::channel_pointer_type< HomogeneousView >::type
Returns C pointer to the the channels of a given color plane of a planar homogeneous view.
Definition: image_view_factory.hpp:118
auto interleaved_view(point< std::ptrdiff_t > dim, Iterator pixels, std::ptrdiff_t rowsize_in_bytes) -> typename type_from_x_iterator< Iterator >::view_t
Constructing image views from raw interleaved pixel data.
Definition: image_view_factory.hpp:69
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
GIL's 2-dimensional view over immutable GIL pixels.
Definition: concepts/image_view.hpp:376
Pixel concept that allows for changing its channels.
Definition: concepts/pixel.hpp:102
Definition: color_convert.hpp:31
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:176
class for color-converting one pixel to another
Definition: color_convert.hpp:328
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:107
Function object that returns a grayscale reference of the K-th channel (specified as a template param...
Definition: image_view_factory.hpp:520
Function object that returns a grayscale reference of the N-th channel of a given reference....
Definition: image_view_factory.hpp:390
Returns the type of a transposed view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:52
Returns the type of a view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:46
Base template for types that model HasDynamicYStepTypeConcept.
Definition: dynamic_step.hpp:21
Given a source image view type View, returns the type of an image view over a given channel of View.
Definition: image_view_factory.hpp:566
Given a source image view type View, returns the type of an image view over a single channel of View.
Definition: image_view_factory.hpp:437
Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pi...
Definition: metafunctions.hpp:218
Determines if the given pixel reference is mutable (i.e. its channels can be changed)
Definition: metafunctions.hpp:231
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:267
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept,...
Definition: pixel.hpp:106
Basic views must be over basic locators.
Definition: metafunctions.hpp:130