roidraw.cpp

00001
00002 /***************************************************************************
00003  *  roidraw.cpp - Implementation of ROI draw filter
00004  *
00005  *  Created: Thu Jul 14 16:01:37 2005
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <filters/roidraw.h>
00025 #include <fvutils/color/color_object_map.h>
00026 #include <fvutils/draw/drawer.h>
00027
00028 #include <cstddef>
00029 
00030 /** @class FilterROIDraw <filters/roidraw.h>
00031  * ROI Drawing filter.
00032  * This filter visually marks the given region of interest.
00033  * @author Tim Niemueller
00034  */
00035 
00036 /** Constructor.
00037  * @param rois optional list of ROIs to draw additionally to the dst_roi
00038  * @param style optional border style (default is INVERTED)
00039  */
00040 FilterROIDraw::FilterROIDraw(const std::list<ROI> *rois, border_style_t style)
00041   : Filter("FilterROIDraw"),
00042   __rois(rois),
00043   __border_style(style)
00044 {
00045   __drawer = new Drawer;
00046 }
00047 
00048 /** Destructor */
00049 FilterROIDraw::~FilterROIDraw() {
00050   delete __drawer;
00051 }
00052
00053 void
00054 FilterROIDraw::draw_roi(const ROI *roi)
00055 {
00056   if (__border_style == DASHED_HINT) {
00057     YUV_t hint_color = ColorObjectMap::get_color(ColorObjectMap::get_instance().get(roi->hint));
00058     __drawer->set_buffer(dst, roi->image_width, roi->image_height);
00059     bool draw_black = false;
00060     fawkes::point_t end;
00061     end.x = std::min(roi->image_width - 1, roi->start.x + roi->width);
00062     end.y = std::min(roi->image_height - 1, roi->start.y + roi->height);
00063
00064     //Top and bottom line
00065     for (unsigned int x = roi->start.x; x <= end.x ; ++x) {
00066       if (!(x % 2)) {
00067         __drawer->set_color(draw_black ? YUV_t::black() : hint_color);
00068         draw_black = !draw_black;
00069       }
00070
00071       __drawer->color_point(x, roi->start.y);
00072       __drawer->color_point(x, end.y);
00073     }
00074
00075     //Side lines
00076     for (unsigned int y = roi->start.y; y <= end.y; ++y) {
00077       if (!(y % 2)) {
00078         __drawer->set_color(draw_black ? YUV_t::black() : hint_color);
00079         draw_black = !draw_black;
00080       }
00081
00082       __drawer->color_point(roi->start.x, y);
00083       __drawer->color_point(end.x, y);
00084     }
00085   }
00086   else {
00087     // destination y-plane
00088     unsigned char *dyp  = dst + (roi->start.y * roi->line_step) + (roi->start.x * roi->pixel_step);
00089
00090     // line starts
00091     unsigned char *ldyp = dyp;  // destination y-plane
00092
00093     // top border
00094     for (unsigned int i = roi->start.x; i < (roi->start.x + roi->width); ++i) {
00095       *dyp = 255 - *dyp;
00096       dyp++;
00097     }
00098
00099     // left and right borders
00100     for (unsigned int i = roi->start.y; i < (roi->start.y + roi->height); ++i) {
00101       ldyp += roi->line_step;
00102       dyp = ldyp;
00103       *dyp = 255 - *dyp;
00104       dyp += roi->width;
00105       *dyp = 255 - *dyp;
00106     }
00107     ldyp += roi->line_step;
00108     dyp = ldyp;
00109
00110     // bottom border
00111     for (unsigned int i = roi->start.x; i < (roi->start.x + roi->width); ++i) {
00112       *dyp = 255 - *dyp;
00113       dyp++;
00114     }
00115   }
00116 }
00117
00118 void
00119 FilterROIDraw::apply()
00120 {
00121   if ( dst_roi ) {
00122     draw_roi(dst_roi);
00123   }
00124   if ( __rois ) {
00125     for (std::list<ROI>::const_iterator r = __rois->begin(); r != __rois->end(); ++r) {
00126       draw_roi(&(*r));
00127     }
00128   }
00129 }
00130
00131 
00132 /** Set ROIs.
00133  * Set a list of ROIs. The list must persist as long as the filter is applied with
00134  * this list. Set to NULL to have it ignored again.
00135  * @param rois list of ROIs to draw additionally to the dst_roi.
00136  */
00137 void
00138 FilterROIDraw::set_rois(const std::list<ROI> *rois)
00139 {
00140   __rois = rois;
00141 }
00142
00143 
00144 /** Sets the preferred style
00145  * @param style The preferred style
00146  */
00147 void
00148 FilterROIDraw::set_style(border_style_t style)
00149 {
00150   __border_style = style;
00151 }