OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimRgbVector.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts (gpotts@remotesensing.org)
6 // Description:
7 //
8 //*************************************************************************
9 // $Id: ossimRgbVector.cpp 11347 2007-07-23 13:01:59Z gpotts $
15 #include <ossim/base/ossimCommon.h>
16 #include <math.h>
17 
19 {
20  theBuf[0] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() +
21  1.402 * (YCbCr.getCr()-128.0))));
22  theBuf[1] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() -
23  0.34414 *(YCbCr.getCb()-128.0) -
24  0.71414*(YCbCr.getCr()-128.0))));
25  theBuf[2] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() +
26  1.772 * ( YCbCr.getCb()-128.0))));
27 
28 }
29 
31 {
32  float h = hsi.getH();
33  float s = hsi.getS();
34  float i = hsi.getI();
35 
36  float r=0;
37  float g=0;
38  float b=0;
39 
40  if(h <= 120.0)
41  {
42  b = i*(1-s);
43 
44  r = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
45  g = 3*i - (r+b);
46  }
47  else if(h <= 240.0)
48  {
49  h-=120;
50 
51  r = i*(1-s);
52  g = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
53  b = 3*i - (r+g);
54  }
55  else if(h <= 360.0)
56  {
57  h-=240;
58 
59  g = i*(1-s);
60  b = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
61  r = 3*i - (g+b);
62  }
63 
64  theBuf[0] = clamp((long)(r*255));
65  theBuf[1] = clamp((long)(g*255));
66  theBuf[2] = clamp((long)(b*255));
67 }
68 
70 {
71  // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
72  // RGB are each returned on [0, 1].
73  float h = hsv.getH(), // unnormalize it
74  s = hsv.getS(),
75  v = hsv.getV();
76  float m, n, f;
77  int i;
79  {
80  theBuf[0] = clamp(ossim::round<int>(v*255));
81  theBuf[1] = clamp(ossim::round<int>(v*255));
82  theBuf[2] = clamp(ossim::round<int>(v*255));
83  }
84  else
85  {
86  h*=6.0; // unnormalize h
87  i = (int)floor(h);
88  f = h - i;
89  if(!(i & 1)) f = 1 - f; // if i is even
90  m = v * (1 - s);
91  n = v * (1 - s * f);
92  switch (i)
93  {
94  case 6:
95  case 0:
96  {
97  theBuf[0] = clamp(ossim::round<int>(v*255));
98  theBuf[1] = clamp(ossim::round<int>(n*255));
99  theBuf[2] = clamp(ossim::round<int>(m*255));
100  break;
101  }
102  case 1:
103  {
104  theBuf[0] = clamp(ossim::round<int>(n*255));
105  theBuf[1] = clamp(ossim::round<int>(v*255));
106  theBuf[2] = clamp(ossim::round<int>(m*255));
107  break;
108  }
109  case 2:
110  {
111  theBuf[0] = clamp(ossim::round<int>(m*255));
112  theBuf[1] = clamp(ossim::round<int>(v*255));
113  theBuf[2] = clamp(ossim::round<int>(n*255));
114  break;
115  }
116  case 3:
117  {
118  theBuf[0] = clamp(ossim::round<int>(m*255));
119  theBuf[1] = clamp(ossim::round<int>(n*255));
120  theBuf[2] = clamp(ossim::round<int>(v*255));
121  break;
122  }
123  case 4:
124  {
125  theBuf[0] = clamp(ossim::round<int>(n*255));
126  theBuf[1] = clamp(ossim::round<int>(m*255));
127  theBuf[2] = clamp(ossim::round<int>(v*255));
128  break;
129  }
130  case 5:
131  {
132  theBuf[0] = clamp(ossim::round<int>(v*255));
133  theBuf[1] = clamp(ossim::round<int>(m*255));
134  theBuf[2] = clamp(ossim::round<int>(n*255));
135  break;
136  }
137  }
138  }
139 }
140 
142 {
143  theBuf[0] = 255 - cmy.getC();
144  theBuf[1] = 255 - cmy.getM();
145  theBuf[2] = 255 - cmy.getY();
146 }
147 
149 {
150  // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
151  // RGB are each returned on [0, 1].
152  float h = hsv.getH(), // unnormalize it
153  s = hsv.getS(),
154  v = hsv.getV();
155  float m, n, f;
156  int i;
158  {
159  theBuf[0] = clamp(ossim::round<int>(v*255));
160  theBuf[1] = clamp(ossim::round<int>(v*255));
161  theBuf[2] = clamp(ossim::round<int>(v*255));
162  }
163  else
164  {
165  h*=6.0; // unnormalize h
166  i = (int)floor(h);
167  f = h - i;
168  if(!(i & 1)) f = 1 - f; // if i is even
169  m = v * (1 - s);
170  n = v * (1 - s * f);
171  switch (i)
172  {
173  case 6:
174  case 0:
175  {
176  theBuf[0] = clamp(ossim::round<int>(v*255));
177  theBuf[1] = clamp(ossim::round<int>(n*255));
178  theBuf[2] = clamp(ossim::round<int>(m*255));
179  break;
180  }
181  case 1:
182  {
183  theBuf[0] = clamp(ossim::round<int>(n*255));
184  theBuf[1] = clamp(ossim::round<int>(v*255));
185  theBuf[2] = clamp(ossim::round<int>(m*255));
186  break;
187  }
188  case 2:
189  {
190  theBuf[0] = clamp(ossim::round<int>(m*255));
191  theBuf[1] = clamp(ossim::round<int>(v*255));
192  theBuf[2] = clamp(ossim::round<int>(n*255));
193  break;
194  }
195  case 3:
196  {
197  theBuf[0] = clamp(ossim::round<int>(m*255));
198  theBuf[1] = clamp(ossim::round<int>(n*255));
199  theBuf[2] = clamp(ossim::round<int>(v*255));
200  break;
201  }
202  case 4:
203  {
204  theBuf[0] = clamp(ossim::round<int>(n*255));
205  theBuf[1] = clamp(ossim::round<int>(m*255));
206  theBuf[2] = clamp(ossim::round<int>(v*255));
207  break;
208  }
209  case 5:
210  {
211  theBuf[0] = clamp(ossim::round<int>(v*255));
212  theBuf[1] = clamp(ossim::round<int>(m*255));
213  theBuf[2] = clamp(ossim::round<int>(n*255));
214  break;
215  }
216  }
217  }
218  return *this;
219 }
220 
222 {
223  theBuf[0] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() +
224  1.402 * (YCbCr.getCr()-128.0))));
225  theBuf[1] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() -
226  0.34414 *(YCbCr.getCb()-128.0) -
227  0.71414*(YCbCr.getCr()-128.0))));
228  theBuf[2] = static_cast<unsigned char>(clamp(ossim::round<int>(YCbCr.getY() +
229  1.772 * ( YCbCr.getCb()-128.0))));
230 
231  return *this;
232 }
233 
235 {
236  float h = hsi.getH();
237  float s = hsi.getS();
238  float i = hsi.getI();
239 
240  float r=0;
241  float g=0;
242  float b=0;
243 
244  if(h <= 120.0)
245  {
246  b = i*(1-s);
247 
248  r = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
249  g = 3*i - (r+b);
250  }
251  else if(h <= 240.0)
252  {
253  h-=120;
254 
255  r = i*(1-s);
256  g = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
257  b = 3*i - (r+g);
258  }
259  else if(h <= 360.0)
260  {
261  h-=240;
262 
263  g = i*(1-s);
264  b = i*(1 + s*cos(RAD_PER_DEG*h)/cos((60-h)*RAD_PER_DEG));
265  r = 3*i - (g+b);
266  }
267 
268  theBuf[0] = clamp((long)(r*255));
269  theBuf[1] = clamp((long)(g*255));
270  theBuf[2] = clamp((long)(b*255));
271 
272  return *this;
273 }
274 
276 {
277  theBuf[0] = 255 - cmy.getC();
278  theBuf[1] = 255 - cmy.getM();
279  theBuf[2] = 255 - cmy.getY();
280 
281  return *this;
282 }
unsigned char theBuf[3]
ossim_float64 getS() const
ossim_float64 getH() const
static long clamp(long colorValue, unsigned char min=0, unsigned char max=255)
static const float OSSIM_HSV_UNDEFINED
float getS() const
ossimRgbVector(unsigned char r=0, unsigned char g=0, unsigned char b=0)
unsigned char getY() const
ossim_uint8 getC() const
os2<< "> n<< " > nendobj n
ossim_float64 getI() const
float getH() const
const ossimRgbVector & operator=(const ossimJpegYCbCrVector &data)
ossim_uint8 getM() const
unsigned char getCr() const
float getV() const
unsigned char getCb() const
#define RAD_PER_DEG
ossim_uint8 getY() const