1 /*
2     Copyright 2005-2007 Adobe Systems Incorporated
3 
4     Use, modification and distribution are subject to the Boost Software License,
5     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt).
7 
8     See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 /*************************************************************************************************/
11 
12 #ifndef GIL_DYNAMICIMAGE_ALGORITHM_HPP
13 #define GIL_DYNAMICIMAGE_ALGORITHM_HPP
14 
15 #include "../../algorithm.hpp"
16 #include "any_image.hpp"
17 #include <boost/bind.hpp>
18 
19 ////////////////////////////////////////////////////////////////////////////////////////
20 /// \file
21 /// \brief Some basic STL-style algorithms when applied to runtime type specified image views
22 /// \author Lubomir Bourdev and Hailin Jin \n
23 ///         Adobe Systems Incorporated
24 /// \date 2005-2007 \n Last updated on September 24, 2006
25 ///
26 ////////////////////////////////////////////////////////////////////////////////////////
27 
28 namespace boost { namespace gil {
29 
30 namespace detail {
31     struct equal_pixels_fn : public binary_operation_obj<equal_pixels_fn,bool> {
32         template <typename V1, typename V2>
apply_compatibleboost::gil::detail::equal_pixels_fn33         GIL_FORCEINLINE bool apply_compatible(const V1& v1, const V2& v2) const {
34             return equal_pixels(v1,v2);
35         }
36     };
37 } // namespace detail
38 
39 /// \ingroup ImageViewSTLAlgorithmsEqualPixels
40 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
41           typename View2>   // Model MutableImageViewConcept
equal_pixels(const any_image_view<Types1> & src,const View2 & dst)42 bool equal_pixels(const any_image_view<Types1>& src, const View2& dst) {
43     return apply_operation(src,boost::bind(detail::equal_pixels_fn(), _1, dst));
44 }
45 
46 /// \ingroup ImageViewSTLAlgorithmsEqualPixels
47 template <typename View1,   // Model ImageViewConcept
48           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
equal_pixels(const View1 & src,const any_image_view<Types2> & dst)49 bool equal_pixels(const View1& src, const any_image_view<Types2>& dst) {
50     return apply_operation(dst,boost::bind(detail::equal_pixels_fn(), src, _1));
51 }
52 
53 /// \ingroup ImageViewSTLAlgorithmsEqualPixels
54 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
55           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
equal_pixels(const any_image_view<Types1> & src,const any_image_view<Types2> & dst)56 bool equal_pixels(const any_image_view<Types1>& src, const any_image_view<Types2>& dst) {
57     return apply_operation(src,dst,detail::equal_pixels_fn());
58 }
59 
60 namespace detail {
61     struct copy_pixels_fn : public binary_operation_obj<copy_pixels_fn> {
62         template <typename View1, typename View2>
apply_compatibleboost::gil::detail::copy_pixels_fn63         GIL_FORCEINLINE void apply_compatible(const View1& src, const View2& dst) const {
64             copy_pixels(src,dst);
65         }
66     };
67 }
68 
69 /// \ingroup ImageViewSTLAlgorithmsCopyPixels
70 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
71           typename View2>   // Model MutableImageViewConcept
copy_pixels(const any_image_view<Types1> & src,const View2 & dst)72 void copy_pixels(const any_image_view<Types1>& src, const View2& dst) {
73     apply_operation(src,boost::bind(detail::copy_pixels_fn(), _1, dst));
74 }
75 
76 /// \ingroup ImageViewSTLAlgorithmsCopyPixels
77 template <typename View1,   // Model ImageViewConcept
78           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
copy_pixels(const View1 & src,const any_image_view<Types2> & dst)79 void copy_pixels(const View1& src, const any_image_view<Types2>& dst) {
80     apply_operation(dst,boost::bind(detail::copy_pixels_fn(), src, _1));
81 }
82 
83 /// \ingroup ImageViewSTLAlgorithmsCopyPixels
84 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
85           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
copy_pixels(const any_image_view<Types1> & src,const any_image_view<Types2> & dst)86 void copy_pixels(const any_image_view<Types1>& src, const any_image_view<Types2>& dst) {
87     apply_operation(src,dst,detail::copy_pixels_fn());
88 }
89 
90 
91 
92 //forward declaration for default_color_converter (see full definition in color_convert.hpp)
93 struct default_color_converter;
94 
95 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
96 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
97           typename View2,   // Model MutableImageViewConcept
98           typename CC>      // Model ColorConverterConcept
copy_and_convert_pixels(const any_image_view<Types1> & src,const View2 & dst,CC cc)99 void copy_and_convert_pixels(const any_image_view<Types1>& src, const View2& dst, CC cc) {
100     apply_operation(src,boost::bind(detail::copy_and_convert_pixels_fn<CC>(cc), _1, dst));
101 }
102 
103 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
104 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
105           typename View2>   // Model MutableImageViewConcept
copy_and_convert_pixels(const any_image_view<Types1> & src,const View2 & dst)106 void copy_and_convert_pixels(const any_image_view<Types1>& src, const View2& dst) {
107     apply_operation(src,boost::bind(detail::copy_and_convert_pixels_fn<default_color_converter>(), _1, dst));
108 }
109 
110 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
111 template <typename View1,   // Model ImageViewConcept
112           typename Types2,  // Model MPL Random Access Container of models of MutableImageViewConcept
113           typename CC>      // Model ColorConverterConcept
copy_and_convert_pixels(const View1 & src,const any_image_view<Types2> & dst,CC cc)114 void copy_and_convert_pixels(const View1& src, const any_image_view<Types2>& dst, CC cc) {
115     apply_operation(dst,boost::bind(detail::copy_and_convert_pixels_fn<CC>(cc), src, _1));
116 }
117 
118 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
119 template <typename View1,   // Model ImageViewConcept
120           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
copy_and_convert_pixels(const View1 & src,const any_image_view<Types2> & dst)121 void copy_and_convert_pixels(const View1& src, const any_image_view<Types2>& dst) {
122     apply_operation(dst,boost::bind(detail::copy_and_convert_pixels_fn<default_color_converter>(), src, _1));
123 }
124 
125 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
126 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
127           typename Types2,  // Model MPL Random Access Container of models of MutableImageViewConcept
128           typename CC>      // Model ColorConverterConcept
copy_and_convert_pixels(const any_image_view<Types1> & src,const any_image_view<Types2> & dst,CC cc)129 void copy_and_convert_pixels(const any_image_view<Types1>& src, const any_image_view<Types2>& dst, CC cc) {
130     apply_operation(src,dst,detail::copy_and_convert_pixels_fn<CC>(cc));
131 }
132 
133 /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
134 template <typename Types1,  // Model MPL Random Access Container of models of ImageViewConcept
135           typename Types2>  // Model MPL Random Access Container of models of MutableImageViewConcept
copy_and_convert_pixels(const any_image_view<Types1> & src,const any_image_view<Types2> & dst)136 void copy_and_convert_pixels(const any_image_view<Types1>& src, const any_image_view<Types2>& dst) {
137     apply_operation(src,dst,detail::copy_and_convert_pixels_fn<default_color_converter>());
138 }
139 
140 namespace detail {
141 template <bool COMPATIBLE> struct fill_pixels_fn1 {
applyboost::gil::detail::fill_pixels_fn1142     template <typename V, typename Value> static void apply(const V& src, const Value& val) { fill_pixels(src,val); }
143 };
144 
145 // copy_pixels invoked on incompatible images
146 template <> struct fill_pixels_fn1<false> {
applyboost::gil::detail::fill_pixels_fn1147     template <typename V, typename Value> static void apply(const V& src, const Value& val) { throw std::bad_cast();}
148 };
149 
150 template <typename Value>
151 struct fill_pixels_fn {
fill_pixels_fnboost::gil::detail::fill_pixels_fn152     fill_pixels_fn(const Value& val) : _val(val) {}
153 
154     typedef void result_type;
operator ()boost::gil::detail::fill_pixels_fn155     template <typename V> result_type operator()(const V& img_view) const {
156         fill_pixels_fn1<pixels_are_compatible<typename V::value_type, Value>::value>::apply(img_view,_val);
157     }
158     Value _val;
159 };
160 }
161 
162 /// \ingroup ImageViewSTLAlgorithmsFillPixels
163 /// \brief fill_pixels for any image view. The pixel to fill with must be compatible with the current view
164 template <typename Types, // Model MPL Random Access Container of models of MutableImageViewConcept
165           typename Value>
fill_pixels(const any_image_view<Types> & img_view,const Value & val)166 void fill_pixels(const any_image_view<Types>& img_view, const Value& val) {
167     apply_operation(img_view,detail::fill_pixels_fn<Value>(val));
168 }
169 
170 
171 } }  // namespace boost::gil
172 
173 #endif
174