Fawkes API  Fawkes Development Version
roidraw.cpp
1 
2 /***************************************************************************
3  * roidraw.cpp - Implementation of ROI draw filter
4  *
5  * Created: Thu Jul 14 16:01:37 2005
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvfilters/roidraw.h>
25 #include <fvutils/color/color_object_map.h>
26 #include <fvutils/draw/drawer.h>
27 
28 #include <cstddef>
29 
30 namespace firevision {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class FilterROIDraw <fvfilters/roidraw.h>
36  * ROI Drawing filter.
37  * This filter visually marks the given region of interest.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param rois optional list of ROIs to draw additionally to the dst_roi
43  * @param style optional border style (default is INVERTED)
44  */
45 FilterROIDraw::FilterROIDraw(const std::list<ROI> *rois, border_style_t style)
46  : Filter("FilterROIDraw"),
47  __rois(rois),
48  __border_style(style)
49 {
50  __drawer = new Drawer;
51 }
52 
53 /** Destructor */
55  delete __drawer;
56 }
57 
58 void
59 FilterROIDraw::draw_roi(const ROI *roi)
60 {
61  if (__border_style == DASHED_HINT) {
62  YUV_t hint_color = ColorObjectMap::get_color(roi->color);
63  __drawer->set_buffer(dst, roi->image_width, roi->image_height);
64  bool draw_black = false;
65  fawkes::upoint_t end;
66  end.x = std::min(roi->image_width - 1, roi->start.x + roi->width);
67  end.y = std::min(roi->image_height - 1, roi->start.y + roi->height);
68 
69  //Top and bottom line
70  for (unsigned int x = roi->start.x; x <= end.x ; ++x) {
71  if (!(x % 2)) {
72  __drawer->set_color(draw_black ? YUV_t::black() : hint_color);
73  draw_black = !draw_black;
74  }
75 
76  __drawer->color_point(x, roi->start.y);
77  __drawer->color_point(x, end.y);
78  }
79 
80  //Side lines
81  for (unsigned int y = roi->start.y; y <= end.y; ++y) {
82  if (!(y % 2)) {
83  __drawer->set_color(draw_black ? YUV_t::black() : hint_color);
84  draw_black = !draw_black;
85  }
86 
87  __drawer->color_point(roi->start.x, y);
88  __drawer->color_point(end.x, y);
89  }
90  }
91  else {
92  // destination y-plane
93  unsigned char *dyp = dst + (roi->start.y * roi->line_step) + (roi->start.x * roi->pixel_step);
94 
95  // line starts
96  unsigned char *ldyp = dyp; // destination y-plane
97 
98  // top border
99  for (unsigned int i = roi->start.x; i < (roi->start.x + roi->width); ++i) {
100  *dyp = 255 - *dyp;
101  dyp++;
102  }
103 
104  // left and right borders
105  for (unsigned int i = roi->start.y; i < (roi->start.y + roi->height); ++i) {
106  ldyp += roi->line_step;
107  dyp = ldyp;
108  *dyp = 255 - *dyp;
109  dyp += roi->width;
110  *dyp = 255 - *dyp;
111  }
112  ldyp += roi->line_step;
113  dyp = ldyp;
114 
115  // bottom border
116  for (unsigned int i = roi->start.x; i < (roi->start.x + roi->width); ++i) {
117  *dyp = 255 - *dyp;
118  dyp++;
119  }
120  }
121 }
122 
123 void
125 {
126  if ( dst_roi ) {
127  draw_roi(dst_roi);
128  }
129  if ( __rois ) {
130  for (std::list<ROI>::const_iterator r = __rois->begin(); r != __rois->end(); ++r) {
131  draw_roi(&(*r));
132  }
133  }
134 }
135 
136 
137 /** Set ROIs.
138  * Set a list of ROIs. The list must persist as long as the filter is applied with
139  * this list. Set to NULL to have it ignored again.
140  * @param rois list of ROIs to draw additionally to the dst_roi.
141  */
142 void
143 FilterROIDraw::set_rois(const std::list<ROI> *rois)
144 {
145  __rois = rois;
146 }
147 
148 
149 /** Sets the preferred style
150  * @param style The preferred style
151  */
152 void
154 {
155  __border_style = style;
156 }
157 
158 } // end namespace firevision
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
Draw to an image.
Definition: drawer.h:34
unsigned int width
ROI width.
Definition: roi.h:121
Region of interest.
Definition: roi.h:58
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
static YUV_t_struct black()
Definition: yuv.h:77
Displays border dashed black and color of hint.
Definition: roidraw.h:44
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
void set_buffer(unsigned char *buffer, unsigned int width, unsigned int height)
Set the buffer to draw to.
Definition: drawer.cpp:62
Filter interface.
Definition: filter.h:35
FilterROIDraw(const std::list< ROI > *rois=0, border_style_t style=INVERTED)
Constructor.
Definition: roidraw.cpp:45
virtual ~FilterROIDraw()
Destructor.
Definition: roidraw.cpp:54
virtual void apply()
Apply the filter.
Definition: roidraw.cpp:124
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
unsigned int height
ROI height.
Definition: roi.h:123
border_style_t
Defines the possible border styles to display a ROI.
Definition: roidraw.h:42
unsigned int line_step
line step
Definition: roi.h:129
YUV pixel.
Definition: yuv.h:59
void color_point(unsigned int x, unsigned int y)
Color the given point.
Definition: drawer.cpp:336
unsigned char * dst
Destination buffer.
Definition: filter.h:67
static YUV_t get_color(color_t color)
YUV_t getter.
unsigned int pixel_step
pixel step
Definition: roi.h:131
color_t color
ROI primary color.
Definition: roi.h:136
ROI * dst_roi
Destination ROI.
Definition: filter.h:72
void set_rois(const std::list< ROI > *rois)
Set ROIs.
Definition: roidraw.cpp:143
void set_style(border_style_t style)
Sets the preferred style.
Definition: roidraw.cpp:153
void set_color(unsigned char y, unsigned char u, unsigned char v)
Set drawing color.
Definition: drawer.cpp:77