MagickCore  6.9.12-76
Convert, Edit, Or Compose Bitmap Images
composite-private.h
1 /*
2  Copyright 1999-2021 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. You may
6  obtain a copy of the License at
7 
8  https://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 #include "magick/artifact.h"
22 #include "magick/color.h"
23 #include "magick/image.h"
24 #include "magick/image-private.h"
25 #include "magick/pixel-private.h"
26 
27 #if defined(__cplusplus) || defined(c_plusplus)
28 extern "C" {
29 #endif
30 
31 /*
32  ImageMagick Alpha Composite Inline Methods (special export)
33 */
34 static inline MagickRealType RoundToUnity(const MagickRealType value)
35 {
36  return(value < 0.0 ? 0.0 : (value > 1.0) ? 1.0 : value);
37 }
38 
39 static inline MagickRealType MagickOver_(const MagickRealType p,
40  const MagickRealType alpha,const MagickRealType q,const MagickRealType beta)
41 {
42  MagickRealType
43  Da,
44  Sa;
45 
46  Sa=1.0-QuantumScale*alpha;
47  Da=1.0-QuantumScale*beta;
48  return(Sa*p+Da*q*(1.0-Sa));
49 }
50 
51 static inline void MagickCompositeOver(const PixelPacket *p,
52  const MagickRealType alpha,const PixelPacket *q,const MagickRealType beta,
53  PixelPacket *composite)
54 {
55  MagickRealType
56  Da,
57  gamma,
58  Sa;
59 
60  /*
61  Compose pixel p over pixel q with the given opacities.
62  */
63  Sa=1.0-QuantumScale*alpha;
64  Da=1.0-QuantumScale*beta;
65  gamma=Sa+Da-Sa*Da;
66 #if !defined(MAGICKCORE_HDRI_SUPPORT)
67  SetPixelOpacity(composite,ClampToQuantum(QuantumRange*(1.0-
68  RoundToUnity(gamma))));
69  gamma=PerceptibleReciprocal(gamma);
70  SetPixelRed(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
71  GetPixelRed(p),alpha,(MagickRealType) GetPixelRed(q),beta)));
72  SetPixelGreen(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
73  GetPixelGreen(p),alpha,(MagickRealType) GetPixelGreen(q),beta)));
74  SetPixelBlue(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
75  GetPixelBlue(p),alpha,(MagickRealType) GetPixelBlue(q),beta)));
76 #else
77  SetPixelOpacity(composite,QuantumRange*(1.0-RoundToUnity(gamma)));
78  gamma=PerceptibleReciprocal(gamma);
79  SetPixelRed(composite,gamma*MagickOver_((MagickRealType)
80  GetPixelRed(p),alpha,(MagickRealType) GetPixelRed(q),beta));
81  SetPixelGreen(composite,gamma*MagickOver_((MagickRealType)
82  GetPixelGreen(p),alpha,(MagickRealType) GetPixelGreen(q),beta));
83  SetPixelBlue(composite,gamma*MagickOver_((MagickRealType)
84  GetPixelBlue(p),alpha,(MagickRealType) GetPixelBlue(q),beta));
85 #endif
86 }
87 
88 static inline void MagickPixelCompositeOver(const MagickPixelPacket *p,
89  const MagickRealType alpha,const MagickPixelPacket *q,
90  const MagickRealType beta,MagickPixelPacket *composite)
91 {
92  MagickRealType
93  Da,
94  gamma,
95  Sa;
96 
97  /*
98  Compose pixel p over pixel q with the given opacities.
99  */
100  Sa=1.0-QuantumScale*alpha;
101  Da=1.0-QuantumScale*beta;
102  gamma=Sa+Da-Sa*Da;
103  composite->opacity=(MagickRealType) (QuantumRange*(1.0-RoundToUnity(gamma)));
104  gamma=PerceptibleReciprocal(gamma);
105  composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
106  composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
107  composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
108  if (q->colorspace == CMYKColorspace)
109  composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
110 }
111 
112 static inline void MagickPixelCompositePlus(const MagickPixelPacket *p,
113  const MagickRealType alpha,const MagickPixelPacket *q,
114  const MagickRealType beta,MagickPixelPacket *composite)
115 {
116  MagickRealType
117  Da,
118  gamma,
119  Sa;
120 
121  /*
122  Add two pixels with the given opacities.
123  */
124  Sa=1.0-QuantumScale*alpha;
125  Da=1.0-QuantumScale*beta;
126  gamma=RoundToUnity(Sa+Da); /* 'Plus' blending -- not 'Over' blending */
127  composite->opacity=(MagickRealType) QuantumRange*(1.0-RoundToUnity(gamma));
128  gamma=PerceptibleReciprocal(gamma);
129  composite->red=gamma*(Sa*p->red+Da*q->red);
130  composite->green=gamma*(Sa*p->green+Da*q->green);
131  composite->blue=gamma*(Sa*p->blue+Da*q->blue);
132  if (q->colorspace == CMYKColorspace)
133  composite->index=gamma*(Sa*p->index+Da*q->index);
134 }
135 
136 /*
137  Blend pixel colors p and q by the amount given.
138 */
139 static inline void MagickPixelCompositeBlend(const MagickPixelPacket *p,
140  const MagickRealType alpha,const MagickPixelPacket *q,
141  const MagickRealType beta,MagickPixelPacket *composite)
142 {
143  MagickPixelCompositePlus(p,(MagickRealType) (QuantumRange-alpha*
144  (QuantumRange-p->opacity)),q,(MagickRealType) (QuantumRange-beta*
145  (QuantumRange-q->opacity)),composite);
146 }
147 
148 /*
149  Blend pixel colors p and q by the amount given and area.
150 */
151 static inline void MagickPixelCompositeAreaBlend(const MagickPixelPacket *p,
152  const MagickRealType alpha,const MagickPixelPacket *q,
153  const MagickRealType beta,const MagickRealType area,
154  MagickPixelPacket *composite)
155 {
156  MagickPixelCompositePlus(p,(MagickRealType) QuantumRange-(1.0-area)*
157  (QuantumRange-alpha),q,(MagickRealType) (QuantumRange-area*(QuantumRange-
158  beta)),composite);
159 }
160 
161 #if defined(__cplusplus) || defined(c_plusplus)
162 }
163 #endif
164 
165 #endif