diff options
author | Thorsten Behrens <thb@openoffice.org> | 2006-07-13 11:03:26 +0000 |
---|---|---|
committer | Thorsten Behrens <thb@openoffice.org> | 2006-07-13 11:03:26 +0000 |
commit | 0177db3f91a28995ca151b63d054cd539cd27212 (patch) | |
tree | 9df894e22693545534b5941067f821610eb40de2 /basebmp/inc/basebmp | |
parent | 3583a216e8101fe4d0f17f913c2efcab58f9b518 (diff) |
#i65904# Swapped mask polarity - now, a zero in the mask denotes opacity; minor code tidying; made drawMaskedBitmap() handle a few more generic cases; switched 24bpp to BGR; adapted tests to modified mask polarity
Diffstat (limited to 'basebmp/inc/basebmp')
-rw-r--r-- | basebmp/inc/basebmp/accessorfunctors.hxx | 66 | ||||
-rw-r--r-- | basebmp/inc/basebmp/accessortraits.hxx | 29 | ||||
-rw-r--r-- | basebmp/inc/basebmp/bitmapdevice.hxx | 24 | ||||
-rw-r--r-- | basebmp/inc/basebmp/colorblendaccessoradapter.hxx | 17 | ||||
-rw-r--r-- | basebmp/inc/basebmp/colormisc.hxx | 31 | ||||
-rw-r--r-- | basebmp/inc/basebmp/colortraits.hxx | 76 | ||||
-rw-r--r-- | basebmp/inc/basebmp/metafunctions.hxx | 15 | ||||
-rw-r--r-- | basebmp/inc/basebmp/packedpixeliterator.hxx | 9 |
8 files changed, 187 insertions, 80 deletions
diff --git a/basebmp/inc/basebmp/accessorfunctors.hxx b/basebmp/inc/basebmp/accessorfunctors.hxx index 1d4645fba865..89a30772459c 100644 --- a/basebmp/inc/basebmp/accessorfunctors.hxx +++ b/basebmp/inc/basebmp/accessorfunctors.hxx @@ -4,9 +4,9 @@ * * $RCSfile: accessorfunctors.hxx,v $ * - * $Revision: 1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: thb $ $Date: 2006-07-12 15:09:44 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -56,34 +56,37 @@ template< typename T > struct XorFunctor : public std::binary_function<T,T,T> //----------------------------------------------------------------------------- -/// Base class for an adaptable ternary functor -template< typename A1, typename A2, typename A3, typename R > struct TernaryFunctorBase -{ - typedef A1 first_argument_type; - typedef A2 second_argument_type; - typedef A3 third_argument_type; - typedef R result_type; -}; - /// Base class, passing on the arg types template< typename T, typename M > struct MaskFunctorBase : public TernaryFunctorBase<T,M,T,T> {}; -/// Let a mask flag decide between two values -template< typename T, typename M > struct GenericOutputMaskFunctor : MaskFunctorBase<T,M> +/** Let a mask flag decide between two values + + @tpl polarity + Mask polarity. When true, a false in the mask denotes + transparency, i.e. the original value will display. And vice + versa. + */ +template< typename T, + typename M, + bool polarity > struct GenericOutputMaskFunctor : public MaskFunctorBase<T,M> { - /// Ternary mask operation - selects v1 for !m == true, v2 otherwise + /// Ternary mask operation - selects v1 for !m == polarity, v2 otherwise T operator()( T v1, M m, T v2 ) const { - return !m ? v1 : v2; + return !m == polarity ? v1 : v2; } }; /** Let a mask bit decide between two values (specialization for integer mask types) */ -template< typename T, typename M > struct IntegerOutputMaskFunctor : MaskFunctorBase<T,M> +template< typename T, + typename M, + bool polarity > struct IntegerOutputMaskFunctor; +template< typename T, + typename M > struct IntegerOutputMaskFunctor<T,M,true> : public MaskFunctorBase<T,M> { /** Mask v with state of m @@ -98,11 +101,29 @@ template< typename T, typename M > struct IntegerOutputMaskFunctor : MaskFunctor return v1*(M)(1-mask) + v2*mask; } }; +template< typename T, + typename M > struct IntegerOutputMaskFunctor<T,M,false> : public MaskFunctorBase<T,M> +{ + /** Mask v with state of m + + @return v2, if m != 0, v1 otherwise. + */ + T operator()( T v1, M m, T v2 ) const + { + typedef typename make_unsigned<T>::type unsigned_T; + + // mask will be 0, iff m == 0, and 1 otherwise + const T mask( unsigned_cast<T>(m | -m) >> (sizeof(unsigned_T)*8 - 1) ); + return v1*mask + v2*(M)(1-mask); + } +}; /** Let a mask bit decide between two values (specialization for binary-valued mask types) */ -template< typename T, typename M > struct FastIntegerOutputMaskFunctor : MaskFunctorBase<T,M> +template< typename T, typename M, bool polarity > struct FastIntegerOutputMaskFunctor; +template< typename T, typename M > struct FastIntegerOutputMaskFunctor<T,M,true> : + public MaskFunctorBase<T,M> { /// Specialization, only valid if mask can only attain 0 or 1 T operator()( T v1, M m, T v2 ) const @@ -112,6 +133,17 @@ template< typename T, typename M > struct FastIntegerOutputMaskFunctor : MaskFun return v1*(M)(1-m) + v2*m; } }; +template< typename T, typename M > struct FastIntegerOutputMaskFunctor<T,M,false> : + public MaskFunctorBase<T,M> +{ + /// Specialization, only valid if mask can only attain 0 or 1 + T operator()( T v1, M m, T v2 ) const + { + OSL_ASSERT(m<=1); + + return v1*m + v2*(M)(1-m); + } +}; //----------------------------------------------------------------------------- diff --git a/basebmp/inc/basebmp/accessortraits.hxx b/basebmp/inc/basebmp/accessortraits.hxx index bb30cc310148..918824cb0e81 100644 --- a/basebmp/inc/basebmp/accessortraits.hxx +++ b/basebmp/inc/basebmp/accessortraits.hxx @@ -4,9 +4,9 @@ * * $RCSfile: accessortraits.hxx,v $ * - * $Revision: 1.3 $ + * $Revision: 1.4 $ * - * last change: $Author: thb $ $Date: 2006-07-12 15:09:44 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -49,16 +49,16 @@ struct FastMask; struct NoFastMask; /// Metafunction to select output mask functor from iterator and mask value type -template< typename T, typename M, typename DUMMY > struct outputMaskFunctorSelector : public +template< typename T, typename M, bool polarity, typename DUMMY > struct outputMaskFunctorSelector : public ifBothScalarIntegral< T, M, - IntegerOutputMaskFunctor< T, M >, - GenericOutputMaskFunctor< T, M > > + IntegerOutputMaskFunctor< T, M, polarity >, + GenericOutputMaskFunctor< T, M, polarity > > { }; -template< typename T, typename M > struct outputMaskFunctorSelector< T, M, FastMask > : public +template< typename T, typename M, bool polarity > struct outputMaskFunctorSelector< T, M, polarity, FastMask > : public ifBothScalarIntegral< T, M, - FastIntegerOutputMaskFunctor< T, M >, - GenericOutputMaskFunctor< T, M > > + FastIntegerOutputMaskFunctor< T, M, polarity >, + GenericOutputMaskFunctor< T, M, polarity > > { }; @@ -71,7 +71,8 @@ template< typename T, typename M > struct outputMaskFunctorSelector< T, M, FastM template< class Accessor, class MaskAccessor, class Iterator, - class MaskIterator > struct maskedAccessorSelector + class MaskIterator, + bool polarity > struct maskedAccessorSelector { typedef TernarySetterFunctionAccessorAdapter< Accessor, @@ -79,6 +80,7 @@ template< class Accessor, typename outputMaskFunctorSelector< typename Accessor::value_type, typename MaskAccessor::value_type, + polarity, NoFastMask > ::type > type; }; @@ -120,8 +122,13 @@ template< class Accessor > struct AccessorTraits */ template< class MaskAccessor, class Iterator, - class MaskIterator > struct masked_accessor : - public maskedAccessorSelector< Accessor,MaskAccessor,Iterator,MaskIterator > + class MaskIterator, + bool polarity > struct masked_accessor : + public maskedAccessorSelector< Accessor, + MaskAccessor, + Iterator, + MaskIterator, + polarity > {}; }; diff --git a/basebmp/inc/basebmp/bitmapdevice.hxx b/basebmp/inc/basebmp/bitmapdevice.hxx index 37bb0e04dfd4..c11a62e5efda 100644 --- a/basebmp/inc/basebmp/bitmapdevice.hxx +++ b/basebmp/inc/basebmp/bitmapdevice.hxx @@ -2,9 +2,9 @@ * * $RCSfile: bitmapdevice.hxx,v $ * - * $Revision: 1.6 $ + * $Revision: 1.7 $ * - * last change: $Author: thb $ $Date: 2006-07-11 11:38:54 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -172,7 +172,7 @@ public: Draw mode to use when changing the pixel value @param rClip - Clip mask to use. If the clip mask is 0 at the given pixel + Clip mask to use. If the clip mask is 1 at the given pixel position, no change will take place. */ void setPixel( const basegfx::B2IPoint& rPt, @@ -233,7 +233,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void drawLine( const basegfx::B2IPoint& rPt1, const basegfx::B2IPoint& rPt2, @@ -273,7 +273,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void drawPolygon( const basegfx::B2DPolygon& rPoly, Color lineColor, @@ -322,7 +322,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void fillPolyPolygon( const basegfx::B2DPolyPolygon& rPoly, Color fillColor, @@ -384,7 +384,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void drawBitmap( const BitmapDeviceSharedPtr& rSrcBitmap, const basegfx::B2IRange& rSrcRect, @@ -453,7 +453,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void drawMaskedColor( Color aSrcColor, const BitmapDeviceSharedPtr& rAlphaMask, @@ -474,7 +474,7 @@ public: and destination bitmap are the same. @param rMask - Bitmap to use as a mask. Pixel with value zero in this mask + Bitmap to use as a mask. Pixel with value != zero in this mask will result in destination pixel not being affected by the blit operation. @@ -507,7 +507,7 @@ public: This method renders a source bitmap into this device, much like the drawBitmap() method. The only difference is the additional mask parameter, which operates much like an - additional clip mask: pixel with value zero in this mask + additional clip mask: pixel with value != zero in this mask result in destination pixel not being modified. @param rSrcBitmap @@ -515,7 +515,7 @@ public: and destination bitmap are the same. @param rMask - Bitmap to use as a mask. Pixel with value zero in this mask + Bitmap to use as a mask. Pixel with value != zero in this mask will result in destination pixel not being affected by the blit operation. @@ -539,7 +539,7 @@ public: @param rClip Clip mask to use. Pixel where the corresponding clip mask - pixel is 0 will not be modified. + pixel is 1 will not be modified. */ void drawMaskedBitmap( const BitmapDeviceSharedPtr& rSrcBitmap, const BitmapDeviceSharedPtr& rMask, diff --git a/basebmp/inc/basebmp/colorblendaccessoradapter.hxx b/basebmp/inc/basebmp/colorblendaccessoradapter.hxx index d86f9efc2876..e318e8579585 100644 --- a/basebmp/inc/basebmp/colorblendaccessoradapter.hxx +++ b/basebmp/inc/basebmp/colorblendaccessoradapter.hxx @@ -4,9 +4,9 @@ * * $RCSfile: colorblendaccessoradapter.hxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: thb $ $Date: 2006-07-12 15:09:44 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -51,8 +51,9 @@ namespace basebmp when blitting through a mask) - there really isn't no other sensible default behaviour for these methods. */ -template< class WrappedAccessor, - typename AlphaType > class ConstantColorBlendSetterAccessorAdapter +template< class WrappedAccessor, + typename AlphaType, + bool polarity > class ConstantColorBlendSetterAccessorAdapter { public: typedef AlphaType alpha_type; @@ -61,10 +62,10 @@ public: private: typename ColorTraits< color_type >:: - template blend_functor<alpha_type>::type maFunctor; - WrappedAccessor maWrappee; - color_type maBlendColor; - value_type maGetterValue; + template blend_functor<alpha_type,polarity>::type maFunctor; + WrappedAccessor maWrappee; + color_type maBlendColor; + value_type maGetterValue; public: ConstantColorBlendSetterAccessorAdapter() : diff --git a/basebmp/inc/basebmp/colormisc.hxx b/basebmp/inc/basebmp/colormisc.hxx index 8cdf665c6145..c4f1ec0126a4 100644 --- a/basebmp/inc/basebmp/colormisc.hxx +++ b/basebmp/inc/basebmp/colormisc.hxx @@ -4,9 +4,9 @@ * * $RCSfile: colormisc.hxx,v $ * - * $Revision: 1.3 $ + * $Revision: 1.4 $ * - * last change: $Author: thb $ $Date: 2006-07-11 11:38:55 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -50,7 +50,8 @@ namespace basebmp { -struct ColorBitmaskOutputMaskFunctor : MaskFunctorBase<Color,sal_uInt8> +template< bool polarity > struct ColorBitmaskOutputMaskFunctor; +template<> struct ColorBitmaskOutputMaskFunctor<true> : MaskFunctorBase<Color,sal_uInt8> { Color operator()( Color v1, sal_uInt8 m, Color v2 ) const { @@ -59,19 +60,31 @@ struct ColorBitmaskOutputMaskFunctor : MaskFunctorBase<Color,sal_uInt8> return Color(v1.toInt32()*(sal_uInt8)(1-m) + v2.toInt32()*m); } }; +template<> struct ColorBitmaskOutputMaskFunctor<false> : MaskFunctorBase<Color,sal_uInt8> +{ + Color operator()( Color v1, sal_uInt8 m, Color v2 ) const + { + OSL_ASSERT(m<=1); + + return Color(v1.toInt32()*m + v2.toInt32()*(sal_uInt8)(1-m)); + } +}; /// Specialized output mask functor for Color value type -template<> struct outputMaskFunctorSelector< Color, sal_uInt8, FastMask > +template<bool polarity> struct outputMaskFunctorSelector< Color, sal_uInt8, polarity, FastMask > { - typedef ColorBitmaskOutputMaskFunctor type; + typedef ColorBitmaskOutputMaskFunctor<polarity> type; }; -struct ColorBlendFunctor : public BlendFunctorBase<Color,sal_uInt8> +template< bool polarity > struct ColorBlendFunctor + : public TernaryFunctorBase<sal_uInt8,Color,Color,Color> { Color operator()( sal_uInt8 alpha, Color v1, Color v2 ) const { + alpha = polarity ? alpha : 255 - alpha; + const sal_uInt8 v1_red( v1.getRed() ); const sal_uInt8 v1_green( v1.getGreen() ); const sal_uInt8 v1_blue( v1.getBlue() ); @@ -96,7 +109,7 @@ template<> struct ColorTraits< Color > typedef sal_uInt8 component_type; /// Metafunction to select blend functor from color and alpha type - template< typename AlphaType > struct blend_functor; + template< typename AlphaType, bool polarity > struct blend_functor; /// Calculate normalized distance between color c1 and c2 static inline double distance( const Color& c1, @@ -117,9 +130,9 @@ template<> struct ColorTraits< Color > }; /// Only defined for 8 bit alpha, currently -template<> template<> struct ColorTraits< Color >::blend_functor< sal_uInt8 > +template<> template<bool polarity> struct ColorTraits< Color >::blend_functor< sal_uInt8, polarity > { - typedef ColorBlendFunctor type; + typedef ColorBlendFunctor<polarity> type; }; } // namespace basebmp diff --git a/basebmp/inc/basebmp/colortraits.hxx b/basebmp/inc/basebmp/colortraits.hxx index 53e2902c004b..0da8425d3a61 100644 --- a/basebmp/inc/basebmp/colortraits.hxx +++ b/basebmp/inc/basebmp/colortraits.hxx @@ -4,9 +4,9 @@ * * $RCSfile: colortraits.hxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: thb $ $Date: 2006-07-11 11:38:55 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -37,24 +37,39 @@ #define INCLUDED_BASEBMP_COLORTRAITS_HXX #include <basebmp/accessoradapters.hxx> -#include <basebmp/colortraits.hxx> +#include <basebmp/metafunctions.hxx> #include <vigra/mathutil.hxx> namespace basebmp { -template< typename ValueType, typename AlphaType > struct BlendFunctorBase +/** Functor template, to calculate alpha blending between two + values. Float case. + + @tpl polarity + When true, 0 means fully transparent, and 1 fully opaque. And vice + versa. + */ +template< typename ValueType, + typename AlphaType, + bool polarity > struct BlendFunctor; +template< typename ValueType, + typename AlphaType > struct BlendFunctor<ValueType,AlphaType,true> + : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> { - typedef AlphaType first_argument_type; - typedef ValueType second_argument_type; - typedef ValueType third_argument_type; - typedef ValueType result_type; + ValueType operator()( AlphaType alpha, + ValueType v1, + ValueType v2 ) const + { + const typename vigra::NumericTraits<AlphaType>::RealPromote fAlpha( + vigra::NumericTraits<AlphaType>::toRealPromote(alpha)); + return (vigra::NumericTraits<AlphaType>::one()-fAlpha)*v1 + fAlpha*v2; + } }; - -/// Functor template, to calculate alpha blending between two values. Float case. -template< typename ValueType, typename AlphaType > struct BlendFunctor : - public BlendFunctorBase<ValueType,AlphaType> +template< typename ValueType, + typename AlphaType > struct BlendFunctor<ValueType,AlphaType,false> + : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> { ValueType operator()( AlphaType alpha, ValueType v1, @@ -62,12 +77,23 @@ template< typename ValueType, typename AlphaType > struct BlendFunctor : { const typename vigra::NumericTraits<AlphaType>::RealPromote fAlpha( vigra::NumericTraits<AlphaType>::toRealPromote(alpha)); - return (vigra::NumericTraits<AlphaType>::one()-fAlpha)*v1 + fAlpha*v2; + return fAlpha*v1 + (vigra::NumericTraits<AlphaType>::one()-fAlpha)*v2; } }; -/// Functor template, to calculate alpha blending between two values. Integer case. -template< typename ValueType, typename AlphaType > struct IntegerBlendFunctor +/** Functor template, to calculate alpha blending between two + values. Integer case. + + @tpl polarity + When true, 0 means fully transparent, and 1 fully opaque. And vice + versa. + */ +template< typename ValueType, + typename AlphaType, + bool polarity > struct IntegerBlendFunctor; +template< typename ValueType, + typename AlphaType > struct IntegerBlendFunctor<ValueType,AlphaType,true> + : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> { ValueType operator()( AlphaType alpha, ValueType v1, @@ -78,16 +104,30 @@ template< typename ValueType, typename AlphaType > struct IntegerBlendFunctor vigra::NumericTraits<AlphaType>::max(); } }; +template< typename ValueType, + typename AlphaType > struct IntegerBlendFunctor<ValueType,AlphaType,false> + : public TernaryFunctorBase<AlphaType,ValueType,ValueType,ValueType> +{ + ValueType operator()( AlphaType alpha, + ValueType v1, + ValueType v2 ) const + { + return (alpha*v1 + + vigra::NumericTraits<AlphaType>::toPromote( + vigra::NumericTraits<AlphaType>::max()-alpha)*v2) / + vigra::NumericTraits<AlphaType>::max(); + } +}; //----------------------------------------------------------------------------- template< typename ColorType > struct ColorTraits { /// Metafunction to select blend functor from color and alpha type - template< typename AlphaType > struct blend_functor : public + template< typename AlphaType, bool polarity > struct blend_functor : public ifScalarIntegral< AlphaType, - IntegerBlendFunctor< ColorType, AlphaType >, - BlendFunctor< ColorType, AlphaType > > {}; + IntegerBlendFunctor< ColorType, AlphaType, polarity >, + BlendFunctor< ColorType, AlphaType, polarity > > {}; /// @return number of color channels static int numChannels() { return 1; } diff --git a/basebmp/inc/basebmp/metafunctions.hxx b/basebmp/inc/basebmp/metafunctions.hxx index 8004906174f1..8d64fc066543 100644 --- a/basebmp/inc/basebmp/metafunctions.hxx +++ b/basebmp/inc/basebmp/metafunctions.hxx @@ -4,9 +4,9 @@ * * $RCSfile: metafunctions.hxx,v $ * - * $Revision: 1.6 $ + * $Revision: 1.7 $ * - * last change: $Author: thb $ $Date: 2006-07-11 11:38:55 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -81,6 +81,17 @@ template <typename T> struct remove_const<const T> //-------------------------------------------------------------- +/// Base class for an adaptable ternary functor +template< typename A1, typename A2, typename A3, typename R > struct TernaryFunctorBase +{ + typedef A1 first_argument_type; + typedef A2 second_argument_type; + typedef A3 third_argument_type; + typedef R result_type; +}; + +//-------------------------------------------------------------- + /** template meta function: ensure that given integer type is unsigned If given integer type is already unsigned, return as-is - diff --git a/basebmp/inc/basebmp/packedpixeliterator.hxx b/basebmp/inc/basebmp/packedpixeliterator.hxx index 91a048d97ac2..9ee27130fe5f 100644 --- a/basebmp/inc/basebmp/packedpixeliterator.hxx +++ b/basebmp/inc/basebmp/packedpixeliterator.hxx @@ -4,9 +4,9 @@ * * $RCSfile: packedpixeliterator.hxx,v $ * - * $Revision: 1.8 $ + * $Revision: 1.9 $ * - * last change: $Author: thb $ $Date: 2006-07-12 22:47:20 $ + * last change: $Author: thb $ $Date: 2006-07-13 12:03:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -660,12 +660,14 @@ public: template< class Accessor, class MaskAccessor, class Iterator, + bool polarity, bool MsbFirst > struct maskedAccessorSelector< Accessor, MaskAccessor, Iterator, PackedPixelIterator< typename MaskAccessor::value_type, 1, - MsbFirst > > + MsbFirst >, + polarity > { typedef TernarySetterFunctionAccessorAdapter< Accessor, @@ -673,6 +675,7 @@ template< class Accessor, typename outputMaskFunctorSelector< typename Accessor::value_type, typename MaskAccessor::value_type, + polarity, FastMask>::type > type; }; |