1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_BASEBMP_COLORBLENDACCESSORADAPTER_HXX
25 #define INCLUDED_BASEBMP_COLORBLENDACCESSORADAPTER_HXX
26 
27 #include <basebmp/colortraits.hxx>
28 
29 namespace basebmp
30 {
31 
32 /** Accessor adapter that blends input value against fixed color value
33 
34     Used to blend an alpha mask 'through' a fixed color value into the
35     destination.
36 
37     The getter functors return a constant value (usually the zero of
38     the value type, this preserves the original destination content
39     when blitting through a mask) - there really isn't no other
40     sensible default behaviour for these methods.
41  */
42 template< class    WrappedAccessor,
43           typename AlphaType,
44           bool     polarity > class ConstantColorBlendSetterAccessorAdapter
45 {
46 public:
47     typedef AlphaType                            alpha_type;
48     typedef AlphaType                            value_type;
49     typedef typename WrappedAccessor::value_type color_type;
50 
51 private:
52     typename ColorTraits< color_type >::
53              template blend_functor<alpha_type,polarity>::type   maFunctor;
54     WrappedAccessor                                              maWrappee;
55     color_type                                                   maBlendColor;
56     value_type                                                   maGetterValue;
57 
58 public:
ConstantColorBlendSetterAccessorAdapter()59     ConstantColorBlendSetterAccessorAdapter() :
60         maFunctor(),
61         maWrappee(),
62         maBlendColor(),
63         maGetterValue()
64     {}
65 
ConstantColorBlendSetterAccessorAdapter(T acc)66     template< class T > explicit ConstantColorBlendSetterAccessorAdapter( T acc ) :
67         maFunctor(),
68         maWrappee(acc),
69         maBlendColor(),
70         maGetterValue()
71     {}
72 
ConstantColorBlendSetterAccessorAdapter(T acc,color_type col)73     template< class T > ConstantColorBlendSetterAccessorAdapter( T          acc,
74                                                                  color_type col ) :
75         maFunctor(),
76         maWrappee(acc),
77         maBlendColor(col),
78         maGetterValue()
79     {}
80 
ConstantColorBlendSetterAccessorAdapter(T acc,color_type col,value_type val)81     template< class T > ConstantColorBlendSetterAccessorAdapter( T          acc,
82                                                                  color_type col,
83                                                                  value_type val ) :
84         maFunctor(),
85         maWrappee(acc),
86         maBlendColor(col),
87         maGetterValue(val)
88     {}
89 
90     // -------------------------------------------------------
91 
setColor(color_type col)92     void        setColor( color_type col ) { maBlendColor=col; }
getColor()93     color_type  getColor() { return maBlendColor; }
setGetterValue(value_type val)94     void        setGetterValue( value_type val ) { maGetterValue=val; }
getGetterValue()95     value_type  getGetterValue() { return maGetterValue; }
96 
97     // -------------------------------------------------------
98 
getWrappedAccessor() const99     WrappedAccessor const& getWrappedAccessor() const { return maWrappee; }
getWrappedAccessor()100     WrappedAccessor&       getWrappedAccessor() { return maWrappee; }
101 
102     // -------------------------------------------------------
103 
104     /// @return constant value, regardless of iterator content
operator ()(IteratorType const &) const105     template< typename IteratorType > value_type operator()(IteratorType const& ) const
106     {
107         return maGetterValue;
108     }
109     /// @return constant value, regardless of iterator content
110     template< typename IteratorType, class Difference >
operator ()(IteratorType const &,Difference const &) const111     value_type operator()(IteratorType const& , Difference const& ) const
112     {
113         return maGetterValue;
114     }
115 
116     // -------------------------------------------------------
117 
118     template< typename V, typename IteratorType >
set(V const & value,IteratorType const & i) const119     void set(V const& value, IteratorType const& i) const
120     {
121         maWrappee.set(
122             maFunctor(
123                 vigra::detail::RequiresExplicitCast<alpha_type>::cast(value),
124                 maWrappee(i),
125                 maBlendColor),
126             i );
127     }
128 
129     template< typename V, typename IteratorType, class Difference >
set(V const & value,IteratorType const & i,Difference const & diff) const130     void set(V const& value, IteratorType const& i, Difference const& diff) const
131     {
132         maWrappee.set(
133             maFunctor(
134                 vigra::detail::RequiresExplicitCast<alpha_type>::cast(value),
135                 maWrappee(i,diff),
136                 maBlendColor),
137             i,
138             diff );
139     }
140 };
141 
142 } // namespace basebmp
143 
144 #endif /* INCLUDED_BASEBMP_COLORBLENDACCESSORADAPTER_HXX */
145