Skip to content

Commit 5a80af2

Browse files
Image: unified HDR and TGA loading; added option to only decode image description
1 parent 91dc6a2 commit 5a80af2

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

TextureLoader/src/Image.cpp

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -287,47 +287,64 @@ void Image::LoadTiffFile(const IDataBlob* pFileData, const ImageLoadInfo& LoadIn
287287
}
288288

289289

290-
static bool LoadHDRFile(const IDataBlob* pSrcHdrBits, IDataBlob* pDstPixels, ImageDesc* pDstImgDesc)
290+
static bool LoadImageSTB(const void* pSrcImage,
291+
size_t ImageSize,
292+
VALUE_TYPE ComponentType,
293+
IDataBlob* pDstPixels,
294+
ImageDesc* pDstImgDesc)
291295
{
292-
Int32 Width = 0, Height = 0, NumComponents = 0;
293-
float* pFloatData = stbi_loadf_from_memory(pSrcHdrBits->GetConstDataPtr<stbi_uc>(), static_cast<Int32>(pSrcHdrBits->GetSize()), &Width, &Height, &NumComponents, 0);
294-
if (pFloatData == nullptr)
296+
int Width = 0;
297+
int Height = 0;
298+
int NumComponents = 0;
299+
void* pDecodedData = nullptr;
300+
if (pDstPixels != nullptr)
295301
{
296-
LOG_ERROR_MESSAGE("Failed to load HDR image from memory. STB supports only 32-bit rle rgbe textures");
297-
return false;
302+
switch (ComponentType)
303+
{
304+
case VT_FLOAT32:
305+
pDecodedData = stbi_loadf_from_memory(static_cast<const stbi_uc*>(pSrcImage), static_cast<int>(ImageSize), &Width, &Height, &NumComponents, 0);
306+
break;
307+
308+
case VT_UINT8:
309+
case VT_INT8:
310+
pDecodedData = stbi_load_from_memory(static_cast<const stbi_uc*>(pSrcImage), static_cast<int>(ImageSize), &Width, &Height, &NumComponents, 0);
311+
break;
312+
313+
case VT_UINT16:
314+
case VT_INT16:
315+
pDecodedData = stbi_load_16_from_memory(static_cast<const stbi_uc*>(pSrcImage), static_cast<int>(ImageSize), &Width, &Height, &NumComponents, 0);
316+
break;
317+
318+
default:
319+
UNEXPECTED("Unexpected component type");
320+
}
321+
322+
if (pDecodedData == nullptr)
323+
{
324+
return false;
325+
}
326+
}
327+
else
328+
{
329+
if (!stbi_info_from_memory(static_cast<const stbi_uc*>(pSrcImage), static_cast<int>(ImageSize), &Width, &Height, &NumComponents))
330+
{
331+
return false;
332+
}
298333
}
299334

300-
pDstImgDesc->ComponentType = VT_FLOAT32;
335+
pDstImgDesc->ComponentType = ComponentType;
301336
pDstImgDesc->Width = static_cast<Uint32>(Width);
302337
pDstImgDesc->Height = static_cast<Uint32>(Height);
303338
pDstImgDesc->NumComponents = NumComponents;
304-
pDstImgDesc->RowStride = pDstImgDesc->Width * pDstImgDesc->NumComponents * sizeof(float);
305-
306-
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
307-
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
308-
stbi_image_free(pFloatData);
309-
return true;
310-
}
311339

