]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/gil/example/interleaved_ref.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / gil / example / interleaved_ref.hpp
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://stlab.adobe.com/gil for most recent version including documentation.
9 */
10
11 /*************************************************************************************************/
12
13
14 ////////////////////////////////////////////////////////////////////////////////////////
15 /// \file
16 /// \brief Example on how to create a new model of a pixel reference
17 /// \author Lubomir Bourdev and Hailin Jin \n
18 /// Adobe Systems Incorporated
19 /// \date 2005-2007 \n Last updated on February 26, 2007
20 //////
21 ////////////////////////////////////////////////////////////////////////////////////////
22
23 #ifndef GIL_INTERLEAVED_REF_HPP
24 #define GIL_INTERLEAVED_REF_HPP
25
26 #include <boost/mpl/range_c.hpp>
27 #include <boost/mpl/vector_c.hpp>
28 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
29
30 namespace boost { namespace gil {
31
32
33 /////////////////////////////////////////////////////////////////////////
34 ///
35 /// A model of an interleaved pixel reference. Holds a pointer to the first channel
36 /// MODELS:
37 /// MutableHomogeneousPixelConcept
38 /// MutableHomogeneousColorBaseConcept
39 /// MutableColorBaseConcept
40 /// HomogeneousColorBaseConcept
41 /// ColorBaseConcept
42 /// HomogeneousPixelBasedConcept
43 /// PixelBasedConcept
44 ///
45 /// For planar reference proxies to work properly, all of their methods must be const-qualified
46 /// and their iterator's reference type must be const-qualified.
47 /// Mutability of the reference proxy is part of its type (in this case, depends on the mutability of ChannelReference)
48 /////////////////////////////////////////////////////////////////////////
49
50 template <typename ChannelReference, // Models ChannelConcept. A channel reference, unsigned char& or const unsigned char&
51 typename Layout> // A layout (includes the color space and channel ordering)
52 struct interleaved_ref {
53 private:
54 typedef typename channel_traits<ChannelReference>::value_type channel_t;
55 typedef typename channel_traits<ChannelReference>::reference channel_reference_t;
56 typedef typename channel_traits<ChannelReference>::const_reference channel_const_reference_t;
57 typedef typename channel_traits<ChannelReference>::pointer channel_pointer_t;
58 public:
59 // Required by ColorBaseConcept
60 typedef Layout layout_t;
61
62 // Copy construction from a compatible type. The copy constructor of references is shallow. The channels themselves are not copied.
63 interleaved_ref(const interleaved_ref& p) : _channels(p._channels) {}
64 template <typename P> interleaved_ref(const P& p) : _channels(p._channels) { check_compatible<P>(); }
65
66 template <typename P> bool operator==(const P& p) const { check_compatible<P>(); return static_equal(*this,p); }
67 template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
68
69 // Required by MutableColorBaseConcept
70
71 // Assignment from a compatible type
72 const interleaved_ref& operator=(const interleaved_ref& p) const { static_copy(p,*this); return *this; }
73 template <typename P> const interleaved_ref& operator=(const P& p) const { check_compatible<P>(); static_copy(p,*this); return *this; }
74
75 // Required by PixelConcept
76 typedef pixel<channel_t, layout_t> value_type;
77 typedef interleaved_ref reference;
78 typedef interleaved_ref<channel_const_reference_t, layout_t> const_reference;
79 static const bool is_mutable = channel_traits<ChannelReference>::is_mutable;
80
81 // Required by HomogeneousPixelConcept
82 ChannelReference operator[](std::size_t i) const { return _channels[i]; }
83
84 // Custom constructor (not part of any concept)
85 explicit interleaved_ref(channel_pointer_t channels) : _channels(channels) {}
86 // This is needed for the reference proxy to work properly
87 const interleaved_ref* operator->() const { return this; }
88 private:
89 channel_pointer_t _channels;
90
91 template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,interleaved_ref> >(); }
92 };
93
94 // Required by ColorBaseConcept
95 template <typename ChannelReference, typename Layout, int K>
96 struct kth_element_type<interleaved_ref<ChannelReference,Layout>,K> {
97 typedef ChannelReference type;
98 };
99
100 template <typename ChannelReference, typename Layout, int K>
101 struct kth_element_reference_type<interleaved_ref<ChannelReference,Layout>,K> {
102 typedef ChannelReference type;
103 };
104
105 template <typename ChannelReference, typename Layout, int K>
106 struct kth_element_const_reference_type<interleaved_ref<ChannelReference,Layout>,K> {
107 typedef ChannelReference type;
108 // typedef typename channel_traits<ChannelReference>::const_reference type;
109 };
110
111
112 // Required by ColorBaseConcept
113 template <int K, typename ChannelReference, typename Layout>
114 typename element_reference_type<interleaved_ref<ChannelReference,Layout> >::type
115 at_c(const interleaved_ref<ChannelReference,Layout>& p) { return p[K]; };
116
117 // Required by HomogeneousColorBaseConcept
118 template <typename ChannelReference, typename Layout>
119 typename element_reference_type<interleaved_ref<ChannelReference,Layout> >::type
120 dynamic_at_c(const interleaved_ref<ChannelReference,Layout>& p, std::size_t n) { return p[n]; };
121
122 namespace detail {
123 struct swap_fn_t {
124 template <typename T> void operator()(T& x, T& y) const {
125 using std::swap;
126 swap(x,y);
127 }
128 };
129 }
130
131 // Required by MutableColorBaseConcept. The default std::swap does not do the right thing for proxy references - it swaps the references, not the values
132 template <typename ChannelReference, typename Layout>
133 void swap(const interleaved_ref<ChannelReference,Layout>& x, const interleaved_ref<ChannelReference,Layout>& y) {
134 static_for_each(x,y,detail::swap_fn_t());
135 };
136
137 // Required by PixelConcept
138 template <typename ChannelReference, typename Layout>
139 struct is_pixel<interleaved_ref<ChannelReference,Layout> > : public boost::mpl::true_ {};
140
141
142 // Required by PixelBasedConcept
143 template <typename ChannelReference, typename Layout>
144 struct color_space_type<interleaved_ref<ChannelReference,Layout> > {
145 typedef typename Layout::color_space_t type;
146 };
147
148 // Required by PixelBasedConcept
149 template <typename ChannelReference, typename Layout>
150 struct channel_mapping_type<interleaved_ref<ChannelReference,Layout> > {
151 typedef typename Layout::channel_mapping_t type;
152 };
153
154 // Required by PixelBasedConcept
155 template <typename ChannelReference, typename Layout>
156 struct is_planar<interleaved_ref<ChannelReference,Layout> > : mpl::false_ {};
157
158 // Required by HomogeneousPixelBasedConcept
159 template <typename ChannelReference, typename Layout>
160 struct channel_type<interleaved_ref<ChannelReference,Layout> > {
161 typedef typename channel_traits<ChannelReference>::value_type type;
162 };
163
164 } } // namespace boost::gil
165
166 #endif