00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <classifiers/qualifiers.h>
00024 #include <core/exceptions/software.h>
00025 #include <fvutils/color/yuv.h>
00026
00027 #include <cstdlib>
00028
00029 using fawkes::point_t;
00030
00031 namespace firevision {
00032 #if 0
00033 }
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 Qualifier::Qualifier()
00045 {
00046 buffer_ = 0;
00047 width_ = 0;
00048 height_ = 0;
00049 size_ = 0;
00050 colorspace_ = CS_UNKNOWN;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 Qualifier::Qualifier(unsigned char* buffer, unsigned int width,
00060 unsigned int height, colorspace_t colorspace)
00061 {
00062 if (!buffer)
00063 throw fawkes::NullPointerException("Qualifier: the buffer may not be null!");
00064 if (!width || !height)
00065 throw fawkes::IllegalArgumentException("Qualifier: width and height may not be 0!");
00066
00067 set_buffer(buffer, width, height);
00068 colorspace_ = colorspace;
00069 }
00070
00071
00072
00073
00074 Qualifier::~Qualifier()
00075 {
00076 }
00077
00078
00079
00080
00081 unsigned char*
00082 Qualifier::get_buffer()
00083 {
00084 return buffer_;
00085 }
00086
00087
00088
00089
00090
00091
00092 void
00093 Qualifier::set_buffer(unsigned char* buffer, unsigned int width,
00094 unsigned int height)
00095 {
00096 buffer_ = buffer;
00097
00098 if (width)
00099 width_ = width;
00100
00101 if (height)
00102 height_ = height;
00103
00104 if (width || height)
00105 size_ = width_ * height_;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 colorspace_t
00115 Qualifier::get_colorspace()
00116 {
00117 return colorspace_;
00118 }
00119
00120
00121
00122
00123
00124 void
00125 Qualifier::set_colorspace(colorspace_t colorspace)
00126 {
00127 colorspace_ = colorspace;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 LumaQualifier::LumaQualifier(unsigned char* buffer, unsigned int width,
00150 unsigned int height, colorspace_t colorspace)
00151 :Qualifier(buffer, width, height, colorspace)
00152 {
00153 }
00154
00155
00156
00157
00158
00159
00160 int
00161 LumaQualifier::get(point_t pixel)
00162 {
00163 if (pixel.x >= width_)
00164 throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00165 if (pixel.y >= height_)
00166 throw fawkes::OutOfBoundsException("LumaQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00167
00168 return buffer_[pixel.y * width_ + pixel.x];
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 SkyblueQualifier::SkyblueQualifier(unsigned char* buffer, unsigned int width,
00190 unsigned int height, colorspace_t colorspace)
00191 :Qualifier(buffer, width, height, colorspace)
00192 {
00193 }
00194
00195
00196
00197
00198
00199
00200 int
00201 SkyblueQualifier::get(point_t pixel)
00202 {
00203 if (pixel.x >= width_)
00204 throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00205 if (pixel.y >= height_)
00206 throw fawkes::OutOfBoundsException("SkyblueQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00207
00208 unsigned int u_addr = size_ + (pixel.y * width_ + pixel.x) / 2;
00209 unsigned char u = buffer_[u_addr];
00210 unsigned char v = 255 - buffer_[u_addr + size_ / 2];
00211
00212 if ((u < threshold_) || (v < threshold_))
00213 return 0;
00214
00215 return u + v;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 YellowQualifier::YellowQualifier(unsigned char* buffer, unsigned int width,
00237 unsigned int height, colorspace_t colorspace)
00238 :Qualifier(buffer, width, height, colorspace)
00239 {
00240 }
00241
00242
00243
00244
00245
00246
00247 int
00248 YellowQualifier::get(point_t pixel)
00249 {
00250 if (pixel.x >= width_)
00251 throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.x, 0, width_);
00252 if (pixel.y >= height_)
00253 throw fawkes::OutOfBoundsException("YellowQualifier: requested Pixel is out of bounds!", pixel.y, 0, height_);
00254
00255 unsigned int y_addr = (pixel.y * width_ + pixel.x);
00256 unsigned int u_addr = size_ + y_addr / 2;
00257 unsigned char y = buffer_[y_addr];
00258 unsigned int u = (255 - buffer_[u_addr]) * y;
00259 unsigned int v = (255 - abs(127 - buffer_[u_addr + size_ / 2]) * 2) * y;
00260
00261 if ((u <= threshold_) || (v <= threshold_))
00262 return 0;
00263
00264 return (u + v);
00265 }
00266
00267 }