312-
static bool LoadTGAFile(const IDataBlob* pSrcTgaBits, IDataBlob* pDstPixels, ImageDesc* pDstImgDesc)
313-
{
314-
Int32 Width = 0, Height = 0, NumComponents = 0;
315-
Uint8* pFloatData = stbi_load_from_memory(pSrcTgaBits->GetConstDataPtr<stbi_uc>(), static_cast<Int32>(pSrcTgaBits->GetSize()), &Width, &Height, &NumComponents, 0);
316-
if (pFloatData == nullptr)
340+
if (pDstPixels != nullptr)
317341
{
318-
LOG_ERROR_MESSAGE("Failed to load TGA image from memory");
319-
return false;
342+
pDstImgDesc->RowStride = pDstImgDesc->Width * pDstImgDesc->NumComponents * GetValueSize(ComponentType);
343+
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
344+
memcpy(pDstPixels->GetDataPtr(), pDecodedData, pDstImgDesc->Height * pDstImgDesc->RowStride);
345+
stbi_image_free(pDecodedData);
320346
}
321347

322-
pDstImgDesc->ComponentType = VT_UINT8;
323-
pDstImgDesc->Width = static_cast<Uint32>(Width);
324-
pDstImgDesc->Height = static_cast<Uint32>(Height);
325-
pDstImgDesc->NumComponents = NumComponents;
326-
pDstImgDesc->RowStride = pDstImgDesc->Width * pDstImgDesc->NumComponents * sizeof(Uint8);
327-
328-
pDstPixels->Resize(pDstImgDesc->Height * pDstImgDesc->RowStride);
329-
memcpy(pDstPixels->GetDataPtr(), pFloatData, pDstImgDesc->Height * pDstImgDesc->RowStride);
330-
stbi_image_free(pFloatData);
331348
return true;
332349
}
333350

@@ -343,45 +360,58 @@ Image::Image(IReferenceCounters* pRefCounters,
343360
}
344361
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_HDR)
345362
{
346-
bool Res = LoadHDRFile(pFileData, m_pData, &m_Desc);
347-
if (!Res)
348-
LOG_ERROR_MESSAGE("Failed to load HDR image");
363+
if (!LoadImageSTB(pFileData->GetConstDataPtr(), pFileData->GetSize(), VT_FLOAT32, m_pData, &m_Desc))
364+
{
365+
LOG_ERROR_MESSAGE("Failed to load HDR image from memory. STB only supports 32-bit rle rgbe textures");
366+
return;
367+
}
349368
}
350369
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_TGA)
351370
{
352-
bool Res = LoadTGAFile(pFileData, m_pData, &m_Desc);
353-
if (!Res)
371+
if (!LoadImageSTB(pFileData->GetConstDataPtr(), pFileData->GetSize(), VT_UINT8, m_pData, &m_Desc))
372+
{
354373
LOG_ERROR_MESSAGE("Failed to load TGA image");
374+
return;
375+
}
355376
}
356377
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_PNG)
357378
{
358-
DECODE_PNG_RESULT Res = DecodePng(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc);
359-
if (Res != DECODE_PNG_RESULT_OK)
379+
if (DecodePng(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc) != DECODE_PNG_RESULT_OK)
380+
{
360381
LOG_ERROR_MESSAGE("Failed to decode png image");
382+
return;
383+
}
361384
}
362385
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_JPEG)
363386
{
364-
DECODE_JPEG_RESULT Res = DecodeJpeg(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc);
365-
if (Res != DECODE_JPEG_RESULT_OK)
387+
if (DecodeJpeg(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc) != DECODE_JPEG_RESULT_OK)
388+
{
366389
LOG_ERROR_MESSAGE("Failed to decode jpeg image");
390+
return;
391+
}
367392
}
368393
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_SGI)
369394
{
370-
auto Res = LoadSGI(pFileData, m_pData, &m_Desc);
371-
if (!Res)
395+
if (!LoadSGI(pFileData, m_pData, &m_Desc))
396+
{
372397
LOG_ERROR_MESSAGE("Failed to load SGI image");
398+
return;
399+
}
373400
}
374401
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_DDS)
375402
{
376403
LOG_ERROR_MESSAGE("An image can't be created from DDS file. Use CreateTextureFromFile() or CreateTextureFromDDS() functions.");
404+
return;
377405
}
378406
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_KTX)
379407
{
380408
LOG_ERROR_MESSAGE("An image can't be created from KTX file. Use CreateTextureFromFile() or CreateTextureFromKTX() functions.");
409+
return;
381410
}
382411
else
383412
{
384413
LOG_ERROR_MESSAGE("Unknown image format.");
414+
return;
385415
}
386416

387417
if (LoadInfo.PermultiplyAlpha && m_Desc.NumComponents == 4)

0 commit comments

Comments
 (0)