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