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_COLORMISC_HXX 25cdf0e10cSrcweir #define INCLUDED_BASEBMP_COLORMISC_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <osl/diagnose.h> 28cdf0e10cSrcweir #include <basebmp/color.hxx> 29cdf0e10cSrcweir #include <basebmp/colortraits.hxx> 30cdf0e10cSrcweir #include <basebmp/accessortraits.hxx> 31cdf0e10cSrcweir #include <vigra/mathutil.hxx> 32cdf0e10cSrcweir 33cdf0e10cSrcweir // Contents of this header moved out of color.hxx, as it is not useful 34cdf0e10cSrcweir // for the general public (drags in vigra and other template 35cdf0e10cSrcweir // functionality, that shouldn't be necessary for the ordinary client 36cdf0e10cSrcweir // of BitmapDevice etc.) 37cdf0e10cSrcweir 38cdf0e10cSrcweir namespace basebmp 39cdf0e10cSrcweir { 40cdf0e10cSrcweir 41cdf0e10cSrcweir template< bool polarity > struct ColorBitmaskOutputMaskFunctor; 42cdf0e10cSrcweir template<> struct ColorBitmaskOutputMaskFunctor<true> : MaskFunctorBase<Color,sal_uInt8> 43cdf0e10cSrcweir { operator ()basebmp::ColorBitmaskOutputMaskFunctor44cdf0e10cSrcweir Color operator()( Color v1, sal_uInt8 m, Color v2 ) const 45cdf0e10cSrcweir { 46cdf0e10cSrcweir OSL_ASSERT(m<=1); 47cdf0e10cSrcweir 48cdf0e10cSrcweir return Color(v1.toInt32()*(sal_uInt8)(1-m) + v2.toInt32()*m); 49cdf0e10cSrcweir } 50cdf0e10cSrcweir }; 51cdf0e10cSrcweir template<> struct ColorBitmaskOutputMaskFunctor<false> : MaskFunctorBase<Color,sal_uInt8> 52cdf0e10cSrcweir { operator ()basebmp::ColorBitmaskOutputMaskFunctor53cdf0e10cSrcweir Color operator()( Color v1, sal_uInt8 m, Color v2 ) const 54cdf0e10cSrcweir { 55cdf0e10cSrcweir OSL_ASSERT(m<=1); 56cdf0e10cSrcweir 57cdf0e10cSrcweir return Color(v1.toInt32()*m + v2.toInt32()*(sal_uInt8)(1-m)); 58cdf0e10cSrcweir } 59cdf0e10cSrcweir }; 60cdf0e10cSrcweir 61cdf0e10cSrcweir /// Specialized output mask functor for Color value type 62cdf0e10cSrcweir template<bool polarity> struct outputMaskFunctorSelector< Color, sal_uInt8, polarity, FastMask > 63cdf0e10cSrcweir { 64cdf0e10cSrcweir typedef ColorBitmaskOutputMaskFunctor<polarity> type; 65cdf0e10cSrcweir }; 66cdf0e10cSrcweir 67cdf0e10cSrcweir template< bool polarity > struct ColorBlendFunctor8 68cdf0e10cSrcweir : public TernaryFunctorBase<sal_uInt8,Color,Color,Color> 69cdf0e10cSrcweir { operator ()basebmp::ColorBlendFunctor870cdf0e10cSrcweir Color operator()( sal_uInt8 alpha, 71cdf0e10cSrcweir Color v1, 72cdf0e10cSrcweir Color v2 ) const 73cdf0e10cSrcweir { 74cdf0e10cSrcweir alpha = polarity ? alpha : 255 - alpha; 75cdf0e10cSrcweir 76cdf0e10cSrcweir const sal_uInt8 v1_red( v1.getRed() ); 77cdf0e10cSrcweir const sal_uInt8 v1_green( v1.getGreen() ); 78cdf0e10cSrcweir const sal_uInt8 v1_blue( v1.getBlue() ); 79cdf0e10cSrcweir 80cdf0e10cSrcweir // using '>> 8' instead of '/ 0x100' is ill-advised (shifted 81cdf0e10cSrcweir // value might be negative). Better rely on decent optimizer 82cdf0e10cSrcweir // here... 83cdf0e10cSrcweir return Color(((((sal_Int32)v2.getRed() - v1_red)*alpha) / 0x100) + v1_red, 84cdf0e10cSrcweir ((((sal_Int32)v2.getGreen() - v1_green)*alpha) / 0x100) + v1_green, 85cdf0e10cSrcweir ((((sal_Int32)v2.getBlue() - v1_blue)*alpha) / 0x100) + v1_blue); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir }; 88cdf0e10cSrcweir 89cdf0e10cSrcweir template< bool polarity > struct ColorBlendFunctor32 90cdf0e10cSrcweir : public TernaryFunctorBase<Color,Color,Color,Color> 91cdf0e10cSrcweir { operator ()basebmp::ColorBlendFunctor3292cdf0e10cSrcweir Color operator()( Color input, 93cdf0e10cSrcweir Color v1, 94cdf0e10cSrcweir Color v2 ) const 95cdf0e10cSrcweir { 96cdf0e10cSrcweir sal_uInt8 alpha = input.getGreyscale(); 97cdf0e10cSrcweir alpha = polarity ? alpha : 255 - alpha; 98cdf0e10cSrcweir 99cdf0e10cSrcweir const sal_uInt8 v1_red( v1.getRed() ); 100cdf0e10cSrcweir const sal_uInt8 v1_green( v1.getGreen() ); 101cdf0e10cSrcweir const sal_uInt8 v1_blue( v1.getBlue() ); 102cdf0e10cSrcweir 103cdf0e10cSrcweir // using '>> 8' instead of '/ 0x100' is ill-advised (shifted 104cdf0e10cSrcweir // value might be negative). Better rely on decent optimizer 105cdf0e10cSrcweir // here... 106cdf0e10cSrcweir return Color(((((sal_Int32)v2.getRed() - v1_red)*alpha) / 0x100) + v1_red, 107cdf0e10cSrcweir ((((sal_Int32)v2.getGreen() - v1_green)*alpha) / 0x100) + v1_green, 108cdf0e10cSrcweir ((((sal_Int32)v2.getBlue() - v1_blue)*alpha) / 0x100) + v1_blue); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir }; 111cdf0e10cSrcweir 112cdf0e10cSrcweir //----------------------------------------------------------------------------- 113cdf0e10cSrcweir 114cdf0e10cSrcweir template<> struct ColorTraits< Color > 115cdf0e10cSrcweir { 116cdf0e10cSrcweir /// @return number of color channels numChannelsbasebmp::ColorTraits117cdf0e10cSrcweir static int numChannels() { return 3; } 118cdf0e10cSrcweir 119cdf0e10cSrcweir /// Type of a color component (i.e. the type of an individual channel) 120cdf0e10cSrcweir typedef sal_uInt8 component_type; 121cdf0e10cSrcweir 122cdf0e10cSrcweir /// Metafunction to select blend functor from color and alpha type 123cdf0e10cSrcweir template< typename AlphaType, bool polarity > struct blend_functor; 124cdf0e10cSrcweir 125cdf0e10cSrcweir /// Calculate normalized distance between color c1 and c2 distancebasebmp::ColorTraits126cdf0e10cSrcweir static inline double distance( const Color& c1, 127cdf0e10cSrcweir const Color& c2 ) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir return (c1 - c2).magnitude(); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir toGreyscalebasebmp::ColorTraits132cdf0e10cSrcweir static inline component_type toGreyscale( const Color& c ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir return c.getGreyscale(); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir fromGreyscalebasebmp::ColorTraits137cdf0e10cSrcweir static inline Color fromGreyscale( component_type c ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir return Color(c,c,c); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir }; 142cdf0e10cSrcweir 143cdf0e10cSrcweir /// The version for plain 8 bit alpha 144cdf0e10cSrcweir template<bool polarity> struct ColorTraits< Color >::blend_functor< sal_uInt8, polarity > 145cdf0e10cSrcweir { 146cdf0e10cSrcweir typedef ColorBlendFunctor8<polarity> type; 147cdf0e10cSrcweir }; 148cdf0e10cSrcweir 149cdf0e10cSrcweir /// The version taking grey value of a Color 150cdf0e10cSrcweir template<bool polarity> struct ColorTraits< Color >::blend_functor< Color, polarity > 151cdf0e10cSrcweir { 152cdf0e10cSrcweir typedef ColorBlendFunctor32<polarity> type; 153cdf0e10cSrcweir }; 154cdf0e10cSrcweir 155cdf0e10cSrcweir } // namespace basebmp 156cdf0e10cSrcweir 157cdf0e10cSrcweir namespace vigra 158cdf0e10cSrcweir { 159cdf0e10cSrcweir 160cdf0e10cSrcweir template<> 161cdf0e10cSrcweir struct NumericTraits<basebmp::Color> 162cdf0e10cSrcweir { 163cdf0e10cSrcweir typedef basebmp::Color Type; 164cdf0e10cSrcweir typedef basebmp::Color Promote; 165cdf0e10cSrcweir typedef basebmp::Color RealPromote; 166cdf0e10cSrcweir typedef std::complex<basebmp::Color> ComplexPromote; 167cdf0e10cSrcweir typedef sal_uInt8 ValueType; 168cdf0e10cSrcweir 169cdf0e10cSrcweir typedef VigraTrueType isIntegral; 170cdf0e10cSrcweir typedef VigraFalseType isScalar; 171cdf0e10cSrcweir typedef VigraTrueType isSigned; 172cdf0e10cSrcweir typedef VigraTrueType isOrdered; 173cdf0e10cSrcweir typedef VigraFalseType isComplex; 174cdf0e10cSrcweir zerovigra::NumericTraits175cdf0e10cSrcweir static Type zero() { return Type(); } onevigra::NumericTraits176cdf0e10cSrcweir static Type one() { return Type(0x01010101); } nonZerovigra::NumericTraits177cdf0e10cSrcweir static Type nonZero() { return Type(0x01010101); } 178cdf0e10cSrcweir toPromotevigra::NumericTraits179cdf0e10cSrcweir static Promote toPromote(const Type& v) { return v; } toRealPromotevigra::NumericTraits180cdf0e10cSrcweir static RealPromote toRealPromote(const Type& v) { return v; } fromPromotevigra::NumericTraits181cdf0e10cSrcweir static Type fromPromote(const Promote& v) { return v; } fromRealPromotevigra::NumericTraits182cdf0e10cSrcweir static Type fromRealPromote(const RealPromote& v) { return v; } 183cdf0e10cSrcweir }; 184cdf0e10cSrcweir 185cdf0e10cSrcweir } // namespace vigra 186cdf0e10cSrcweir 187cdf0e10cSrcweir #endif /* INCLUDED_BASEBMP_COLORMISC_HXX */ 188