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 <core/exceptions/software.h>
24#include <fvclassifiers/qualifiers.h>
25#include <fvutils/color/yuv.h>
26
27#include <cstdlib>
28
30
31namespace firevision {
32
33/** @class Qualifier qualifiers.h <apps/nao_loc/qualifiers.h>
34 * Abstract Qualifier for a single pixel
35 *
36 * @author Christof Rath
37 */
38
39/** Default constructor
40 */
42{
43 buffer_ = 0;
44 width_ = 0;
45 height_ = 0;
46 size_ = 0;
47 colorspace_ = CS_UNKNOWN;
48}
49
50/** Constructor.
51 * @param buffer containing the image
52 * @param width of the image
53 * @param height of the image
54 * @param colorspace the colorspace in action
55 */
56Qualifier::Qualifier(unsigned char *buffer,
57 unsigned int width,
58 unsigned int height,
59 colorspace_t colorspace)
60{
61 if (!buffer)
62 throw fawkes::NullPointerException("Qualifier: the buffer may not be null!");
63 if (!width || !height)
64 throw fawkes::IllegalArgumentException("Qualifier: width and height may not be 0!");
65
66 set_buffer(buffer, width, height);
67 colorspace_ = colorspace;
68}
69
70/** Destructor.
71 */
73{
74}
75
76/** Get buffer.
77 * @return pointer to buffer
78 */
79unsigned char *
81{
82 return buffer_;
83}
84
85/** buffer setter
86 * @param buffer containing the image
87 * @param width of the image (if 0 the param will be ignored)
88 * @param height of the image (if 0 the param will be ignored)
89 */
90void
91Qualifier::set_buffer(unsigned char *buffer, unsigned int width, unsigned int height)
92{
93 buffer_ = buffer;
94
95 if (width)
96 width_ = width;
97
98 if (height)
99 height_ = height;
100
101 if (width || height)
102 size_ = width_ * height_;
103}
104
105/** Get colorspace.
106 * @return colorspace
107 */
108colorspace_t
110{
111 return colorspace_;
112}
113
114/** colorspace setter
115 * @param colorspace the colorspace in action
116 */
117void
118Qualifier::set_colorspace(colorspace_t colorspace)
119{
120 colorspace_ = colorspace;
121}
122
123/** @class LumaQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
124 * LumaQualifier for a single pixel.
125 * Uses the value of the Y-channel
126 *
127 * @author Christof Rath
128 */
129
130/** Constructor.
131 * @param buffer containing the image
132 * @param width of the image
133 * @param height of the image
134 * @param colorspace the colorspace in action
135 */
136LumaQualifier::LumaQualifier(unsigned char *buffer,
137 unsigned int width,
138 unsigned int height,
139 colorspace_t colorspace)
140: Qualifier(buffer, width, height, colorspace)
141{
142}
143
144/** Getter.
145 * @param pixel the pixel of interest
146 * @return a corresponding int value
147 */
148int
150{
151 if (pixel.x >= width_)
152 throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!",
153 pixel.x,
154 0,
155 width_);
156 if (pixel.y >= height_)
157 throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!",
158 pixel.y,
159 0,
160 height_);
161
162 return buffer_[pixel.y * width_ + pixel.x];
163}
164
165/** @class SkyblueQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
166 * SkyblueQualifier for a single pixel.
167 * Uses the value of the U/V-channels
168 *
169 * @author Christof Rath
170 */
171
172/** Constructor.
173 * @param buffer containing the image
174 * @param width of the image
175 * @param height of the image
176 * @param colorspace the colorspace in action
177 */
178SkyblueQualifier::SkyblueQualifier(unsigned char *buffer,
179 unsigned int width,
180 unsigned int height,
181 colorspace_t colorspace)
182: Qualifier(buffer, width, height, colorspace)
183{
184}
185
186/** Getter.
187 * @param pixel the pixel of interest
188 * @return a corresponding int value
189 */
190int
192{
193 if (pixel.x >= width_)
194 throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!",
195 pixel.x,
196 0,
197 width_);
198 if (pixel.y >= height_)
199 throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!",
200 pixel.y,
201 0,
202 height_);
203
204 unsigned int u_addr = size_ + (pixel.y * width_ + pixel.x) / 2;
205 unsigned char u = buffer_[u_addr];
206 unsigned char v = 255 - buffer_[u_addr + size_ / 2];
207
208 if ((u < threshold_) || (v < threshold_))
209 return 0;
210
211 return u + v;
212}
213
214/** @class YellowQualifier qualifiers.h <apps/nao_loc/qualifiers.h>
215 * YellowQualifier for a single pixel.
216 * Uses the value of the U/V-channels
217 *
218 * @author Christof Rath
219 */
220
221/** Constructor.
222 * @param buffer containing the image
223 * @param width of the image
224 * @param height of the image
225 * @param colorspace the colorspace in action
226 */
227YellowQualifier::YellowQualifier(unsigned char *buffer,
228 unsigned int width,
229 unsigned int height,
230 colorspace_t colorspace)
231: Qualifier(buffer, width, height, colorspace)
232{
233}
234
235/** Getter.
236 * @param pixel the pixel of interest
237 * @return a corresponding int value
238 */
239int
241{
242 if (pixel.x >= width_)
243 throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!",
244 pixel.x,
245 0,
246 width_);
247 if (pixel.y >= height_)
248 throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!",
249 pixel.y,
250 0,
251 height_);
252
253 unsigned int y_addr = (pixel.y * width_ + pixel.x);
254 unsigned int u_addr = size_ + y_addr / 2;
255 unsigned char y = buffer_[y_addr];
256 unsigned int u = (255 - buffer_[u_addr]) * y;
257 unsigned int v = (255 - abs(127 - buffer_[u_addr + size_ / 2]) * 2) * y;
258
259 if ((u <= threshold_) || (v <= threshold_))
260 return 0;
261
262 return (u + v);
263}
264
265} // end namespace firevision
Expected parameter is missing.
Definition: software.h:80
A NULL pointer was supplied where not allowed.
Definition: software.h:32
Index out of bounds.
Definition: software.h:86
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:149
Abstract Qualifier for a single pixel.
Definition: qualifiers.h:31
unsigned int size_
Size of the buffer.
Definition: qualifiers.h:63
Qualifier()
Default constructor.
Definition: qualifiers.cpp:41
unsigned int width_
Width of the buffer.
Definition: qualifiers.h:58
virtual colorspace_t get_colorspace()
Get colorspace.
Definition: qualifiers.cpp:109
unsigned int height_
Height of the buffer.
Definition: qualifiers.h:60
virtual unsigned char * get_buffer()
Get buffer.
Definition: qualifiers.cpp:80
colorspace_t colorspace_
Colorspace of the buffer.
Definition: qualifiers.h:66
virtual void set_colorspace(colorspace_t colorspace)
colorspace setter
Definition: qualifiers.cpp:118
virtual void set_buffer(unsigned char *buffer, unsigned int width=0, unsigned int height=0)
buffer setter
Definition: qualifiers.cpp:91
unsigned char * buffer_
Image buffer.
Definition: qualifiers.h:55
virtual ~Qualifier()
Destructor.
Definition: qualifiers.cpp:72
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:191
virtual int get(fawkes::upoint_t pixel)
Getter.
Definition: qualifiers.cpp:240
Point with cartesian coordinates as unsigned integers.
Definition: types.h:35
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37