Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePlainPerPixelProcessing.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 
45 using namespace BIAS;
46 using namespace std;
47 
48 
49 int main(int argc, char* argv[]) {
50 
51  Param params(true);
52 
53  string* shaderFile = params.AddParamString("shader", "shader to be loaded","",'s');
54  string* outputFileName = params.AddParamString("output", "Filename of output image", "", 'o');
55  string* inputFileName = params.AddParamString("input", "Filename of input image", "", 'i');
56  int* iterations = params.AddParamInt("iterations", "how often shall the shader be run iteratively?", 1, 1, INT_MAX, 'I');
57 
58  if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv)) {
59  return 0;
60  }
61 
62  if(argc<2){params.Usage();return 0;}
63 
64  Image<unsigned char> inputImage;
65  bool hasInputImage_ = false;
66  unsigned int width = 640;
67  unsigned int height = 480;
68  if(inputFileName->size() > 0 ) {
69  if(ImageIO::Load(*inputFileName, inputImage) !=0 ) {
70  BIASERR("error loading input image "<<*inputFileName);
71  return -1;
72  };
73  hasInputImage_ = true;
74  width = inputImage.GetWidth();
75  height = inputImage.GetHeight();
76  }
77 
78  glfPBuffer pbuffer;
79  try {
80 
82  cfg.width = 5;
83  cfg.height = 5;
84  cfg.doubleBuffer = 0;
85  cfg.redSize = 8;
86  cfg.greenSize = 8;
87  cfg.blueSize = 8;
88  cfg.alphaSize = 0;
89  cfg.depthSize = 24;
90  cfg.stencilSize = 0;
91 
92  pbuffer.Init(cfg);
93 
94 
95 
96  glfTexture2D outputTexture;
97  outputTexture.Create();
98  outputTexture.Set();
99  outputTexture.Allocate(width, height, GL_RGB);
100 
102  fbo.Create();
103 
104 
105  PlainPerPixelProcessing gpuProcess;
106  glfTexture2D inputTexture;
107  vector<glfTexture2D*> inputTexturesVec;
108  if(hasInputImage_) {
109  inputTexture.Create();
110  inputTexture.Set();
111  inputTexture.UploadImage(inputImage);
112  inputTexturesVec.push_back(&inputTexture);
113  gpuProcess.SetTextures(inputTexturesVec);
114  }
115 
116  gpuProcess.SetRenderTarget(&fbo);
117  gpuProcess.SetOutputSize(width, height);
118  gpuProcess.SetFragmentShaderFromFile(*shaderFile);
119 
120  for(int i=0; i<*iterations; i++) {
121  fbo.AttachTexture(outputTexture, GL_COLOR_ATTACHMENT0_EXT); //required to refresh the bound id
122  fbo.CheckComplete();
123  fbo.ClearColorBuffer(0.0, 1.0, 1.0, 0.0);
124  gpuProcess.Execute(); //will trigger call to Bind() on the respective texture object.
125  SwapGLObjects(outputTexture, inputTexture);
126  }
127  if(*iterations>0) {
128  SwapGLObjects(outputTexture, inputTexture);
129  }
130 
131  Image<unsigned char> outputImage;
132  outputTexture.CopyToImage(outputImage);
133  ImageIO::Save(*outputFileName, outputImage);
134 
135 
136  } catch (glfException& e) {
137  std::cout << "Error: " << e.GetMessageString() << std::endl;
138  pbuffer.Destroy();
139  return -1;
140  }
141 
142 
143  pbuffer.Destroy();
144 
145 
146  return 0;
147 }
void SetFragmentShaderFromFile(const std::string &fileName)
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
BIASOpenGLFramework_EXPORT void SwapGLObjects(glfTexture &A, glfTexture &B)
Definition: glfTexture.cpp:31
void UploadImage(const BIAS::ImageBase &image, GLenum internalFormat=0, int mipmap=0)
Uploads a BIAS::Image to a mipmap of the texture.
void SetOutputSize(unsigned int width, unsigned int height)
Returns the currently set texture set;.
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
unsigned int GetWidth() const
Definition: ImageBase.hh:312
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 Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
void CheckComplete() const
Checks whether the framebuffer object is supported in its current state.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
GLX pbuffer rendering context.
Class rendering quad and executing a fragment shader.
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.
void SetRenderTarget(BIAS::glfRenderTarget *rt)
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.
void SetTextures(const std::vector< BIAS::glfTexture2D * > &inputTextures)
Adds textures in their respective index order and assigns them the default names img&lt;index&gt; in the sh...
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.