00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <filters/morphology/segenerator.h>
00026
00027 #include <utils/math/angle.h>
00028
00029 #include <fvutils/draw/drawer.h>
00030 #include <fvutils/color/colorspaces.h>
00031 #include <fvutils/writers/png.h>
00032 #include <fvutils/writers/fvraw.h>
00033
00034 #include <cstdlib>
00035 #include <cstring>
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 unsigned char *
00055 SEGenerator::linear(unsigned int width, unsigned int height,
00056 unsigned int *proposed_center_x, unsigned int *proposed_center_y,
00057 float slope_angle_rad)
00058 {
00059
00060
00061
00062
00063
00064
00065
00066 if ( height == 0 ) return NULL;
00067 if ( width == 0) return NULL;
00068
00069
00070 unsigned char *tmp = (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, width, height));
00071 memset(tmp, 0, colorspace_buffer_size(YUV422_PLANAR, width, height));
00072 Drawer *d = new Drawer();
00073 d->set_buffer(tmp, width, height);
00074 d->set_color(1, 0, 0);
00075
00076 float a = fawkes::normalize_mirror_rad( slope_angle_rad );
00077
00078 if ( (a == M_PI/2) || (a == -M_PI/2) ) {
00079
00080
00081 d->draw_line(0, 0, 0, height - 1);
00082 } else {
00083
00084
00085 if ( a > M_PI / 2) a -= M_PI;
00086 if ( a < - M_PI / 2) a += M_PI;
00087
00088 int y = (int)roundf(((float)width - 1.f) * tan( a ));
00089
00090 if ( y < 0) {
00091
00092 d->draw_line( 0, 0, width - 1, -y );
00093 } else {
00094
00095 d->draw_line( 0, y, width - 1, 0 );
00096 }
00097 }
00098
00099 delete d;
00100
00101 unsigned char *se = (unsigned char *)malloc(width * height);
00102 memcpy(se, tmp, width * height);
00103
00104 PNGWriter *png = new PNGWriter();
00105 png->set_dimensions( width, height );
00106 png->set_buffer(YUV422_PLANAR, tmp);
00107 png->set_filename("se_test.png");
00108 png->write();
00109 delete png;
00110
00111 FvRawWriter *fvraw = new FvRawWriter("se_test.raw", width, height, YUV422_PLANAR, tmp);
00112 fvraw->write();
00113 delete fvraw;
00114
00115 free( tmp );
00116
00117 if ( (proposed_center_x != NULL) && (proposed_center_y != NULL) ) {
00118 unsigned int min_x = width;
00119 unsigned int max_x = 0;
00120 unsigned int min_y = height;
00121 unsigned int max_y = 0;
00122 for (unsigned int h = 0; h < height; ++h) {
00123 for (unsigned int w = 0; w < width; ++w) {
00124 if ( se[ h * width + w ] != 0 ) {
00125 if ( w < min_x ) min_x = w;
00126 if ( w > max_x ) max_x = w;
00127 if ( h < min_y ) min_y = h;
00128 if ( h > max_y ) max_y = h;
00129 }
00130 }
00131 }
00132
00133 *proposed_center_x = min_x + (max_x - min_x) / 2;
00134 *proposed_center_y = min_y + (max_y - min_y) / 2;
00135 }
00136
00137 return se;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 unsigned char *
00148 SEGenerator::square(unsigned int width, unsigned int height)
00149 {
00150 unsigned char *se = (unsigned char *)malloc(width * height);
00151 memset(se, 1, width * height);
00152 return se;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 void
00164 SEGenerator::drawSE(unsigned char *yuv422planar_buffer, unsigned char *mask, unsigned int width, unsigned int height)
00165 {
00166 memset(yuv422planar_buffer, 128, colorspace_buffer_size(YUV422_PLANAR, width, height) );
00167 for (unsigned int h = 0; h < height; ++h) {
00168 for (unsigned int w = 0; w < width; ++w) {
00169 if ( mask[ h * width + w ] != 0 ) {
00170 yuv422planar_buffer[ h * width + w ] = 255;
00171 }
00172 }
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 void
00185 SEGenerator::drawSEbw(unsigned char *yuv422planar_buffer, unsigned char *mask, unsigned int width, unsigned int height)
00186 {
00187 memset(yuv422planar_buffer, 128, colorspace_buffer_size(YUV422_PLANAR, width, height) );
00188 memset(yuv422planar_buffer, 255, width * height );
00189 for (unsigned int h = 0; h < height; ++h) {
00190 for (unsigned int w = 0; w < width; ++w) {
00191 if ( mask[ h * width + w ] != 0 ) {
00192 yuv422planar_buffer[ h * width + w ] = 0;
00193 }
00194 }
00195 }
00196 }