Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePlainPerPixelProcessing.cpp

Example for using the PlainPerPixelProcessing on the GPU

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExamplePlainPerPixelProcessing.cpp
@relates PlainPerPixelProcessing
@brief Example for using the PlainPerPixelProcessing on the GPU
@ingroup g_examples
@ingroup g_openglframework
@author MIP
*/
#include <iostream>
#include <OpenGLFramework/Base/glfFramebufferObject.hh>
#include <OpenGLFramework/SpecializedBatches/PlainPerPixelProcessing.hh>
#include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
#include <Utils/Param.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/Image.hh>
#include <Utils/IOUtils.hh>
using namespace BIAS;
using namespace std;
int main(int argc, char* argv[]) {
Param params(true);
string* shaderFile = params.AddParamString("shader", "shader to be loaded","",'s');
string* outputFileName = params.AddParamString("output", "Filename of output image", "", 'o');
string* inputFileName = params.AddParamString("input", "Filename of input image", "", 'i');
int* iterations = params.AddParamInt("iterations", "how often shall the shader be run iteratively?", 1, 1, INT_MAX, 'I');
if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv)) {
return 0;
}
if(argc<2){params.Usage();return 0;}
bool hasInputImage_ = false;
unsigned int width = 640;
unsigned int height = 480;
if(inputFileName->size() > 0 ) {
if(ImageIO::Load(*inputFileName, inputImage) !=0 ) {
BIASERR("error loading input image "<<*inputFileName);
return -1;
};
hasInputImage_ = true;
width = inputImage.GetWidth();
height = inputImage.GetHeight();
}
glfPBuffer pbuffer;
try {
cfg.width = 5;
cfg.height = 5;
cfg.doubleBuffer = 0;
cfg.redSize = 8;
cfg.greenSize = 8;
cfg.blueSize = 8;
cfg.alphaSize = 0;
cfg.depthSize = 24;
cfg.stencilSize = 0;
pbuffer.Init(cfg);
glfTexture2D outputTexture;
outputTexture.Create();
outputTexture.Set();
outputTexture.Allocate(width, height, GL_RGB);
fbo.Create();
glfTexture2D inputTexture;
vector<glfTexture2D*> inputTexturesVec;
if(hasInputImage_) {
inputTexture.Create();
inputTexture.Set();
inputTexture.UploadImage(inputImage);
inputTexturesVec.push_back(&inputTexture);
gpuProcess.SetTextures(inputTexturesVec);
}
gpuProcess.SetRenderTarget(&fbo);
gpuProcess.SetOutputSize(width, height);
gpuProcess.SetFragmentShaderFromFile(*shaderFile);
for(int i=0; i<*iterations; i++) {
fbo.AttachTexture(outputTexture, GL_COLOR_ATTACHMENT0_EXT); //required to refresh the bound id
fbo.ClearColorBuffer(0.0, 1.0, 1.0, 0.0);
gpuProcess.Execute(); //will trigger call to Bind() on the respective texture object.
SwapGLObjects(outputTexture, inputTexture);
}
if(*iterations>0) {
SwapGLObjects(outputTexture, inputTexture);
}
Image<unsigned char> outputImage;
outputTexture.CopyToImage(outputImage);
ImageIO::Save(*outputFileName, outputImage);
} catch (glfException& e) {
std::cout << "Error: " << e.GetMessageString() << std::endl;
pbuffer.Destroy();
return -1;
}
pbuffer.Destroy();
return 0;
}