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_COLORTRAITS_HXX 25cdf0e10cSrcweir #define INCLUDED_BASEBMP_COLORTRAITS_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <basebmp/accessoradapters.hxx> 28cdf0e10cSrcweir #include <basebmp/metafunctions.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <vigra/mathutil.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace basebmp 33cdf0e10cSrcweir { 34cdf0e10cSrcweir 35cdf0e10cSrcweir /** Functor template, to calculate alpha blending between two 36cdf0e10cSrcweir values. Float case. 37cdf0e10cSrcweir 38cdf0e10cSrcweir @tpl polarity 39cdf0e10cSrcweir When true, 0 means fully transparent, and 1 fully opaque. And vice 40cdf0e10cSrcweir versa. 41cdf0e10cSrcweir */ 42cdf0e10cSrcweir template< typename ValueType, 43cdf0e10cSrcweir typename AlphaType, 44cdf0e10cSrcweir bool polarity > struct BlendFunctor; 45cdf0e10cSrcweir template< typename ValueType, 46cdf0e10cSrcweir typename AlphaType > struct BlendFunctor<ValueType,AlphaType,true> 47cdf0e10cSrcweir : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> 48cdf0e10cSrcweir { operator ()basebmp::BlendFunctor49cdf0e10cSrcweir ValueType operator()( AlphaType alpha, 50cdf0e10cSrcweir ValueType v1, 51cdf0e10cSrcweir ValueType v2 ) const 52cdf0e10cSrcweir { 53cdf0e10cSrcweir const typename vigra::NumericTraits<AlphaType>::RealPromote fAlpha( 54cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::toRealPromote(alpha)); 55cdf0e10cSrcweir return (vigra::NumericTraits<AlphaType>::one()-fAlpha)*v1 + fAlpha*v2; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir }; 58cdf0e10cSrcweir template< typename ValueType, 59cdf0e10cSrcweir typename AlphaType > struct BlendFunctor<ValueType,AlphaType,false> 60cdf0e10cSrcweir : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> 61cdf0e10cSrcweir { operator ()basebmp::BlendFunctor62cdf0e10cSrcweir ValueType operator()( AlphaType alpha, 63cdf0e10cSrcweir ValueType v1, 64cdf0e10cSrcweir ValueType v2 ) const 65cdf0e10cSrcweir { 66cdf0e10cSrcweir const typename vigra::NumericTraits<AlphaType>::RealPromote fAlpha( 67cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::toRealPromote(alpha)); 68cdf0e10cSrcweir return fAlpha*v1 + (vigra::NumericTraits<AlphaType>::one()-fAlpha)*v2; 69cdf0e10cSrcweir } 70cdf0e10cSrcweir }; 71cdf0e10cSrcweir 72cdf0e10cSrcweir /** Functor template, to calculate alpha blending between two 73cdf0e10cSrcweir values. Integer case. 74cdf0e10cSrcweir 75cdf0e10cSrcweir @tpl polarity 76cdf0e10cSrcweir When true, 0 means fully transparent, and 1 fully opaque. And vice 77cdf0e10cSrcweir versa. 78cdf0e10cSrcweir */ 79cdf0e10cSrcweir template< typename ValueType, 80cdf0e10cSrcweir typename AlphaType, 81cdf0e10cSrcweir bool polarity > struct IntegerBlendFunctor; 82cdf0e10cSrcweir template< typename ValueType, 83cdf0e10cSrcweir typename AlphaType > struct IntegerBlendFunctor<ValueType,AlphaType,true> 84cdf0e10cSrcweir : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> 85cdf0e10cSrcweir { operator ()basebmp::IntegerBlendFunctor86cdf0e10cSrcweir ValueType operator()( AlphaType alpha, 87cdf0e10cSrcweir ValueType v1, 88cdf0e10cSrcweir ValueType v2 ) const 89cdf0e10cSrcweir { 90cdf0e10cSrcweir return (vigra::NumericTraits<AlphaType>::toPromote( 91cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::max()-alpha)*v1 + alpha*v2) / 92cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::max(); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir }; 95cdf0e10cSrcweir template< typename ValueType, 96cdf0e10cSrcweir typename AlphaType > struct IntegerBlendFunctor<ValueType,AlphaType,false> 97cdf0e10cSrcweir : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> 98cdf0e10cSrcweir { operator ()basebmp::IntegerBlendFunctor99cdf0e10cSrcweir ValueType operator()( AlphaType alpha, 100cdf0e10cSrcweir ValueType v1, 101cdf0e10cSrcweir ValueType v2 ) const 102cdf0e10cSrcweir { 103cdf0e10cSrcweir return (alpha*v1 + 104cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::toPromote( 105cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::max()-alpha)*v2) / 106cdf0e10cSrcweir vigra::NumericTraits<AlphaType>::max(); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir }; 109cdf0e10cSrcweir 110cdf0e10cSrcweir //----------------------------------------------------------------------------- 111cdf0e10cSrcweir 112cdf0e10cSrcweir template< typename ColorType > struct ColorTraits 113cdf0e10cSrcweir { 114cdf0e10cSrcweir /// Metafunction to select blend functor from color and alpha type 115cdf0e10cSrcweir template< typename AlphaType, bool polarity > struct blend_functor : public 116cdf0e10cSrcweir ifScalarIntegral< AlphaType, 117cdf0e10cSrcweir IntegerBlendFunctor< ColorType, AlphaType, polarity >, 118cdf0e10cSrcweir BlendFunctor< ColorType, AlphaType, polarity > > {}; 119cdf0e10cSrcweir 120cdf0e10cSrcweir /// @return number of color channels numChannelsbasebmp::ColorTraits121cdf0e10cSrcweir static int numChannels() { return 1; } 122cdf0e10cSrcweir 123cdf0e10cSrcweir /// Type of a color component (i.e. the type of an individual channel) 124cdf0e10cSrcweir typedef ColorType component_type; 125cdf0e10cSrcweir 126cdf0e10cSrcweir /// Calculate normalized distance between color c1 and c2 distancebasebmp::ColorTraits127cdf0e10cSrcweir static inline vigra::NormTraits<ColorType> distance( ColorType c1, 128cdf0e10cSrcweir ColorType c2 ) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir return vigra::norm(c1 - c2); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir toGreyscalebasebmp::ColorTraits133cdf0e10cSrcweir static inline component_type toGreyscale( ColorType c ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir return c; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir fromGreyscalebasebmp::ColorTraits138cdf0e10cSrcweir static inline ColorType fromGreyscale( component_type c ) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir return c; 141cdf0e10cSrcweir } 142cdf0e10cSrcweir }; 143cdf0e10cSrcweir 144cdf0e10cSrcweir } // namespace basebmp 145cdf0e10cSrcweir 146cdf0e10cSrcweir #endif /* INCLUDED_BASEBMP_COLORTRAITS_HXX */ 147