]> granicus.if.org Git - imagemagick/blob - magick/composite-private.h
(no commit message)
[imagemagick] / magick / 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 "magick/color.h"
30 #include "magick/image.h"
31 #include "magick/image-private.h"
32
33 static inline MagickRealType RoundToUnity(const MagickRealType value)
34 {
35   return(value < 0.0 ? 0.0 : (value > 1.0) ? 1.0 : value);
36 }
37
38 static inline MagickRealType MagickOver_(const MagickRealType p,
39   const MagickRealType alpha,const MagickRealType q,const MagickRealType beta)
40 {
41   return((1.0-QuantumScale*alpha)*p+(1.0-QuantumScale*beta)*q*
42     QuantumScale*alpha);
43 }
44
45 static inline void MagickCompositeOver(const PixelPacket *p,
46   const MagickRealType alpha,const PixelPacket *q,const MagickRealType beta,
47   PixelPacket *composite)
48 {
49   MagickRealType
50     gamma;
51
52   /*
53     Compose pixel p over pixel q with the given opacities.
54   */
55   if (alpha == TransparentOpacity)
56     {
57       if (composite != q)
58         *composite=(*q);
59       return;
60     }
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   SetRedPixelComponent(composite,gamma*MagickOver_((MagickRealType)
66     GetRedPixelComponent(p),alpha,(MagickRealType)
67     GetRedPixelComponent(q),beta)+0.5);
68   SetGreenPixelComponent(composite,gamma*MagickOver_((MagickRealType)
69     GetGreenPixelComponent(p),alpha,(MagickRealType)
70     GetGreenPixelComponent(q),beta)+0.5);
71   SetBluePixelComponent(composite,gamma*MagickOver_((MagickRealType)
72     GetBluePixelComponent(p),alpha,(MagickRealType)
73     GetBluePixelComponent(q),beta)+0.5);
74 #else
75   SetOpacityPixelComponent(composite,QuantumRange*(1.0-gamma));
76   gamma=1.0/(gamma <= MagickEpsilon ? 1.0 : gamma);
77   SetRedPixelComponent(composite,gamma*MagickOver_((MagickRealType)
78     GetRedPixelComponent(p),alpha,(MagickRealType)
79     GetRedPixelComponent(q),beta));
80   SetGreenPixelComponent(composite,gamma*MagickOver_((MagickRealType)
81     GetGreenPixelComponent(p),alpha,(MagickRealType)
82     GetGreenPixelComponent(q),beta));
83   SetBluePixelComponent(composite,gamma*MagickOver_((MagickRealType)
84     GetBluePixelComponent(p),alpha,(MagickRealType)
85     GetBluePixelComponent(q),beta));
86 #endif
87 }
88
89 static inline void MagickPixelCompositeOver(const MagickPixelPacket *p,
90   const MagickRealType alpha,const MagickPixelPacket *q,
91   const MagickRealType beta,MagickPixelPacket *composite)
92 {
93   MagickRealType
94     gamma;
95
96   /*
97     Compose pixel p over pixel q with the given opacities.
98   */
99   if (alpha == OpaqueOpacity)
100     {
101       *composite=(*p);
102       return;
103     }
104   gamma=1.0-QuantumScale*QuantumScale*alpha*beta;
105   composite->opacity=(MagickRealType) QuantumRange*(1.0-gamma);
106   gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
107   composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
108   composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
109   composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
110   if (q->colorspace == CMYKColorspace)
111     composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
112 }
113
114 static inline void MagickPixelCompositePlus(const MagickPixelPacket *p,
115   const MagickRealType alpha,const MagickPixelPacket *q,
116   const MagickRealType beta,MagickPixelPacket *composite)
117 {
118   MagickRealType
119     Da,
120     gamma,
121     Sa;
122
123   /*
124     Add two pixels with the given opacities.
125   */
126   Sa=1.0-QuantumScale*alpha;
127   Da=1.0-QuantumScale*beta;
128   gamma=RoundToUnity(Sa+Da);  /* 'Plus' blending -- not 'Over' blending */
129   composite->opacity=(MagickRealType) QuantumRange*(1.0-gamma);
130   gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
131   composite->red=gamma*(Sa*p->red+Da*q->red);
132   composite->green=gamma*(Sa*p->green+Da*q->green);
133   composite->blue=gamma*(Sa*p->blue+Da*q->blue);
134   if (q->colorspace == CMYKColorspace)
135     composite->index=gamma*(Sa*p->index+Da*q->index);
136 }
137
138 /*
139   Blend pixel colors p and q by the amount given.
140 */
141 static inline void MagickPixelCompositeBlend(const MagickPixelPacket *p,
142   const MagickRealType alpha,const MagickPixelPacket *q,
143   const MagickRealType beta,MagickPixelPacket *composite)
144 {
145   MagickPixelCompositePlus(p,(MagickRealType) (QuantumRange-alpha*
146     (QuantumRange-p->opacity)),q,(MagickRealType) (QuantumRange-beta*
147     (QuantumRange-q->opacity)),composite);
148 }
149
150 /*
151   Blend pixel colors p and q by the amount given and area.
152 */
153 static inline void MagickPixelCompositeAreaBlend(const MagickPixelPacket *p,
154   const MagickRealType alpha,const MagickPixelPacket *q,
155   const MagickRealType beta,const MagickRealType area,
156   MagickPixelPacket *composite)
157 {
158   MagickPixelCompositePlus(p,(MagickRealType) QuantumRange-(1.0-area)*
159     (QuantumRange-alpha),q,(MagickRealType) (QuantumRange-area*(QuantumRange-
160     beta)),composite);
161 }
162
163 #if defined(__cplusplus) || defined(c_plusplus)
164 }
165 #endif
166
167 #endif