Commit 1139bbd0 authored by Janis Born's avatar Janis Born
Browse files

add function that writes TextureData to a PPM file

parent 90049b9a
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_DATA_TEXTUREDATAPPMWRITER_HH
#define ACGL_OPENGL_DATA_TEXTUREDATAPPMWRITER_HH
/**
* Helper function for writing the contents of a TextureData object into a PPM file.
* Expects 8 bit RGBA texture data (as can be provided by Texture::getImageData)
*/
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Data/TextureData.hh>
#include <string>
namespace ACGL{
namespace OpenGL{
bool saveTextureDataToPPM(const ConstSharedTextureData& _textureData, const std::string& _filename);
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_DATA_TEXTUREDATAPPMWRITER_HH
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Data/TextureDataPPMWriter.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <fstream>
using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;
bool ACGL::OpenGL::saveTextureDataToPPM(const ConstSharedTextureData& _textureData, const std::string& _filename)
{
std::ofstream outFileStream(_filename.c_str(), std::ios_base::out | std::ios_base::binary);
if(!outFileStream.good())
{
error() << "saveTextureDataToPPM: Could not open file " << _filename << std::endl;
return false;
}
// Use the binary-encoded RGB texture format
outFileStream << "P6" << std::endl;
// Width and height
outFileStream << _textureData->getWidth() << " " << _textureData->getHeight() << std::endl;
// Maximum value
uint_t maxValue;
if(_textureData->getType() == GL_UNSIGNED_BYTE)
{
maxValue = 255;
}
else
{
error() << "saveTextureDataToPPM: Unsupported data format" << std::endl;
return false;
}
outFileStream << maxValue;
// Single whitespace as a delimiter
outFileStream << "\n";
// Now, write the image data in binary format
for(uint_t y = _textureData->getHeight()-1; y > 0; --y)
{
for(uint_t x = 0; x < _textureData->getWidth(); ++x)
{
uint_t i = (y * _textureData->getWidth()) + x;
outFileStream.put(_textureData->getData()[(4*i + 0) * getGLTypeSize(_textureData->getType())]);
outFileStream.put(_textureData->getData()[(4*i + 1) * getGLTypeSize(_textureData->getType())]);
outFileStream.put(_textureData->getData()[(4*i + 2) * getGLTypeSize(_textureData->getType())]);
}
}
outFileStream.close();
return true;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment