Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePlainPerPixelProcessingTemporal.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT 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 /**
26  @example ExamplePlainPerPixelProcessing.cpp
27  @relates PlainPerPixelProcessing
28  @brief Example for using the PlainPerPixelProcessing on the GPU
29  @ingroup g_examples
30  @ingroup g_openglframework
31  @author MIP
32 */
33 
34 #include <iostream>
35 
36 #include <OpenGLFramework/Base/glfFramebufferObject.hh>
37 #include <OpenGLFramework/SpecializedBatches/PlainPerPixelProcessing.hh>
38 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
39 
40 #include <Utils/Param.hh>
41 #include <Base/Image/ImageIO.hh>
42 #include <Base/Image/Image.hh>
43 #include <Utils/IOUtils.hh>
44 #include <Base/Common/FileHandling.hh>
45 
46 #include <list>
47 #include <vector>
48 
49 using namespace BIAS;
50 using namespace std;
51 
52 
53 
54 bool InitTemporalWindow(const vector<string>& filenames,
55  int halfWinSize,
56  int& firstValidIndex, int& lastValidIndex,
57  list<glfTexture2D*>& textureList);
58 bool ShiftWindow(const vector<string>& filenames,
59  int currentIndex,
60  list<glfTexture2D*>& textureList);
61 
62 void ClearList(list<glfTexture2D*>& textureList);
63 
64 
65 int main(int argc, char* argv[]) {
66 
67  Param params(true);
68 
69  string* shaderFile = params.AddParamString("shader", "shader to be loaded");
70  string* outputPrefix = params.AddParamString("outputPrefix", "", "", 'o');
71  string* inputFileNameList = params.AddParamString("inputFileNameList",
72  "", "", 'i');
73 
74  int* temporalWindowHalfSize =
75  params.AddParamInt("tempHW", "half win size of the temporal window", 0, 0);
76 
77 
78  if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv)) {
79  return 0;
80  }
81 
82 
83  if(inputFileNameList->size() == 0 ) {
84  BIASERR("some input image list is required!");
85  }
86 
87  vector<string> filenames;
88  if(Param::ParseListFile(*inputFileNameList, filenames)!=0){
89  BIASERR("error parsing image list!");
90  return -1;
91  }
92 
93  //check if enough images are present and preload the first window
94  // information
95 
96 
97 
98  list<glfTexture2D*> textureList;
99  glfPBuffer pbuffer;
100  try {
101 
103  cfg.width = 5;
104  cfg.height = 5;
105  cfg.doubleBuffer = 0;
106  cfg.redSize = 8;
107  cfg.greenSize = 8;
108  cfg.blueSize = 8;
109  cfg.alphaSize = 0;
110  cfg.depthSize = 24;
111  cfg.stencilSize = 0;
112 
113  pbuffer.Init(cfg);
114 
115 
116 
117  int firstValidIndex, lastValidIndex;
118 
119 
120  if(! InitTemporalWindow(filenames,
121  *temporalWindowHalfSize,
122  firstValidIndex, lastValidIndex,
123  textureList) ) {
124  throw -1;
125  }
126 
127  unsigned int width = textureList.front()->GetWidth();
128  unsigned int height = textureList.front()->GetHeight();
129 
130  glfTexture2D outputTexture;
131  outputTexture.Create();
132  outputTexture.Set();
133  outputTexture.Allocate(width, height, GL_RGB);
134 
136  fbo.Create();
137  fbo.AttachTexture(outputTexture, GL_COLOR_ATTACHMENT0_EXT);
138  fbo.CheckComplete();
139  fbo.ClearColorBuffer(0.0, 1.0, 1.0, 0.0);
140 
141 
142  PlainPerPixelProcessing gpuProcess(true);
143 
144  gpuProcess.SetRenderTarget(&fbo);
145  gpuProcess.SetOutputSize(width, height);
146  gpuProcess.SetFragmentShaderFromFile(*shaderFile);
147 
148  Image<unsigned char> outputImage;
149 
150  int i = firstValidIndex;
151  do {
152  //keep in mind the first window has been loaded already
153  gpuProcess.SetTextures(textureList);
154  gpuProcess.Execute();
155  outputTexture.CopyToImage(outputImage);
156  string path, base, suffix;
157  FileHandling::SplitName(filenames[i], path, base, suffix);
158  ImageIO::Save(path+(*outputPrefix)+base+suffix, outputImage);
159  cout<<" -> "<< filenames[i] <<endl;
160  i++;
161  if(!ShiftWindow(filenames, i, textureList) )
162  throw -1;
163 
164 
165  } while(i<lastValidIndex);
166 
167 
168  ClearList(textureList);
169 
170  }
171  catch (glfException& e) {
172  std::cout << "Error: " << e.GetMessageString() << std::endl;
173  ClearList(textureList);
174  pbuffer.Destroy();
175  return -1;
176  }
177  catch (...) {
178  ClearList(textureList);
179  pbuffer.Destroy();
180  return -1;
181  }
182 
183 
184  pbuffer.Destroy();
185 
186 
187  return 0;
188 }
189 
190 /** Do not call witout proper GL Context.
191 **/
192 
193 bool InitTemporalWindow(const vector<string>& filenames,
194  int halfWinSize,
195  int& firstValidIndex, int& lastValidIndex,
196  list<glfTexture2D*>& textureList)
197 /*,
198  list<glfTexture2D*>& texturePtList) */
199 {
200  const int winElements = halfWinSize*2+1;
201  const int allElements = static_cast<int>(filenames.size());
202  if( winElements > allElements ) {
203  BIASERR("temporal window and number of filenames do not fit!");
204  return false;
205  }
206  firstValidIndex = halfWinSize;//zero based indexing
207  lastValidIndex = allElements - 1 - halfWinSize;
208 
209 
210 
211  Image<unsigned char> tmpImg;
212  for( int i=0; i<winElements; i++) {
213  if(ImageIO::Load(filenames[i], tmpImg) != 0) {
214  BIASERR("error loadin image "<<filenames[i]);
215  return false;
216  }
217  textureList.push_front(new glfTexture2D);
218  textureList.front()->Create();
219  textureList.front()->Set();
220  textureList.front()->UploadImage(tmpImg);
221 
222  }
223 
224  return true;
225 }
226 
227 bool ShiftWindow(const vector<string>& filenames,
228  int currentIndex,
229  list<glfTexture2D*>& textureList)
230 {
231  Image<unsigned char> tmpImg;
232  if(ImageIO::Load(filenames[currentIndex], tmpImg) != 0) {
233  BIASERR("error loadin image "<<filenames[currentIndex]);
234  return false;
235  }
236 
237 ;
238  textureList.push_front(new glfTexture2D);
239  textureList.front()->Create();
240  textureList.front()->Set();
241  textureList.front()->UploadImage(tmpImg);
242  delete textureList.back();
243  textureList.pop_back();
244 
245  return true;
246 }
247 
248 void ClearList(list<glfTexture2D*>& textureList) {
249  list<glfTexture2D*>::iterator it=textureList.begin();
250  for(;it!=textureList.end(); it++) {
251  delete (*it);
252  }
253 }
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
void AttachTexture(const glfTexture &texture, GLenum attachmentPoint, int attachedMipMapLevel=0, int zSlice=0, GLenum cubeMapSide=0)
Attaches a texture to an attachment point which can be.
A 2D texture.
Definition: glfTexture2D.hh:40
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
void CopyToImage(ImageBase &image, int mipmap=0)
Copies the pixels of a mipmap of the texture to a BIAS::ImageBase.
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
virtual void Destroy()
Uninitializes the rendering context.
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
void CheckComplete() const
Checks whether the framebuffer object is supported in its current state.
static void SplitName(const std::string &fullname, std::string &dir, std::string &base, std::string &suffix)
Split full path into:
GLX pbuffer rendering context.
Class rendering quad and executing a fragment shader.
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
Definition: Param.cpp:1853
static bool ParseCommandLineEvalHelp(Param &params, int argc, char *argv[])
parses the command line, adds parameter &quot;help&quot;
Definition: IOUtils.cpp:176
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
virtual void Init(const glfRenderingContextConfig &config)
Initializes the rendering context with the given configuration.
This class Param provides generic support for parameters.
Definition: Param.hh:231
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
void Set(GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, GLint textureNr=GL_TEXTURE0)
Convenience wrapper.
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
Configuration for a rendering context.
void Create()
Creates the framebuffer object.