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
30using namespace fawkes;
31
32namespace 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 */
51Filter::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. */
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 */
88void
89Filter::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 */
106void
107Filter::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 */
122void
123Filter::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 */
134void
135Filter::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 */
147const char *
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 */
157void
158Filter::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
Index out of bounds.
Definition: software.h:86
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:66
virtual void set_dst_buffer(unsigned char *buf, ROI *roi)
Set the destination buffer.
Definition: filter.cpp:123
unsigned int _max_num_buffers
Maximum number of buffers.
Definition: filter.h:56
virtual const char * name()
Get filter name.
Definition: filter.cpp:148
char * _name
Filter name.
Definition: filter.h:58
void shrink_region(ROI *r, unsigned int n)
This shrinks the regions as needed for a N x N matrix.
Definition: filter.cpp:158
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:61
Filter(const char *name, unsigned int max_num_buffers=1)
Constructor.
Definition: filter.cpp:51
virtual ~Filter()
Destructor.
Definition: filter.cpp:71
unsigned char * dst
Destination buffer.
Definition: filter.h:63
ROI * dst_roi
Destination ROI.
Definition: filter.h:68
orientation_t * ori
Orientations, one for each source image.
Definition: filter.h:71
virtual void set_src_buffer(unsigned char *buf, ROI *roi, orientation_t ori=ORI_HORIZONTAL, unsigned int buffer_num=0)
Set source buffer with orientation.
Definition: filter.cpp:89
virtual void set_orientation(orientation_t ori, unsigned int buffer_num)
Set the orientation to apply the filter in.
Definition: filter.cpp:135
Region of interest.
Definition: roi.h:55
unsigned int height
ROI height.
Definition: roi.h:119
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int width
ROI width.
Definition: roi.h:117
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:121
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:123
Fawkes library namespace.
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37