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 "MagickCore/color.h"
30 #include "MagickCore/image.h"
31 #include "MagickCore/image-private.h"
32 #include "MagickCore/pixel-accessor.h"
34 static inline MagickRealType MagickOver_(const MagickRealType p,
35 const MagickRealType alpha,const MagickRealType q,const MagickRealType beta)
41 Sa=QuantumScale*alpha;
43 return(Sa*p-Sa*Da*q+Da*q);
46 static inline void CompositePixelOver(const Image *image,const PixelPacket *p,
47 const MagickRealType alpha,const Quantum *q,const MagickRealType beta,
56 Compose pixel p over pixel q with the given opacities.
58 if (fabs(alpha-TransparentAlpha) < MagickEpsilon)
62 SetPixelRed(image,GetPixelRed(image,q),composite);
63 SetPixelGreen(image,GetPixelGreen(image,q),composite);
64 SetPixelBlue(image,GetPixelBlue(image,q),composite);
65 SetPixelAlpha(image,GetPixelAlpha(image,q),composite);
69 Sa=QuantumScale*alpha;
72 #if !defined(MAGICKCORE_HDRI_SUPPORT)
73 SetPixelAlpha(image,(Quantum) (QuantumRange*gamma+0.5),composite);
74 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
75 SetPixelRed(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->red,
76 alpha,(MagickRealType) GetPixelRed(image,q),beta)+0.5),composite);
77 SetPixelGreen(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->green,
78 alpha,(MagickRealType) GetPixelGreen(image,q),beta)+0.5),composite);
79 SetPixelBlue(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->blue,
80 alpha,(MagickRealType) GetPixelBlue(image,q),beta)+0.5),composite);
82 SetPixelAlpha(image,QuantumRange*gamma,composite);
83 gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
84 SetPixelRed(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->red,
85 alpha,(MagickRealType) GetPixelRed(image,q),beta)),composite);
86 SetPixelGreen(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->green,
87 alpha,(MagickRealType) GetPixelGreen(image,q),beta)),composite);
88 SetPixelBlue(image,(Quantum) (gamma*MagickOver_((MagickRealType) p->blue,
89 alpha,(MagickRealType) GetPixelBlue(image,q),beta)),composite);
93 static inline void CompositePixelInfoOver(const PixelInfo *p,
94 const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
103 Compose pixel p over pixel q with the given opacities.
105 if (fabs(alpha-OpaqueAlpha) < MagickEpsilon)
110 Sa=QuantumScale*alpha;
111 Da=QuantumScale*beta,
112 gamma=Sa*(-Da)+Sa+Da;
113 composite->alpha=(MagickRealType) QuantumRange*gamma;
114 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
115 composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
116 composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
117 composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
118 if (q->colorspace == CMYKColorspace)
119 composite->black=gamma*MagickOver_(p->black,alpha,q->black,beta);
122 static inline MagickRealType RoundToUnity(const MagickRealType value)
124 return(value < 0.0 ? 0.0 : (value > 1.0) ? 1.0 : value);
127 static inline void CompositePixelInfoPlus(const PixelInfo *p,
128 const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
129 PixelInfo *composite)
137 Add two pixels with the given opacities.
139 Sa=QuantumScale*alpha;
140 Da=QuantumScale*beta;
141 gamma=RoundToUnity(Sa+Da); /* 'Plus' blending -- not 'Over' blending */
142 composite->alpha=(MagickRealType) QuantumRange*gamma;
143 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
144 composite->red=gamma*(Sa*p->red+Da*q->red);
145 composite->green=gamma*(Sa*p->green+Da*q->green);
146 composite->blue=gamma*(Sa*p->blue+Da*q->blue);
147 if (q->colorspace == CMYKColorspace)
148 composite->black=gamma*(Sa*p->black+Da*q->black);
151 static inline void CompositePixelInfoAreaBlend(const PixelInfo *p,
152 const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
153 const MagickRealType area,PixelInfo *composite)
156 Blend pixel colors p and q by the amount given and area.
158 CompositePixelInfoPlus(p,(MagickRealType) (1.0-area)*alpha,q,(MagickRealType)
159 (area*beta),composite);
162 static inline void CompositePixelInfoBlend(const PixelInfo *p,
163 const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
164 PixelInfo *composite)
167 Blend pixel colors p and q by the amount given.
169 CompositePixelInfoPlus(p,(MagickRealType) (alpha*p->alpha),q,(MagickRealType)
170 (beta*q->alpha),composite);
173 #if defined(__cplusplus) || defined(c_plusplus)