Skip to content

Commit fdd9bc2

Browse files
Texture Loader: Added TGA format support (close #231)
1 parent 64fa5a3 commit fdd9bc2

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

TextureLoader/interface/Image.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ DILIGENT_TYPED_ENUM(IMAGE_FILE_FORMAT, Uint8){
6666

6767
// HDR file
6868
IMAGE_FILE_FORMAT_HDR,
69+
70+
// TGA file
71+
IMAGE_FILE_FORMAT_TGA,
6972
};
7073

7174
/// Image loading information

TextureLoader/src/Image.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#define STB_IMAGE_IMPLEMENTATION
5656
#define STB_IMAGE_STATIC
5757
#define STBI_ONLY_HDR
58+
#define STBI_ONLY_TGA
5859
#include "../../ThirdParty/stb/stb_image.h"
5960
#ifdef __clang__
6061
# pragma clang diagnostic pop
@@ -300,7 +301,28 @@ static bool LoadHDRFile(IDataBlob* pSrcHdrBits, IDataBlob* pDstPixels, ImageDesc
300301
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
301302
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
302303
stbi_image_free(pFloatData);
304+
return true;
305+
}
306+
307+
static bool LoadTGAFile(IDataBlob* pSrcTgaBits, IDataBlob* pDstPixels, ImageDesc* pDstImgDesc)
308+
{
309+
Int32 Width = 0, Height = 0, NumComponents = 0;
310+
Uint8* pFloatData = stbi_load_from_memory(static_cast<const Uint8*>(pSrcTgaBits->GetConstDataPtr()), static_cast<Int32>(pSrcTgaBits->GetSize()), &Width, &Height, &NumComponents, 0);
311+
if (pFloatData == nullptr)
312+
{
313+
LOG_ERROR_MESSAGE("Failed to load TGA image from memory");
314+
return false;
315+
}
316+
317+
pDstImgDesc->ComponentType = VT_UINT8;
318+
pDstImgDesc->Width = static_cast<Uint32>(Width);
319+
pDstImgDesc->Height = static_cast<Uint32>(Height);
320+
pDstImgDesc->NumComponents = NumComponents;
321+
pDstImgDesc->RowStride = pDstImgDesc->Width * pDstImgDesc->NumComponents * sizeof(Uint8);
303322

323+
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
324+
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
325+
stbi_image_free(pFloatData);
304326
return true;
305327
}
306328

@@ -320,6 +342,12 @@ Image::Image(IReferenceCounters* pRefCounters,
320342
if (!Res)
321343
LOG_ERROR_MESSAGE("Failed to load HDR image");
322344
}
345+
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_TGA)
346+
{
347+
bool Res = LoadTGAFile(pFileData, m_pData.RawPtr(), &m_Desc);
348+
if (!Res)
349+
LOG_ERROR_MESSAGE("Failed to load TGA image");
350+
}
323351
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_PNG)
324352
{
325353
auto Res = DecodePng(pFileData, m_pData.RawPtr(), &m_Desc);
@@ -512,6 +540,9 @@ IMAGE_FILE_FORMAT Image::GetFileFormat(const Uint8* pData, size_t Size, const ch
512540
if (Size >= 11 && memcmp(pData, HDRFileIdentifier, sizeof(HDRFileIdentifier)) == 0)
513541
return IMAGE_FILE_FORMAT_HDR;
514542

543+
if (Size >= 3 && pData[0] == 0x00 && pData[1] == 0x00 && pData[2] == 0x02)
544+
return IMAGE_FILE_FORMAT_TGA;
545+
515546
if (Size >= 2 && pData[0] == 0x01 && pData[1] == 0xDA)
516547
return IMAGE_FILE_FORMAT_SGI;
517548
}
@@ -548,6 +579,8 @@ IMAGE_FILE_FORMAT Image::GetFileFormat(const Uint8* pData, size_t Size, const ch
548579
return IMAGE_FILE_FORMAT_SGI;
549580
else if (Extension == "hdr")
550581
return IMAGE_FILE_FORMAT_HDR;
582+
else if (Extension == "tga")
583+
return IMAGE_FILE_FORMAT_TGA;
551584
else
552585
LOG_ERROR_MESSAGE("Unrecognized image file extension", Extension);
553586
}

TextureLoader/src/TextureLoaderImpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ TextureLoaderImpl::TextureLoaderImpl(IReferenceCounters* pRefCounters,
139139
ImgFileFormat == IMAGE_FILE_FORMAT_JPEG ||
140140
ImgFileFormat == IMAGE_FILE_FORMAT_TIFF ||
141141
ImgFileFormat == IMAGE_FILE_FORMAT_SGI ||
142-
ImgFileFormat == IMAGE_FILE_FORMAT_HDR)
142+
ImgFileFormat == IMAGE_FILE_FORMAT_HDR ||
143+
ImgFileFormat == IMAGE_FILE_FORMAT_TGA)
143144
{
144145
ImageLoadInfo ImgLoadInfo;
145146
ImgLoadInfo.Format = ImgFileFormat;

0 commit comments

Comments
 (0)