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