2 Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 http://www.imagemagick.org/script/license.php
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 MagickCore image composite private methods.
18 #ifndef _MAGICKCORE_COMPOSITE_PRIVATE_H
19 #define _MAGICKCORE_COMPOSITE_PRIVATE_H
21 #if defined(__cplusplus) || defined(c_plusplus)
26 ImageMagick Alpha Composite Inline Methods (special export)
29 #include "magick/color.h"
30 #include "magick/image.h"
31 #include "magick/image-private.h"
33 static inline MagickRealType RoundToUnity(const MagickRealType value)
35 return(value < 0.0 ? 0.0 : (value > 1.0) ? 1.0 : value);
38 static inline MagickRealType MagickOver_(const MagickRealType p,
39 const MagickRealType alpha,const MagickRealType q,const MagickRealType beta)
41 return((1.0-QuantumScale*alpha)*p+
42 (1.0-QuantumScale*beta)*q*QuantumScale*alpha);
45 static inline void MagickCompositeOver(const PixelPacket *p,
46 const MagickRealType alpha,const PixelPacket *q,const MagickRealType beta,
47 PixelPacket *composite)
53 Compose pixel p over pixel q with the given opacities.
55 if (alpha == TransparentOpacity)
61 gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
62 #if !defined(MAGICKCORE_HDRI_SUPPORT)
63 composite->opacity=(Quantum) (QuantumRange*(1.0-gamma)+0.5);
64 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
65 composite->red=(Quantum) (gamma*MagickOver_((MagickRealType) p->red,alpha,
66 (MagickRealType) q->red,beta)+0.5);
67 composite->green=(Quantum) (gamma*MagickOver_((MagickRealType) p->green,alpha,
68 (MagickRealType) q->green,beta)+0.5);
69 composite->blue=(Quantum) (gamma*MagickOver_((MagickRealType) p->blue,alpha,
70 (MagickRealType) q->blue,beta)+0.5);
72 composite->opacity=(Quantum) (QuantumRange*(1.0-gamma));
73 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
74 composite->red=(Quantum) (gamma*MagickOver_((MagickRealType) p->red,alpha,
75 (MagickRealType) q->red,beta));
76 composite->green=(Quantum) (gamma*MagickOver_((MagickRealType) p->green,alpha,
77 (MagickRealType) q->green,beta));
78 composite->blue=(Quantum) (gamma*MagickOver_((MagickRealType) p->blue,alpha,
79 (MagickRealType) q->blue,beta));
83 static inline void MagickPixelCompositeOver(const MagickPixelPacket *p,
84 const MagickRealType alpha,const MagickPixelPacket *q,
85 const MagickRealType beta,MagickPixelPacket *composite)
91 Compose pixel p over pixel q with the given opacities.
93 if (alpha == OpaqueOpacity)
98 gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
99 composite->opacity=(MagickRealType) QuantumRange*(1.0-gamma);
100 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
101 composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
102 composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
103 composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
104 if (q->colorspace == CMYKColorspace)
105 composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
108 static inline void MagickPixelCompositePlus(const MagickPixelPacket *p,
109 const MagickRealType alpha,const MagickPixelPacket *q,
110 const MagickRealType beta,MagickPixelPacket *composite)
118 Add two pixels with the given opacities.
120 Sa=1.0-QuantumScale*alpha;
121 Da=1.0-QuantumScale*beta;
122 gamma=RoundToUnity(Sa+Da); /* 'Plus' blending -- not 'Over' blending */
123 composite->opacity=(MagickRealType) QuantumRange*(1.0-gamma);
124 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
125 composite->red=gamma*(Sa*p->red+Da*q->red);
126 composite->green=gamma*(Sa*p->green+Da*q->green);
127 composite->blue=gamma*(Sa*p->blue+Da*q->blue);
128 if (q->colorspace == CMYKColorspace)
129 composite->index=gamma*(Sa*p->index+Da*q->index);
133 Blend pixel colors p and q by the amount given.
135 static inline void MagickPixelCompositeBlend(const MagickPixelPacket *p,
136 const MagickRealType alpha,const MagickPixelPacket *q,
137 const MagickRealType beta,MagickPixelPacket *composite)
139 MagickPixelCompositePlus(p,(MagickRealType) (QuantumRange-alpha*
140 (QuantumRange-p->opacity)),q,(MagickRealType) (QuantumRange-beta*
141 GetAlphaPixelComponent(q)),composite);
145 Blend pixel colors p and q by the amount given and area.
147 static inline void MagickPixelCompositeAreaBlend(const MagickPixelPacket *p,
148 const MagickRealType alpha,const MagickPixelPacket *q,
149 const MagickRealType beta,const MagickRealType area,
150 MagickPixelPacket *composite)
152 MagickPixelCompositePlus(p,(MagickRealType) QuantumRange-(1.0-area)*
153 (QuantumRange-alpha),q,(MagickRealType) (QuantumRange-area*(QuantumRange-
157 #if defined(__cplusplus) || defined(c_plusplus)