Commit 65e63f05 authored by Janis Born's avatar Janis Born

support color space selection when loading images using loadTexture2D

parent 711bddc5
/***********************************************************************
* Copyright 2011-2013 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
#ifndef ACGL_OPENGL_DATA_COLORSPACE_HH
#define ACGL_OPENGL_DATA_COLORSPACE_HH
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/GL.hh>
namespace ACGL{
namespace OpenGL{
enum ColorSpace
{
COLOR_SPACE_AUTO_DETECT,
COLOR_SPACE_LINEAR,
COLOR_SPACE_SRGB
};
/// Converts an OpenGL color format to an equivalent format of the given color space. If there is no matching format, the format is returned unchanged.
GLenum formatToColorSpace(GLenum _inputFormat, ColorSpace _colorSpace);
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_DATA_COLORSPACE_HH
......@@ -13,8 +13,9 @@
*/
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Data/TextureData.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/OpenGL/Data/ColorSpace.hh>
#include <ACGL/OpenGL/Data/TextureData.hh>
#include <string>
#include <map>
......
......@@ -12,7 +12,7 @@
*/
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Data/TextureDataLoadStore.hh>
#include <ACGL/OpenGL/Data/ColorSpace.hh>
#include <ACGL/OpenGL/Objects/Texture.hh>
#include <string>
......@@ -21,13 +21,13 @@ namespace ACGL{
namespace OpenGL{
//! loads the texture and creates mip maps
SharedTexture2D loadTexture2D(const std::string& _filename);
SharedTexture2D loadTexture2D(const std::string& _filename, ColorSpace _colorSpace = COLOR_SPACE_AUTO_DETECT);
//! loads the texture including mipmaps from a DDS file
//! supports DXT1, DXT3 and DXT5 compression
SharedTexture2D loadTexture2DFromDDS (const std::string& _filename);
SharedTexture3D loadTexture3DFromDDS (const std::string& _filename);
SharedTextureCubeMap loadTextureCubeMapFromDDS(const std::string& _filename);
SharedTexture2D loadTexture2DFromDDS (const std::string& _filename, ColorSpace _colorSpace = COLOR_SPACE_AUTO_DETECT);
SharedTexture3D loadTexture3DFromDDS (const std::string& _filename, ColorSpace _colorSpace = COLOR_SPACE_AUTO_DETECT);
SharedTextureCubeMap loadTextureCubeMapFromDDS(const std::string& _filename, ColorSpace _colorSpace = COLOR_SPACE_AUTO_DETECT);
}
}
/***********************************************************************
* Copyright 2011-2013 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
#include <ACGL/OpenGL/Data/ColorSpace.hh>
using namespace ACGL;
using namespace ACGL::OpenGL;
// Format conversion helpers
namespace
{
GLenum formatToLinear(GLenum _format)
{
switch(_format)
{
// Core
case GL_SRGB: return GL_RGB;
case GL_SRGB8: return GL_RGB8;
case GL_SRGB_ALPHA: return GL_RGBA;
case GL_SRGB8_ALPHA8: return GL_RGBA8;
case GL_COMPRESSED_SRGB: return GL_COMPRESSED_RGB;
case GL_COMPRESSED_SRGB_ALPHA: return GL_COMPRESSED_RGBA;
case GL_COMPRESSED_SRGB8_ETC2: return GL_COMPRESSED_RGB8_ETC2;
case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC: return GL_COMPRESSED_RGBA8_ETC2_EAC;
case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2: return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
// EXT_texture_sRGB, EXT_texture_compression_s3tc
case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
// OpenGL 4.2?
//case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM: return GL_COMPRESSED_RGBA_BPTC_UNORM;
}
return _format;
}
GLenum formatToSRGB(GLenum _format)
{
switch(_format)
{
// Core
case GL_RGB: return GL_SRGB;
case GL_RGB8: return GL_SRGB8;
case GL_RGBA: return GL_SRGB_ALPHA;
case GL_RGBA8: return GL_SRGB8_ALPHA8;
case GL_COMPRESSED_RGB: return GL_COMPRESSED_SRGB;
case GL_COMPRESSED_RGBA: return GL_COMPRESSED_SRGB_ALPHA;
case GL_COMPRESSED_RGB8_ETC2: return GL_COMPRESSED_SRGB8_ETC2;
case GL_COMPRESSED_RGBA8_ETC2_EAC: return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2: return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
// EXT_texture_sRGB, EXT_texture_compression_s3tc
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
// OpenGL 4.2?
//case GL_COMPRESSED_RGBA_BPTC_UNORM: return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM;
}
return _format;
}
}
GLenum ACGL::OpenGL::formatToColorSpace(GLenum _inputFormat, ColorSpace _colorSpace)
{
switch(_colorSpace)
{
case COLOR_SPACE_LINEAR: return formatToLinear(_inputFormat);
case COLOR_SPACE_SRGB: return formatToSRGB(_inputFormat);
default: return _inputFormat;
}
}
......@@ -5,6 +5,7 @@
**********************************************************************/
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <ACGL/OpenGL/Data/TextureDataLoadStore.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
......@@ -15,17 +16,17 @@ using namespace ACGL::Utils;
namespace ACGL{
namespace OpenGL{
SharedTexture2D loadTexture2D( const std::string &_filename )
SharedTexture2D loadTexture2D(const std::string& _filename, ColorSpace _colorSpace)
{
std::string fileEnding = StringHelpers::getFileEnding(_filename);
if(fileEnding == "dds")
{
return loadTexture2DFromDDS(_filename);
return loadTexture2DFromDDS(_filename, _colorSpace);
}
else
{
SharedTexture2D texture = SharedTexture2D( new Texture2D() );
SharedTextureData data = loadTextureData( _filename );
SharedTexture2D texture = std::make_shared<Texture2D>();
SharedTextureData data = loadTextureData(_filename);
if (!data) {
ACGL::Utils::error() << "can't create Texture from file " << _filename << " creating small empty texture instead." << std::endl;
texture->resize( glm::uvec2(4,4) );
......
......@@ -18,7 +18,7 @@ using namespace nv_dds;
namespace {
GLenum getDDSInternalFormat(bool compressed, GLenum format)
GLenum getDDSInternalFormat(bool compressed, GLenum format, ColorSpace _colorSpace = COLOR_SPACE_AUTO_DETECT)
{
GLenum internal_format = format;
......@@ -28,6 +28,9 @@ GLenum getDDSInternalFormat(bool compressed, GLenum format)
if(format == GL_BGR) internal_format = GL_RGB;
if(format == GL_RED) internal_format = GL_RED;
// Transform internal format according to desired color space
internal_format = formatToColorSpace(internal_format, _colorSpace);
return internal_format;
}
......@@ -54,14 +57,14 @@ void texImage3D(bool compressed, GLenum target, GLint level, GLenum internalform
namespace ACGL {
namespace OpenGL {
SharedTexture2D loadTexture2DFromDDS(const std::string& _filename)
SharedTexture2D loadTexture2DFromDDS(const std::string& _filename, ColorSpace _colorSpace)
{
CDDSImage image;
SharedTexture2D texture;
if(image.load(_filename))
{
GLenum internal_format = getDDSInternalFormat(image.is_compressed(), image.get_format());
GLenum internal_format = getDDSInternalFormat(image.is_compressed(), image.get_format(), _colorSpace);
if(image.is_volume() || image.is_cubemap())
{
......@@ -91,7 +94,7 @@ SharedTexture2D loadTexture2DFromDDS(const std::string& _filename)
return texture;
}
SharedTexture3D loadTexture3DFromDDS(const std::string& _filename)
SharedTexture3D loadTexture3DFromDDS(const std::string& _filename, ColorSpace _colorSpace)
{
CDDSImage image;
SharedTexture3D texture;
......@@ -128,7 +131,7 @@ SharedTexture3D loadTexture3DFromDDS(const std::string& _filename)
return texture;
}
SharedTextureCubeMap loadTextureCubeMapFromDDS(const std::string& _filename)
SharedTextureCubeMap loadTextureCubeMapFromDDS(const std::string& _filename, ColorSpace _colorSpace)
{
CDDSImage image;
SharedTextureCubeMap texture;
......
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