00001 #include "image.h"
00002 #include "rgba.h"
00003 #include <string>
00004
00005 #ifndef COLOUR_PROPERTIES_TOOL_H
00006 #define COLOUR_PROPERTIES_TOOL_H
00007
00008 namespace mimas {
00017 template <typename T>
00018 class colour_properties_tool : public object
00019 {
00020 private:
00021 void interpolate(image< rgba< T > > &orig, image< rgba< T > > &ref, double weight);
00022
00023 public:
00024 colour_properties_tool(void) {}
00025 void brightness(image< rgba< T > > &im, double brightval);
00026 void saturation(image< rgba< T > > &im, double satval);
00027 void contrast(image< rgba< T > > &im, double contval);
00028 void setChannel(image< rgba< T > > &im, T val, const char *channel);
00029
00030 };
00031
00032 template <typename T>
00033 void colour_properties_tool<T>::interpolate(image< rgba< T > > &orig, image< rgba< T > > &ref, double weight)
00034 {
00035 double outr, outg, outb;
00036
00037 for (int j=0; j<orig.getHeight(); j++)
00038 for (int i=0; i<orig.getWidth(); i++)
00039 {
00040 rgba< T > *origPixel = &orig.pixel( i, j );
00041 const rgba< T > *refPixel = &ref.pixel( i, j );
00042 outr=(1.0-weight)*(double)refPixel->getRed() + weight*(double)origPixel->getRed();
00043 outg=(1.0-weight)*(double)refPixel->getGreen() + weight*(double)origPixel->getGreen();
00044 outb=(1.0-weight)*(double)refPixel->getBlue() + weight*(double)origPixel->getBlue();
00045
00046 outr=(outr>(T)255)?(T)255:outr;
00047 outg=(outg>(T)255)?(T)255:outg;
00048 outb=(outb>(T)255)?(T)255:outb;
00049 outr=(outr<(T)0)?(T)0:outr;
00050 outg=(outg<(T)0)?(T)0:outg;
00051 outb=(outb<(T)0)?(T)0:outb;
00052
00053 origPixel->setRed((T)outr);
00054 origPixel->setGreen((T)outg);
00055 origPixel->setBlue((T)outb);
00056 }
00057 }
00058
00059
00060 template <typename T>
00061 void colour_properties_tool<T>::brightness(image< rgba< T > > &im, double weight)
00062 {
00063 image< rgba< T > > ref;
00064 double w;
00065
00066
00067
00068
00069 rgba< T > initVal;
00070
00071 if (weight>=0.0)
00072 {
00073 w=1.0-weight;
00074 initVal = rgba< T >( 255, 255, 255 );
00075 }
00076 else
00077 {
00078 w=1.0+weight;
00079 initVal = rgba< T >( 0, 0, 0 );
00080 }
00081
00082 ref.init(im.getWidth(),im.getHeight());
00083 ref.fill( initVal );
00084 interpolate(im,ref,w);
00085 }
00086
00087 template <typename T>
00088 void colour_properties_tool<T>::saturation(image< rgba< T > > &im, double weight)
00089 {
00090 image< rgba< T > > ref;
00091 double w;
00092
00093 w=weight+1.0;
00094
00095 ref=im;
00096
00097 double gray;
00098 for (int j=0; j<ref.getHeight(); j++)
00099 for (int i=0; i<ref.getWidth(); i++)
00100 {
00101 const rgba< T > *pixel = &ref.pixel( i, j );
00102 gray = (double)( pixel->getRed() +
00103 pixel->getGreen() +
00104 pixel->getBlue() )/3.0;
00105 ref.pixel( i, j ) = rgba< T >( gray, gray, gray );
00106 }
00107
00108 interpolate(im,ref,w);
00109
00110 }
00111
00112 template <typename T>
00113 void colour_properties_tool<T>::contrast(image< rgba< T > > &im, double weight)
00114 {
00115 image< rgba< T > > ref;
00116 double w;
00117
00118 w=weight+1.0;
00119
00120 ref.init(im.getWidth(),im.getHeight());
00121 ref.fill( rgba< T >( 127, 127, 127 ) );
00122
00123 interpolate(im,ref,w);
00124
00125 }
00126
00127 template <typename T>
00128 void colour_properties_tool<T>::setChannel(image< rgba< T > > &im, T val, const char *channel)
00129 {
00130 bool R,G,B,A;
00131 typedef std::string string;
00132 string c(channel);
00133
00134 R=c.find("R")!=string::npos?true:false;
00135 G=c.find("G")!=string::npos?true:false;
00136 B=c.find("B")!=string::npos?true:false;
00137 A=c.find("A")!=string::npos?true:false;
00138
00139
00140 MMERROR( (int)(R+G+B+A) == (signed)c.length(), mimasexception, ,
00141 "colour_properties_tool<T>::zeroChannel() - illegal channel "
00142 "string" );
00143
00144 for (int j=0; j<im.getHeight(); j++)
00145 for (int i=0; i<im.getWidth(); i++)
00146 {
00147 rgba< T > *pixel = &im.pixel( i, j );
00148 if (R) pixel->setRed( (T)val );
00149 if (G) pixel->setGreen( (T)val );
00150 if (B) pixel->setBlue( (T)val );
00151 if (A) pixel->setAlpha( (T)val );
00152 }
00153 }
00154
00155 }
00156
00157 #endif