Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions nel/include/nel/misc/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const uint32 DXT_HEADER = NL_MAKEFOURCC('D', 'X', 'T', '\0');
const uint32 PNG_HEADER = NL_MAKEFOURCC(0x89, 'P', 'N', 'G');
const uint32 JPG_HEADER = NL_MAKEFOURCC(0xff, 0xd8, 0xff, 0xe0);
const uint32 GIF_HEADER = NL_MAKEFOURCC('G', 'I', 'F', '8');
const uint32 KTX_HEADER = NL_MAKEFOURCC(0xAB, 'K', 'T', 'X');


// dwLinearSize is valid
Expand Down Expand Up @@ -145,6 +146,16 @@ private :
uint8 readGIF( NLMISC::IStream &f );


/**
* Read a KTX1 from an IStream.
* KTX files can contain compressed (DXTC) or uncompressed texture data.
* \param f IStream (must be a reading stream)
* \param mipMapSkip number of mipmaps to skip
* \return image depth if succeed, 0 else
*/
uint8 readKTX( NLMISC::IStream &f, uint mipMapSkip );


/**
* Change bitmap format
*
Expand Down Expand Up @@ -330,21 +341,23 @@ private :
void swap(CBitmap &other);

/**
* Read a bitmap(TGA, JPEG, PNG or DDS) from an IStream.
* Bitmap supported are DDS (DXTC1, DXTC1 with Alpha, DXTC3, DXTC5), PNG, JPEG and
* uncompressed TGA (24 and 32 bits).
* Read a bitmap(TGA, JPEG, PNG, DDS or KTX) from an IStream.
* Bitmap supported are DDS (DXTC1, DXTC1 with Alpha, DXTC3, DXTC5),
* KTX (DXTC1, DXTC3, DXTC5, RGBA, Luminance, Alpha),
* PNG, JPEG and uncompressed TGA (24 and 32 bits).
* \param IStream The stream must be in reading mode.
* \param mipMapSkip if the file is a DDS with mipMap. N=mipMapSkip mipmaps are skipped.
* \param mipMapSkip if the file is a DDS or KTX with mipMap. N=mipMapSkip mipmaps are skipped.
* \return image depth (24 or 32), or 0 if load failed
* \throw ESeekFailed : seek has failed
*/
uint8 load(NLMISC::IStream &f, uint mipMapSkip=0);


/**
* Determinate the bitmap size from a bitmap(TGA or DDS) from an IStream. load just header of the file.
* Bitmap supported are DDS (DXTC1, DXTC1 with Alpha, DXTC3, DXTC5), PNG, JPEG and
* uncompressed TGA (24 and 32 bits).
* Determinate the bitmap size from a bitmap(TGA, DDS or KTX) from an IStream. load just header of the file.
* Bitmap supported are DDS (DXTC1, DXTC1 with Alpha, DXTC3, DXTC5),
* KTX (DXTC1, DXTC3, DXTC5, RGBA, Luminance, Alpha),
* PNG, JPEG and uncompressed TGA (24 and 32 bits).
* NB: at the end, f is seeked to begin.
* \param IStream The stream must be in reading mode.
* \param width the width of the image. 0 if fails.
Expand Down
39 changes: 39 additions & 0 deletions nel/src/misc/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ uint8 CBitmap::load(NLMISC::IStream &f, uint mipMapSkip)
}
#endif // USE_GIF

if (fileType == KTX_HEADER)
{
#ifdef NEL_ALL_BITMAP_WHITE
uint8 result = readKTX(f, mipMapSkip);
MakeWhite (*this);
return result;
#else // NEL_ALL_BITMAP_WHITE
return readKTX(f, mipMapSkip);
#endif // NEL_ALL_BITMAP_WHITE
}

// assuming it's TGA
NLMISC::IStream::TSeekOrigin origin= f.begin;
if(!f.seek (0, origin))
Expand Down Expand Up @@ -3667,6 +3678,34 @@ void CBitmap::loadSize(NLMISC::IStream &f, uint32 &retWidth, uint32 &retHeight)
retWidth = lsWidth;
retHeight = lsHeight;
}
else if(fileType == KTX_HEADER)
{
// skip remaining 8 bytes of KTX identifier
f.seek(8, IStream::current);

// read endianness indicator using raw read (KTX has its own endianness handling)
uint32 endianness;
f.serialBuffer((uint8 *)&endianness, 4);
bool mustSwap = (endianness == 0x01020304);

// skip glType, glTypeSize, glFormat, glInternalFormat, glBaseInternalFormat (5 uint32s)
f.seek(5 * 4, IStream::current);

// read pixelWidth and pixelHeight using raw reads
uint32 w, h;
f.serialBuffer((uint8 *)&w, 4);
f.serialBuffer((uint8 *)&h, 4);

if (mustSwap)
{
NLMISC_BSWAP32(w);
NLMISC_BSWAP32(h);
}

retWidth = w;
// Per spec: pixelHeight 0 means 1D texture, treat as height 1
retHeight = (h == 0) ? 1 : h;
}
// assuming it's TGA
else
{
Expand Down
Loading