From 66a0c5b9e3f8a7fde0bec2869b6d0b87977bca41 Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 4 Nov 2014 20:25:07 +0000 Subject: [PATCH] Added ColorCMYK to Magick++. Moved alpha property to ColorRGB. Fixed casting to PixelInfo of methods in the Image class. --- Magick++/lib/Color.cpp | 252 ++++++++++++++++++++++++++-------- Magick++/lib/Image.cpp | 60 ++------ Magick++/lib/Magick++/Color.h | 97 +++++++++++-- 3 files changed, 281 insertions(+), 128 deletions(-) diff --git a/Magick++/lib/Color.cpp b/Magick++/lib/Color.cpp index e46af3f96..de3aa3fa9 100644 --- a/Magick++/lib/Color.cpp +++ b/Magick++/lib/Color.cpp @@ -81,10 +81,11 @@ Magick::Color::Color(const Quantum red_,const Quantum green_, _pixelOwn(true), _pixelType(RGBPixel) { - quantumRed(red_); - quantumGreen(green_); - quantumBlue(blue_); quantumAlpha(OpaqueAlpha); + quantumBlack(0); + quantumBlue(blue_); + quantumGreen(green_); + quantumRed(red_); } Magick::Color::Color(const Quantum red_,const Quantum green_, @@ -94,10 +95,11 @@ Magick::Color::Color(const Quantum red_,const Quantum green_, _pixelOwn(true), _pixelType(RGBAPixel) { - quantumRed(red_); - quantumGreen(green_); - quantumBlue(blue_); quantumAlpha(alpha_); + quantumBlack(0); + quantumBlue(blue_); + quantumGreen(green_); + quantumRed(red_); } Magick::Color::Color(const char *color_) @@ -124,13 +126,10 @@ Magick::Color::Color(const Magick::Color &color_) Magick::Color::Color(const PixelInfo &color_) : _pixel(new PixelInfo), _isValid(true), - _pixelOwn(true), - _pixelType(RGBPixel) + _pixelOwn(true) { *_pixel=color_; - - if (color_.alpha != OpaqueAlpha) - _pixelType=RGBAPixel; + setPixelType(color_); } Magick::Color::Color(const std::string &color_) @@ -179,10 +178,7 @@ const Magick::Color& Magick::Color::operator=(const char *color_) const Magick::Color& Magick::Color::operator=(const MagickCore::PixelInfo &color_) { *_pixel=color_; - if (color_.alpha != OpaqueAlpha) - _pixelType=RGBAPixel; - else - _pixelType=RGBPixel; + setPixelType(color_); return(*this); } @@ -197,15 +193,13 @@ const Magick::Color& Magick::Color::operator=(const std::string &color_) if (QueryColorCompliance(color_.c_str(),AllCompliance,&target_color, exceptionInfo)) { - quantumRed(target_color.red); - quantumGreen(target_color.green); - quantumBlue(target_color.blue); quantumAlpha(target_color.alpha); + quantumBlack(target_color.black); + quantumBlue(target_color.blue); + quantumGreen(target_color.green); + quantumRed(target_color.red); - if (quantumAlpha() != OpaqueAlpha) - _pixelType=RGBAPixel; - else - _pixelType=RGBPixel; + setPixelType(target_color); } else _isValid = false; @@ -230,30 +224,22 @@ Magick::Color::operator std::string() const if (!isValid()) return std::string("none"); - pixel.colorspace=RGBColorspace; + pixel.colorspace=(_pixelType == RGBAPixel || _pixelType == RGBAPixel) ? + RGBColorspace : CMYKColorspace; pixel.alpha_trait=_pixelType == RGBAPixel ? BlendPixelTrait : UndefinedPixelTrait; pixel.depth=MAGICKCORE_QUANTUM_DEPTH; - pixel.red=_pixel->red; - pixel.green=_pixel->green; - pixel.blue=_pixel->blue; pixel.alpha=_pixel->alpha; pixel.alpha_trait=_pixel->alpha_trait; + pixel.black=_pixel->black; + pixel.blue=_pixel->blue; + pixel.green=_pixel->green; + pixel.red=_pixel->red; GetColorTuple(&pixel,MagickTrue,colorbuf); return(std::string(colorbuf)); } -void Magick::Color::alpha(const double alpha_) -{ - quantumAlpha(scaleDoubleToQuantum(alpha_)); -} - -double Magick::Color::alpha(void) const -{ - return scaleQuantumToDouble(quantumAlpha()); -} - bool Magick::Color::isValid(void) const { return(_isValid); @@ -279,15 +265,38 @@ void Magick::Color::quantumAlpha(const Magick::Quantum alpha_) { _pixel->alpha=alpha_; if (alpha_ == QuantumRange) - _pixel->alpha_trait=UndefinedPixelTrait; + { + _pixel->alpha_trait=UndefinedPixelTrait; + if (_pixelType == RGBAPixel) + _pixelType=RGBPixel; + else if (_pixelType == CMYKAPixel) + _pixelType=CMYKPixel; + } else - _pixel->alpha_trait=BlendPixelTrait; + { + _pixel->alpha_trait=BlendPixelTrait; + if (_pixelType == RGBPixel) + _pixelType=RGBAPixel; + else if (_pixelType == CMYKPixel) + _pixelType=CMYKAPixel; + } _isValid=true; } Magick::Quantum Magick::Color::quantumAlpha(void) const { - return _pixel->alpha; + return(_pixel->alpha); +} + +void Magick::Color::quantumBlack(const Magick::Quantum black_) +{ + _pixel->black=black_; + _isValid=true; +} + +Magick::Quantum Magick::Color::quantumBlack(void) const +{ + return(_pixel->black); } void Magick::Color::quantumBlue(const Magick::Quantum blue_) @@ -298,7 +307,7 @@ void Magick::Color::quantumBlue(const Magick::Quantum blue_) Magick::Quantum Magick::Color::quantumBlue(void) const { - return _pixel->blue; + return(_pixel->blue); } void Magick::Color::quantumGreen(const Magick::Quantum green_) @@ -309,7 +318,7 @@ void Magick::Color::quantumGreen(const Magick::Quantum green_) Magick::Quantum Magick::Color::quantumGreen(void) const { - return _pixel->green; + return(_pixel->green); } void Magick::Color::quantumRed(const Magick::Quantum red_) @@ -323,6 +332,14 @@ Magick::Quantum Magick::Color::quantumRed(void) const return _pixel->red; } +Magick::Color::Color(PixelType pixelType_) + : _pixel(new PixelInfo), + _isValid(false), + _pixelOwn(true), + _pixelType(pixelType_) +{ +} + Magick::Color::Color(PixelInfo* rep_,PixelType pixelType_) : _pixel(rep_), _isValid(true), @@ -344,24 +361,132 @@ void Magick::Color::pixel(PixelInfo *rep_,PixelType pixelType_) Magick::Quantum Magick::Color::scaleDoubleToQuantum(const double double_) { - return (static_cast(double_*QuantumRange)); + return(static_cast(double_*QuantumRange)); } double Magick::Color::scaleQuantumToDouble(const Magick::Quantum quantum_) { #if (MAGICKCORE_QUANTUM_DEPTH < 32) - return (static_cast(quantum_)/QuantumRange); + return(static_cast(quantum_)/QuantumRange); #else - return (quantum_/QuantumRange); + return(quantum_/QuantumRange); #endif } void Magick::Color::initPixel() { - _pixel->red=0; - _pixel->green=0; - _pixel->blue=0; _pixel->alpha=OpaqueAlpha; + _pixel->alpha_trait=UndefinedPixelTrait; + _pixel->black=0; + _pixel->blue=0; + _pixel->green=0; + _pixel->red=0; +} + +void Magick::Color::setPixelType(const PixelInfo &color_) +{ + if (color_.colorspace == CMYKColorspace) + _pixelType=color_.alpha_trait != UndefinedPixelTrait ? CMYKAPixel : + CMYKPixel; + else + _pixelType=color_.alpha_trait != UndefinedPixelTrait ? RGBAPixel : + RGBPixel; +} + +Magick::ColorCMYK::ColorCMYK(void) + : Color(CMYKPixel) +{ +} + +Magick::ColorCMYK::ColorCMYK(const Magick::Color &color_) + : Color(color_) +{ +} + +Magick::ColorCMYK::ColorCMYK(const double cyan_,const double magenta_, + const double yellow_,const double black_) + : Color(CMYKPixel) +{ + cyan(cyan_); + magenta(magenta_); + yellow(yellow_); + black(black_); +} + +Magick::ColorCMYK::ColorCMYK(const double cyan_,const double magenta_, + const double yellow_,const double black_,const double alpha_) + : Color(CMYKAPixel) +{ + cyan(cyan_); + magenta(magenta_); + yellow(yellow_); + black(black_); + alpha(alpha_); +} + +Magick::ColorCMYK::~ColorCMYK(void) +{ +} + +Magick::ColorCMYK& Magick::ColorCMYK::operator=(const Magick::Color& color_) +{ + *static_cast(this)=color_; + return(*this); +} + +void Magick::ColorCMYK::alpha(const double alpha_) +{ + quantumAlpha(scaleDoubleToQuantum(alpha_)); +} + +double Magick::ColorCMYK::alpha(void) const +{ + return(scaleQuantumToDouble(quantumAlpha())); +} + +void Magick::ColorCMYK::black(const double black_) +{ + quantumBlack(scaleDoubleToQuantum(black_)); +} + +double Magick::ColorCMYK::black(void) const +{ + return(scaleQuantumToDouble(quantumBlack())); +} + +void Magick::ColorCMYK::cyan(const double cyan_) +{ + quantumRed(scaleDoubleToQuantum(cyan_)); +} + +double Magick::ColorCMYK::cyan(void) const +{ + return(scaleQuantumToDouble(quantumRed())); +} + +void Magick::ColorCMYK::magenta(const double magenta_) +{ + quantumGreen(scaleDoubleToQuantum(magenta_)); +} + +double Magick::ColorCMYK::magenta(void) const +{ + return(scaleQuantumToDouble(quantumGreen())); +} + +void Magick::ColorCMYK::yellow(const double yellow_) +{ + quantumBlue(scaleDoubleToQuantum(yellow_)); +} + +double Magick::ColorCMYK::yellow(void) const +{ + return(scaleQuantumToDouble(quantumBlue())); +} + +Magick::ColorCMYK::ColorCMYK(PixelInfo *rep_,PixelType pixelType_) + : Color(rep_,pixelType_) +{ } Magick::ColorGray::ColorGray(void) @@ -378,7 +503,6 @@ Magick::ColorGray::ColorGray(double shade_) : Color(scaleDoubleToQuantum(shade_),scaleDoubleToQuantum(shade_), scaleDoubleToQuantum(shade_)) { - quantumAlpha(OpaqueAlpha); } Magick::ColorGray::~ColorGray() @@ -433,7 +557,6 @@ Magick::ColorHSL::ColorHSL(const double hue_,const double saturation_, quantumRed(red); quantumGreen(green); quantumBlue(blue); - quantumAlpha(OpaqueAlpha); } Magick::ColorHSL::~ColorHSL() @@ -566,7 +689,6 @@ Magick::ColorMono::ColorMono(const bool mono_) : Color((mono_ ? QuantumRange : 0),(mono_ ? QuantumRange : 0), (mono_ ? QuantumRange : 0)) { - quantumAlpha(OpaqueAlpha); } Magick::ColorMono::ColorMono(const Magick::Color &color_) @@ -616,7 +738,6 @@ Magick::ColorRGB::ColorRGB(const double red_,const double green_, : Color(scaleDoubleToQuantum(red_),scaleDoubleToQuantum(green_), scaleDoubleToQuantum(blue_)) { - quantumAlpha(OpaqueAlpha); } Magick::ColorRGB::ColorRGB(const double red_,const double green_, @@ -636,6 +757,16 @@ Magick::ColorRGB& Magick::ColorRGB::operator=(const Magick::Color& color_) return(*this); } +void Magick::ColorRGB::alpha(const double alpha_) +{ + quantumAlpha(scaleDoubleToQuantum(alpha_)); +} + +double Magick::ColorRGB::alpha(void) const +{ + return(scaleQuantumToDouble(quantumAlpha())); +} + void Magick::ColorRGB::blue(const double blue_) { quantumBlue(scaleDoubleToQuantum(blue_)); @@ -643,7 +774,7 @@ void Magick::ColorRGB::blue(const double blue_) double Magick::ColorRGB::blue(void) const { - return scaleQuantumToDouble(quantumBlue()); + return(scaleQuantumToDouble(quantumBlue())); } void Magick::ColorRGB::green(const double green_) @@ -653,7 +784,7 @@ void Magick::ColorRGB::green(const double green_) double Magick::ColorRGB::green(void) const { - return scaleQuantumToDouble(quantumGreen()); + return(scaleQuantumToDouble(quantumGreen())); } void Magick::ColorRGB::red(const double red_) @@ -663,7 +794,7 @@ void Magick::ColorRGB::red(const double red_) double Magick::ColorRGB::red(void) const { - return scaleQuantumToDouble(quantumRed()); + return(scaleQuantumToDouble(quantumRed())); } Magick::ColorRGB::ColorRGB(PixelInfo *rep_,PixelType pixelType_) @@ -685,7 +816,6 @@ Magick::ColorYUV::ColorYUV(const double y_,const double u_,const double v_) : Color() { convert(y_, u_, v_); - quantumAlpha(OpaqueAlpha); } Magick::ColorYUV::~ColorYUV(void) @@ -705,8 +835,8 @@ void Magick::ColorYUV::u(const double u_) double Magick::ColorYUV::u(void) const { - return scaleQuantumToDouble((-0.14740 * quantumRed()) - (0.28950 * - quantumGreen()) + (0.43690 * quantumBlue())); + return(scaleQuantumToDouble((-0.14740 * quantumRed()) - (0.28950 * + quantumGreen()) + (0.43690 * quantumBlue()))); } void Magick::ColorYUV::v(const double v_) @@ -716,8 +846,8 @@ void Magick::ColorYUV::v(const double v_) double Magick::ColorYUV::v(void) const { - return scaleQuantumToDouble((0.61500 * quantumRed()) - (0.51500 * - quantumGreen()) - (0.10000 * quantumBlue())); + return(scaleQuantumToDouble((0.61500 * quantumRed()) - (0.51500 * + quantumGreen()) - (0.10000 * quantumBlue()))); } void Magick::ColorYUV::y(const double y_) @@ -727,8 +857,8 @@ void Magick::ColorYUV::y(const double y_) double Magick::ColorYUV::y ( void ) const { - return scaleQuantumToDouble((0.29900 * quantumRed()) + (0.58700 * - quantumGreen()) + (0.11400 * quantumBlue())); + return(scaleQuantumToDouble((0.29900 * quantumRed()) + (0.58700 * + quantumGreen()) + (0.11400 * quantumBlue()))); } void Magick::ColorYUV::convert(const double y_,const double u_,const double v_) diff --git a/Magick++/lib/Image.cpp b/Magick++/lib/Image.cpp index 4a46681cf..67c0c84f2 100644 --- a/Magick++/lib/Image.cpp +++ b/Magick++/lib/Image.cpp @@ -360,9 +360,7 @@ void Magick::Image::alphaColor(const Color &alphaColor_) Magick::Color Magick::Image::alphaColor(void) const { - return(Color(ClampToQuantum(constImage()->matte_color.red), - ClampToQuantum(constImage()->matte_color.green), - ClampToQuantum(constImage()->matte_color.blue))); + return(Color(constImage()->matte_color)); } void Magick::Image::antiAlias(const bool flag_) @@ -2286,7 +2284,6 @@ void Magick::Image::colorize(const unsigned int alphaRed_, *newImage; PixelInfo - pixel, target; if (!penColor_.isValid()) @@ -2295,12 +2292,7 @@ void Magick::Image::colorize(const unsigned int alphaRed_, FormatLocaleString(blend,MaxTextExtent,"%u/%u/%u",alphaRed_,alphaGreen_, alphaBlue_); - GetPixelInfo(image(),&target); - pixel=static_cast(penColor_); - target.red=pixel.red; - target.green=pixel.green; - target.blue=pixel.blue; - target.alpha=pixel.alpha; + target=static_cast(penColor_); GetPPException; newImage=ColorizeImage(image(),blend,&target,exceptionInfo); replaceImage(newImage); @@ -2861,16 +2853,11 @@ void Magick::Image::floodFillAlpha(const ssize_t x_,const ssize_t y_, const unsigned int alpha_,const bool invert_) { PixelInfo - pixel, target; modifyImage(); - GetPixelInfo(constImage(),&target); - pixel=static_cast(pixelColor(x_,y_)); - target.red=pixel.red; - target.green=pixel.green; - target.blue=pixel.blue; + target=static_cast(pixelColor(x_,y_)); target.alpha=alpha_; GetPPException; SetPPChannelMask(AlphaChannel); @@ -2884,16 +2871,11 @@ void Magick::Image::floodFillAlpha(const ssize_t x_,const ssize_t y_, const unsigned int alpha_,const Color &target_,const bool invert_) { PixelInfo - pixel, target; modifyImage(); - GetPixelInfo(constImage(),&target); - pixel=static_cast(target_); - target.red=pixel.red; - target.green=pixel.green; - target.blue=pixel.blue; + target=static_cast(target_); target.alpha=alpha_; GetPPException; SetPPChannelMask(AlphaChannel); @@ -3295,25 +3277,12 @@ void Magick::Image::levelColors(const Color &blackColor_, { PixelInfo black, - pixel, white; modifyImage(); - GetPixelInfo(image(),&black); - pixel=static_cast(blackColor_); - black.red=pixel.red; - black.green=pixel.green; - black.blue=pixel.blue; - black.alpha=pixel.alpha; - - GetPixelInfo(image(),&white); - pixel=static_cast(whiteColor_); - white.red=pixel.red; - white.green=pixel.green; - white.blue=pixel.blue; - white.alpha=pixel.alpha; - + black=static_cast(blackColor_); + white=static_cast(whiteColor_); GetPPException; (void) LevelImageColors(image(),&black,&white,invert_ == true ? MagickTrue : MagickFalse,exceptionInfo); @@ -3325,25 +3294,12 @@ void Magick::Image::levelColorsChannel(const ChannelType channel_, { PixelInfo black, - pixel, white; modifyImage(); - GetPixelInfo(image(),&black); - pixel=static_cast(blackColor_); - black.red=pixel.red; - black.green=pixel.green; - black.blue=pixel.blue; - black.alpha=pixel.alpha; - - GetPixelInfo(image(),&white); - pixel=static_cast(whiteColor_); - white.red=pixel.red; - white.green=pixel.green; - white.blue=pixel.blue; - white.alpha=pixel.alpha; - + black=static_cast(blackColor_); + white=static_cast(whiteColor_); GetPPException; SetPPChannelMask(channel_); (void) LevelImageColors(image(),&black,&white,invert_ == true ? diff --git a/Magick++/lib/Magick++/Color.h b/Magick++/lib/Magick++/Color.h index 20c0fec74..619e4b43d 100644 --- a/Magick++/lib/Magick++/Color.h +++ b/Magick++/lib/Magick++/Color.h @@ -77,10 +77,6 @@ namespace Magick // Return color specification string operator std::string() const; - // Alpha level (range 0.0=0 to 1.0=QuantumRange) - void alpha(const double alpha_); - double alpha(void) const; - // Does object contain valid color? void isValid(const bool valid_); bool isValid(void) const; @@ -89,15 +85,19 @@ namespace Magick void quantumAlpha(const Quantum alpha_); Quantum quantumAlpha(void) const; - // Blue color (range 0 to QuantumRange) + // Black color (range 0 to QuantumRange) + void quantumBlack(const Quantum black_); + Quantum quantumBlack(void) const; + + // Blue/Yellow color (range 0 to QuantumRange) void quantumBlue(const Quantum blue_); Quantum quantumBlue(void) const; - // Green color (range 0 to QuantumRange) + // Green/Magenta color (range 0 to QuantumRange) void quantumGreen(const Quantum green_); Quantum quantumGreen(void) const; - // Red color (range 0 to QuantumRange) + // Red/Cyan color (range 0 to QuantumRange) void quantumRed(const Quantum red_); Quantum quantumRed(void) const; @@ -106,9 +106,15 @@ namespace Magick // PixelType specifies the interpretation of PixelInfo members // CYMKPixel: // Cyan = red - // Yellow = green - // Magenta = blue - // Black(K) = alpha + // Magenta = green + // Yellow = blue + // Black(K) = black + // CYMKPixel: + // Cyan = red + // Magenta = green + // Yellow = blue + // Black(K) = black + // Alpha = alpha // RGBPixel: // Red = red; // Green = green; @@ -120,7 +126,8 @@ namespace Magick // Alpha = alpha; enum PixelType { - CYMKPixel, + CMYKPixel, + CMYKAPixel, RGBPixel, RGBAPixel }; @@ -129,6 +136,9 @@ namespace Magick // Used to point Color at a pixel in an image Color(PixelInfo *rep_,PixelType pixelType_); + // Constructor to construct with PixelType + Color(PixelType pixelType_); + // Set pixel // Used to point Color at a pixel in an image void pixel(PixelInfo *rep_,PixelType pixelType_); @@ -155,6 +165,59 @@ namespace Magick // Common initializer for PixelInfo representation void initPixel(); + + // Sets the pixel type using the specified PixelInfo. + void setPixelType(const PixelInfo &color_); + }; + + class MagickPPExport ColorCMYK: public Color + { + public: + + // Default constructor + ColorCMYK(void); + + // Copy constructor + ColorCMYK(const Color &color_); + + // Construct ColorCMYK using the specified CMYK values + ColorCMYK(const double cyan_,const double magenta_,const double yellow_, + const double black_); + + // Construct ColorCMYK using the specified CMYKA values + ColorCMYK(const double cyan_,const double magenta_,const double yellow_, + const double black_,const double alpha_); + + // Destructor + ~ColorCMYK(void); + + // Assignment operator from base class + ColorCMYK& operator=(const Color& color_); + + // Alpha level (range 0 to 1.0) + void alpha(const double alpha_); + double alpha(void) const; + + // Black/Key color (range 0 to 1.0) + void black(const double black_); + double black(void) const; + + // Black/Key color (range 0.0 to 1.0) + void cyan(const double cyan_); + double cyan(void) const; + + // Magenta color (range 0 to 1.0) + void magenta(const double magenta_); + double magenta(void) const; + + // Yellow color (range 0 to 1.0) + void yellow(const double yellow_); + double yellow(void) const; + + protected: + + // Constructor to construct with PixelInfo* + ColorCMYK(PixelInfo *rep_,PixelType pixelType_); }; // @@ -277,12 +340,12 @@ namespace Magick // Copy constructor ColorRGB(const Color &color_); - // Construct ColorRGBA using the specified RGB values + // Construct ColorRGB using the specified RGB values ColorRGB(const double red_,const double green_,const double blue_); - // Construct ColorRGBA using the specified RGBA values + // Construct ColorRGB using the specified RGBA values ColorRGB(const double red_,const double green_,const double blue_, - const double alpha); + const double alpha_); // Destructor ~ColorRGB(void); @@ -290,11 +353,15 @@ namespace Magick // Assignment operator from base class ColorRGB& operator=(const Color& color_); + // Alpha level (range 0 to 1.0) + void alpha(const double alpha_); + double alpha(void) const; + // Blue color (range 0.0 to 1.0) void blue(const double blue_); double blue(void) const; - // Green color (range 0 to QuantumRange) + // Green color (range 0 to 1.0) void green(const double green_); double green(void) const; -- 2.40.0