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/gray.hpp>
13 #include <boost/gil/metafunctions.hpp>
14 #include <boost/gil/point.hpp>
31 namespace boost {
namespace gil {
32 struct default_color_converter;
34 template <
typename T>
struct dynamic_x_step_type;
35 template <
typename T>
struct dynamic_y_step_type;
36 template <
typename T>
struct transposed_type;
40 template <
typename View>
45 template <
typename View>
51 template <
typename Iterator>
54 Iterator pixels, std::ptrdiff_t rowsize_in_bytes) {
56 return RView(width, height,
typename RView::locator(pixels, rowsize_in_bytes));
61 template <
typename Iterator>
63 std::ptrdiff_t rowsize_in_bytes)
67 return RView(dim,
typename RView::locator(pixels, rowsize_in_bytes));
75 template <
typename View,
bool IsMutable>
struct channel_pointer_type_impl;
77 template <
typename View>
struct channel_pointer_type_impl<View, true> {
78 typedef typename channel_type<View>::type* type;
80 template <
typename View>
struct channel_pointer_type_impl<View, false> {
81 typedef const typename channel_type<View>::type* type;
84 template <
typename View>
struct channel_pointer_type
85 :
public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
90 template <
typename HomogeneousView>
93 BOOST_STATIC_ASSERT((boost::is_pointer<typename HomogeneousView::x_iterator>::value));
95 return &gil::at_c<0>(
view(0,0));
100 template <
typename HomogeneousView>
103 return dynamic_at_c(view.row_begin(0),plane_index);
115 template <
typename SrcConstRefP,
typename DstP,
typename CC=default_color_converter >
123 DstP operator()(SrcConstRefP srcP)
const {
132 template <
typename SrcView,
typename CC,
typename DstP,
typename SrcP>
133 struct _color_converted_view_type {
136 typedef typename SrcView::template add_deref<deref_t> add_ref_t;
138 typedef typename add_ref_t::type type;
139 static type make(
const SrcView& sv,CC cc) {
return add_ref_t::make(sv,deref_t(cc));}
143 template <
typename SrcView,
typename CC,
typename DstP>
144 struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
145 typedef SrcView type;
146 static type make(
const SrcView& sv,CC) {
return sv;}
153 template <
typename SrcView,
typename DstP,
typename CC=default_color_converter>
157 typename SrcView::value_type> {
164 template <
typename DstP,
typename View,
typename CC>
171 template <
typename DstP,
typename View>
172 inline typename color_converted_view_type<View,DstP>::type
182 template <
typename View>
183 inline typename dynamic_y_step_type<View>::type flipped_up_down_view(
const View& src) {
184 typedef typename dynamic_y_step_type<View>::type RView;
185 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
193 template <
typename View>
194 inline typename dynamic_x_step_type<View>::type flipped_left_right_view(
const View& src) {
195 typedef typename dynamic_x_step_type<View>::type RView;
196 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
204 template <
typename View>
205 inline typename dynamic_xy_step_transposed_type<View>::type transposed_view(
const View& src) {
206 typedef typename dynamic_xy_step_transposed_type<View>::type RView;
207 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(0,0),1,1,
true));
215 template <
typename View>
216 inline typename dynamic_xy_step_transposed_type<View>::type rotated90cw_view(
const View& src) {
217 typedef typename dynamic_xy_step_transposed_type<View>::type RView;
218 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,
true));
226 template <
typename View>
227 inline typename dynamic_xy_step_transposed_type<View>::type rotated90ccw_view(
const View& src) {
228 typedef typename dynamic_xy_step_transposed_type<View>::type RView;
229 return RView(src.height(),src.width(),
typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,
true));
237 template <
typename View>
238 inline typename dynamic_xy_step_type<View>::type rotated180_view(
const View& src) {
239 typedef typename dynamic_xy_step_type<View>::type RView;
240 return RView(src.dimensions(),
typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
248 template <
typename View>
249 inline View subimage_view(
const View& src,
const typename View::point_t& topleft,
const typename View::point_t& dimensions) {
250 return View(dimensions,src.xy_at(topleft));
254 template <
typename View>
255 inline View subimage_view(
const View& src,
int xMin,
int yMin,
int width,
int height) {
256 return View(width,height,src.xy_at(xMin,yMin));
264 template <
typename View>
265 inline typename dynamic_xy_step_type<View>::type subsampled_view(
const View& src,
typename View::coord_t xStep,
typename View::coord_t yStep) {
266 assert(xStep>0 && yStep>0);
267 typedef typename dynamic_xy_step_type<View>::type RView;
268 return RView((src.width()+(xStep-1))/xStep,(src.height()+(yStep-1))/yStep,
269 typename RView::xy_locator(src.xy_at(0,0),xStep,yStep));
273 template <
typename View>
274 inline typename dynamic_xy_step_type<View>::type subsampled_view(
const View& src,
const typename View::point_t& step) {
275 return subsampled_view(src,step.x,step.y);
283 template <
typename View,
bool AreChannelsTogether>
struct __nth_channel_view_basic;
287 template <
typename View>
288 struct __nth_channel_view_basic<View,false> {
289 typedef typename view_type<typename channel_type<View>::type, gray_layout_t,
false,
true, view_is_mutable<View>::value>::type type;
291 static type make(
const View& src,
int n) {
292 typedef typename type::xy_locator locator_t;
293 typedef typename type::x_iterator x_iterator_t;
294 typedef typename iterator_adaptor_get_base<x_iterator_t>::type x_iterator_base_t;
295 x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
296 return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
301 template <
typename View>
302 struct __nth_channel_view_basic<View,true> {
303 typedef typename view_type<typename channel_type<View>::type, gray_layout_t,
false,
false, view_is_mutable<View>::value>::type type;
304 static type make(
const View& src,
int n) {
305 typedef typename type::x_iterator x_iterator_t;
306 return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
310 template <
typename View,
bool IsBasic>
struct __nth_channel_view;
313 template <
typename View>
struct __nth_channel_view<View,true> {
315 typedef typename View::x_iterator src_x_iterator;
319 BOOST_STATIC_CONSTANT(
bool, adjacent=
320 !iterator_is_step<src_x_iterator>::value &&
321 (is_planar<src_x_iterator>::value ||
322 num_channels<View>::value==1));
324 typedef typename __nth_channel_view_basic<View,adjacent>::type type;
326 static type make(
const View& src,
int n) {
327 return __nth_channel_view_basic<View,adjacent>::make(src,n);
335 template <
typename SrcP>
340 typedef typename remove_reference<SrcP>::type src_pixel_t;
342 typedef typename src_pixel_t::const_reference const_ref_t;
348 typedef SrcP argument_type;
349 typedef typename mpl::if_c<is_mutable, ref_t, value_type>::type reference;
350 typedef reference result_type;
357 result_type operator()(argument_type srcP)
const {
358 return result_type(srcP[_n]);
362 template <
typename View>
struct __nth_channel_view<View,false> {
365 typedef typename View::template add_deref<deref_t> AD;
367 typedef typename AD::type type;
368 static type make(
const View& src,
int n) {
369 return AD::make(src, deref_t(n));
380 template <
typename View>
384 typedef detail::__nth_channel_view<View,view_is_basic<View>::value> VB;
386 typedef typename VB::type type;
387 static type make(
const View& src,
int n) {
return VB::make(src,n); }
392 template <
typename View>
393 typename nth_channel_view_type<View>::type nth_channel_view(
const View& src,
int n) {
408 template <
int K,
typename View,
bool AreChannelsTogether>
struct __kth_channel_view_basic;
412 template <
int K,
typename View>
413 struct __kth_channel_view_basic<K,View,false> {
415 typedef typename kth_element_type<typename View::value_type,K>::type channel_t;
417 typedef typename view_type<channel_t, gray_layout_t, false, true, view_is_mutable<View>::value>::type type;
419 static type make(
const View& src) {
420 typedef typename type::xy_locator locator_t;
421 typedef typename type::x_iterator x_iterator_t;
422 typedef typename iterator_adaptor_get_base<x_iterator_t>::type x_iterator_base_t;
423 x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
424 return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
429 template <
int K,
typename View>
430 struct __kth_channel_view_basic<K,View,true> {
432 typedef typename kth_element_type<typename View::value_type, K>::type channel_t;
434 typedef typename view_type<channel_t, gray_layout_t, false, false, view_is_mutable<View>::value>::type type;
435 static type make(
const View& src) {
436 typedef typename type::x_iterator x_iterator_t;
437 return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
441 template <
int K,
typename View,
bool IsBasic>
struct __kth_channel_view;
444 template <
int K,
typename View>
struct __kth_channel_view<K,View,true> {
446 typedef typename View::x_iterator src_x_iterator;
450 BOOST_STATIC_CONSTANT(
bool, adjacent=
451 !iterator_is_step<src_x_iterator>::value &&
452 (is_planar<src_x_iterator>::value ||
453 num_channels<View>::value==1));
455 typedef typename __kth_channel_view_basic<K,View,adjacent>::type type;
457 static type make(
const View& src) {
458 return __kth_channel_view_basic<K,View,adjacent>::make(src);
466 template <
int K,
typename SrcP>
471 typedef typename remove_reference<SrcP>::type src_pixel_t;
472 typedef typename kth_element_type<src_pixel_t, K>::type channel_t;
473 typedef typename src_pixel_t::const_reference const_ref_t;
479 typedef SrcP argument_type;
480 typedef typename mpl::if_c<is_mutable, ref_t, value_type>::type reference;
481 typedef reference result_type;
486 result_type operator()(argument_type srcP)
const {
487 return result_type(gil::at_c<K>(srcP));
491 template <
int K,
typename View>
struct __kth_channel_view<K,View,false> {
494 typedef typename View::template add_deref<deref_t> AD;
496 typedef typename AD::type type;
497 static type make(
const View& src) {
498 return AD::make(src, deref_t());
509 template <
int K,
typename View>
513 typedef detail::__kth_channel_view<K,View,view_is_basic<View>::value> VB;
515 typedef typename VB::type type;
516 static type make(
const View& src) {
return VB::make(src); }
520 template <
int K,
typename View>
521 typename kth_channel_view_type<K,View>::type kth_channel_view(
const View& src) {
Returns the type of a transposed view that has a dynamic step along both X and Y. ...
Definition: image_view_factory.hpp:46
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:50
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept.
Definition: metafunctions.hpp:30
Function object that given a source pixel, returns it converted to a given color space and channel de...
Definition: image_view_factory.hpp:116
Returns the type of a view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:41
Given a source image view type View, returns the type of an image view over a single channel of ViewI...
Definition: image_view_factory.hpp:381
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:90
Function object that returns a grayscale reference of the K-th channel (specified as a template param...
Definition: image_view_factory.hpp:468
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:154
Determines if the given pixel reference is mutable (i.e. its channels can be changed) ...
Definition: metafunctions.hpp:170
Function object that returns a grayscale reference of the N-th channel of a given reference...
Definition: image_view_factory.hpp:337
detail::channel_pointer_type< HomogeneousView >::type planar_view_get_raw_data(const HomogeneousView &view, int plane_index)
Returns C pointer to the the channels of a given color plane of a planar homogeneous view...
Definition: image_view_factory.hpp:101
auto interleaved_view(point< std::size_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:62
Pixel concept that allows for changing its channels.
Definition: concepts.hpp:911
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:203
detail::channel_pointer_type< HomogeneousView >::type interleaved_view_get_raw_data(const HomogeneousView &view)
Returns C pointer to the the channels of an interleaved homogeneous view.
Definition: image_view_factory.hpp:91
Definition: color_convert.hpp:30
GIL's 2-dimensional view over immutable GIL pixels.
Definition: concepts.hpp:2020
const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:460
Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pi...
Definition: metafunctions.hpp:160
class for color-converting one pixel to another
Definition: color_convert.hpp:282
Basic views must be over basic locators.
Definition: metafunctions.hpp:100
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:510
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: concepts.hpp:42