Skip to content

Commit 08b31f1

Browse files
SGI Loader: added option to only decode image description
1 parent 5a80af2 commit 08b31f1

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

TextureLoader/interface/SGILoader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ DILIGENT_BEGIN_NAMESPACE(Diligent)
3131
/// Loads an SGI image.
3232

3333
/// \param [in] pSGIData - SGI image data.
34+
/// \param [in] DataSize - Size of the data.
3435
/// \param [out] pDstPixels - Destination pixels data blob. The pixels are always tightly packed
3536
/// (for instance, components of a 3-channel image will be written as |r|g|b|r|g|b|r|g|b|...).
3637
/// \param [out] pDstImgDesc - Image description.
3738
/// \return true if the image has been loaded successfully, and false otherwise.
38-
bool DILIGENT_GLOBAL_FUNCTION(LoadSGI)(const IDataBlob* pSGIData,
39-
IDataBlob* pDstPixels,
40-
ImageDesc* pDstImgDesc);
39+
///
40+
/// \remarks If pDstPixels is null, the function will only decode the image description and return true.
41+
bool DILIGENT_GLOBAL_FUNCTION(LoadSGI)(const void* pSGIData,
42+
size_t DataSize,
43+
IDataBlob* pDstPixels,
44+
ImageDesc* pDstImgDesc);
4145

4246
DILIGENT_END_NAMESPACE // namespace Diligent

TextureLoader/src/Image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ Image::Image(IReferenceCounters* pRefCounters,
392392
}
393393
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_SGI)
394394
{
395-
if (!LoadSGI(pFileData, m_pData, &m_Desc))
395+
if (!LoadSGI(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc))
396396
{
397397
LOG_ERROR_MESSAGE("Failed to load SGI image");
398398
return;

TextureLoader/src/SGILoader.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ static_assert(sizeof(SGIHeader) == 512, "must be 512 bytes");
5858
} // namespace
5959

6060
// http://paulbourke.net/dataformats/sgirgb/sgiversion.html
61-
bool LoadSGI(const IDataBlob* pSGIData,
62-
IDataBlob* pDstPixels,
63-
ImageDesc* pDstImgDesc)
61+
bool LoadSGI(const void* pSGIData,
62+
size_t DataSize,
63+
IDataBlob* pDstPixels,
64+
ImageDesc* pDstImgDesc)
6465
{
65-
VERIFY_EXPR(pSGIData != nullptr && pDstPixels != nullptr && pDstImgDesc != nullptr);
66-
const Uint8* pDataStart = pSGIData->GetConstDataPtr<Uint8>();
67-
const size_t Size = pSGIData->GetSize();
68-
const Uint8* pDataEnd = pDataStart + Size;
66+
VERIFY_EXPR(pSGIData != nullptr && pDstImgDesc != nullptr);
67+
const Uint8* pDataStart = static_cast<const Uint8*>(pSGIData);
68+
const Uint8* pDataEnd = pDataStart + DataSize;
6969
const Uint8* pSrcPtr = pDataStart;
7070

71-
if (Size < sizeof(SGIHeader))
71+
if (DataSize < sizeof(SGIHeader))
7272
{
73-
LOG_ERROR_MESSAGE("The SGI data size (", Size, ") is smaller than the size of required SGI header (", sizeof(SGIHeader), ").");
73+
LOG_ERROR_MESSAGE("The SGI data size (", DataSize, ") is smaller than the size of required SGI header (", sizeof(SGIHeader), ").");
7474
return false;
7575
}
7676

77-
const auto& Header = reinterpret_cast<const SGIHeader&>(*pSrcPtr);
77+
const SGIHeader& Header = reinterpret_cast<const SGIHeader&>(*pSrcPtr);
7878
pSrcPtr += sizeof(SGIHeader);
7979

8080
constexpr Uint16 SGIMagic = 0xda01u;
@@ -111,6 +111,9 @@ bool LoadSGI(const IDataBlob* pSGIData,
111111
return false;
112112
}
113113

114+
if (pDstPixels == nullptr)
115+
return true;
116+
114117
pDstImgDesc->RowStride = Width * NumChannels * BytesPerChannel;
115118
pDstPixels->Resize(size_t{Height} * pDstImgDesc->RowStride);
116119
Uint8* pDstPtr = pDstPixels->GetDataPtr<Uint8>();
@@ -151,14 +154,14 @@ bool LoadSGI(const IDataBlob* pSGIData,
151154
// RLE-compressed SGI image
152155

153156
// Offsets table starts at byte 512 and is Height * NumChannels * 4 bytes long.
154-
const auto TableSize = sizeof(Uint32) * Height * NumChannels;
155-
const auto* OffsetTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
157+
const size_t TableSize = sizeof(Uint32) * Height * NumChannels;
158+
const Uint32* OffsetTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
156159
pSrcPtr += TableSize;
157160
if (pSrcPtr > pDataEnd)
158161
return false;
159162

160163
// Length table follows the offsets table and is the same size.
161-
const auto* LengthTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
164+
const Uint32* LengthTableBE = reinterpret_cast<const Uint32*>(pSrcPtr);
162165
pSrcPtr += TableSize;
163166
if (pSrcPtr > pDataEnd)
164167
return false;
@@ -207,11 +210,11 @@ bool LoadSGI(const IDataBlob* pSGIData,
207210
{
208211
// Each unsigned int in the offset table is the offset (from the file start) to the
209212
// start of the compressed data of each scanline for each channel.
210-
const auto RleOff = PlatformMisc::SwapBytes(OffsetTableBE[y + c * Height]);
213+
const Uint32 RleOff = PlatformMisc::SwapBytes(OffsetTableBE[y + c * Height]);
211214
// The size table tells the size of the compressed data (unsigned int) of each scanline.
212-
const auto RleLen = PlatformMisc::SwapBytes(LengthTableBE[y + c * Height]);
215+
const Uint32 RleLen = PlatformMisc::SwapBytes(LengthTableBE[y + c * Height]);
213216

214-
auto* DstLine = pDstPtr + y * size_t{pDstImgDesc->RowStride} + c;
217+
Uint8* DstLine = pDstPtr + y * size_t{pDstImgDesc->RowStride} + c;
215218
if (!ReadLine(DstLine, pDataStart + RleOff, pSrcPtr + RleOff + RleLen))
216219
return false;
217220
}
@@ -230,10 +233,11 @@ bool LoadSGI(const IDataBlob* pSGIData,
230233

231234
extern "C"
232235
{
233-
void Diligent_LoadSGI(const Diligent::IDataBlob* pSGIData,
234-
Diligent::IDataBlob* pDstPixels,
235-
Diligent::ImageDesc* pDstImgDesc)
236+
void Diligent_LoadSGI(const void* pSGIData,
237+
size_t DataSize,
238+
Diligent::IDataBlob* pDstPixels,
239+
Diligent::ImageDesc* pDstImgDesc)
236240
{
237-
Diligent::LoadSGI(pSGIData, pDstPixels, pDstImgDesc);
241+
Diligent::LoadSGI(pSGIData, DataSize, pDstPixels, pDstImgDesc);
238242
}
239243
}

0 commit comments

Comments
 (0)