% August 1996 %
% %
% %
-% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
#include "MagickCore/log.h"
#include "MagickCore/memory_.h"
#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/pixel-private.h"
#include "MagickCore/quantum.h"
#include "MagickCore/quantum-private.h"
#include "MagickCore/random_.h"
% %
% %
% %
+% C o n v e r t H C L T o R G B %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% ConvertHCLToRGB() transforms a (hue, chroma, luma) to a (red, green,
+% blue) triple.
+%
+% The format of the ConvertHCLToRGBImage method is:
+%
+% void ConvertHCLToRGB(const double hue,const double chroma,
+% const double luma,double *red,double *green,double *blue)
+%
+% A description of each parameter follows:
+%
+% o hue, chroma, luma: A double value representing a
+% component of the HCL color space.
+%
+% o red, green, blue: A pointer to a pixel component of type Quantum.
+%
+*/
+MagickPrivate void ConvertHCLToRGB(const double hue,const double chroma,
+ const double luma,double *red,double *green,double *blue)
+{
+ double
+ b,
+ c,
+ g,
+ h,
+ m,
+ r,
+ x,
+ z;
+
+ /*
+ Convert HCL to RGB colorspace.
+ */
+ assert(red != (double *) NULL);
+ assert(green != (double *) NULL);
+ assert(blue != (double *) NULL);
+ h=6.0*hue;
+ c=chroma;
+ x=c*(1.0-fabs(fmod(h,2.0)-1.0));
+ r=0.0;
+ g=0.0;
+ b=0.0;
+ if ((0.0 <= h) && (h < 1.0))
+ {
+ r=c;
+ g=x;
+ }
+ else
+ if ((1.0 <= h) && (h < 2.0))
+ {
+ r=x;
+ g=c;
+ }
+ else
+ if ((2.0 <= h) && (h < 3.0))
+ {
+ g=c;
+ b=x;
+ }
+ else
+ if ((3.0 <= h) && (h < 4.0))
+ {
+ g=x;
+ b=c;
+ }
+ else
+ if ((4.0 <= h) && (h < 5.0))
+ {
+ r=x;
+ b=c;
+ }
+ else
+ if ((5.0 <= h) && (h < 6.0))
+ {
+ r=c;
+ b=x;
+ }
+ m=luma-(0.298839*r+0.586811*g+0.114350*b);
+ /*
+ Choose saturation strategy to clip it into the RGB cube; hue and luma are
+ preserved and chroma may be changed.
+ */
+ z=1.0;
+ if (m < 0.0)
+ {
+ z=luma/(luma-m);
+ m=0.0;
+ }
+ else
+ if ((m+c) > 1.0)
+ {
+ z=(1.0-luma)/(m+c-luma);
+ m=1.0-z*c;
+ }
+ *red=QuantumRange*(z*r+m);
+ *green=QuantumRange*(z*g+m);
+ *blue=QuantumRange*(z*b+m);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
% C o n v e r t H S B T o R G B %
% %
% %
% The format of the ConvertHSBToRGBImage method is:
%
% void ConvertHSBToRGB(const double hue,const double saturation,
-% const double brightness,Quantum *red,Quantum *green,Quantum *blue)
+% const double brightness,double *red,double *green,double *blue)
%
% A description of each parameter follows:
%
%
*/
MagickPrivate void ConvertHSBToRGB(const double hue,const double saturation,
- const double brightness,Quantum *red,Quantum *green,Quantum *blue)
+ const double brightness,double *red,double *green,double *blue)
{
- MagickRealType
+ double
f,
h,
p,
/*
Convert HSB to RGB colorspace.
*/
- assert(red != (Quantum *) NULL);
- assert(green != (Quantum *) NULL);
- assert(blue != (Quantum *) NULL);
+ assert(red != (double *) NULL);
+ assert(green != (double *) NULL);
+ assert(blue != (double *) NULL);
if (saturation == 0.0)
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*brightness);
+ *red=QuantumRange*brightness;
*green=(*red);
*blue=(*red);
return;
case 0:
default:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*brightness);
- *green=ClampToQuantum((MagickRealType) QuantumRange*t);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*p);
+ *red=QuantumRange*brightness;
+ *green=QuantumRange*t;
+ *blue=QuantumRange*p;
break;
}
case 1:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*q);
- *green=ClampToQuantum((MagickRealType) QuantumRange*brightness);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*p);
+ *red=QuantumRange*q;
+ *green=QuantumRange*brightness;
+ *blue=QuantumRange*p;
break;
}
case 2:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*p);
- *green=ClampToQuantum((MagickRealType) QuantumRange*brightness);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*t);
+ *red=QuantumRange*p;
+ *green=QuantumRange*brightness;
+ *blue=QuantumRange*t;
break;
}
case 3:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*p);
- *green=ClampToQuantum((MagickRealType) QuantumRange*q);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*brightness);
+ *red=QuantumRange*p;
+ *green=QuantumRange*q;
+ *blue=QuantumRange*brightness;
break;
}
case 4:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*t);
- *green=ClampToQuantum((MagickRealType) QuantumRange*p);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*brightness);
+ *red=QuantumRange*t;
+ *green=QuantumRange*p;
+ *blue=QuantumRange*brightness;
break;
}
case 5:
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*brightness);
- *green=ClampToQuantum((MagickRealType) QuantumRange*p);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*q);
+ *red=QuantumRange*brightness;
+ *green=QuantumRange*p;
+ *blue=QuantumRange*q;
break;
}
}
% The format of the ConvertHSLToRGBImage method is:
%
% void ConvertHSLToRGB(const double hue,const double saturation,
-% const double lightness,Quantum *red,Quantum *green,Quantum *blue)
+% const double lightness,double *red,double *green,double *blue)
%
% A description of each parameter follows:
%
%
*/
-static inline MagickRealType ConvertHueToRGB(MagickRealType m1,
- MagickRealType m2,MagickRealType hue)
+static inline double ConvertHueToRGB(double m1,double m2,double hue)
{
if (hue < 0.0)
hue+=1.0;
}
MagickExport void ConvertHSLToRGB(const double hue,const double saturation,
- const double lightness,Quantum *red,Quantum *green,Quantum *blue)
+ const double lightness,double *red,double *green,double *blue)
{
- MagickRealType
+ double
b,
g,
r,
/*
Convert HSL to RGB colorspace.
*/
- assert(red != (Quantum *) NULL);
- assert(green != (Quantum *) NULL);
- assert(blue != (Quantum *) NULL);
+ assert(red != (double *) NULL);
+ assert(green != (double *) NULL);
+ assert(blue != (double *) NULL);
if (saturation == 0)
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*lightness);
+ *red=QuantumRange*lightness;
*green=(*red);
*blue=(*red);
return;
r=ConvertHueToRGB(m1,m2,hue+1.0/3.0);
g=ConvertHueToRGB(m1,m2,hue);
b=ConvertHueToRGB(m1,m2,hue-1.0/3.0);
- *red=ClampToQuantum((MagickRealType) QuantumRange*r);
- *green=ClampToQuantum((MagickRealType) QuantumRange*g);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*b);
+ *red=QuantumRange*r;
+ *green=QuantumRange*g;
+ *blue=QuantumRange*b;
}
\f
/*
% The format of the ConvertHWBToRGBImage method is:
%
% void ConvertHWBToRGB(const double hue,const double whiteness,
-% const double blackness,Quantum *red,Quantum *green,Quantum *blue)
+% const double blackness,double *red,double *green,double *blue)
%
% A description of each parameter follows:
%
%
*/
MagickPrivate void ConvertHWBToRGB(const double hue,const double whiteness,
- const double blackness,Quantum *red,Quantum *green,Quantum *blue)
+ const double blackness,double *red,double *green,double *blue)
{
- MagickRealType
+ double
b,
f,
g,
/*
Convert HWB to RGB colorspace.
*/
- assert(red != (Quantum *) NULL);
- assert(green != (Quantum *) NULL);
- assert(blue != (Quantum *) NULL);
+ assert(red != (double *) NULL);
+ assert(green != (double *) NULL);
+ assert(blue != (double *) NULL);
v=1.0-blackness;
- if (hue == 0.0)
+ if (hue == -1.0)
{
- *red=ClampToQuantum((MagickRealType) QuantumRange*v);
- *green=ClampToQuantum((MagickRealType) QuantumRange*v);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*v);
+ *red=QuantumRange*v;
+ *green=QuantumRange*v;
+ *blue=QuantumRange*v;
return;
}
i=(ssize_t) floor(6.0*hue);
case 4: r=n; g=whiteness; b=v; break;
case 5: r=v; g=whiteness; b=n; break;
}
- *red=ClampToQuantum((MagickRealType) QuantumRange*r);
- *green=ClampToQuantum((MagickRealType) QuantumRange*g);
- *blue=ClampToQuantum((MagickRealType) QuantumRange*b);
+ *red=QuantumRange*r;
+ *green=QuantumRange*g;
+ *blue=QuantumRange*b;
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
+% C o n v e r t R G B T o H C L %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% ConvertRGBToHCL() transforms a (red, green, blue) to a (hue, chroma,
+% luma) triple.
+%
+% The format of the ConvertRGBToHCL method is:
+%
+% void ConvertRGBToHCL(const double red,const double green,
+% const double blue,double *hue,double *chroma,double *luma)
+%
+% A description of each parameter follows:
+%
+% o red, green, blue: A Quantum value representing the red, green, and
+% blue component of a pixel.
+%
+% o hue, chroma, luma: A pointer to a double value representing a
+% component of the HCL color space.
+%
+*/
+
+static inline double MagickMax(const double x,const double y)
+{
+ if (x > y)
+ return(x);
+ return(y);
+}
+
+static inline double MagickMin(const double x,const double y)
+{
+ if (x < y)
+ return(x);
+ return(y);
+}
+
+MagickPrivate void ConvertRGBToHCL(const double red,const double green,
+ const double blue,double *hue,double *chroma,double *luma)
+{
+ double
+ b,
+ c,
+ g,
+ h,
+ max,
+ r;
+
+ /*
+ Convert RGB to HCL colorspace.
+ */
+ assert(hue != (double *) NULL);
+ assert(chroma != (double *) NULL);
+ assert(luma != (double *) NULL);
+ r=red;
+ g=green;
+ b=blue;
+ max=MagickMax(r,MagickMax(g,b));
+ c=max-(double) MagickMin(r,MagickMin(g,b));
+ h=0.0;
+ if (c == 0.0)
+ h=0.0;
+ else
+ if (red == max)
+ h=fmod((g-b)/c+6.0,6.0);
+ else
+ if (green == max)
+ h=((b-r)/c)+2.0;
+ else
+ if (blue == max)
+ h=((r-g)/c)+4.0;
+ *hue=(h/6.0);
+ *chroma=QuantumScale*c;
+ *luma=QuantumScale*(0.298839*r+0.586811*g+0.114350*b);
}
\f
/*
%
% The format of the ConvertRGBToHSB method is:
%
-% void ConvertRGBToHSB(const Quantum red,const Quantum green,
-% const Quantum blue,double *hue,double *saturation,double *brightness)
+% void ConvertRGBToHSB(const double red,const double green,
+% const double blue,double *hue,double *saturation,double *brightness)
%
% A description of each parameter follows:
%
% component of the HSB color space.
%
*/
-MagickPrivate void ConvertRGBToHSB(const Quantum red,const Quantum green,
- const Quantum blue,double *hue,double *saturation,double *brightness)
+MagickPrivate void ConvertRGBToHSB(const double red,const double green,
+ const double blue,double *hue,double *saturation,double *brightness)
{
- MagickRealType
+ double
+ b,
delta,
+ g,
max,
- min;
+ min,
+ r;
/*
Convert RGB to HSB colorspace.
*hue=0.0;
*saturation=0.0;
*brightness=0.0;
- min=(MagickRealType) (red < green ? red : green);
- if ((MagickRealType) blue < min)
- min=(MagickRealType) blue;
- max=(MagickRealType) (red > green ? red : green);
- if ((MagickRealType) blue > max)
- max=(MagickRealType) blue;
+ r=red;
+ g=green;
+ b=blue;
+ min=r < g ? r : g;
+ if (b < min)
+ min=b;
+ max=r > g ? r : g;
+ if (b > max)
+ max=b;
if (max == 0.0)
return;
delta=max-min;
- *saturation=(double) (delta/max);
- *brightness=(double) (QuantumScale*max);
+ *saturation=delta/max;
+ *brightness=QuantumScale*max;
if (delta == 0.0)
return;
- if ((MagickRealType) red == max)
- *hue=(double) ((green-(MagickRealType) blue)/delta);
+ if (r == max)
+ *hue=(g-b)/delta;
else
- if ((MagickRealType) green == max)
- *hue=(double) (2.0+(blue-(MagickRealType) red)/delta);
+ if (g == max)
+ *hue=2.0+(b-r)/delta;
else
- *hue=(double) (4.0+(red-(MagickRealType) green)/delta);
+ *hue=4.0+(r-g)/delta;
*hue/=6.0;
if (*hue < 0.0)
*hue+=1.0;
%
% The format of the ConvertRGBToHSL method is:
%
-% void ConvertRGBToHSL(const Quantum red,const Quantum green,
-% const Quantum blue,double *hue,double *saturation,double *lightness)
+% void ConvertRGBToHSL(const double red,const double green,
+% const double blue,double *hue,double *saturation,double *lightness)
%
% A description of each parameter follows:
%
% component of the HSL color space.
%
*/
-
-static inline double MagickMax(const double x,const double y)
-{
- if (x > y)
- return(x);
- return(y);
-}
-
-static inline double MagickMin(const double x,const double y)
+MagickExport void ConvertRGBToHSL(const double red,const double green,
+ const double blue,double *hue,double *saturation,double *lightness)
{
- if (x < y)
- return(x);
- return(y);
-}
-
-MagickExport void ConvertRGBToHSL(const Quantum red,const Quantum green,
- const Quantum blue,double *hue,double *saturation,double *lightness)
-{
- MagickRealType
+ double
b,
delta,
g,
%
% The format of the ConvertRGBToHWB method is:
%
-% void ConvertRGBToHWB(const Quantum red,const Quantum green,
-% const Quantum blue,double *hue,double *whiteness,double *blackness)
+% void ConvertRGBToHWB(const double red,const double green,
+% const double blue,double *hue,double *whiteness,double *blackness)
%
% A description of each parameter follows:
%
% component of the HWB color space.
%
*/
-MagickPrivate void ConvertRGBToHWB(const Quantum red,const Quantum green,
- const Quantum blue,double *hue,double *whiteness,double *blackness)
+MagickPrivate void ConvertRGBToHWB(const double red,const double green,
+ const double blue,double *hue,double *whiteness,double *blackness)
{
- long
- i;
-
- MagickRealType
+ double
+ b,
f,
+ g,
+ p,
+ r,
v,
w;
assert(hue != (double *) NULL);
assert(whiteness != (double *) NULL);
assert(blackness != (double *) NULL);
- w=(MagickRealType) MagickMin((double) red,MagickMin((double) green,(double)
- blue));
- v=(MagickRealType) MagickMax((double) red,MagickMax((double) green,(double)
- blue));
+ r=red;
+ g=green;
+ b=blue;
+ w=MagickMin(r,MagickMin(g,b));
+ v=MagickMax(r,MagickMax(g,b));
*blackness=1.0-QuantumScale*v;
*whiteness=QuantumScale*w;
if (v == w)
{
- *hue=0.0;
+ *hue=(-1.0);
return;
}
- f=((MagickRealType) red == w) ? green-(MagickRealType) blue :
- (((MagickRealType) green == w) ? blue-(MagickRealType) red : red-
- (MagickRealType) green);
- i=((MagickRealType) red == w) ? 3 : (((MagickRealType) green == w) ? 5 : 1);
- *hue=((double) i-f/(v-1.0*w))/6.0;
+ f=(r == w) ? g-b : ((g == w) ? b-r : r-g);
+ p=(r == w) ? 3.0 : ((g == w) ? 5.0 : 1.0);
+ *hue=(p-f/(v-1.0*w))/6.0;
}
\f
/*
% The format of the GenerateDifferentialNoise method is:
%
% double GenerateDifferentialNoise(RandomInfo *random_info,
-% const Quantum pixel,const NoiseType noise_type,
-% const MagickRealType attenuate)
+% const Quantum pixel,const NoiseType noise_type,const double attenuate)
%
% A description of each parameter follows:
%
%
*/
MagickPrivate double GenerateDifferentialNoise(RandomInfo *random_info,
- const Quantum pixel,const NoiseType noise_type,const MagickRealType attenuate)
+ const Quantum pixel,const NoiseType noise_type,const double attenuate)
{
-#define NoiseEpsilon (attenuate*1.0e-5)
-#define SigmaUniform (attenuate*4.0)
-#define SigmaGaussian (attenuate*4.0)
-#define SigmaImpulse (attenuate*0.10)
-#define SigmaLaplacian (attenuate*10.0)
-#define SigmaMultiplicativeGaussian (attenuate*1.0)
-#define SigmaPoisson (attenuate*0.05)
-#define TauGaussian (attenuate*20.0)
+#define SigmaUniform (attenuate*0.015625)
+#define SigmaGaussian (attenuate*0.015625)
+#define SigmaImpulse (attenuate*0.1)
+#define SigmaLaplacian (attenuate*0.0390625)
+#define SigmaMultiplicativeGaussian (attenuate*0.5)
+#define SigmaPoisson (attenuate*12.5)
+#define SigmaRandom (attenuate)
+#define TauGaussian (attenuate*0.078125)
double
alpha,
case UniformNoise:
default:
{
- noise=(double) pixel+ScaleCharToQuantum((unsigned char)
- (SigmaUniform*(alpha)));
+ noise=(double) (pixel+QuantumRange*SigmaUniform*(alpha-0.5));
break;
}
case GaussianNoise:
gamma=sqrt(-2.0*log(alpha));
sigma=gamma*cos((double) (2.0*MagickPI*beta));
tau=gamma*sin((double) (2.0*MagickPI*beta));
- noise=(double) pixel+sqrt((double) pixel)*SigmaGaussian*sigma+
- TauGaussian*tau;
- break;
- }
- case MultiplicativeGaussianNoise:
- {
- if (alpha <= NoiseEpsilon)
- sigma=(double) QuantumRange;
- else
- sigma=sqrt(-2.0*log(alpha));
- beta=GetPseudoRandomValue(random_info);
- noise=(double) pixel+pixel*SigmaMultiplicativeGaussian*sigma/2.0*
- cos((double) (2.0*MagickPI*beta));
+ noise=(double) (pixel+sqrt((double) pixel)*SigmaGaussian*sigma+
+ QuantumRange*TauGaussian*tau);
break;
}
case ImpulseNoise:
{
if (alpha < (SigmaImpulse/2.0))
noise=0.0;
- else
- if (alpha >= (1.0-(SigmaImpulse/2.0)))
- noise=(double) QuantumRange;
- else
- noise=(double) pixel;
+ else
+ if (alpha >= (1.0-(SigmaImpulse/2.0)))
+ noise=(double) QuantumRange;
+ else
+ noise=(double) pixel;
break;
}
case LaplacianNoise:
{
if (alpha <= 0.5)
{
- if (alpha <= NoiseEpsilon)
- noise=(double) pixel-(double) QuantumRange;
+ if (alpha <= MagickEpsilon)
+ noise=(double) (pixel-QuantumRange);
else
- noise=(double) pixel+ScaleCharToQuantum((unsigned char)
- (SigmaLaplacian*log((2.0*alpha))+0.5));
+ noise=(double) (pixel+QuantumRange*SigmaLaplacian*log(2.0*alpha)+
+ 0.5);
break;
}
beta=1.0-alpha;
- if (beta <= (0.5*NoiseEpsilon))
+ if (beta <= (0.5*MagickEpsilon))
noise=(double) (pixel+QuantumRange);
else
- noise=(double) pixel-ScaleCharToQuantum((unsigned char)
- (SigmaLaplacian*log((2.0*beta))+0.5));
+ noise=(double) (pixel-QuantumRange*SigmaLaplacian*log(2.0*beta)+0.5);
+ break;
+ }
+ case MultiplicativeGaussianNoise:
+ {
+ sigma=1.0;
+ if (alpha > MagickEpsilon)
+ sigma=sqrt(-2.0*log(alpha));
+ beta=GetPseudoRandomValue(random_info);
+ noise=(double) (pixel+pixel*SigmaMultiplicativeGaussian*sigma*
+ cos((double) (2.0*MagickPI*beta))/2.0);
break;
}
case PoissonNoise:
register ssize_t
i;
- poisson=exp(-SigmaPoisson*ScaleQuantumToChar(pixel));
+ poisson=exp(-SigmaPoisson*QuantumScale*pixel);
for (i=0; alpha > poisson; i++)
{
beta=GetPseudoRandomValue(random_info);
alpha*=beta;
}
- noise=(double) ScaleCharToQuantum((unsigned char) (i/SigmaPoisson));
+ noise=(double) (QuantumRange*i/SigmaPoisson);
break;
}
case RandomNoise:
{
- noise=(double) QuantumRange*alpha;
+ noise=(double) (QuantumRange*SigmaRandom*alpha);
break;
}
}
gamma=fabs(sigma);
if (gamma <= MagickEpsilon)
return(3UL);
- alpha=1.0/(2.0*gamma*gamma);
- beta=(double) (1.0/(MagickSQ2PI*gamma));
+ alpha=MagickEpsilonReciprocal(2.0*gamma*gamma);
+ beta=(double) MagickEpsilonReciprocal((double) MagickSQ2PI*gamma);
for (width=5; ; )
{
normalize=0.0;
gamma=fabs(sigma);
if (gamma <= MagickEpsilon)
return(3UL);
- alpha=1.0/(2.0*gamma*gamma);
- beta=(double) (1.0/(Magick2PI*gamma*gamma));
+ alpha=MagickEpsilonReciprocal(2.0*gamma*gamma);
+ beta=(double) MagickEpsilonReciprocal((double) Magick2PI*gamma*gamma);
for (width=5; ; )
{
normalize=0.0;