Commit a7340b9a authored by Janis Born's avatar Janis Born

add convertTextureData function

parent 6d348769
......@@ -143,6 +143,11 @@ private:
ACGL_SMARTPOINTER_TYPEDEFS(TextureData)
//! Converts the texture data in _from to the target format and type given in
//! _to. Overwrites width, height, and depth in _to. Old texture data is removed
//! and new memory is allocated.
void convertTextureData(const SharedTextureData& _from, const SharedTextureData& _to);
} // OpenGL
} // ACGL
......
......@@ -7,8 +7,8 @@
#include <ACGL/OpenGL/Data/TextureData.hh>
#include <ACGL/Utils/Memory.hh>
using namespace ACGL;
using namespace ACGL::OpenGL;
namespace ACGL {
namespace OpenGL {
GLsizei TextureData::getPackAlignment() const
{
......@@ -304,3 +304,74 @@ void TextureData::setTexel( glm::uvec2 _texCoord, glm::vec4 _color )
}
}
float grayscaleMixdown(float _r, float _g, float _b)
{
return 0.299f * _r + 0.587f * _g + 0.114f * _b;
}
glm::vec4 convertTexelNumChannels(glm::vec4 _texel, GLsizei _from, GLsizei _to)
{
if (_from == _to) {
return _texel;
}
else if (_from == 1) {
switch (_to) {
case 2: return {_texel.r, 1.0, 0.0, 0.0};
case 3: return {_texel.r, _texel.r, _texel.r, 0.0};
case 4: return {_texel.r, _texel.r, _texel.r, 1.0};
}
}
else if (_from == 2) {
switch (_to) {
case 1: return {_texel.r, 0.0, 0.0, 0.0};
case 3: return {_texel.r, _texel.r, _texel.r, _texel.g};
case 4: return {_texel.r, _texel.r, _texel.r, _texel.g};
}
}
else if (_from == 3) {
switch (_to) {
case 1: return {grayscaleMixdown(_texel.r, _texel.g, _texel.b), 0.0, 0.0, 0.0};
case 2: return {grayscaleMixdown(_texel.r, _texel.g, _texel.b), 1.0, 0.0, 0.0};
case 4: return {_texel.r, _texel.r, _texel.r, 1.0};
}
}
else if (_from == 4) {
switch (_to) {
case 1: return {grayscaleMixdown(_texel.r, _texel.g, _texel.b), 0.0, 0.0, 0.0};
case 2: return {grayscaleMixdown(_texel.r, _texel.g, _texel.b), 1.0, 0.0, 0.0};
case 3: return {_texel.r, _texel.r, _texel.r, 0.0};
}
}
return _texel;
}
void convertTextureData(const SharedTextureData& _from, const SharedTextureData& _to)
{
assert(_from);
assert(_to);
if (!_from->getData()) {
ACGL::Utils::error() << "Cannot convert TextureData: source TextureData contains no data" << std::endl;
return;
}
// Setup target texture dimensions
_to->setWidth(_from->getWidth());
_to->setHeight(_from->getHeight());
_to->setDepth(_from->getDepth());
// Allocate new memory
_to->deleteData();
GLubyte* data = new GLubyte[_to->getSizeInBytes()];
_to->setData(data);
// Transfer pixels
for (GLsizei y = 0; y < _to->getHeight(); ++y) {
for (GLsizei x = 0; x < _to->getWidth(); ++x) {
auto texel = convertTexelNumChannels(_from->getTexel({x, y}), _from->getNumberOfChannels(), _to->getNumberOfChannels());
_to->setTexel({x, y}, texel);
}
}
}
} // namespace OpenGL
} // namespace ACGL
Markdown is supported
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