Here is my code (please, note that i'm just beginning c++ programming ).
//CImageLoaderKTX.h
Code: Select all
#ifndef __C_IMAGE_LOADER_KTX_H_INCLUDED__
#define __C_IMAGE_LOADER_KTX_H_INCLUDED__
#include "IrrCompileConfig.h"
#if defined(_IRR_COMPILE_WITH_KTX_LOADER_)
#include "IImageLoader.h"
namespace irr
{
namespace video
{
struct ktxHeader //header specification -> http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
{
c8 identifier[4];
u32 endianness;
u32 glType;
u32 glTypeSize;
u32 glFormat;
u32 glInternalFormat;
u32 glBaseInternalFormat;
u32 pixelWidth;
u32 pixelHeight;
u32 pixelDepth;
u32 numberOfArrayElements;
u32 numberOfFaces;
u32 numberOfMipmapLevels;
u32 bytesOfKeyValueData;
};
class CImageLoaderKTX: public IImageLoader
{
public:
virtual bool isALoadableFileExtension(const io::path&filename) const _IRR_OVERRIDE_;
virtual bool isALoadableFileFormat(io::IReadFile*file) const _IRR_OVERRIDE_;
virtual IImage*loadImage(io::IReadFile*file) const _IRR_OVERRIDE_;
};
}
}
#endif
#endif
Code: Select all
#include "CImageLoaderKTX.h"
#if defined(_IRR_COMPILE_WITH_KTX_LOADER_)
#include "IReadFile.h"
#include "os.h"
#include "CColorConverter.h"
#include "CImage.h"
#include "irrString.h"
namespace irr
{
namespace video
{
bool CImageLoaderKTX::isALoadableFileExtension(const io::path&filename) const
{
return core::hasFileExtension(filename, "ktx");
}
bool CImageLoaderKTX::isALoadableFileFormat(io::IReadFile*file) const
{
//TODO -> didn't implement yet
return true;
}
//loadImage -> I used CImageLoaderDDS::loadImage(io::IReadFile*file) as reference
IImage*CImageLoaderKTX::loadImage(io::IReadFile*file) const
{
ktxHeader header;
IImage*image = 0;
u32 dataSize; //size of loaded image
//read KTX header
file->seek(0);
file->read(&header, sizeof(ktxHeader));
/*
KTX files contain header + optional metadata + actual image data.
We need to skip header + metadata (size=header.bytesOfKeyValueData) to read image data.
*/
file->seek(sizeof(ktxHeader) + header.bytesOfKeyValueData);
//the next line was taken from DDS loader - don't completely understand it
dataSize = header.pixelWidth * header.pixelHeight * header.pixelDepth * 3; //3 bytes for R8G8B8 format
//next -> we allocate data for actual image and read it
u8*data = new u8[dataSize];
file->read(data, dataSize);
//we always assume that format is ECF_ETC1 + always have mipmaps
image = new CImage(ECF_ETC1, core::dimension2d<u32>(header.pixelWidth, header.pixelHeight), data, true, true, true, true);
return image;
}
IImageLoader*createImageLoaderKTX()
{
return new CImageLoaderKTX();
}
}
}
#endif
Can anyone say what's wrong?