Fawkes API  Fawkes Development Version
qualifiers.cpp
1 
2 /***************************************************************************
3  * qualifiers.cpp - Pixel qualifier
4  *
5  * Created: Mon Jun 09 22:54:00 2008
6  * Copyright 2008 Christof Rath <c.rath@student.tugraz.at>
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <fvclassifiers/qualifiers.h>
24 #include <core/exceptions/software.h>
25 #include <fvutils/color/yuv.h>
26 
27 #include <cstdlib>
28 
29 using fawkes::upoint_t;
30 
31 namespace firevision {
32 #if 0 /* just to make Emacs auto-indent happy */
33 }
34 #endif
35 
36 /** @class Qualifier qualifiers.h <apps/nao_loc/qualifiers.h>
37  * Abstract Qualifier for a single pixel
38  *
39  * @author Christof Rath
40  */
41 
42 /** Default constructor
43  */
45 {
46  buffer_ = 0;
47  width_ = 0;
48  height_ = 0;
49  size_ = 0;
50  colorspace_ = CS_UNKNOWN;
51 }
52 
53 /** Constructor.
54  * @param buffer containing the image
55  * @param width of the image
56  * @param height of the image
57  * @param colorspace the colorspace in action
58  */
59 Qualifier::Qualifier(unsigned char* buffer, unsigned int width,
60  unsigned int height, colorspace_t colorspace)
61 {
62  if (!buffer)
63  throw fawkes::NullPointerException("Qualifier: the buffer may not be null!");
64  if (!width || !height)
65  throw fawkes::IllegalArgumentException("Qualifier: width and height may not be 0!");
66 
67  set_buffer(buffer, width, height);
68  colorspace_ = colorspace;
69 }
70 
71 
72 /** Destructor.
73  */
75 {
76 }
77 
78 /** Get buffer.
79  * @return pointer to buffer
80  */
81 unsigned char*
83 {
84  return buffer_;
85 }
86 
87 /** buffer setter
88  * @param buffer containing the image
89  * @param width of the image (if 0 the param will be ignored)
90  * @param height of the image (if 0 the param will be ignored)
91  */
92 void
93 Qualifier::set_buffer(unsigned char* buffer, unsigned int width,
94  unsigned int height)
95 {
96  buffer_ = buffer;
97 
98  if (width)
99  width_ = width;
100 
101  if (height)
102  height_ = height;
103 
104  if (width || height)
105  size_ = width_ * height_;
106 }
107 
108 
109 
110 
111 /** Get colorspace.
112  * @return colorspace
113  */
114 colorspace_t
116 {
117  return colorspace_;
118 }
119 
120 
121 /** colorspace setter
122  * @param colorspace the colorspace in action
123  */
124 void
125 Qualifier::set_colorspace(colorspace_t colorspace)
126 {
127  colorspace_ = colorspace;
128 }
129 
130 
131 
132 
133 
134 
135 /** @class LumaQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
136  * LumaQualifier for a single pixel.
137  * Uses the value of the Y-channel
138  *
139  * @author Christof Rath
140  */
141 
142 
143 /** Constructor.
144  * @param buffer containing the image
145  * @param width of the image
146  * @param height of the image
147  * @param colorspace the colorspace in action
148  */
149 LumaQualifier::LumaQualifier(unsigned char* buffer, unsigned int width,
150  unsigned int height, colorspace_t colorspace)
151  :Qualifier(buffer, width, height, colorspace)
152 {
153 }
154 
155 
156 /** Getter.
157  * @param pixel the pixel of interest
158  * @return a corresponding int value
159  */
160 int
162 {
163  if (pixel.x >= width_)
164  throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
165  if (pixel.y >= height_)
166  throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
167 
168  return buffer_[pixel.y * width_ + pixel.x];
169 }
170 
171 
172 
173 
174 
175 
176 /** @class SkyblueQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
177  * SkyblueQualifier for a single pixel.
178  * Uses the value of the U/V-channels
179  *
180  * @author Christof Rath
181  */
182 
183 /** Constructor.
184  * @param buffer containing the image
185  * @param width of the image
186  * @param height of the image
187  * @param colorspace the colorspace in action
188  */
189 SkyblueQualifier::SkyblueQualifier(unsigned char* buffer, unsigned int width,
190  unsigned int height, colorspace_t colorspace)
191  :Qualifier(buffer, width, height, colorspace)
192 {
193 }
194 
195 
196 /** Getter.
197  * @param pixel the pixel of interest
198  * @return a corresponding int value
199  */
200 int
202 {
203  if (pixel.x >= width_)
204  throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
205  if (pixel.y >= height_)
206  throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
207 
208  unsigned int u_addr = size_ + (pixel.y * width_ + pixel.x) / 2;
209  unsigned char u = buffer_[u_addr];
210  unsigned char v = 255 - buffer_[u_addr + size_ / 2];
211 
212  if ((u < threshold_) || (v < threshold_))
213  return 0;
214 
215  return u + v;
216 }
217 
218 
219 
220 
221 
222 
223 /** @class YellowQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
224  * YellowQualifier for a single pixel.
225  * Uses the value of the U/V-channels
226  *
227  * @author Christof Rath
228  */
229 
230 /** Constructor.
231  * @param buffer containing the image
232  * @param width of the image
233  * @param height of the image
234  * @param colorspace the colorspace in action
235  */
236 YellowQualifier::YellowQualifier(unsigned char* buffer, unsigned int width,
237  unsigned int height, colorspace_t colorspace)
238  :Qualifier(buffer, width, height, colorspace)
239 {
240 }
241 
242 
243 /** Getter.
244  * @param pixel the pixel of interest
245  * @return a corresponding int value
246  */
247 int
249 {
250  if (pixel.x >= width_)
251  throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
252  if (pixel.y >= height_)
253  throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
254 
255  unsigned int y_addr = (pixel.y * width_ + pixel.x);
256  unsigned int u_addr = size_ + y_addr / 2;
257  unsigned char y = buffer_[y_addr];
258  unsigned int u = (255 - buffer_[u_addr]) * y;
259  unsigned int v = (255 - abs(127 - buffer_[u_addr + size_ / 2]) * 2) * y;
260 
261  if ((u <= threshold_) || (v <= threshold_))
262  return 0;
263 
264  return (u + v);
265 }
266 
267 } // end namespace firevision
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:201
virtual void set_colorspace(colorspace_t colorspace)
colorspace setter
Definition: qualifiers.cpp:125
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
A NULL pointer was supplied where not allowed.
Definition: software.h:34
virtual void set_buffer(unsigned char *buffer, unsigned int width=0, unsigned int height=0)
buffer setter
Definition: qualifiers.cpp:93
virtual unsigned char * get_buffer()
Get buffer.
Definition: qualifiers.cpp:82
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:161
virtual colorspace_t get_colorspace()
Get colorspace.
Definition: qualifiers.cpp:115
unsigned int size_
Size of the buffer.
Definition: qualifiers.h:67
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:248
unsigned int width_
Width of the buffer.
Definition: qualifiers.h:62
colorspace_t colorspace_
Colorspace of the buffer.
Definition: qualifiers.h:70
unsigned char * buffer_
Image buffer.
Definition: qualifiers.h:59
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
Qualifier()
Default constructor.
Definition: qualifiers.cpp:44
Index out of bounds.
Definition: software.h:88
Expected parameter is missing.
Definition: software.h:82
virtual ~Qualifier()
Destructor.
Definition: qualifiers.cpp:74
unsigned int height_
Height of the buffer.
Definition: qualifiers.h:64
Abstract Qualifier for a single pixel.
Definition: qualifiers.h:34