MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
gem.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% GGGG EEEEE M M %
7% G E MM MM %
8% G GG EEE M M M %
9% G G E M M %
10% GGGG EEEEE M M %
11% %
12% %
13% Graphic Gems - Graphic Support Methods %
14% %
15% Software Design %
16% Cristy %
17% August 1996 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39␌
40/*
41 Include declarations.
42*/
43#include "MagickCore/studio.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/draw.h"
46#include "MagickCore/gem.h"
47#include "MagickCore/gem-private.h"
48#include "MagickCore/image.h"
49#include "MagickCore/image-private.h"
50#include "MagickCore/log.h"
51#include "MagickCore/memory_.h"
52#include "MagickCore/pixel-accessor.h"
53#include "MagickCore/quantum.h"
54#include "MagickCore/quantum-private.h"
55#include "MagickCore/random_.h"
56#include "MagickCore/resize.h"
57#include "MagickCore/transform.h"
58#include "MagickCore/signature-private.h"
59␌
60/*
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62% %
63% %
64% %
65% E x p a n d A f f i n e %
66% %
67% %
68% %
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% ExpandAffine() computes the affine's expansion factor, i.e. the square root
72% of the factor by which the affine transform affects area. In an affine
73% transform composed of scaling, rotation, shearing, and translation, returns
74% the amount of scaling.
75%
76% The format of the ExpandAffine method is:
77%
78% double ExpandAffine(const AffineMatrix *affine)
79%
80% A description of each parameter follows:
81%
82% o expansion: ExpandAffine returns the affine's expansion factor.
83%
84% o affine: A pointer the affine transform of type AffineMatrix.
85%
86*/
87MagickExport double ExpandAffine(const AffineMatrix *affine)
88{
89 const double
90 determinant = affine->sx*affine->sy-affine->rx*affine->ry;
91
92 assert(affine != (const AffineMatrix *) NULL);
93 return(sqrt(fabs(determinant)));
94}
95␌
96/*
97%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98% %
99% %
100% %
101% G e n e r a t e D i f f e r e n t i a l N o i s e %
102% %
103% %
104% %
105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106%
107% GenerateDifferentialNoise() generates differential noise.
108%
109% The format of the GenerateDifferentialNoise method is:
110%
111% double GenerateDifferentialNoise(RandomInfo *random_info,
112% const Quantum pixel,const NoiseType noise_type,const double attenuate)
113%
114% A description of each parameter follows:
115%
116% o random_info: the random info.
117%
118% o pixel: noise is relative to this pixel value.
119%
120% o noise_type: the type of noise.
121%
122% o attenuate: attenuate the noise.
123%
124*/
125MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info,
126 const Quantum pixel,const NoiseType noise_type,const double attenuate)
127{
128#define SigmaUniform (attenuate*0.015625)
129#define SigmaGaussian (attenuate*0.015625)
130#define SigmaImpulse (attenuate*0.1)
131#define SigmaLaplacian (attenuate*0.0390625)
132#define SigmaMultiplicativeGaussian (attenuate*0.5)
133#define SigmaPoisson (attenuate*12.5)
134#define SigmaRandom (attenuate)
135#define TauGaussian (attenuate*0.078125)
136
137 double
138 alpha,
139 beta,
140 noise,
141 sigma;
142
143 alpha=GetPseudoRandomValue(random_info);
144 switch (noise_type)
145 {
146 case UniformNoise:
147 default:
148 {
149 noise=(double) pixel+(double) QuantumRange*SigmaUniform*(alpha-0.5);
150 break;
151 }
152 case GaussianNoise:
153 {
154 double
155 gamma,
156 tau;
157
158 if (alpha <= MagickEpsilon)
159 alpha=1.0;
160 beta=GetPseudoRandomValue(random_info);
161 gamma=sqrt(-2.0*log(alpha));
162 sigma=gamma*cos((double) (2.0*MagickPI*beta));
163 tau=gamma*sin((double) (2.0*MagickPI*beta));
164 noise=(double) pixel+sqrt((double) pixel)*SigmaGaussian*sigma+
165 (double) QuantumRange*TauGaussian*tau;
166 break;
167 }
168 case ImpulseNoise:
169 {
170 if (alpha < (SigmaImpulse/2.0))
171 noise=0.0;
172 else
173 if (alpha >= (1.0-(SigmaImpulse/2.0)))
174 noise=(double) QuantumRange;
175 else
176 noise=(double) pixel;
177 break;
178 }
179 case LaplacianNoise:
180 {
181 if (alpha <= 0.5)
182 {
183 if (alpha <= MagickEpsilon)
184 noise=(double) (pixel-QuantumRange);
185 else
186 noise=(double) pixel+(double) QuantumRange*SigmaLaplacian*
187 log(2.0*alpha)+0.5;
188 break;
189 }
190 beta=1.0-alpha;
191 if (beta <= (0.5*MagickEpsilon))
192 noise=(double) (pixel+QuantumRange);
193 else
194 noise=(double) pixel-(double) QuantumRange*SigmaLaplacian*
195 log(2.0*beta)+0.5;
196 break;
197 }
198 case MultiplicativeGaussianNoise:
199 {
200 sigma=1.0;
201 if (alpha > MagickEpsilon)
202 sigma=sqrt(-2.0*log(alpha));
203 beta=GetPseudoRandomValue(random_info);
204 noise=(double) pixel+(double) pixel*SigmaMultiplicativeGaussian*sigma*
205 cos((double) (2.0*MagickPI*beta))/2.0;
206 break;
207 }
208 case PoissonNoise:
209 {
210 double
211 poisson;
212
213 ssize_t
214 i;
215
216 poisson=exp(-SigmaPoisson*QuantumScale*(double) pixel);
217 for (i=0; alpha > poisson; i++)
218 {
219 beta=GetPseudoRandomValue(random_info);
220 alpha*=beta;
221 }
222 noise=(double) QuantumRange*i*MagickSafeReciprocal(SigmaPoisson);
223 break;
224 }
225 case RandomNoise:
226 {
227 noise=(double) QuantumRange*SigmaRandom*alpha;
228 break;
229 }
230 }
231 return(noise);
232}
233␌
234/*
235%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236% %
237% %
238% %
239% G e t O p t i m a l K e r n e l W i d t h %
240% %
241% %
242% %
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244%
245% GetOptimalKernelWidth() computes the optimal kernel radius for a convolution
246% filter. Start with the minimum value of 3 pixels and walk out until we drop
247% below the threshold of one pixel numerical accuracy.
248%
249% The format of the GetOptimalKernelWidth method is:
250%
251% size_t GetOptimalKernelWidth(const double radius,
252% const double sigma)
253%
254% A description of each parameter follows:
255%
256% o width: GetOptimalKernelWidth returns the optimal width of a
257% convolution kernel.
258%
259% o radius: the radius of the Gaussian, in pixels, not counting the center
260% pixel.
261%
262% o sigma: the standard deviation of the Gaussian, in pixels.
263%
264*/
265MagickPrivate size_t GetOptimalKernelWidth1D(const double radius,
266 const double sigma)
267{
268 double
269 alpha,
270 beta,
271 gamma,
272 normalize,
273 value;
274
275 size_t
276 width;
277
278 ssize_t
279 i,
280 j;
281
282 if (IsEventLogging() != MagickFalse)
283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
284 if (radius > MagickEpsilon)
285 return((size_t) (2.0*ceil(radius)+1.0));
286 gamma=fabs(sigma);
287 if (gamma <= MagickEpsilon)
288 return(3UL);
289 alpha=MagickSafeReciprocal(2.0*gamma*gamma);
290 beta=(double) MagickSafeReciprocal((double) MagickSQ2PI*gamma);
291 for (width=5; ; )
292 {
293 normalize=0.0;
294 j=(ssize_t) (width-1)/2;
295 for (i=(-j); i <= j; i++)
296 normalize+=exp(-((double) (i*i))*alpha)*beta;
297 value=exp(-((double) (j*j))*alpha)*beta/normalize;
298 if ((value < QuantumScale) || (value < MagickEpsilon))
299 break;
300 width+=2;
301 }
302 return((size_t) (width-2));
303}
304
305MagickPrivate size_t GetOptimalKernelWidth2D(const double radius,
306 const double sigma)
307{
308 double
309 alpha,
310 beta,
311 gamma,
312 normalize,
313 value;
314
315 size_t
316 width;
317
318 ssize_t
319 j,
320 u,
321 v;
322
323 if (IsEventLogging() != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
325 if (radius > MagickEpsilon)
326 return((size_t) (2.0*ceil(radius)+1.0));
327 gamma=fabs(sigma);
328 if (gamma <= MagickEpsilon)
329 return(3UL);
330 alpha=MagickSafeReciprocal(2.0*gamma*gamma);
331 beta=(double) MagickSafeReciprocal((double) Magick2PI*gamma*gamma);
332 for (width=5; ; )
333 {
334 normalize=0.0;
335 j=(ssize_t) (width-1)/2;
336 for (v=(-j); v <= j; v++)
337 for (u=(-j); u <= j; u++)
338 normalize+=exp(-((double) (u*u+v*v))*alpha)*beta;
339 value=exp(-((double) (j*j))*alpha)*beta/normalize;
340 if ((value < QuantumScale) || (value < MagickEpsilon))
341 break;
342 width+=2;
343 }
344 return((size_t) (width-2));
345}
346
347MagickPrivate size_t GetOptimalKernelWidth(const double radius,
348 const double sigma)
349{
350 return(GetOptimalKernelWidth1D(radius,sigma));
351}