KSquare Utilities
PixelValue.cpp
Go to the documentation of this file.
1 /* PixelValue.cpp -- Class that represents one pixel; works in conjunction with the Raster class.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include <memory>
6 #include <math.h>
7 #include <limits.h>
8 #include <fstream>
9 #include <string>
10 #include <iostream>
11 #include <vector>
12 #include "MemoryDebug.h"
13 using namespace std;
14 
15 
16 #include "PixelValue.h"
17 using namespace KKB;
18 
19 
20 
21 //****************************************
22 //* Predefined Pixel Colors *
23 //****************************************
24 PixelValue PixelValue::Aqua ( 0, 255, 255);
27 PixelValue PixelValue::Brown (165, 42, 42); // #A52A2A
28 PixelValue PixelValue::Cyan ( 0, 255, 255);
29 PixelValue PixelValue::FireBrick (176, 34, 34); // #B22222
31 PixelValue PixelValue::Indigo ( 75, 0, 130); // #4B0082
33 PixelValue PixelValue::Orange (255, 165, 0); // #FFA500
34 PixelValue PixelValue::Pink (255, 105, 180); // #FF69B4
37 PixelValue PixelValue::Teal ( 0, 128, 128); // #008080
38 PixelValue PixelValue::Violet (238, 130, 238); // #EE82EE
39 PixelValue PixelValue::White (255, 255, 255);
41 
42 
43 
45  r (0),
46  g (0),
47  b (0)
48 {
49 }
50 
51 
52 PixelValue::PixelValue (const PixelValue& pixelValue):
53  r(pixelValue.r),
54  g(pixelValue.g),
55  b(pixelValue.b)
56 {
57 }
58 
59 
61  r(_r), g(_g), b(_b)
62 {
63 }
64 
65 
67 {
68  r = right.r;
69  g = right.g;
70  b = right.b;
71  return *this;
72 }
73 
74 
75 /**
76  * Will multiply the individual color channels by the right operand and return the resultant new PixelValue.
77  */
78 PixelValue PixelValue::operator* (double fact) const
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 }
85 
86 
87 
88 bool PixelValue::operator== (const PixelValue& right) const
89 {
90  return ((r == right.r) && (g == right.b) && (b == right.b));
91 }
92 
93 
94 
95 bool PixelValue::operator!= (const PixelValue& right) const
96 {
97  return ((r != right.r) || (g != right.b) || (b != right.b));
98 }
99 
100 
101 
102 void PixelValue::ToHSI (float& hue,
103  float& sat,
104  float& intensity
105  ) const
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 */
130 
131 
132 
134  float sat,
135  float intensity
136  )
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 */
234 
235 
236 
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 }
PixelValue(const PixelValue &pixelValue)
Definition: PixelValue.cpp:52
__int32 kkint32
Definition: KKBaseTypes.h:88
static PixelValue Magenta
Definition: PixelValue.h:42
PixelValue & operator=(const PixelValue &right)
Definition: PixelValue.cpp:66
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
void ToHSI(float &hue, float &sat, float &intensity) const
Computes the equivalent HSI values; RGB -> HSI.
Definition: PixelValue.cpp:102
static PixelValue Blue
Definition: PixelValue.h:36
static PixelValue Orange
Definition: PixelValue.h:43
PixelValue()
Constructs a &#39;PixelValue&#39; instance from the the three provided values.
Definition: PixelValue.cpp:44
KKTHread * KKTHreadPtr
static PixelValue Teal
Definition: PixelValue.h:47
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
static PixelValue Aqua
Definition: PixelValue.h:34
static PixelValue Black
Definition: PixelValue.h:35
PixelValue(uchar _r, uchar _g, uchar _b)
Constructs a &#39;PixelValue&#39; instance using the provided values for the color components.
Definition: PixelValue.cpp:60
bool operator!=(const PixelValue &right) const
Definition: PixelValue.cpp:95
static PixelValue Brown
Definition: PixelValue.h:37
static PixelValue Pink
Definition: PixelValue.h:44
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
static PixelValue Indigo
Definition: PixelValue.h:41
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
static PixelValue White
Definition: PixelValue.h:49
static PixelValue Yellow
Definition: PixelValue.h:50
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
static PixelValue Purple
Definition: PixelValue.h:45
PixelValue operator*(double fact) const
Definition: PixelValue.cpp:78
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
static PixelValue Red
Definition: PixelValue.h:46
KKStr ToStr() const
Creates a displayable string reflecting the values of the three RGB components.
Definition: PixelValue.cpp:237
static PixelValue FromHSI(float hue, float sat, float intensity)
Will create an instance of PixelValue from the HSI values provided (HSI -> RGB).
Definition: PixelValue.cpp:133
static PixelValue Violet
Definition: PixelValue.h:48
static PixelValue FireBrick
Definition: PixelValue.h:39
static PixelValue Green
Definition: PixelValue.h:40
bool operator==(const PixelValue &right) const
Definition: PixelValue.cpp:88
Used by the Raster Class to represent the contents of one pixel.
Definition: PixelValue.h:22
static PixelValue Cyan
Definition: PixelValue.h:38