]> granicus.if.org Git - imagemagick/blob - MagickCore/composite-private.h
(no commit message)
[imagemagick] / MagickCore / composite-private.h
1 /*
2   Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization
3   dedicated to making software imaging solutions freely available.
4
5   You may not use this file except in compliance with the License.
6   obtain a copy of the License at
7
8     http://www.imagemagick.org/script/license.php
9
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.
15
16   MagickCore image composite private methods.
17 */
18 #ifndef _MAGICKCORE_COMPOSITE_PRIVATE_H
19 #define _MAGICKCORE_COMPOSITE_PRIVATE_H
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 /*
26   ImageMagick Alpha Composite Inline Methods (special export)
27 */
28
29 #include "MagickCore/color.h"
30 #include "MagickCore/image.h"
31 #include "MagickCore/image-private.h"
32 #include "MagickCore/pixel-accessor.h"
33
34 static inline MagickRealType MagickOver_(const MagickRealType p,
35   const MagickRealType alpha,const MagickRealType q,const MagickRealType beta)
36 {
37   MagickRealType
38     Da,
39     Sa;
40
41   Sa=QuantumScale*alpha;
42   Da=QuantumScale*beta;
43   return(Sa*p-Sa*Da*q+Da*q);
44 }
45
46 static inline void CompositePixelOver(const Image *image,const PixelInfo *p,
47   const MagickRealType alpha,const Quantum *q,const MagickRealType beta,
48   Quantum *composite)
49 {
50   MagickRealType
51     Da,
52     gamma,
53     Sa;
54
55   /*
56     Compose pixel p over pixel q with the given opacities.
57   */
58   if (fabs(alpha-TransparentAlpha) < MagickEpsilon)
59     {
60       if (composite != q)
61         {
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);
66         }
67       return;
68     }
69   Sa=QuantumScale*alpha;
70   Da=QuantumScale*beta,
71   gamma=Sa*(-Da)+Sa+Da;
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);
81 #else
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);
90 #endif
91 }
92
93 static inline void CompositePixelInfoOver(const PixelInfo *p,
94   const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
95   PixelInfo *composite)
96 {
97   MagickRealType
98     Da,
99     gamma,
100     Sa;
101
102   /*
103     Compose pixel p over pixel q with the given opacities.
104   */
105   if (fabs(alpha-OpaqueAlpha) < MagickEpsilon)
106     {
107       *composite=(*p);
108       return;
109     }
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);
120 }
121
122 static inline MagickRealType RoundToUnity(const MagickRealType value)
123 {
124   return(value < 0.0 ? 0.0 : (value > 1.0) ? 1.0 : value);
125 }
126
127 static inline void CompositePixelInfoPlus(const PixelInfo *p,
128   const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
129   PixelInfo *composite)
130 {
131   MagickRealType
132     Da,
133     gamma,
134     Sa;
135
136   /*
137     Add two pixels with the given opacities.
138   */
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);
149 }
150
151 static inline void CompositePixelInfoAreaBlend(const PixelInfo *p,
152   const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
153   const MagickRealType area,PixelInfo *composite)
154 {
155   /*
156     Blend pixel colors p and q by the amount given and area.
157   */
158   CompositePixelInfoPlus(p,(MagickRealType) (1.0-area)*alpha,q,(MagickRealType)
159     (area*beta),composite);
160 }
161
162 static inline void CompositePixelInfoBlend(const PixelInfo *p,
163   const MagickRealType alpha,const PixelInfo *q,const MagickRealType beta,
164   PixelInfo *composite)
165 {
166   /*
167     Blend pixel colors p and q by the amount given.
168   */
169   CompositePixelInfoPlus(p,(MagickRealType) (alpha*p->alpha),q,(MagickRealType)
170     (beta*q->alpha),composite);
171 }
172
173 #if defined(__cplusplus) || defined(c_plusplus)
174 }
175 #endif
176
177 #endif