KSquare Utilities
KKB::PixelValue Class Reference

Used by the Raster Class to represent the contents of one pixel. More...

#include <PixelValue.h>

Public Member Functions

 PixelValue ()
 Constructs a 'PixelValue' instance from the the three provided values. More...
 
 PixelValue (uchar _r, uchar _g, uchar _b)
 Constructs a 'PixelValue' instance using the provided values for the color components. More...
 
 PixelValue (const PixelValue &pixelValue)
 
bool operator!= (const PixelValue &right) const
 
PixelValue operator* (double fact) const
 
PixelValueoperator= (const PixelValue &right)
 
bool operator== (const PixelValue &right) const
 
void ToHSI (float &hue, float &sat, float &intensity) const
 Computes the equivalent HSI values; RGB -> HSI. More...
 
KKStr ToStr () const
 Creates a displayable string reflecting the values of the three RGB components. More...
 

Static Public Member Functions

static PixelValue FromHSI (float hue, float sat, float intensity)
 Will create an instance of PixelValue from the HSI values provided (HSI -> RGB). More...
 

Public Attributes

uchar b
 
uchar g
 
uchar r
 

Static Public Attributes

static PixelValue Aqua
 
static PixelValue Black
 
static PixelValue Blue
 
static PixelValue Brown
 
static PixelValue Cyan
 
static PixelValue FireBrick
 
static PixelValue Green
 
static PixelValue Indigo
 
static PixelValue Magenta
 
static PixelValue Orange
 
static PixelValue Pink
 
static PixelValue Purple
 
static PixelValue Red
 
static PixelValue Teal
 
static PixelValue Violet
 
static PixelValue White
 
static PixelValue Yellow
 

Detailed Description

Used by the Raster Class to represent the contents of one pixel.

There are several predefined colors to pick from or you can work with the individual color channels separately.

See also
Raster

Definition at line 22 of file PixelValue.h.

Constructor & Destructor Documentation

PixelValue::PixelValue ( )

Constructs a 'PixelValue' instance from the the three provided values.

Definition at line 44 of file PixelValue.cpp.

References b, g, and r.

44  :
45  r (0),
46  g (0),
47  b (0)
48 {
49 }
PixelValue::PixelValue ( uchar  _r,
uchar  _g,
uchar  _b 
)

Constructs a 'PixelValue' instance using the provided values for the color components.

Definition at line 60 of file PixelValue.cpp.

References b, g, and r.

Referenced by KKB::SegmentorOTSU::ClassAverageRGB(), KKB::Chart::CreateRaster(), FromHSI(), operator*(), and KKB::Raster::ThresholdInHSI().

60  :
61  r(_r), g(_g), b(_b)
62 {
63 }
PixelValue::PixelValue ( const PixelValue pixelValue)

Definition at line 52 of file PixelValue.cpp.

References b, g, and r.

Referenced by KKB::Chart::Series::Series().

52  :
53  r(pixelValue.r),
54  g(pixelValue.g),
55  b(pixelValue.b)
56 {
57 }

Member Function Documentation

PixelValue PixelValue::FromHSI ( float  hue,
float  sat,
float  intensity 
)
static

Will create an instance of PixelValue from the HSI values provided (HSI -> RGB).

Used "http://www.codeguru.com/forum/archive/index.php/t-134892.html" for inspiration.

Parameters
[in]hueAngle between 0 and (2 x Pi).
[in]satSaturation of color, a number between 0.0 and 1.0.
[in]intensityIntensity.

Definition at line 133 of file PixelValue.cpp.

References Black, and PixelValue().

Referenced by KKB::Raster::CreateColor(), and KKB::Raster::CreateColorImageFromLabels().

