Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clfFilter.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <iostream>
26 #include <OpenCLFramework/Filter/clfFilter.hh>
27 #include <Base/Common/BIASpragma.hh>
28 using namespace std;
29 using namespace BIAS;
30 
31 namespace BIAS {
32 
33  template <class InputStorageType, class OutputStorageType>
36  context_ = ctx;
37  filterMask_ = NULL;
38  winSize_ = 0;
39  tmpImage_ = context_->CreateImage2D();
40  filterMask_ = context_->CreateBuffer();
41  program_ = context_->CreateProgram();
42  program_->AddSource("/OpenCLFramework/Filter/cl/filteroperation.cl");
43  program_->Build();
44  programName_ = "Convolve";
45  program_->AddKernel(programName_);
46  program_->AddKernel("Clear");
47  }
48 
49  template <class InputStorageType, class OutputStorageType>
52  delete tmpImage_;
53  delete filterMask_;
54  delete program_;
55  }
56 
57  template <class InputStorageType, class OutputStorageType>
59  SetFilter_(unsigned int size, const float *mask) {
60  winSize_ = size;
61  try {
62  filterMask_->Allocate( winSize_*winSize_ * sizeof(float), true, false );
63  filterMask_->WriteToBuffer( (float*)mask );
64  } catch (clfException &e) {
65  BIASERR(e.GetDetailedString());
66  return 1;
67  }
68  return 0;
69  }
70 
71  template <class InputStorageType, class OutputStorageType>
73  SetFilter1D_(unsigned int size, const float *mask) {
74  winSize_ = size;
75  try {
76  filterMask_->Allocate( winSize_ * sizeof(float), true, false );
77  filterMask_->WriteToBuffer( (float*)mask );
78  } catch (clfException &e) {
79  BIASERR(e.GetDetailedString());
80  return 1;
81  }
82  return 0;
83  }
84 
85  template <class InputStorageType, class OutputStorageType>
87  Build(unsigned int size) {
88  winSize_ = size;
89  // defaulting to mean filter
90  float *mask = new float[winSize_*winSize_];
91  for (unsigned int i=0;i<winSize_*winSize_;i++)
92  mask[i] = 1.0f / float(winSize_*winSize_);
93  SetFilter_(winSize_, mask);
94  delete mask;
95  return 0;
96  }
97 
98  template <class InputStorageType, class OutputStorageType>
101  int ret = 0;
102  try {
103  ret = Filter_(programName_, src, dst);
104  } catch (clfException &e) {
105  BIASERR(e.GetDetailedString());
106  return 1;
107  }
108  return ret;
109  }
110 
111  template <class InputStorageType, class OutputStorageType>
113  Clear(float color, clfImage2D *image) {
114  try {
115  program_->KernelSetArgument( "Clear", 0, color );
116  program_->KernelSetArgument( "Clear", 1, *image );
117  unsigned int w,h;
118  image->GetImageDim(w,h);
119  unsigned int localX = 16;
120  unsigned int localY = 16;
121  if (w%16) localX = 1;
122  if (h%16) localY = 1;
123  context_->RunOn2DRange( *program_, "Clear", w,h, localX,localY);
124  } catch (clfException &e) {
125  BIASERR(e.GetDetailedString());
126  return 1;
127  }
128  return 0;
129  }
130 
131 
132  template <class InputStorageType, class OutputStorageType>
134  FilterSep_(std::string nameX,std::string nameY, clfImage2D *src, clfImage2D *dst) {
135  int ret = 0;
136  try {
137  tmpImage_->AllocateFromTemplate( *src, false, false);
138  ret = Filter_(nameX, src, tmpImage_);
139  ret += Filter_(nameY, tmpImage_, dst);
140  } catch (clfException &e) {
141  BIASERR(e.GetDetailedString());
142  return 1;
143  }
144  return ret;
145  }
146 
147  template <class InputStorageType, class OutputStorageType>
149  Filter_(std::string name, clfImage2D *src, clfImage2D *dst, int blockX, int blockY) {
150  try {
151  program_->KernelSetArgument( "Clear", 0, 0 );
152  program_->KernelSetArgument( "Clear", 1, *dst );
153  unsigned int w,h;
154  dst->GetImageDim(w,h);
155  context_->RunOn2DRange( *program_, "Clear", w,h, 16,16);
156  program_->KernelSetArgument( name, 0, *src );
157  program_->KernelSetArgument( name, 1, *filterMask_ );
158  program_->KernelSetArgument( name, 2, (int)winSize_ );
159  program_->KernelSetArgument( name, 3, *dst );
160  context_->RunOn2DRange( *program_, name, w, h, 16, 16);
161  } catch (clfException &e) {
162  BIASERR(e.GetDetailedString());
163  return 1;
164  }
165  return 0;
166  }
167 
168  template class BIASOpenCLFramework_EXPORT clfFilter<float, float>;
169  template class BIASOpenCLFramework_EXPORT clfFilter<unsigned char, unsigned char>;
170 
171 } /* namespace DECS */
void GetImageDim(unsigned int &width, unsigned int &height) const
Definition: clfImage2D.hh:110
OpenCL Image2D wrapper.
Definition: clfImage2D.hh:46
const std::string & GetDetailedString() const
detailed combination of all info available
OpenCL Context wrapper.
Definition: clfContext.hh:49
clf Exception wrapper, is thrown in case of most clf errors
Definition: clfException.hh:48
clfImage2D * CreateImage2D()
create buffer object
Definition: clfContext.hh:87