Skip to content

Commit

Permalink
Texture Loader: Added TGA format support (close #231)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets committed Aug 14, 2024
1 parent 64fa5a3 commit fdd9bc2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions TextureLoader/interface/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ DILIGENT_TYPED_ENUM(IMAGE_FILE_FORMAT, Uint8){

// HDR file
IMAGE_FILE_FORMAT_HDR,

// TGA file
IMAGE_FILE_FORMAT_TGA,
};

/// Image loading information
Expand Down
33 changes: 33 additions & 0 deletions TextureLoader/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#define STBI_ONLY_HDR
#define STBI_ONLY_TGA
#include "../../ThirdParty/stb/stb_image.h"
#ifdef __clang__
# pragma clang diagnostic pop
Expand Down Expand Up @@ -300,7 +301,28 @@ static bool LoadHDRFile(IDataBlob* pSrcHdrBits, IDataBlob* pDstPixels, ImageDesc
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
stbi_image_free(pFloatData);
return true;
}

static bool LoadTGAFile(IDataBlob* pSrcTgaBits, IDataBlob* pDstPixels, ImageDesc* pDstImgDesc)
{
Int32 Width = 0, Height = 0, NumComponents = 0;
Uint8* pFloatData = stbi_load_from_memory(static_cast<const Uint8*>(pSrcTgaBits->GetConstDataPtr()), static_cast<Int32>(pSrcTgaBits->GetSize()), &Width, &Height, &NumComponents, 0);
if (pFloatData == nullptr)
{
LOG_ERROR_MESSAGE("Failed to load TGA image from memory");
return false;
}

pDstImgDesc->ComponentType = VT_UINT8;
pDstImgDesc->Width = static_cast<Uint32>(Width);
pDstImgDesc->Height = static_cast<Uint32>(Height);
pDstImgDesc->NumComponents = NumComponents;
pDstImgDesc->RowStride = pDstImgDesc->Width * pDstImgDesc->NumComponents * sizeof(Uint8);

pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
stbi_image_free(pFloatData);
return true;
}

Expand All @@ -320,6 +342,12 @@ Image::Image(IReferenceCounters* pRefCounters,
if (!Res)
LOG_ERROR_MESSAGE("Failed to load HDR image");
}
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_TGA)
{
bool Res = LoadTGAFile(pFileData, m_pData.RawPtr(), &m_Desc);
if (!Res)
LOG_ERROR_MESSAGE("Failed to load TGA image");
}
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_PNG)
{
auto Res = DecodePng(pFileData, m_pData.RawPtr(), &m_Desc);
Expand Down Expand Up @@ -512,6 +540,9 @@ IMAGE_FILE_FORMAT Image::GetFileFormat(const Uint8* pData, size_t Size, const ch
if (Size >= 11 && memcmp(pData, HDRFileIdentifier, sizeof(HDRFileIdentifier)) == 0)
return IMAGE_FILE_FORMAT_HDR;

if (Size >= 3 && pData[0] == 0x00 && pData[1] == 0x00 && pData[2] == 0x02)
return IMAGE_FILE_FORMAT_TGA;

if (Size >= 2 && pData[0] == 0x01 && pData[1] == 0xDA)
return IMAGE_FILE_FORMAT_SGI;
}
Expand Down Expand Up @@ -548,6 +579,8 @@ IMAGE_FILE_FORMAT Image::GetFileFormat(const Uint8* pData, size_t Size, const ch
return IMAGE_FILE_FORMAT_SGI;
else if (Extension == "hdr")
return IMAGE_FILE_FORMAT_HDR;
else if (Extension == "tga")
return IMAGE_FILE_FORMAT_TGA;
else
LOG_ERROR_MESSAGE("Unrecognized image file extension", Extension);
}
Expand Down
3 changes: 2 additions & 1 deletion TextureLoader/src/TextureLoaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ TextureLoaderImpl::TextureLoaderImpl(IReferenceCounters* pRefCounters,
ImgFileFormat == IMAGE_FILE_FORMAT_JPEG ||
ImgFileFormat == IMAGE_FILE_FORMAT_TIFF ||
ImgFileFormat == IMAGE_FILE_FORMAT_SGI ||
ImgFileFormat == IMAGE_FILE_FORMAT_HDR)
ImgFileFormat == IMAGE_FILE_FORMAT_HDR ||
ImgFileFormat == IMAGE_FILE_FORMAT_TGA)
{
ImageLoadInfo ImgLoadInfo;
ImgLoadInfo.Format = ImgFileFormat;
Expand Down

0 comments on commit fdd9bc2

Please sign in to comment.