]> granicus.if.org Git - imagemagick/blob - magick/gem.c
(no commit message)
[imagemagick] / magick / 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 %                                 John Cristy                                 %
17 %                                 August 1996                                 %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2009 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 %    http://www.imagemagick.org/script/license.php                            %
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 \f
40 /*
41   Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/color-private.h"
45 #include "magick/draw.h"
46 #include "magick/gem.h"
47 #include "magick/image.h"
48 #include "magick/image-private.h"
49 #include "magick/log.h"
50 #include "magick/memory_.h"
51 #include "magick/pixel-private.h"
52 #include "magick/quantum.h"
53 #include "magick/random_.h"
54 #include "magick/resize.h"
55 #include "magick/transform.h"
56 #include "magick/signature-private.h"
57 \f
58 /*
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %                                                                             %
61 %                                                                             %
62 %                                                                             %
63 %   C o n v e r t H S B T o R G B                                             %
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %
69 %  ConvertHSBToRGB() transforms a (hue, saturation, brightness) to a (red,
70 %  green, blue) triple.
71 %
72 %  The format of the ConvertHSBToRGBImage method is:
73 %
74 %      void ConvertHSBToRGB(const double hue,const double saturation,
75 %        const double brightness,Quantum *red,Quantum *green,Quantum *blue)
76 %
77 %  A description of each parameter follows:
78 %
79 %    o hue, saturation, brightness: A double value representing a
80 %      component of the HSB color space.
81 %
82 %    o red, green, blue: A pointer to a pixel component of type Quantum.
83 %
84 */
85 MagickExport void ConvertHSBToRGB(const double hue,const double saturation,
86   const double brightness,Quantum *red,Quantum *green,Quantum *blue)
87 {
88   MagickRealType
89     f,
90     h,
91     p,
92     q,
93     t;
94
95   /*
96     Convert HSB to RGB colorspace.
97   */
98   assert(red != (Quantum *) NULL);
99   assert(green != (Quantum *) NULL);
100   assert(blue != (Quantum *) NULL);
101   if (saturation == 0.0)
102     {
103       *red=RoundToQuantum((MagickRealType) QuantumRange*brightness);
104       *green=(*red);
105       *blue=(*red);
106       return;
107     }
108   h=6.0*(hue-floor(hue));
109   f=h-floor((double) h);
110   p=brightness*(1.0-saturation);
111   q=brightness*(1.0-saturation*f);
112   t=brightness*(1.0-(saturation*(1.0-f)));
113   switch ((int) h)
114   {
115     case 0:
116     default:
117     {
118       *red=RoundToQuantum((MagickRealType) QuantumRange*brightness);
119       *green=RoundToQuantum((MagickRealType) QuantumRange*t);
120       *blue=RoundToQuantum((MagickRealType) QuantumRange*p);
121       break;
122     }
123     case 1:
124     {
125       *red=RoundToQuantum((MagickRealType) QuantumRange*q);
126       *green=RoundToQuantum((MagickRealType) QuantumRange*brightness);
127       *blue=RoundToQuantum((MagickRealType) QuantumRange*p);
128       break;
129     }
130     case 2:
131     {
132       *red=RoundToQuantum((MagickRealType) QuantumRange*p);
133       *green=RoundToQuantum((MagickRealType) QuantumRange*brightness);
134       *blue=RoundToQuantum((MagickRealType) QuantumRange*t);
135       break;
136     }
137     case 3:
138     {
139       *red=RoundToQuantum((MagickRealType) QuantumRange*p);
140       *green=RoundToQuantum((MagickRealType) QuantumRange*q);
141       *blue=RoundToQuantum((MagickRealType) QuantumRange*brightness);
142       break;
143     }
144     case 4:
145     {
146       *red=RoundToQuantum((MagickRealType) QuantumRange*t);
147       *green=RoundToQuantum((MagickRealType) QuantumRange*p);
148       *blue=RoundToQuantum((MagickRealType) QuantumRange*brightness);
149       break;
150     }
151     case 5:
152     {
153       *red=RoundToQuantum((MagickRealType) QuantumRange*brightness);
154       *green=RoundToQuantum((MagickRealType) QuantumRange*p);
155       *blue=RoundToQuantum((MagickRealType) QuantumRange*q);
156       break;
157     }
158   }
159 }
160 \f
161 /*
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 %                                                                             %
164 %                                                                             %
165 %                                                                             %
166 %   C o n v e r t H S L T o R G B                                             %
167 %                                                                             %
168 %                                                                             %
169 %                                                                             %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 %
172 %  ConvertHSLToRGB() transforms a (hue, saturation, lightness) to a (red,
173 %  green, blue) triple.
174 %
175 %  The format of the ConvertHSLToRGBImage method is:
176 %
177 %      void ConvertHSLToRGB(const double hue,const double saturation,
178 %        const double lightness,Quantum *red,Quantum *green,Quantum *blue)
179 %
180 %  A description of each parameter follows:
181 %
182 %    o hue, saturation, lightness: A double value representing a
183 %      component of the HSL color space.
184 %
185 %    o red, green, blue: A pointer to a pixel component of type Quantum.
186 %
187 */
188
189 static inline MagickRealType ConvertHueToRGB(MagickRealType m1,
190   MagickRealType m2,MagickRealType hue)
191 {
192   if (hue < 0.0)
193     hue+=1.0;
194   if (hue > 1.0)
195     hue-=1.0;
196   if ((6.0*hue) < 1.0)
197     return(m1+6.0*(m2-m1)*hue);
198   if ((2.0*hue) < 1.0)
199     return(m2);
200   if ((3.0*hue) < 2.0)
201     return(m1+6.0*(m2-m1)*(2.0/3.0-hue));
202   return(m1);
203 }
204
205 MagickExport void ConvertHSLToRGB(const double hue,const double saturation,
206   const double lightness,Quantum *red,Quantum *green,Quantum *blue)
207 {
208   MagickRealType
209     b,
210     g,
211     r,
212     m1,
213     m2;
214
215   /*
216     Convert HSL to RGB colorspace.
217   */
218   assert(red != (Quantum *) NULL);
219   assert(green != (Quantum *) NULL);
220   assert(blue != (Quantum *) NULL);
221   if (saturation == 0)
222     {
223       *red=RoundToQuantum((MagickRealType) QuantumRange*lightness);
224       *green=(*red);
225       *blue=(*red);
226       return;
227     }
228   if (lightness <= 0.5)
229     m2=lightness*(saturation+1.0);
230   else
231     m2=(lightness+saturation)-(lightness*saturation);
232   m1=2.0*lightness-m2;
233   r=ConvertHueToRGB(m1,m2,hue+1.0/3.0);
234   g=ConvertHueToRGB(m1,m2,hue);
235   b=ConvertHueToRGB(m1,m2,hue-1.0/3.0);
236   *red=RoundToQuantum((MagickRealType) QuantumRange*r);
237   *green=RoundToQuantum((MagickRealType) QuantumRange*g);
238   *blue=RoundToQuantum((MagickRealType) QuantumRange*b);
239 }
240 \f
241 /*
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %   C o n v e r t H W B T o R G B                                             %
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 %
252 %  ConvertHWBToRGB() transforms a (hue, whiteness, blackness) to a (red, green,
253 %  blue) triple.
254 %
255 %  The format of the ConvertHWBToRGBImage method is:
256 %
257 %      void ConvertHWBToRGB(const double hue,const double whiteness,
258 %        const double blackness,Quantum *red,Quantum *green,Quantum *blue)
259 %
260 %  A description of each parameter follows:
261 %
262 %    o hue, whiteness, blackness: A double value representing a
263 %      component of the HWB color space.
264 %
265 %    o red, green, blue: A pointer to a pixel component of type Quantum.
266 %
267 */
268 MagickExport void ConvertHWBToRGB(const double hue,const double whiteness,
269   const double blackness,Quantum *red,Quantum *green,Quantum *blue)
270 {
271   MagickRealType
272     b,
273     f,
274     g,
275     n,
276     r,
277     v;
278
279   register long
280     i;
281
282   /*
283     Convert HWB to RGB colorspace.
284   */
285   assert(red != (Quantum *) NULL);
286   assert(green != (Quantum *) NULL);
287   assert(blue != (Quantum *) NULL);
288   v=1.0-blackness;
289   if (hue == 0.0)
290     {
291       *red=RoundToQuantum((MagickRealType) QuantumRange*v);
292       *green=RoundToQuantum((MagickRealType) QuantumRange*v);
293       *blue=RoundToQuantum((MagickRealType) QuantumRange*v);
294       return;
295     }
296   i=(long) floor(6.0*hue);
297   f=6.0*hue-i;
298   if ((i & 0x01) != 0)
299     f=1.0-f;
300   n=whiteness+f*(v-whiteness);  /* linear interpolation */
301   switch (i)
302   {
303     default:
304     case 6:
305     case 0: r=v; g=n; b=whiteness; break;
306     case 1: r=n; g=v; b=whiteness; break;
307     case 2: r=whiteness; g=v; b=n; break;
308     case 3: r=whiteness; g=n; b=v; break;
309     case 4: r=n; g=whiteness; b=v; break;
310     case 5: r=v; g=whiteness; b=n; break;
311   }
312   *red=RoundToQuantum((MagickRealType) QuantumRange*r);
313   *green=RoundToQuantum((MagickRealType) QuantumRange*g);
314   *blue=RoundToQuantum((MagickRealType) QuantumRange*b);
315 }
316 \f
317 /*
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 %                                                                             %
320 %                                                                             %
321 %                                                                             %
322 %   C o n v e r t R G B T o H S B                                             %
323 %                                                                             %
324 %                                                                             %
325 %                                                                             %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 %
328 %  ConvertRGBToHSB() transforms a (red, green, blue) to a (hue, saturation,
329 %  brightness) triple.
330 %
331 %  The format of the ConvertRGBToHSB method is:
332 %
333 %      void ConvertRGBToHSB(const Quantum red,const Quantum green,
334 %        const Quantum blue,double *hue,double *saturation,double *brightness)
335 %
336 %  A description of each parameter follows:
337 %
338 %    o red, green, blue: A Quantum value representing the red, green, and
339 %      blue component of a pixel..
340 %
341 %    o hue, saturation, brightness: A pointer to a double value representing a
342 %      component of the HSB color space.
343 %
344 */
345 MagickExport void ConvertRGBToHSB(const Quantum red,const Quantum green,
346   const Quantum blue,double *hue,double *saturation,double *brightness)
347 {
348   MagickRealType
349     delta,
350     max,
351     min;
352
353   /*
354     Convert RGB to HSB colorspace.
355   */
356   assert(hue != (double *) NULL);
357   assert(saturation != (double *) NULL);
358   assert(brightness != (double *) NULL);
359   *hue=0.0;
360   *saturation=0.0;
361   *brightness=0.0;
362   min=(MagickRealType) (red < green ? red : green);
363   if ((MagickRealType) blue < min)
364     min=(MagickRealType) blue;
365   max=(MagickRealType) (red > green ? red : green);
366   if ((MagickRealType) blue > max)
367     max=(MagickRealType) blue;
368   if (max == 0.0)
369     return;
370   delta=max-min;
371   *saturation=(double) (delta/max);
372   *brightness=(double) (QuantumScale*max);
373   if (delta == 0.0)
374     return;
375   if (red == max)
376     *hue=((((max-blue)/6.0)+(delta/2.0))-(((max-green)/6.0)+(delta/2.0)))/delta;
377   else
378     if (green == max)
379       *hue=(1.0/3.0)+((((max-red)/6.0)+(delta/2.0))-(((max-blue)/6.0)+
380         (delta/2.0)))/delta;
381     else
382       if (blue == max)
383         *hue=(2.0/3.0)+((((max-green)/6.0)+(delta/2.0))-(((max-red)/6.0)+
384           (delta/2.0)))/delta;
385   if (*hue < 0.0)
386     *hue+=1.0;
387   if (*hue > 1.0)
388     *hue-=1.0;
389 }
390 \f
391 /*
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %                                                                             %
394 %                                                                             %
395 %                                                                             %
396 %   C o n v e r t R G B T o H S L                                             %
397 %                                                                             %
398 %                                                                             %
399 %                                                                             %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 %
402 %  ConvertRGBToHSL() transforms a (red, green, blue) to a (hue, saturation,
403 %  lightness) triple.
404 %
405 %  The format of the ConvertRGBToHSL method is:
406 %
407 %      void ConvertRGBToHSL(const Quantum red,const Quantum green,
408 %        const Quantum blue,double *hue,double *saturation,double *lightness)
409 %
410 %  A description of each parameter follows:
411 %
412 %    o red, green, blue: A Quantum value representing the red, green, and
413 %      blue component of a pixel..
414 %
415 %    o hue, saturation, lightness: A pointer to a double value representing a
416 %      component of the HSL color space.
417 %
418 */
419
420 static inline double MagickMax(const double x,const double y)
421 {
422   if (x > y)
423     return(x);
424   return(y);
425 }
426
427 static inline double MagickMin(const double x,const double y)
428 {
429   if (x < y)
430     return(x);
431   return(y);
432 }
433
434 MagickExport void ConvertRGBToHSL(const Quantum red,const Quantum green,
435   const Quantum blue,double *hue,double *saturation,double *lightness)
436 {
437   MagickRealType
438     b,
439     delta,
440     g,
441     max,
442     min,
443     r;
444
445   /*
446     Convert RGB to HSL colorspace.
447   */
448   assert(hue != (double *) NULL);
449   assert(saturation != (double *) NULL);
450   assert(lightness != (double *) NULL);
451   r=QuantumScale*red;
452   g=QuantumScale*green;
453   b=QuantumScale*blue;
454   max=MagickMax(r,MagickMax(g,b));
455   min=MagickMin(r,MagickMin(g,b));
456   *lightness=(double) ((min+max)/2.0);
457   delta=max-min;
458   if (delta == 0.0)
459     {
460       *hue=0.0;
461       *saturation=0.0;
462       return;
463     }
464   if (*lightness < 0.5)
465     *saturation=(double) (delta/(min+max));
466   else
467     *saturation=(double) (delta/(2.0-max-min));
468   if (r == max)
469     *hue=((((max-b)/6.0)+(delta/2.0))-(((max-g)/6.0)+(delta/2.0)))/delta;
470   else
471     if (g == max)
472       *hue=(1.0/3.0)+((((max-r)/6.0)+(delta/2.0))-(((max-b)/6.0)+(delta/2.0)))/
473         delta;
474     else
475       if (b == max)
476         *hue=(2.0/3.0)+((((max-g)/6.0)+(delta/2.0))-(((max-r)/6.0)+
477           (delta/2.0)))/delta;
478   if (*hue < 0.0)
479     *hue+=1.0;
480   if (*hue > 1.0)
481     *hue-=1.0;
482 }
483 \f
484 /*
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 %                                                                             %
487 %                                                                             %
488 %                                                                             %
489 %   C o n v e r t R G B T o H W B                                             %
490 %                                                                             %
491 %                                                                             %
492 %                                                                             %
493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 %
495 %  ConvertRGBToHWB() transforms a (red, green, blue) to a (hue, whiteness,
496 %  blackness) triple.
497 %
498 %  The format of the ConvertRGBToHWB method is:
499 %
500 %      void ConvertRGBToHWB(const Quantum red,const Quantum green,
501 %        const Quantum blue,double *hue,double *whiteness,double *blackness)
502 %
503 %  A description of each parameter follows:
504 %
505 %    o red, green, blue: A Quantum value representing the red, green, and
506 %      blue component of a pixel.
507 %
508 %    o hue, whiteness, blackness: A pointer to a double value representing a
509 %      component of the HWB color space.
510 %
511 */
512 MagickExport void ConvertRGBToHWB(const Quantum red,const Quantum green,
513   const Quantum blue,double *hue,double *whiteness,double *blackness)
514 {
515   MagickRealType
516     f,
517     v,
518     w;
519
520   register long
521     i;
522
523   /*
524     Convert RGB to HWB colorspace.
525   */
526   assert(hue != (double *) NULL);
527   assert(whiteness != (double *) NULL);
528   assert(blackness != (double *) NULL);
529   w=(MagickRealType) MagickMin((double) red,MagickMin((double) green,(double)
530     blue));
531   v=(MagickRealType) MagickMax((double) red,MagickMax((double) green,(double)
532     blue));
533   *blackness=1.0-QuantumScale*v;
534   *whiteness=QuantumScale*w;
535   if (v == w)
536     {
537       *hue=0.0;
538       return;
539     }
540   f=((MagickRealType) red == w) ? green-(MagickRealType) blue :
541     (((MagickRealType) green == w) ? blue-(MagickRealType) red : red-
542     (MagickRealType) green);
543   i=((MagickRealType) red == w) ? 3 : (((MagickRealType) green == w) ? 5 : 1);
544   *hue=((double) i-f/(v-1.0*w))/6.0;
545 }
546 \f
547 /*
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 %                                                                             %
550 %                                                                             %
551 %                                                                             %
552 %   E x p a n d A f f i n e                                                   %
553 %                                                                             %
554 %                                                                             %
555 %                                                                             %
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 %
558 %  ExpandAffine() computes the affine's expansion factor, i.e. the square root
559 %  of the factor by which the affine transform affects area. In an affine
560 %  transform composed of scaling, rotation, shearing, and translation, returns
561 %  the amount of scaling.
562 %
563 %  The format of the ExpandAffine method is:
564 %
565 %      double ExpandAffine(const AffineMatrix *affine)
566 %
567 %  A description of each parameter follows:
568 %
569 %    o expansion: Method ExpandAffine returns the affine's expansion factor.
570 %
571 %    o affine: A pointer the affine transform of type AffineMatrix.
572 %
573 */
574 MagickExport double ExpandAffine(const AffineMatrix *affine)
575 {
576   assert(affine != (const AffineMatrix *) NULL);
577   return(sqrt(fabs(affine->sx*affine->sy-affine->rx*affine->ry)));
578 }
579 \f
580 /*
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 %                                                                             %
583 %                                                                             %
584 %                                                                             %
585 %   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                         %
586 %                                                                             %
587 %                                                                             %
588 %                                                                             %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 %
591 %  GenerateDifferentialNoise() generates differentual noise.
592 %
593 %  The format of the GenerateDifferentialNoise method is:
594 %
595 %      double GenerateDifferentialNoise(RandomInfo *random_info,
596 %        const Quantum pixel,const NoiseType noise_type,
597 %        const MagickRealType attenuate)
598 %
599 %  A description of each parameter follows:
600 %
601 %    o random_info: the random info.
602 %
603 %    o pixel: noise is relative to this pixel value.
604 %
605 %    o noise_type: the type of noise.
606 %
607 %    o attenuate:  attenuate the noise.
608 %
609 */
610 MagickExport double GenerateDifferentialNoise(RandomInfo *random_info,
611   const Quantum pixel,const NoiseType noise_type,const MagickRealType attenuate)
612 {
613 #define NoiseEpsilon  (attenuate*1.0e-5)
614 #define SigmaUniform  (attenuate*4.0)
615 #define SigmaGaussian  (attenuate*4.0)
616 #define SigmaImpulse  (attenuate*0.10)
617 #define SigmaLaplacian (attenuate*10.0)
618 #define SigmaMultiplicativeGaussian  (attenuate*1.0)
619 #define SigmaPoisson  (attenuate*0.05)
620 #define TauGaussian  (attenuate*20.0)
621
622   double
623     alpha,
624     beta,
625     noise,
626     sigma;
627
628   alpha=GetPseudoRandomValue(random_info);
629   switch (noise_type)
630   {
631     case UniformNoise:
632     default:
633     {
634       noise=(double) pixel+ScaleCharToQuantum((unsigned char)
635         (SigmaUniform*(alpha)));
636       break;
637     }
638     case GaussianNoise:
639     {
640       double
641         tau;
642
643       if (alpha == 0.0)
644         alpha=1.0;
645       beta=GetPseudoRandomValue(random_info);
646       sigma=sqrt(-2.0*log(alpha))*cos(2.0*MagickPI*beta);
647       tau=sqrt(-2.0*log(alpha))*sin(2.0*MagickPI*beta);
648       noise=(double) pixel+sqrt((double) pixel)*SigmaGaussian*sigma+
649         TauGaussian*tau;
650       break;
651     }
652     case MultiplicativeGaussianNoise:
653     {
654       if (alpha <= NoiseEpsilon)
655         sigma=(double) QuantumRange;
656       else
657         sigma=sqrt(-2.0*log(alpha));
658       beta=GetPseudoRandomValue(random_info);
659       noise=(double) pixel+pixel*SigmaMultiplicativeGaussian*sigma/2.0*
660         cos((2.0*MagickPI*beta));
661       break;
662     }
663     case ImpulseNoise:
664     {
665       if (alpha < (SigmaImpulse/2.0))
666         noise=0.0;
667        else
668          if (alpha >= (1.0-(SigmaImpulse/2.0)))
669            noise=(double) QuantumRange;
670          else
671            noise=(double) pixel;
672       break;
673     }
674     case LaplacianNoise:
675     {
676       if (alpha <= 0.5)
677         {
678           if (alpha <= NoiseEpsilon)
679             noise=(double) pixel-(double) QuantumRange;
680           else
681             noise=(double) pixel+ScaleCharToQuantum((unsigned char)
682               (SigmaLaplacian*log((2.0*alpha))+0.5));
683           break;
684         }
685       beta=1.0-alpha;
686       if (beta <= (0.5*NoiseEpsilon))
687         noise=(double) (pixel+QuantumRange);
688       else
689         noise=(double) pixel-ScaleCharToQuantum((unsigned char)
690           (SigmaLaplacian*log((2.0*beta))+0.5));
691       break;
692     }
693     case PoissonNoise:
694     {
695       double
696         poisson;
697
698       register long
699         i;
700
701       poisson=exp(-SigmaPoisson*ScaleQuantumToChar(pixel));
702       for (i=0; alpha > poisson; i++)
703       {
704         beta=GetPseudoRandomValue(random_info);
705         alpha*=beta;
706       }
707       noise=(double) ScaleCharToQuantum((unsigned char) (i/SigmaPoisson));
708       break;
709     }
710     case RandomNoise:
711     {
712       noise=(double) QuantumRange*GetPseudoRandomValue(random_info);
713       break;
714     }
715   }
716   return(noise);
717 }
718 \f
719 /*
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 %                                                                             %
722 %                                                                             %
723 %                                                                             %
724 %   G e t O p t i m a l K e r n e l W i d t h                                 %
725 %                                                                             %
726 %                                                                             %
727 %                                                                             %
728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 %
730 %  GetOptimalKernelWidth() computes the optimal kernel radius for a convolution
731 %  filter.  Start with the minimum value of 3 pixels and walk out until we drop
732 %  below the threshold of one pixel numerical accuracy.
733 %
734 %  The format of the GetOptimalKernelWidth method is:
735 %
736 %      unsigned long GetOptimalKernelWidth(const double radius,
737 %        const double sigma)
738 %
739 %  A description of each parameter follows:
740 %
741 %    o width: Method GetOptimalKernelWidth returns the optimal width of
742 %      a convolution kernel.
743 %
744 %    o radius: the radius of the Gaussian, in pixels, not counting the center
745 %      pixel.
746 %
747 %    o sigma: the standard deviation of the Gaussian, in pixels.
748 %
749 */
750 MagickExport unsigned long GetOptimalKernelWidth1D(const double radius,
751   const double sigma)
752 {
753   long
754     width;
755
756   MagickRealType
757     normalize,
758     value;
759
760   register long
761     u;
762
763   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
764   if (radius > 0.0)
765     return((unsigned long) (2.0*ceil(radius)+1.0));
766   if (fabs(sigma) <= MagickEpsilon)
767     return(1);
768   for (width=5; ; )
769   {
770     normalize=0.0;
771     for (u=(-width/2); u <= (width/2); u++)
772       normalize+=exp(-((double) u*u)/(2.0*sigma*sigma))/(MagickSQ2PI*sigma);
773     u=width/2;
774     value=exp(-((double) u*u)/(2.0*sigma*sigma))/(MagickSQ2PI*sigma)/normalize;
775     if ((long) (QuantumRange*value) <= 0L)
776       break;
777     width+=2;
778   }
779   return((unsigned long) (width-2));
780 }
781
782 MagickExport unsigned long GetOptimalKernelWidth2D(const double radius,
783   const double sigma)
784 {
785
786   long
787     width;
788
789   MagickRealType
790     alpha,
791     normalize,
792     value;
793
794   register long
795     u,
796     v;
797
798   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
799   if (radius > 0.0)
800     return((unsigned long) (2.0*ceil(radius)+1.0));
801   if (fabs(sigma) <= MagickEpsilon)
802     return(1);
803   for (width=5; ; )
804   {
805     normalize=0.0;
806     for (v=(-width/2); v <= (width/2); v++)
807     {
808       for (u=(-width/2); u <= (width/2); u++)
809       {
810         alpha=exp(-((double) u*u+v*v)/(2.0*sigma*sigma));
811         normalize+=alpha/(2.0*MagickPI*sigma*sigma);
812       }
813     }
814     v=width/2;
815     value=exp(-((double) v*v)/(2.0*sigma*sigma))/normalize;
816     if ((long) (QuantumRange*value) <= 0L)
817       break;
818     width+=2;
819   }
820   return((unsigned long) (width-2));
821 }
822
823 MagickExport unsigned long  GetOptimalKernelWidth(const double radius,
824   const double sigma)
825 {
826   return(GetOptimalKernelWidth1D(radius,sigma));
827 }