Fawkes API  Fawkes Development Version
filter.cpp
1 
2 /***************************************************************************
3  * filter.cpp - Abstract class defining a filter
4  *
5  * Created: Mon May 19 15:47:44 2007
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/filter.h>
25 
26 #include <core/exceptions/software.h>
27 #include <cstdlib>
28 #include <cstring>
29 
30 using namespace fawkes;
31 
32 namespace firevision {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class Filter <fvfilters/filter.h>
38  * Filter interface.
39  * This class defines the general interface that filters are used with.
40  *
41  * @author Tim Niemueller
42  *
43  * @fn void Filter::apply() = 0
44  * Apply the filter.
45  * Apply the filter to the given source and destination
46  * buffers with given width and height and orientation
47  * (ori may be ignored for some filters).
48  */
49 
50 /** Constructor.
51  * @param name name of the filter
52  * @param max_num_buffers The maximum number of source buffers that can be set.
53  */
54 Filter::Filter(const char *name, unsigned int max_num_buffers)
55 {
56  if ( max_num_buffers == 0 ) {
57  throw OutOfBoundsException("Need to set at least one buffer", 0, 1, 0xFFFFFFFF);
58  }
59 
60  _name = strdup(name);
61  _max_num_buffers = max_num_buffers;
62 
63  src = (unsigned char **)malloc(_max_num_buffers * sizeof(unsigned char *));
64  memset(src, 0, _max_num_buffers * sizeof(unsigned char *));
65 
66  src_roi = (ROI **)malloc(_max_num_buffers * sizeof(ROI *));
67  memset(src_roi, 0, _max_num_buffers * sizeof(ROI *));
68 
69  ori = (orientation_t *)malloc(_max_num_buffers * sizeof(orientation_t));
70  memset(ori, 0, _max_num_buffers * sizeof(orientation_t));
71 }
72 
73 
74 /** Destructor. */
75 Filter::~Filter()
76 {
77  free(_name);
78  free(src);
79  free(src_roi);
80  free(ori);
81 }
82 
83 /** Set source buffer with orientation.
84  * @param buf Buffer to use as source image
85  * @param roi Region Of Interest to work on
86  * @param ori Orientation to apply the filter in, maybe ignored
87  * in some filters
88  * @param buffer_num source buffer to set for filter that need
89  * multiple src buffers
90  * @exception OutOfBoundsException Thrown if buffer_num is illegal
91  */
92 void
93 Filter::set_src_buffer(unsigned char *buf,
94  ROI *roi,
95  orientation_t ori,
96  unsigned int buffer_num)
97 {
98  if ( buffer_num >= _max_num_buffers ) {
99  throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
100  }
101 
102  src[buffer_num] = buf;
103  src_roi[buffer_num] = roi;
104  this->ori[buffer_num] = ori;
105 }
106 
107 
108 /** Set source buffer.
109  * @param buf Buffer to use as source image
110  * @param roi Region Of Interest to work on
111  * @param buffer_num source buffer to set for filter that need multiple src buffers
112  * @exception OutOfBoundsException Thrown if buffer_num is illegal
113  */
114 void
115 Filter::set_src_buffer(unsigned char *buf,
116  ROI *roi,
117  unsigned int buffer_num)
118 {
119  if ( buffer_num >= _max_num_buffers ) {
120  throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
121  }
122 
123  src[buffer_num] = buf;
124  src_roi[buffer_num] = roi;
125  ori[buffer_num] = ORI_HORIZONTAL;
126 }
127 
128 
129 /** Set the destination buffer.
130  * @param buf Buffer to use as destination image
131  * @param roi Region Of Interest where the result is put in the dst image
132  */
133 void
134 Filter::set_dst_buffer(unsigned char *buf, ROI *roi)
135 {
136  dst = buf;
137  dst_roi = roi;
138 }
139 
140 
141 /** Set the orientation to apply the filter in.
142  * Maybe ignored by some filters.
143  * @param ori Orientation
144  * @param buffer_num buffer this orientation applies to
145  */
146 void
147 Filter::set_orientation(orientation_t ori, unsigned int buffer_num)
148 {
149  if ( buffer_num >= _max_num_buffers ) {
150  throw OutOfBoundsException("Invalid buffer number", buffer_num, 0, _max_num_buffers);
151  }
152 
153  this->ori[buffer_num] = ORI_HORIZONTAL;
154 }
155 
156 
157 /** Get filter name
158  * @return filter name
159  */
160 const char *
161 Filter::name()
162 {
163  return _name;
164 }
165 
166 
167 /** This shrinks the regions as needed for a N x N matrix.
168  * @param r ROI to shrink
169  * @param n size of the matrix
170  */
171 void
172 Filter::shrink_region(ROI *r, unsigned int n)
173 {
174  if (r->start.x < (n/2)) {
175  r->start.x = n/2;
176  }
177  if (r->start.y < (n/2)) {
178  r->start.y = n/2;
179  }
180  if ( (r->start.x + r->width) >= (r->image_width - (n/2)) ) {
181  r->width -= (r->start.x + r->width) - (r->image_width - (n/2));
182  }
183  if ( (r->start.y + r->height) >= (r->image_height - (n/2)) ) {
184  r->height -= (r->start.y + r->height) - (r->image_height - (n/2));
185  }
186 }
187 
188 } // end namespace firevision
Fawkes library namespace.
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
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
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
unsigned int height
ROI height.
Definition: roi.h:123
Index out of bounds.
Definition: software.h:88