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