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