137 {
138  // Used "http://www.codeguru.com/forum/archive/index.php/t-134892.html" for inspiration.
139  if (intensity == 0.0f)
140  return PixelValue::Black;
141 
142  if (sat == 0.0f)
143  {
144  // gray-scale image
145  kkint32 greyValue = (kkint32)(0.5f + intensity * 255.0f);
146  uchar gv = (uchar)greyValue;
147  return PixelValue (gv, gv, gv);
148  }
149 
150  float domainOffset = 0.0f;
151  float red = 0.0, green = 0.0, blue = 0.0;
152  if (hue < (1.0f / 6.0f))
153  {
154  //domainOffset = H;
155  //R = I;
156  //B = I * (1-S);
157  //G = B + (I-B)*domainOffset*6;
158  domainOffset = hue;
159  red = intensity;
160  blue = intensity * (1.0f - sat);
161  green = blue + (intensity - blue) * domainOffset * 6.0f;
162  }
163 
164  else if (hue < (2.0f / 6.0f))
165  {
166  //domainOffset = H - 1.0/6.0;
167  //G = I;
168  //B = I * (1-S);
169  //R = G - (I-B)*domainOffset*6;
170 
171  // yellow domain; red ascending
172  domainOffset = hue - 1.0f / 6.0f;
173  green = intensity;
174  blue = intensity * (1.0f - sat);
175  red = green - (intensity - blue) * domainOffset * 6.0f;
176  }
177 
178  else if (hue < (3.0f / 6.0f))
179  {
180  //domainOffset = H-2.0/6;
181  //G = I;
182  //R = I * (1-S);
183  //B = R + (I-R)*domainOffset * 6; // green domain; blue descending
184  domainOffset = hue - (2.0f / 6.0f);
185  green = intensity;
186  red = intensity * (1.0f - sat);
187  blue = red - (intensity - red) * domainOffset * 6.0f;
188  }
189 
190  else if (hue < (4.0f / 6.0f))
191  {
192  //domainOffset = H - 3.0/6;
193  //B = I;
194  //R = I * (1-S);
195  //G = B - (I-R) * domainOffset * 6;
196  // cyan domain, green ascending
197  domainOffset = hue - (3.0f / 6.0f);
198  blue = intensity;
199  red = intensity * (1.0f - sat);
200  green = blue - (intensity - red) * domainOffset * 6.0f;
201  }
202 
203  else if (hue < (5.0f / 6.0f))
204  {
205  //domainOffset = H - 4.0/6;
206  //B = I;
207  //G = I * (1-S);
208  //R = G + (I-G) * domainOffset * 6;
209  // blue domain, red ascending
210  domainOffset = hue - (4.0f / 6.0f);
211  blue = intensity;
212  green = intensity * (1.0f - sat);
213  red = green + (intensity - green) * domainOffset * 6.0f;
214  }
215 
216  else
217  {
218  //domainOffset = H - 5.0/6;
219  //R = I;
220  //G = I * (1-S);
221  //B = R - (I-G) * domainOffset * 6;
222  // Magenta domain, blue descending
223  domainOffset = hue - (5.0f / 6.0f);
224  red = intensity;
225  green = intensity * (1.0f - sat);
226  blue = red - (intensity - green) * domainOffset * 6.0f;
227  }
228 
229  return PixelValue ((uchar)(0.5f + red * 255.0f),
230  (uchar)(0.5f + green * 255.0f),
231  (uchar)(0.5f + blue * 255.0f)
232  );
233 } /* FromHSI */
__int32 kkint32
Definition: KKBaseTypes.h:88
PixelValue()
Constructs a &#39;PixelValue&#39; instance from the the three provided values.
Definition: PixelValue.cpp:44
static PixelValue Black
Definition: PixelValue.h:35
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
bool PixelValue::operator!= ( const PixelValue right) const

Definition at line 95 of file PixelValue.cpp.

References b, g, and r.

96 {
97  return ((r != right.r) || (g != right.b) || (b != right.b));
98 }
PixelValue PixelValue::operator* ( double  fact) const

Will multiply the individual color channels by the right operand and return the resultant new PixelValue.

Definition at line 78 of file PixelValue.cpp.

References b, g, PixelValue(), and r.

79 {
80  uchar newR = (uchar)((double)r * fact + 0.5);
81  uchar newG = (uchar)((double)g * fact + 0.5);
82  uchar newB = (uchar)((double)b * fact + 0.5);
83  return PixelValue (newR, newG, newB);
84 }
PixelValue()
Constructs a &#39;PixelValue&#39; instance from the the three provided values.
Definition: PixelValue.cpp:44
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
PixelValue & PixelValue::operator= ( const PixelValue right)

Definition at line 66 of file PixelValue.cpp.

References b, g, and r.

