Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_BASEBMP_ACCESSORFUNCTORS_HXX
21 : #define INCLUDED_BASEBMP_ACCESSORFUNCTORS_HXX
22 :
23 : #include <osl/diagnose.h>
24 : #include <basebmp/metafunctions.hxx>
25 :
26 : #include <functional>
27 :
28 : namespace basebmp
29 : {
30 :
31 : // Some common accessor functors
32 :
33 :
34 :
35 : /// combine two values via XOR
36 : template< typename T > struct XorFunctor : public std::binary_function<T,T,T>
37 : {
38 0 : T operator()( T v1, T v2 ) const { return v1 ^ v2; }
39 : };
40 :
41 :
42 :
43 : /// Base class, passing on the arg types
44 : template< typename T, typename M > struct MaskFunctorBase :
45 : public TernaryFunctorBase<T,M,T,T> {};
46 :
47 :
48 : /** Let a mask flag decide between two values
49 :
50 : @tpl polarity
51 : Mask polarity. When true, a false in the mask denotes
52 : transparency, i.e. the original value will display. And vice
53 : versa.
54 : */
55 : template< typename T,
56 : typename M,
57 : bool polarity > struct GenericOutputMaskFunctor : public MaskFunctorBase<T,M>
58 : {
59 : /// Ternary mask operation - selects v1 for !m == polarity, v2 otherwise
60 0 : T operator()( T v1, M m, T v2 ) const
61 : {
62 0 : return !m == polarity ? v1 : v2;
63 : }
64 : };
65 :
66 : /** Let a mask bit decide between two values (specialization for
67 : integer mask types)
68 : */
69 : template< typename T,
70 : typename M,
71 : bool polarity > struct IntegerOutputMaskFunctor;
72 : template< typename T,
73 : typename M > struct IntegerOutputMaskFunctor<T,M,true> : public MaskFunctorBase<T,M>
74 : {
75 : /** Mask v with state of m
76 :
77 : @return v2, if m != 0, v1 otherwise.
78 : */
79 : T operator()( T v1, M m, T v2 ) const
80 : {
81 : typedef typename make_unsigned<T>::type unsigned_T;
82 :
83 : // mask will be 0, iff m == 0, and 1 otherwise
84 : const T mask( unsigned_cast<T>(m | -m) >> (sizeof(unsigned_T)*8 - 1) );
85 : return v1*(M)(1-mask) + v2*mask;
86 : }
87 : };
88 : template< typename T,
89 : typename M > struct IntegerOutputMaskFunctor<T,M,false> : public MaskFunctorBase<T,M>
90 : {
91 : /** Mask v with state of m
92 :
93 : @return v2, if m != 0, v1 otherwise.
94 : */
95 : T operator()( T v1, M m, T v2 ) const
96 : {
97 : typedef typename make_unsigned<T>::type unsigned_T;
98 :
99 : // mask will be 0, iff m == 0, and 1 otherwise
100 : const T mask( unsigned_cast<T>(m | -m) >> (sizeof(unsigned_T)*8 - 1) );
101 : return v1*mask + v2*(M)(1-mask);
102 : }
103 : };
104 :
105 : /** Let a mask bit decide between two values (specialization for
106 : binary-valued mask types)
107 : */
108 : template< typename T, typename M, bool polarity > struct FastIntegerOutputMaskFunctor;
109 : template< typename T, typename M > struct FastIntegerOutputMaskFunctor<T,M,true> :
110 : public MaskFunctorBase<T,M>
111 : {
112 : /// Specialization, only valid if mask can only attain 0 or 1
113 : T operator()( T v1, M m, T v2 ) const
114 : {
115 : OSL_ASSERT(m<=1);
116 :
117 : return v1*(M)(1-m) + v2*m;
118 : }
119 : };
120 : template< typename T, typename M > struct FastIntegerOutputMaskFunctor<T,M,false> :
121 : public MaskFunctorBase<T,M>
122 : {
123 : /// Specialization, only valid if mask can only attain 0 or 1
124 0 : T operator()( T v1, M m, T v2 ) const
125 : {
126 : OSL_ASSERT(m<=1);
127 :
128 0 : return v1*m + v2*(M)(1-m);
129 : }
130 : };
131 :
132 :
133 :
134 : /** Split a pair value from a JoinImageAccessorAdapter into its
135 : individual values, and pass it on to a ternary functor
136 :
137 : This wrapper is an adaptable binary functor, and can thus be used
138 : with a BinarySetterFunctionAccessorAdapter. Useful e.g. for
139 : out-of-image alpha channel, or a masked image.
140 :
141 : @tpl Functor
142 : An adaptable ternary functor (as can e.g. be passed to the
143 : TernarySetterFunctionAccessorAdapter)
144 : */
145 : template< typename Functor > struct BinaryFunctorSplittingWrapper :
146 : public std::binary_function<typename Functor::first_argument_type,
147 : std::pair<typename Functor::third_argument_type,
148 : typename Functor::second_argument_type>,
149 : typename Functor::result_type>
150 : {
151 : #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
152 : // making all members public, if no member template friends
153 : private:
154 : template<class A> friend struct BinaryFunctorSplittingWrapper;
155 : #endif
156 : Functor maFunctor;
157 :
158 : public:
159 0 : BinaryFunctorSplittingWrapper() : maFunctor() {}
160 :
161 : template< class A > explicit
162 : BinaryFunctorSplittingWrapper(
163 : BinaryFunctorSplittingWrapper<A> const& src ) : maFunctor(src.maFunctor) {}
164 :
165 : template< class F > explicit
166 : BinaryFunctorSplittingWrapper( F const& func ) : maFunctor(func) {}
167 :
168 0 : typename Functor::result_type operator()(
169 : typename Functor::first_argument_type v1,
170 : std::pair< typename Functor::third_argument_type,
171 : typename Functor::second_argument_type > const& v2 ) const
172 : {
173 0 : return maFunctor( v1, v2.second, v2.first );
174 : }
175 : };
176 :
177 : } // namespace basebmp
178 :
179 : #endif /* INCLUDED_BASEBMP_ACCESSORFUNCTORS_HXX */
180 :
181 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|