1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASEBMP_COLORTRAITS_HXX
21 #define INCLUDED_BASEBMP_COLORTRAITS_HXX
23 #include <basebmp/accessoradapters.hxx>
24 #include <basebmp/metafunctions.hxx>
26 #include <vigra/mathutil.hxx>
31 /** Functor template, to calculate alpha blending between two
35 When true, 0 means fully transparent, and 1 fully opaque. And vice
38 template< typename ValueType
,
40 bool polarity
> struct BlendFunctor
;
41 template< typename ValueType
,
42 typename AlphaType
> struct BlendFunctor
<ValueType
,AlphaType
,true>
43 : public TernaryFunctorBase
<AlphaType
,ValueType
,ValueType
,ValueType
>
45 ValueType
operator()( AlphaType alpha
,
49 const typename
vigra::NumericTraits
<AlphaType
>::RealPromote
fAlpha(
50 vigra::NumericTraits
<AlphaType
>::toRealPromote(alpha
));
51 return (vigra::NumericTraits
<AlphaType
>::one()-fAlpha
)*v1
+ fAlpha
*v2
;
54 template< typename ValueType
,
55 typename AlphaType
> struct BlendFunctor
<ValueType
,AlphaType
,false>
56 : public TernaryFunctorBase
<AlphaType
,ValueType
,ValueType
,ValueType
>
58 ValueType
operator()( AlphaType alpha
,
62 const typename
vigra::NumericTraits
<AlphaType
>::RealPromote
fAlpha(
63 vigra::NumericTraits
<AlphaType
>::toRealPromote(alpha
));
64 return fAlpha
*v1
+ (vigra::NumericTraits
<AlphaType
>::one()-fAlpha
)*v2
;
68 /** Functor template, to calculate alpha blending between two
72 When true, 0 means fully transparent, and 1 fully opaque. And vice
75 template< typename ValueType
,
77 bool polarity
> struct IntegerBlendFunctor
;
78 template< typename ValueType
,
79 typename AlphaType
> struct IntegerBlendFunctor
<ValueType
,AlphaType
,true>
80 : public TernaryFunctorBase
<AlphaType
,ValueType
,ValueType
,ValueType
>
82 ValueType
operator()( AlphaType alpha
,
86 return (vigra::NumericTraits
<AlphaType
>::toPromote(
87 vigra::NumericTraits
<AlphaType
>::max()-alpha
)*v1
+ alpha
*v2
) /
88 vigra::NumericTraits
<AlphaType
>::max();
91 template< typename ValueType
,
92 typename AlphaType
> struct IntegerBlendFunctor
<ValueType
,AlphaType
,false>
93 : public TernaryFunctorBase
<AlphaType
,ValueType
,ValueType
,ValueType
>
95 ValueType
operator()( AlphaType alpha
,
100 vigra::NumericTraits
<AlphaType
>::toPromote(
101 vigra::NumericTraits
<AlphaType
>::max()-alpha
)*v2
) /
102 vigra::NumericTraits
<AlphaType
>::max();
106 //-----------------------------------------------------------------------------
108 template< typename ColorType
> struct ColorTraits
110 /// Metafunction to select blend functor from color and alpha type
111 template< typename AlphaType
, bool polarity
> struct blend_functor
: public
112 ifScalarIntegral
< AlphaType
,
113 IntegerBlendFunctor
< ColorType
, AlphaType
, polarity
>,
114 BlendFunctor
< ColorType
, AlphaType
, polarity
> > {};
116 /// @return number of color channels
117 static int numChannels() { return 1; }
119 /// Type of a color component (i.e. the type of an individual channel)
120 typedef ColorType component_type
;
122 /// Calculate normalized distance between color c1 and c2
123 static inline vigra::NormTraits
<ColorType
> distance( ColorType c1
,
126 return vigra::norm(c1
- c2
);
129 static inline component_type
toGreyscale( ColorType c
)
134 static inline ColorType
fromGreyscale( component_type c
)
140 } // namespace basebmp
142 #endif /* INCLUDED_BASEBMP_COLORTRAITS_HXX */
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */