13 #ifndef GIL_ALGORITHM_HPP
14 #define GIL_ALGORITHM_HPP
24 #include <boost/config.hpp>
25 #include <boost/utility/enable_if.hpp>
26 #include <boost/mpl/and.hpp>
27 #include <boost/mpl/or.hpp>
49 namespace boost {
namespace gil {
51 template <
typename ChannelPtr,
typename ColorSpace>
53 template <
typename Iterator>
55 template <
typename StepIterator>
98 template <
typename Derived,
typename Result=
void>
100 typedef Result result_type;
102 template <
typename V1,
typename V2> BOOST_FORCEINLINE
103 result_type operator()(
const std::pair<const V1*,const V2*>& p)
const {
107 template <
typename V1,
typename V2> BOOST_FORCEINLINE
108 result_type operator()(
const V1& v1,
const V2& v2)
const {
112 result_type operator()(
const error_t&)
const {
throw std::bad_cast(); }
116 template <
typename V1,
typename V2>
117 BOOST_FORCEINLINE result_type apply(
const V1& v1,
const V2& v2, mpl::false_)
const {
118 return ((
const Derived*)
this)->apply_incompatible(v1,v2);
122 template <
typename V1,
typename V2>
123 BOOST_FORCEINLINE result_type apply(
const V1& v1,
const V2& v2, mpl::true_)
const {
124 return ((
const Derived*)
this)->apply_compatible(v1,v2);
128 template <
typename V1,
typename V2>
129 BOOST_FORCEINLINE result_type apply_incompatible(
const V1&,
const V2&)
const {
130 throw std::bad_cast();
148 template<
typename T,
typename Cs>
157 template<
typename T,
typename Cs>
165 namespace boost {
namespace gil {
167 template <
typename I,
typename O>
struct copy_fn {
168 BOOST_FORCEINLINE I operator()(I first, I last, O dst)
const {
return std::copy(first,last,dst); }
176 template<
typename Cs,
typename IC1,
typename IC2> BOOST_FORCEINLINE
178 boost::gil::gil_function_requires<boost::gil::ChannelsCompatibleConcept<typename std::iterator_traits<IC1>::value_type,
typename std::iterator_traits<IC2>::value_type> >();
179 static_for_each(first,last,dst,boost::gil::detail::copy_fn<IC1,IC2>());
180 return dst+(last-first);
184 namespace boost {
namespace gil {
188 template <
typename I,
typename O>
190 BOOST_FORCEINLINE
void operator()(I src,
typename std::iterator_traits<I>::difference_type n, O dst)
const {
std::copy(src,src+n, dst); }
194 template <
typename IL,
typename O>
196 typedef typename std::iterator_traits<iterator_from_2d<IL> >::difference_type diff_t;
198 gil_function_requires<PixelLocatorConcept<IL> >();
199 gil_function_requires<MutablePixelIteratorConcept<O> >();
201 diff_t l=src.width()-src.x_pos();
202 diff_t numToCopy=(n<l ? n:l);
203 detail::copy_n(src.x(), numToCopy, dst);
212 template <
typename I,
typename OL>
214 typedef typename std::iterator_traits<I>::difference_type diff_t;
216 gil_function_requires<PixelIteratorConcept<I> >();
217 gil_function_requires<MutablePixelLocatorConcept<OL> >();
219 diff_t l=dst.width()-dst.x_pos();
220 diff_t numToCopy=(n<l ? n:l);
221 detail::copy_n(src, numToCopy, dst.x());
230 template <
typename IL,
typename OL>
232 typedef typename iterator_from_2d<IL>::difference_type diff_t;
234 gil_function_requires<PixelLocatorConcept<IL> >();
235 gil_function_requires<MutablePixelLocatorConcept<OL> >();
236 if (src.x_pos()!=dst.x_pos() || src.width()!=dst.width()) {
242 diff_t l=dst.width()-dst.x_pos();
243 diff_t numToCopy=(n<l ? n : l);
244 detail::copy_n(src.x(), numToCopy, dst.x());
252 template <
typename SrcIterator,
typename DstIterator>
253 BOOST_FORCEINLINE DstIterator copy_with_2d_iterators(SrcIterator first, SrcIterator last, DstIterator dst) {
254 typedef typename SrcIterator::x_iterator src_x_iterator;
255 typedef typename DstIterator::x_iterator dst_x_iterator;
257 typename SrcIterator::difference_type n = last - first;
259 if (first.is_1d_traversable()) {
260 if (dst.is_1d_traversable())
265 if (dst.is_1d_traversable())
266 copier_n<SrcIterator,dst_x_iterator>()(first,n, dst.x());
268 copier_n<SrcIterator,DstIterator>()(first,n,dst);
278 template <
typename IL,
typename OL>
280 return boost::gil::detail::copy_with_2d_iterators(first,last,dst);
284 namespace boost {
namespace gil {
287 template <
typename View1,
typename View2> BOOST_FORCEINLINE
289 assert(src.dimensions()==dst.dimensions());
290 detail::copy_with_2d_iterators(src.begin(),src.end(),dst.begin());
306 template <
typename CC>
307 class copy_and_convert_pixels_fn :
public binary_operation_obj<copy_and_convert_pixels_fn<CC> > {
311 typedef typename binary_operation_obj<copy_and_convert_pixels_fn<CC> >::result_type result_type;
312 copy_and_convert_pixels_fn() {}
313 copy_and_convert_pixels_fn(CC cc_in) : _cc(cc_in) {}
315 template <
typename V1,
typename V2> BOOST_FORCEINLINE
316 result_type apply_incompatible(
const V1& src,
const V2& dst)
const {
317 copy_pixels(color_converted_view<typename V2::value_type>(src,_cc),dst);
321 template <
typename V1,
typename V2> BOOST_FORCEINLINE
322 result_type apply_compatible(
const V1& src,
const V2& dst)
const {
329 template <
typename V1,
typename V2,
typename CC>
331 void copy_and_convert_pixels(
const V1& src,
const V2& dst,CC cc) {
332 detail::copy_and_convert_pixels_fn<CC> ccp(cc);
336 struct default_color_converter;
339 template <
typename View1,
typename View2>
341 void copy_and_convert_pixels(
const View1& src,
const View2& dst) {
342 detail::copy_and_convert_pixels_fn<default_color_converter> ccp;
366 template <
typename IL,
typename V>
368 boost::gil::gil_function_requires<boost::gil::MutablePixelLocatorConcept<IL> >();
369 if (first.is_1d_traversable()) {
373 std::ptrdiff_t n=last-first;
375 std::ptrdiff_t numToDo=std::min<const std::ptrdiff_t>(n,(std::ptrdiff_t)(first.width()-first.x_pos()));
376 fill_n(first.x(), numToDo, val);
384 namespace boost {
namespace gil {
388 template <
typename It,
typename P>
389 void operator()(It first, It last,
const P& p_in) {
394 template <
typename It,
typename P>
396 void fill_aux(It first, It last,
const P& p, mpl::true_) {
400 template <
typename It,
typename P>
402 void fill_aux(It first, It last,
const P& p,mpl::false_) {
409 template <
typename View,
typename Value> BOOST_FORCEINLINE
411 if (img_view.is_1d_traversable())
412 detail::fill_aux(img_view.begin().x(), img_view.end().x(),
413 val,is_planar<View>());
415 for (std::ptrdiff_t y=0; y<img_view.height(); ++y)
416 detail::fill_aux(img_view.row_begin(y),img_view.row_end(y),
417 val,is_planar<View>());
431 template <
typename It> BOOST_FORCEINLINE
432 void destruct_range_impl( It first
434 ,
typename enable_if< mpl::and_< is_pointer< It >
435 , mpl::not_< boost::has_trivial_destructor<
typename std::iterator_traits<It>::value_type > >
440 while (first!=last) {
446 template <
typename It> BOOST_FORCEINLINE
447 void destruct_range_impl( It
449 ,
typename enable_if< mpl::or_< mpl::not_< is_pointer< It > >
450 , boost::has_trivial_destructor<
typename std::iterator_traits< It >::value_type >
455 template <
typename It> BOOST_FORCEINLINE
456 void destruct_range(It first, It last) {
457 destruct_range_impl( first
462 struct std_destruct_t {
463 template <
typename It>
void operator()(It first, It last)
const { destruct_range(first,last); }
467 template <
typename It>
469 void destruct_aux(It first, It last, mpl::true_) {
470 static_for_each(first,last,std_destruct_t());
473 template <
typename It>
475 void destruct_aux(It first, It last, mpl::false_) {
476 destruct_range(first,last);
482 template <
typename View> BOOST_FORCEINLINE
484 if (img_view.is_1d_traversable())
485 detail::destruct_aux(img_view.begin().x(), img_view.end().x(),
488 for (std::ptrdiff_t y=0; y<img_view.height(); ++y)
489 detail::destruct_aux(img_view.row_begin(y),img_view.row_end(y),
506 template <
typename It,
typename P>
508 void uninitialized_fill_aux(It first, It last,
509 const P& p, mpl::true_) {
512 typedef typename std::iterator_traits<It>::value_type pixel_t;
513 while (channel < num_channels<pixel_t>::value) {
514 std::uninitialized_fill(dynamic_at_c(first,channel), dynamic_at_c(last,channel),
515 dynamic_at_c(p,channel));
519 for (
int c=0; c<channel; ++c)
520 destruct_range(dynamic_at_c(first,c), dynamic_at_c(last,c));
527 template <
typename It,
typename P>
529 void uninitialized_fill_aux(It first, It last,
530 const P& p,mpl::false_) {
531 std::uninitialized_fill(first,last,p);
539 template <
typename View,
typename Value>
541 if (img_view.is_1d_traversable())
542 detail::uninitialized_fill_aux(img_view.begin().x(), img_view.end().x(),
543 val,is_planar<View>());
545 typename View::y_coord_t y = 0;
547 for (y=0; y<img_view.height(); ++y)
548 detail::uninitialized_fill_aux(img_view.row_begin(y),img_view.row_end(y),
549 val,is_planar<View>());
551 for (
typename View::y_coord_t y0=0; y0<y; ++y0)
552 detail::destruct_aux(img_view.row_begin(y0),img_view.row_end(y0), is_planar<View>());
569 template <
typename It> BOOST_FORCEINLINE
570 void default_construct_range_impl(It first, It last, mpl::true_) {
571 typedef typename std::iterator_traits<It>::value_type value_t;
574 while (first!=last) {
575 new (first) value_t();
579 destruct_range(first1,first);
584 template <
typename It> BOOST_FORCEINLINE
585 void default_construct_range_impl(It, It, mpl::false_) {}
587 template <
typename It> BOOST_FORCEINLINE
588 void default_construct_range(It first, It last) { default_construct_range_impl(first, last,
typename is_pointer<It>::type()); }
591 template <
typename It>
593 void default_construct_aux(It first, It last, mpl::true_) {
596 typedef typename std::iterator_traits<It>::value_type pixel_t;
597 while (channel < num_channels<pixel_t>::value) {
598 default_construct_range(dynamic_at_c(first,channel), dynamic_at_c(last,channel));
602 for (
int c=0; c<channel; ++c)
603 destruct_range(dynamic_at_c(first,c), dynamic_at_c(last,c));
609 template <
typename It>
611 void default_construct_aux(It first, It last, mpl::false_) {
612 default_construct_range(first,last);
615 template <
typename View,
bool IsPlanar>
616 struct has_trivial_pixel_constructor :
public boost::has_trivial_constructor<typename View::value_type> {};
617 template <
typename View>
618 struct has_trivial_pixel_constructor<View, true> :
public boost::has_trivial_constructor<typename channel_type<View>::type> {};
622 template<
typename View,
bool B > BOOST_FORCEINLINE
623 void default_construct_pixels_impl(
const View& img_view
624 , boost::enable_if< is_same< mpl::bool_< B >
630 if( img_view.is_1d_traversable() )
632 detail::default_construct_aux( img_view.begin().x()
639 typename View::y_coord_t y = 0;
642 for( y = 0; y < img_view.height(); ++y )
644 detail::default_construct_aux( img_view.row_begin( y )
645 ,img_view.row_end( y )
651 for (
typename View::y_coord_t y0 = 0; y0 < y; ++y0 )
653 detail::destruct_aux( img_view.row_begin(y0)
654 , img_view.row_end(y0)
669 template <
typename View>
671 detail::default_construct_pixels_impl< View
672 , detail::has_trivial_pixel_constructor< View
673 , is_planar< View >::value
690 template <
typename It1,
typename It2>
692 void uninitialized_copy_aux(It1 first1, It1 last1,
693 It2 first2, mpl::true_) {
696 typedef typename std::iterator_traits<It1>::value_type pixel_t;
697 while (channel < num_channels<pixel_t>::value) {
698 std::uninitialized_copy(dynamic_at_c(first1,channel), dynamic_at_c(last1,channel), dynamic_at_c(first2,channel));
703 std::advance(last2, std::distance(first1,last1));
704 for (
int c=0; c<channel; ++c)
705 destruct_range(dynamic_at_c(first2,c), dynamic_at_c(last2,c));
710 template <
typename It1,
typename It2>
712 void uninitialized_copy_aux(It1 first1, It1 last1,
713 It2 first2,mpl::false_) {
714 std::uninitialized_copy(first1,last1,first2);
722 template <
typename View1,
typename View2>
724 typedef mpl::bool_<is_planar<View1>::value && is_planar<View2>::value> is_planar;
725 assert(view1.dimensions()==view2.dimensions());
726 if (view1.is_1d_traversable() && view2.is_1d_traversable())
727 detail::uninitialized_copy_aux(view1.begin().x(), view1.end().x(),
731 typename View1::y_coord_t y = 0;
733 for (y=0; y<view1.height(); ++y)
734 detail::uninitialized_copy_aux(view1.row_begin(y), view1.row_end(y),
738 for (
typename View1::y_coord_t y0=0; y0<y; ++y0)
739 detail::destruct_aux(view2.row_begin(y0),view2.row_end(y0), is_planar());
761 template <
typename V,
typename F>
762 F for_each_pixel(
const V& img, F fun) {
763 if (img.is_1d_traversable()) {
764 return std::for_each(img.begin().x(), img.end().x(), fun);
766 for (std::ptrdiff_t y=0; y<img.height(); ++y)
767 std::for_each(img.row_begin(y),img.row_end(y),fun);
777 template <
typename View,
typename F>
778 F for_each_pixel_position(
const View& img, F fun) {
779 typename View::xy_locator loc=img.xy_at(0,0);
780 for (std::ptrdiff_t y=0; y<img.height(); ++y) {
781 for (std::ptrdiff_t x=0; x<img.width(); ++x, ++loc.x())
783 loc.x()-=img.width(); ++loc.y();
800 template <
typename View,
typename F>
802 if (v.is_1d_traversable()) {
803 std::generate(v.begin().x(), v.end().x(), fun);
805 for (std::ptrdiff_t y=0; y<v.height(); ++y)
806 std::generate(v.row_begin(y),v.row_end(y),fun);
820 template <
typename I1,
typename I2> BOOST_FORCEINLINE
bool equal_n(I1 i1, std::ptrdiff_t n, I2 i2);
823 template <
typename I1,
typename I2>
825 BOOST_FORCEINLINE
bool operator()(I1 i1, std::ptrdiff_t n, I2 i2)
const {
return std::equal(i1,i1+n, i2); }
830 template<
typename T,
typename Cs>
836 template<
typename T,
typename Cs>
842 template<
typename IC,
typename Cs>
845 std::ptrdiff_t numBytes=n*
sizeof(
typename std::iterator_traits<IC>::value_type);
847 for (std::ptrdiff_t i=0; i<mpl::size<Cs>::value; ++i)
848 if (memcmp(dynamic_at_c(i1,i), dynamic_at_c(i2,i), numBytes)!=0)
855 template <
typename Loc,
typename I2>
858 gil_function_requires<boost::gil::PixelLocatorConcept<Loc> >();
859 gil_function_requires<boost::gil::PixelIteratorConcept<I2> >();
861 std::ptrdiff_t num=std::min<const std::ptrdiff_t>(n, i1.width()-i1.x_pos());
862 if (!equal_n(i1.x(), num, i2))
873 template <
typename I1,
typename Loc>
876 gil_function_requires<boost::gil::PixelIteratorConcept<I1> >();
877 gil_function_requires<boost::gil::PixelLocatorConcept<Loc> >();
879 std::ptrdiff_t num=std::min<const std::ptrdiff_t>(n,i2.width()-i2.x_pos());
880 if (!equal_n(i1, num, i2.x()))
891 template <
typename Loc1,
typename Loc2>
894 gil_function_requires<boost::gil::PixelLocatorConcept<Loc1> >();
895 gil_function_requires<boost::gil::PixelLocatorConcept<Loc2> >();
896 if (i1.x_pos()!=i2.x_pos() || i1.width()!=i2.width()) {
898 if (*i1++!=*i2++)
return false;
902 std::ptrdiff_t num=std::min<const std::ptrdiff_t>(n,i2.width()-i2.x_pos());
903 if (!equal_n(i1.x(), num, i2.x()))
914 template <
typename I1,
typename I2> BOOST_FORCEINLINE
915 bool equal_n(I1 i1, std::ptrdiff_t n, I2 i2) {
916 return detail::equal_n_fn<I1,I2>()(i1,n,i2);
932 template <
typename Loc1,
typename Loc2> BOOST_FORCEINLINE
934 boost::gil::gil_function_requires<boost::gil::PixelLocatorConcept<Loc1> >();
935 boost::gil::gil_function_requires<boost::gil::PixelLocatorConcept<Loc2> >();
936 std::ptrdiff_t n=last-first;
937 if (first.is_1d_traversable()) {
938 if (first2.is_1d_traversable())
939 return boost::gil::detail::equal_n_fn<typename Loc1::x_iterator,typename Loc2::x_iterator>()(first.x(),n, first2.x());
941 return boost::gil::detail::equal_n_fn<typename Loc1::x_iterator,boost::gil::iterator_from_2d<Loc2> >()(first.x(),n, first2);
943 if (first2.is_1d_traversable())
951 namespace boost {
namespace gil {
954 template <
typename View1,
typename View2> BOOST_FORCEINLINE
956 assert(v1.dimensions()==v2.dimensions());
957 return std::equal(v1.begin(),v1.end(),v2.begin());
972 template <
typename View1,
typename View2,
typename F> BOOST_FORCEINLINE
974 assert(src.dimensions()==dst.dimensions());
975 for (std::ptrdiff_t y=0; y<src.height(); ++y) {
976 typename View1::x_iterator srcIt=src.row_begin(y);
977 typename View2::x_iterator dstIt=dst.row_begin(y);
978 for (std::ptrdiff_t x=0; x<src.width(); ++x)
979 dstIt[x]=fun(srcIt[x]);
986 template <
typename View1,
typename View2,
typename View3,
typename F> BOOST_FORCEINLINE
988 for (std::ptrdiff_t y=0; y<dst.height(); ++y) {
989 typename View1::x_iterator srcIt1=src1.row_begin(y);
990 typename View2::x_iterator srcIt2=src2.row_begin(y);
991 typename View3::x_iterator dstIt=dst.row_begin(y);
992 for (std::ptrdiff_t x=0; x<dst.width(); ++x)
993 dstIt[x]=fun(srcIt1[x],srcIt2[x]);
1004 template <
typename View1,
typename View2,
typename F> BOOST_FORCEINLINE
1006 assert(src.dimensions()==dst.dimensions());
1007 typename View1::xy_locator loc=src.xy_at(0,0);
1008 for (std::ptrdiff_t y=0; y<src.height(); ++y) {
1009 typename View2::x_iterator dstIt=dst.row_begin(y);
1010 for (std::ptrdiff_t x=0; x<src.width(); ++x, ++loc.x())
1012 loc.x()-=src.width(); ++loc.y();
1019 template <
typename View1,
typename View2,
typename View3,
typename F> BOOST_FORCEINLINE
1021 assert(src1.dimensions()==dst.dimensions());
1022 assert(src2.dimensions()==dst.dimensions());
1023 typename View1::xy_locator loc1=src1.xy_at(0,0);
1024 typename View2::xy_locator loc2=src2.xy_at(0,0);
1025 for (std::ptrdiff_t y=0; y<src1.height(); ++y) {
1026 typename View3::x_iterator dstIt=dst.row_begin(y);
1027 for (std::ptrdiff_t x=0; x<src1.width(); ++x, ++loc1.x(), ++loc2.x())
1028 dstIt[x]=fun(loc1,loc2);
1029 loc1.x()-=src1.width(); ++loc1.y();
1030 loc2.x()-=src2.width(); ++loc2.y();
void uninitialized_fill_pixels(const View &img_view, const Value &val)
std::uninitialized_fill for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed pixels
Definition: algorithm.hpp:540
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept.
Definition: metafunctions.hpp:43
void generate_pixels(const View &v, F fun)
std::generate for image views
Definition: algorithm.hpp:801
An iterator over planar pixels. Models HomogeneousColorBaseConcept, PixelIteratorConcept, HomogeneousPixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept.
Definition: algorithm.hpp:52
Concept check classes for GIL concepts.
Definition: algorithm.hpp:189
Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept.
Definition: iterator_from_2d.hpp:51
Memory-based pixel locator. Models: PixelLocatorConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConceptThe class takes a step iterator as a parameter. The step iterator provides navigation along the vertical axis while its base iterator provides horizontal navigation.
Definition: algorithm.hpp:56
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:955
void fill(boost::gil::iterator_from_2d< IL > first, boost::gil::iterator_from_2d< IL > last, const V &val)
std::fill(I,I,V) with I being a iterator_from_2d
Definition: algorithm.hpp:367
Returns whether two views are compatible.
Definition: gil_concept.hpp:2141
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:288
BOOST_FORCEINLINE void fill_pixels(const View &img_view, const Value &val)
std::fill for image views
Definition: algorithm.hpp:410
void default_construct_pixels(const View &img_view)
Invokes the in-place default constructor on every pixel of the (uninitialized) view. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place default-constructed pixels.
Definition: algorithm.hpp:670
BOOST_FORCEINLINE boost::gil::iterator_from_2d< OL > copy1(boost::gil::iterator_from_2d< IL > first, boost::gil::iterator_from_2d< IL > last, boost::gil::iterator_from_2d< OL > dst)
std::copy(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:279
BOOST_FORCEINLINE boost::gil::planar_pixel_iterator< IC2, Cs > copy(boost::gil::planar_pixel_iterator< IC1, Cs > first, boost::gil::planar_pixel_iterator< IC1, Cs > last, boost::gil::planar_pixel_iterator< IC2, Cs > dst)
Copy when both src and dst are planar pointers is copy for each channel.
Definition: algorithm.hpp:177
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:933
A generic binary operation on viewsUse this class as a convenience superclass when defining an operat...
Definition: algorithm.hpp:99
struct to do std::fill
Definition: algorithm.hpp:387
void uninitialized_copy_pixels(const View1 &view1, const View2 &view2)
std::uninitialized_copy for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed objects
Definition: algorithm.hpp:723
Methods for constructing image views from raw data or other image views /.
BOOST_FORCEINLINE void destruct_pixels(const View &img_view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:483
MEMORY-BASED STEP ITERATOR.
Definition: algorithm.hpp:54
A model of a heterogeneous pixel that is not byte aligned. Examples are bitmap (1-bit pixels) or 6-bi...