From ebfd8f8f810c9a41a4ec4919630046ad3baedb84 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 11 Mar 2014 18:27:37 -0400 Subject: [PATCH] Override LoadImage for ktx files --- IO/src/ImageLoaderKTX.cpp | 58 +++++++++++++++++++++++++++++++-------- IO/src/ImageLoaderKTX.h | 5 ++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/IO/src/ImageLoaderKTX.cpp b/IO/src/ImageLoaderKTX.cpp index edb1dc0..60d4ec1 100644 --- a/IO/src/ImageLoaderKTX.cpp +++ b/IO/src/ImageLoaderKTX.cpp @@ -62,6 +62,8 @@ #include "ScopedAllocator.h" #include "GLDefines.h" +#include "Image.h" +#include "CompressedImage.h" class ByteReader { private: @@ -138,8 +140,31 @@ ImageLoaderKTX::ImageLoaderKTX(const uint8 *rawData, const int32 rawDataSz) ImageLoaderKTX::~ImageLoaderKTX() { } +FasTC::Image<> *ImageLoaderKTX::LoadImage() { + + // Get rid of the pixel data if it exists... + if(m_PixelData) { + delete m_PixelData; + m_PixelData = NULL; + } + + if(!ReadData()) { + return NULL; + } + + if(!m_bIsCompressed) { + uint32 *pixels = reinterpret_cast(m_PixelData); + return new FasTC::Image<>(m_Width, m_Height, pixels); + } + + return new CompressedImage(m_Width, m_Height, m_Format, m_PixelData); +} + bool ImageLoaderKTX::ReadData() { + // Default is uncompressed + m_bIsCompressed = false; + ByteReader rdr (m_RawData, m_NumRawDataBytes); // First, check to make sure that the identifier is present... @@ -246,13 +271,24 @@ bool ImageLoaderKTX::ReadData() { return false; } - if(glType == 0 && - glFormat == 0 && - glInternalFormat == GL_COMPRESSED_RGBA_BPTC_UNORM) { - fprintf(stderr, "KTX loader - BPTC compressed textures unsupported!\n"); - return false; - // Load compressed texture... - // rdr.Advance(pixelWidth * pixelHeight); + m_Width = pixelWidth; + m_Height = pixelHeight; + + if(glType == 0 && glFormat == 0) { + switch(glInternalFormat) { + case GL_COMPRESSED_RGBA_BPTC_UNORM: + m_Format = FasTC::eCompressionFormat_BPTC; + default: + fprintf(stderr, "KTX loader - texture format (0x%x) unsupported!\n", glInternalFormat); + return false; + } + + uint32 dataSize = CompressedImage::GetCompressedSize(pixelWidth * pixelHeight * 4, m_Format); + m_PixelData = new uint8[dataSize]; + memcpy(m_PixelData, rdr.GetData(), dataSize); + rdr.Advance(dataSize); + + m_bIsCompressed = true; } else { if(glType != GL_BYTE) { @@ -267,10 +303,10 @@ bool ImageLoaderKTX::ReadData() { // We should have RGBA8 data here so we can simply load it // as we normally would. - m_Width = pixelWidth; - m_Height = pixelHeight; - LoadFromPixelBuffer(reinterpret_cast(rdr.GetData())); - rdr.Advance(pixelWidth * pixelHeight * 4); + uint32 pixelDataSz = m_Width * m_Height * 4; + m_PixelData = new uint8[pixelDataSz]; + memcpy(m_PixelData, rdr.GetData(), pixelDataSz); + rdr.Advance(pixelDataSz); } return rdr.GetBytesLeft() == 0; } diff --git a/IO/src/ImageLoaderKTX.h b/IO/src/ImageLoaderKTX.h index 42df221..64dc74a 100644 --- a/IO/src/ImageLoaderKTX.h +++ b/IO/src/ImageLoaderKTX.h @@ -54,6 +54,7 @@ #define _IO_SRC_IMAGE_LOADER_KTX_H_ #include "ImageLoader.h" +#include "CompressionFormat.h" class ImageLoaderKTX : public ImageLoader { public: @@ -68,8 +69,12 @@ class ImageLoaderKTX : public ImageLoader { m_Processor = proc; } + virtual FasTC::Image<> *LoadImage(); private: KTXKeyValueProcessor m_Processor; + + bool m_bIsCompressed; + FasTC::ECompressionFormat m_Format; }; #endif // _IO_SRC_IMAGE_LOADER_KTX_H_