/* THIS FILE IS PROVIDED ONLY FOR YOUR INFORMATION AND REFERENCE. CORRECTNESS OF THIS IS NOT GUARANTEED. THIS IS TAKEN FROM IMAGEMAGICK (V5.2.3) SOURCE CODE. PLEASE GOT TO IMAGEMAGICK HOMEPAGE LINK ON THE COURSE WEBSITE FOR MORE INFORMATION. */ /* Include declarations. */ #include "magick.h" #include "defines.h" /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % T r a n s f o r m H S L % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method TransformHSL converts a (red, green, blue) to a (hue, saturation, % luminosity) triple. % % The format of the TransformHSL method is: % % void TransformHSL(const Quantum red,const Quantum green, % const Quantum blue,double *hue,double *saturation,double *luminosity) % % 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, saturation, luminosity: A pointer to a double value representing a % component of the HSL color space. % % */ MagickExport void TransformHSL(const Quantum red,const Quantum green, const Quantum blue,double *hue,double *saturation,double *luminosity) { double b, delta, g, max, min, r; /* Convert RGB to HSL colorspace. */ assert(hue != (double *) NULL); assert(saturation != (double *) NULL); assert(luminosity != (double *) NULL); r=(double) red/MaxRGB; g=(double) green/MaxRGB; b=(double) blue/MaxRGB; max=Max(r,Max(g,b)); min=Min(r,Min(g,b)); *hue=(-1.0); *saturation=0.0; *luminosity=0.5000000000000001*(min+max); delta=max-min; if (delta == 0.0) return; *saturation=delta/((*luminosity <= 0.5) ? (min+max) : (2.0-max-min)); if (r == max) *hue=(g == min ? 5.0+(max-b)/delta : 1.0-(max-g)/delta); else if (g == max) *hue=(b == min ? 1.0+(max-r)/delta : 3.0-(max-b)/delta); else *hue=(r == min ? 3.0+(max-g)/delta : 5.0-(max-r)/delta); *hue/=6.0; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % H S L T r a n s f o r m % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method HSLTransform converts a (hue, saturation, luminosity) to a % (red, green, blue) triple. % % The format of the HSLTransformImage method is: % % void HSLTransform(const double hue,const double saturation, % const double luminosity,Quantum *red,Quantum *green,Quantum *blue) % % A description of each parameter follows: % % o hue, saturation, luminosity: A double value representing a % component of the HSL color space. % % o red, green, blue: A pointer to a pixel component of type Quantum. % % */ MagickExport void HSLTransform(const double hue,const double saturation, const double luminosity,Quantum *red,Quantum *green,Quantum *blue) { double b, g, r, v, x, y, z; /* Convert HSL to RGB colorspace. */ assert(red != (Quantum *) NULL); assert(green != (Quantum *) NULL); assert(blue != (Quantum *) NULL); v=(luminosity <= 0.5) ? (luminosity*(1.0+saturation)) : (luminosity+saturation-luminosity*saturation); if ((saturation == 0.0) || (hue == -1.0)) { *red=MaxRGB*luminosity+0.5; *green=MaxRGB*luminosity+0.5; *blue=MaxRGB*luminosity+0.5; return; } y=2.0*luminosity-v; x=y+(v-y)*(6.0*hue-floor(6.0*hue)); z=v-(v-y)*(6.0*hue-floor(6.0*hue)); switch ((int) (6.0*hue)) { case 0: r=v; g=x; b=y; break; case 1: r=z; g=v; b=y; break; case 2: r=y; g=v; b=x; break; case 3: r=y; g=z; b=v; break; case 4: r=x; g=y; b=v; break; case 5: r=v; g=y; b=z; break; default: r=v; g=x; b=y; break; } *red=MaxRGB*r+0.5; *green=MaxRGB*g+0.5; *blue=MaxRGB*b+0.5; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % I n t e r p o l a t e C o l o r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method InterpolateColor applies bi-linear interpolation between a pixel and % it's neighbors. % % The format of the InterpolateColor method is: % % PixelPacket InterpolateColor(Image *image,const double x_offset, % const double y_offset) % % A description of each parameter follows: % % o image: The address of a structure of type Image. % % o x_offset,y_offset: A double representing the current (x,y) position of % the pixel. % % */ MagickExport PixelPacket InterpolateColor(Image *image,const double x_offset, const double y_offset) { double alpha, beta; PixelPacket color, p, q, r, s; register double x, y; assert(image != (Image *) NULL); assert(image->signature == MagickSignature); x=x_offset+0.5; y=y_offset+0.5; if ((x < -1.0) || (x >= image->columns) || (y < -1.0) || (y >= image->rows)) return(image->background_color); p=image->background_color; if ((x >= 0.0) && (y >= 0.0)) p=GetOnePixel(image,(int) x,(int) y); q=image->background_color; if (((x+1.0) < image->columns) && (y >= 0.0)) q=GetOnePixel(image,(int) (x+1.0),(int) y); r=image->background_color; if ((x >= 0.0) && ((y+1.0) < image->rows)) r=GetOnePixel(image,(int) x,(int) (y+1.0)); s=image->background_color; if (((x+1.0) < image->columns) && ((y+1.0) < image->rows)) s=GetOnePixel(image,(int) (x+1.0),(int) (y+1.0)); x-=floor(x); y-=floor(y); alpha=1.0-x; beta=1.0-y; color.red=beta*(alpha*p.red+x*q.red)+y*(alpha*r.red+x*s.red)+0.5; color.green=beta*(alpha*p.green+x*q.green)+y*(alpha*r.green+x*s.green)+0.5; color.blue=beta*(alpha*p.blue+x*q.blue)+y*(alpha*r.blue+x*s.blue)+0.5; color.opacity= beta*(alpha*p.opacity+x*q.opacity)+y*(alpha*r.opacity+x*s.opacity)+0.5; return(color); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % M o d u l a t e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method Modulate modulates the hue, saturation, and brightness of an % image. % % The format of the ModulateImage method is: % % void Modulate(double percent_hue,double percent_saturation, % double percent_brightness,Quantum *red,Quantum *green,Quantum *blue) % blue) % % A description of each parameter follows: % % o percent_hue, percent_saturation, percent_brightness: A double value % representing the percent change in a component of the HSL color space. % % o red, green, blue: A pointer to a pixel component of type Quantum. % % */ MagickExport void Modulate(double percent_hue,double percent_saturation, double percent_brightness,Quantum *red,Quantum *green,Quantum *blue) { double brightness, hue, saturation; /* Increase or decrease color brightness, saturation, or hue. */ assert(red != (Quantum *) NULL); assert(green != (Quantum *) NULL); assert(blue != (Quantum *) NULL); TransformHSL(*red,*green,*blue,&hue,&saturation,&brightness); brightness*=0.0100000000000001*percent_brightness; if (brightness < 0.0) brightness=0.0; else if (brightness > 1.0) brightness=1.0; saturation*=0.0100000000000001*percent_saturation; if (saturation < 0.0) saturation=0.0; else if (saturation > 1.0) saturation=1.0; if (hue != -1.0) { hue*=0.0100000000000001*percent_hue; if (hue < 0.0) hue+=1.0; else if (hue > 1.0) hue-=1.0; } HSLTransform(hue,saturation,brightness,red,green,blue); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % C o n s t r a s t % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Method Contrast enhances the intensity differences between the lighter % and darker elements of the image. % % The format of the Contrast method is: % % void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue) % % A description of each parameter follows: % % o sign: A positive value enhances the contrast otherwise it is reduced. % % o red, green, blue: A pointer to a pixel component of type Quantum. % % */ MagickExport void Contrast(const int sign,Quantum *red,Quantum *green, Quantum *blue) { double alpha, brightness, hue, saturation; /* Enhance contrast: dark color become darker, light color become lighter. */ assert(red != (Quantum *) NULL); assert(green != (Quantum *) NULL); assert(blue != (Quantum *) NULL); TransformHSL(*red,*green,*blue,&hue,&saturation,&brightness); alpha=0.5000000000000001; brightness+=alpha*sign*(alpha*(sin(M_PI*(brightness-alpha))+1.0)-brightness); if (brightness > 1.0) brightness=1.0; else if (brightness < 0.0) brightness=0.0; HSLTransform(hue,saturation,brightness,red,green,blue); }