Referenced by KKB::Raster::CreateColor(), KKB::Raster::CreateColorImageFromLabels(), and KKB::Raster::CreateColorWithBlobsLabeldByColor().

67 {
68  r = right.r;
69  g = right.g;
70  b = right.b;
71  return *this;
72 }
bool PixelValue::operator== ( const PixelValue right) const

Definition at line 88 of file PixelValue.cpp.

References b, g, and r.

89 {
90  return ((r == right.r) && (g == right.b) && (b == right.b));
91 }
void PixelValue::ToHSI ( float &  hue,
float &  sat,
float &  intensity 
) const

Computes the equivalent HSI values; RGB -> HSI.

Used "http://www.codeguru.com/forum/archive/index.php/t-134892.html" for inspiration.

Parameters
[out]hueAngle in radians.
[out]satSaturation between 0.0 and 1.0.
[out]intensityIntensity between 0.0 and 1.0.

Definition at line 102 of file PixelValue.cpp.

References b, g, and r.

Referenced by KKB::Raster::ThresholdInHSI().

106 {
107  float redF = (float)r / 255.0f;
108  float greenF = (float)g / 255.0f;
109  float blueF = (float)b / 255.0f;
110 
111  if (blueF <= greenF)
112  {
113  // compute hue with this formula
114  hue = (float)acos ((0.5 * ((redF - greenF) + (redF - blueF))) / sqrt ((pow ((redF - greenF), 2) + (redF - blueF) * (greenF - blueF))));
115  }
116 
117  // Otherwise
118  else
119  {
120  // compute hue with this formula
121  hue = (float)(2.0f * 3.1415926535897932384626433832795 - acos ((0.5 * ((redF - greenF) + (redF - blueF)))) / sqrt ((pow ((redF - greenF), 2) + (redF - blueF) * (greenF - blueF))));
122  }
123 
124  // compute saturation
125  sat = 1 - 3 * min (min (redF, greenF), blueF) / (redF + greenF + blueF);
126 
127  // compute intensity
128  intensity = (redF + greenF + blueF) / 3;
129 } /* ToHSI */
#define min(X, Y)
Definition: UsfCasCor.h:244
KKStr PixelValue::ToStr ( ) const

Creates a displayable string reflecting the values of the three RGB components.

Definition at line 237 of file PixelValue.cpp.

References b, KKB::KKStr::Concat(), g, KKB::KKStr::operator+(), KKB::operator+(), r, and KKB::StrFormatInt().

238 {
239  KKStr s = "(" + KKB::StrFormatInt ((kkint32)r, "ZZ0") + ","
240  + KKB::StrFormatInt ((kkint32)g, "ZZ0") + ","
241  + KKB::StrFormatInt ((kkint32)b, "ZZ0") + ")";
242  return s;
243 }
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004

Member Data Documentation

PixelValue PixelValue::Aqua
static

Definition at line 34 of file PixelValue.h.

PixelValue PixelValue::Black
static

Definition at line 35 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorImageFromLabels(), and FromHSI().

PixelValue PixelValue::Blue
static

Definition at line 36 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Brown
static

Definition at line 37 of file PixelValue.h.

PixelValue PixelValue::Cyan
static

Definition at line 38 of file PixelValue.h.

PixelValue PixelValue::FireBrick
static

Definition at line 39 of file PixelValue.h.

PixelValue PixelValue::Green
static

Definition at line 40 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Indigo
static

Definition at line 41 of file PixelValue.h.

PixelValue PixelValue::Magenta
static

Definition at line 42 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Orange
static

Definition at line 43 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Pink
static

Definition at line 44 of file PixelValue.h.

PixelValue PixelValue::Purple
static

Definition at line 45 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Red
static

Definition at line 46 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Teal
static

Definition at line 47 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().

PixelValue PixelValue::Violet
static

Definition at line 48 of file PixelValue.h.

PixelValue PixelValue::White
static

Definition at line 49 of file PixelValue.h.

Referenced by KKB::Raster::CreateColor().

PixelValue PixelValue::Yellow
static

Definition at line 50 of file PixelValue.h.

Referenced by KKB::Raster::CreateColorWithBlobsLabeldByColor().


The documentation for this class was generated from the following files: