Fawkes API Fawkes Development Version

filter.cpp

00001 
00002 /***************************************************************************
00003  *  filter.cpp - Abstract class defining a filter
00004  *
00005  *  Created: Mon May 19 15:47:44 2007
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/filter.h>
00025 
00026 #include <core/exceptions/software.h>
00027 #include <cstdlib>
00028 #include <cstring>
00029 
00030 using namespace fawkes;
00031 
00032 namespace firevision {
00033 #if 0 /* just to make Emacs auto-indent happy */
00034 }
00035 #endif
00036 
00037 /** @class Filter <filters/filter.h>
00038  * Filter interface.
00039  * This class defines the general interface that filters are used with.
00040  *
00041  * @author Tim Niemueller
00042  *
00043  * @fn void Filter::apply() = 0
00044  * Apply the filter.
00045  * Apply the filter to the given source and destination
00046  * buffers with given width and height and orientation
00047  * (ori may be ignored for some filters).
00048  */
00049 
00050 /** Constructor.
00051  * @param name name of the filter
00052  * @param max_num_buffers The maximum number of source buffers that can be set.
00053  */
00054 Filter::Filter(const char *name, unsigned int max_num_buffers)
00055 {
00056   if ( max_num_buffers == 0 ) {
00057     throw OutOfBoundsException("Need to set at least one buffer", 0, 1, 0xFFFFFFFF);
00058   }
00059 
00060   _name = strdup(name);
00061   _max_num_buffers = max_num_buffers;
00062 
00063   src = (unsigned char **)malloc(_max_num_buffers * sizeof(unsigned char *));
00064   memset(src, 0, _max_num_buffers * sizeof(unsigned char *));
00065 
00066   src_roi = (ROI **)malloc(_max_num_buffers * sizeof(ROI *));
00067   memset(src_roi, 0, _max_num_buffers * sizeof(ROI *));
00068 
00069   ori = (orientation_t *)malloc(_max_num_buffers * sizeof(orientation_t));
00070   memset(ori, 0, _max_num_buffers * sizeof(orientation_t));
00071 }
00072 
00073 
00074 /** Destructor. */
00075 Filter::~Filter()
00076 {
00077   free(_name);
00078   free(src);
00079   free(src_roi);
00080   free(ori);
00081 }
00082 
00083 /** Set source buffer with orientation.
00084  * @param buf Buffer to use as source image
00085  * @param roi Region Of Interest to work on
00086  * @param ori Orientation to apply the filter in, maybe ignored
00087  *            in some filters
00088  * @param buffer_num source buffer to set for filter that need
00089  *                   multiple src buffers
00090  * @exception OutOfBoundsException Thrown if buffer_num is illegal
00091  */
00092 void
00093 Filter::set_src_buffer(unsigned char *buf,
00094                        ROI *roi,
00095                        orientation_t ori,
00096                        unsigned int buffer_num)
00097 {
00098   if ( buffer_num >= _max_num_buffers ) {
00099     throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
00100   }
00101 
00102   src[buffer_num]       = buf;
00103   src_roi[buffer_num]   = roi;
00104   this->ori[buffer_num] = ori;
00105 }
00106 
00107 
00108 /** Set source buffer.
00109  * @param buf Buffer to use as source image
00110  * @param roi Region Of Interest to work on
00111  * @param buffer_num source buffer to set for filter that need multiple src buffers
00112  * @exception OutOfBoundsException Thrown if buffer_num is illegal
00113  */
00114 void
00115 Filter::set_src_buffer(unsigned char *buf,
00116                        ROI *roi,
00117                        unsigned int buffer_num)
00118 {
00119   if ( buffer_num >= _max_num_buffers ) {
00120     throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
00121   }
00122 
00123   src[buffer_num]     = buf;
00124   src_roi[buffer_num] = roi;
00125   ori[buffer_num]     = ORI_HORIZONTAL;
00126 }
00127 
00128 
00129 /** Set the destination buffer.
00130  * @param buf Buffer to use as destination image
00131  * @param roi Region Of Interest where the result is put in the dst image
00132  */
00133 void
00134 Filter::set_dst_buffer(unsigned char *buf, ROI *roi)
00135 {
00136   dst     = buf;
00137   dst_roi = roi;
00138 }
00139 
00140 
00141 /** Set the orientation to apply the filter in.
00142  * Maybe ignored by some filters.
00143  * @param ori Orientation
00144  * @param buffer_num buffer this orientation applies to
00145  */
00146 void
00147 Filter::set_orientation(orientation_t ori, unsigned int buffer_num)
00148 {
00149   if ( buffer_num >= _max_num_buffers ) {
00150     throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
00151   }
00152 
00153   this->ori[buffer_num] = ORI_HORIZONTAL;
00154 }
00155 
00156 
00157 /** Get filter name
00158  * @return filter name
00159  */
00160 const char *
00161 Filter::name()
00162 {
00163   return _name;
00164 }
00165 
00166 
00167 /** This shrinks the regions as needed for a N x N matrix.
00168  * @param r ROI to shrink
00169  * @param n size of the matrix
00170  */
00171 void
00172 Filter::shrink_region(ROI *r, unsigned int n)
00173 {
00174   if (r->start.x < (n/2)) {
00175     r->start.x = n/2;
00176   }
00177   if (r->start.y < (n/2)) {
00178     r->start.y = n/2;
00179   }
00180   if ( (r->start.x + r->width) >= (r->image_width - (n/2)) ) {
00181     r->width -= (r->start.x + r->width) - (r->image_width - (n/2));
00182   }
00183   if ( (r->start.y + r->height) >= (r->image_height - (n/2)) ) {
00184     r->height -= (r->start.y + r->height) - (r->image_height - (n/2));
00185   }
00186 }
00187 
00188 } // end namespace firevision
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends