Skip to content

Commit 91dc6a2

Browse files
DecodePng and DecodeJpeg: take raw pointer to the source image instead of data blob
1 parent 20ade3a commit 91dc6a2

File tree

8 files changed

+46
-36
lines changed

8 files changed

+46
-36
lines changed

Tests/DiligentToolsTest/src/JPEGCodecTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ TEST(Tools_TextureLoader, JPEGCodec)
6464

6565
{
6666
ImageDesc DecodedImgDesc;
67-
EXPECT_EQ(DecodeJpeg(pJpgData, nullptr, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
67+
EXPECT_EQ(DecodeJpeg(pJpgData->GetConstDataPtr(), pJpgData->GetSize(), nullptr, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
6868

6969
EXPECT_EQ(DecodedImgDesc.Width, TestImgWidth);
7070
EXPECT_EQ(DecodedImgDesc.Height, TestImgHeight);
@@ -76,7 +76,7 @@ TEST(Tools_TextureLoader, JPEGCodec)
7676
RefCntAutoPtr<IDataBlob> pDecodedPixelsBlob = DataBlobImpl::Create();
7777

7878
ImageDesc DecodedImgDesc;
79-
ASSERT_EQ(DecodeJpeg(pJpgData, pDecodedPixelsBlob, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
79+
ASSERT_EQ(DecodeJpeg(pJpgData->GetConstDataPtr(), pJpgData->GetSize(), pDecodedPixelsBlob, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
8080

8181
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
8282
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);

Tests/DiligentToolsTest/src/PNGCodecTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEST(Tools_TextureLoader, PNGCodec)
7272

7373
{
7474
ImageDesc DecodedImgDesc;
75-
EXPECT_EQ(DecodePng(pPngData, nullptr, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
75+
EXPECT_EQ(DecodePng(pPngData->GetConstDataPtr(), pPngData->GetSize(), nullptr, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
7676

7777
EXPECT_EQ(DecodedImgDesc.Width, TestImgWidth);
7878
EXPECT_EQ(DecodedImgDesc.Height, TestImgHeight);
@@ -84,7 +84,7 @@ TEST(Tools_TextureLoader, PNGCodec)
8484
RefCntAutoPtr<IDataBlob> pDecodedPixelsBlob = DataBlobImpl::Create();
8585

8686
ImageDesc DecodedImgDesc;
87-
ASSERT_EQ(DecodePng(pPngData, pDecodedPixelsBlob, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
87+
ASSERT_EQ(DecodePng(pPngData->GetConstDataPtr(), pPngData->GetSize(), pDecodedPixelsBlob, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
8888

8989
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
9090
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);

TextureLoader/interface/JPEGCodec.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,17 @@ DILIGENT_TYPED_ENUM(ENCODE_JPEG_RESULT, Uint32)
6565
/// Decodes jpeg image.
6666

6767
/// \param [in] pSrcJpegBits - JPEG image encoded bits.
68+
/// \param [in] JpegDataSize - Size of the encoded JPEG image data.
6869
/// \param [out] pDstPixels - Decoded pixels data blob. The pixels are always tightly packed
6970
/// (for instance, components of 3-channel image will be written as |r|g|b|r|g|b|r|g|b|...).
7071
/// \param [out] pDstImgDesc - Decoded image description.
7172
/// \return Decoding result, see Diligent::DECODE_JPEG_RESULT.
7273
///
7374
/// \remarks If pDstPixels is null, the function will only decode the image description and return DECODE_JPEG_RESULT_OK.
74-
DECODE_JPEG_RESULT DILIGENT_GLOBAL_FUNCTION(DecodeJpeg)(const IDataBlob* pSrcJpegBits,
75-
IDataBlob* pDstPixels,
76-
ImageDesc* pDstImgDesc);
75+
DECODE_JPEG_RESULT DILIGENT_GLOBAL_FUNCTION(DecodeJpeg)(const void* pSrcJpegBits,
76+
size_t JpegDataSize,
77+
IDataBlob* pDstPixels,
78+
ImageDesc* pDstImgDesc);
7779

7880

7981
/// Encodes an image jpeg PNG format.

TextureLoader/interface/PNGCodec.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ DILIGENT_TYPED_ENUM(ENCODE_PNG_RESULT, Uint32)
7171
/// Decodes png image.
7272

7373
/// \param [in] pSrcPngBits - PNG image encoded bits.
74+
/// \param [in] PngDataSize - Size of the PNG image data, in bytes.
7475
/// \param [out] pDstPixels - Decoded pixels data blob. The pixels are always tightly packed
7576
/// (for instance, components of 3-channel image will be written as |r|g|b|r|g|b|r|g|b|...).
7677
/// \param [out] pDstImgDesc - Decoded image description.
7778
/// \return Decoding result, see Diligent::DECODE_PNG_RESULT.
7879
///
7980
/// \remarks If pDstPixels is null, the function will only decode the image description.
80-
DECODE_PNG_RESULT DILIGENT_GLOBAL_FUNCTION(DecodePng)(const IDataBlob* pSrcPngBits,
81-
IDataBlob* pDstPixels,
82-
ImageDesc* pDstImgDesc);
81+
DECODE_PNG_RESULT DILIGENT_GLOBAL_FUNCTION(DecodePng)(const void* pSrcPngBits,
82+
size_t PngDataSize,
83+
IDataBlob* pDstPixels,
84+
ImageDesc* pDstImgDesc);
8385

8486
/// Encodes an image into PNG format.
8587

TextureLoader/src/Image.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ Image::Image(IReferenceCounters* pRefCounters,
355355
}
356356
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_PNG)
357357
{
358-
auto Res = DecodePng(pFileData, m_pData, &m_Desc);
358+
DECODE_PNG_RESULT Res = DecodePng(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc);
359359
if (Res != DECODE_PNG_RESULT_OK)
360360
LOG_ERROR_MESSAGE("Failed to decode png image");
361361
}
362362
else if (LoadInfo.Format == IMAGE_FILE_FORMAT_JPEG)
363363
{
364-
auto Res = DecodeJpeg(pFileData, m_pData, &m_Desc);
364+
DECODE_JPEG_RESULT Res = DecodeJpeg(pFileData->GetConstDataPtr(), pFileData->GetSize(), m_pData, &m_Desc);
365365
if (Res != DECODE_JPEG_RESULT_OK)
366366
LOG_ERROR_MESSAGE("Failed to decode jpeg image");
367367
}

TextureLoader/src/JPEGCodec.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ my_error_exit(j_common_ptr cinfo)
5858
longjmp(myerr->setjmp_buffer, 1);
5959
}
6060

61-
DECODE_JPEG_RESULT Diligent_DecodeJpeg(const IDataBlob* pSrcJpegBits,
62-
IDataBlob* pDstPixels,
63-
ImageDesc* pDstImgDesc)
61+
DECODE_JPEG_RESULT Diligent_DecodeJpeg(const void* pSrcJpegBits,
62+
size_t JpegDataSize,
63+
IDataBlob* pDstPixels,
64+
ImageDesc* pDstImgDesc)
6465
{
6566
if (!pSrcJpegBits || !pDstImgDesc)
6667
return DECODE_JPEG_RESULT_INVALID_ARGUMENTS;
@@ -94,8 +95,8 @@ DECODE_JPEG_RESULT Diligent_DecodeJpeg(const IDataBlob* pSrcJpegBits,
9495
jpeg_create_decompress(&cinfo);
9596

9697
// Step 2: specify data source
97-
const unsigned char* pSrcPtr = IDataBlob_GetConstDataPtr(pSrcJpegBits, 0);
98-
unsigned long SrcSize = (unsigned long)IDataBlob_GetSize(pSrcJpegBits);
98+
const unsigned char* pSrcPtr = pSrcJpegBits;
99+
unsigned long SrcSize = (unsigned long)JpegDataSize;
99100
jpeg_mem_src(&cinfo, pSrcPtr, SrcSize);
100101

101102
// Step 3: read file parameters with jpeg_read_header()

TextureLoader/src/PNGCodec.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ typedef struct PNGReadFnState PNGReadFnState;
4343
static void PngReadCallback(png_structp pngPtr, png_bytep data, png_size_t length)
4444
{
4545
PNGReadFnState* pState = (PNGReadFnState*)(png_get_io_ptr(pngPtr));
46-
const void* pSrcPtr = IDataBlob_GetConstDataPtr(pState->pPngBits, pState->Offset);
46+
const void* pSrcPtr = (const char*)pState->pPngBits + pState->Offset;
4747
memcpy(data, pSrcPtr, length);
4848
pState->Offset += length;
4949
}
5050

51-
DECODE_PNG_RESULT Diligent_DecodePng(const IDataBlob* pSrcPngBits,
52-
IDataBlob* pDstPixels,
53-
ImageDesc* pDstImgDesc)
51+
DECODE_PNG_RESULT Diligent_DecodePng(const void* pSrcPngBits,
52+
size_t PngDataSize,
53+
IDataBlob* pDstPixels,
54+
ImageDesc* pDstImgDesc)
5455
{
5556
if (!pSrcPngBits || !pDstImgDesc)
5657
return DECODE_PNG_RESULT_INVALID_ARGUMENTS;
@@ -60,7 +61,7 @@ DECODE_PNG_RESULT Diligent_DecodePng(const IDataBlob* pSrcPngBits,
6061
// https://gist.github.com/niw/5963798
6162

6263
const size_t PngSigSize = 8;
63-
png_const_bytep pngsig = (png_const_bytep)IDataBlob_GetConstDataPtr(pSrcPngBits, 0);
64+
png_const_bytep pngsig = (png_const_bytep)pSrcPngBits;
6465
//Let LibPNG check the signature. If this function returns 0, everything is OK.
6566
if (png_sig_cmp(pngsig, 0, PngSigSize) != 0)
6667
{

TextureLoader/src/TextureLoaderImpl.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@
5050

5151
extern "C"
5252
{
53-
Diligent::DECODE_PNG_RESULT Diligent_DecodePng(const Diligent::IDataBlob* pSrcPngBits,
54-
Diligent::IDataBlob* pDstPixels,
55-
Diligent::ImageDesc* pDstImgDesc);
53+
Diligent::DECODE_PNG_RESULT Diligent_DecodePng(const void* pSrcPngBits,
54+
size_t PngDataSize,
55+
Diligent::IDataBlob* pDstPixels,
56+
Diligent::ImageDesc* pDstImgDesc);
5657

5758
Diligent::ENCODE_PNG_RESULT Diligent_EncodePng(const Diligent::Uint8* pSrcPixels,
5859
Diligent::Uint32 Width,
@@ -61,9 +62,10 @@ extern "C"
6162
int PngColorType,
6263
Diligent::IDataBlob* pDstPngBits);
6364

64-
Diligent::DECODE_JPEG_RESULT Diligent_DecodeJpeg(const Diligent::IDataBlob* pSrcJpegBits,
65-
Diligent::IDataBlob* pDstPixels,
66-
Diligent::ImageDesc* pDstImgDesc);
65+
Diligent::DECODE_JPEG_RESULT Diligent_DecodeJpeg(const void* pSrcJpegBits,
66+
size_t JpegDataSize,
67+
Diligent::IDataBlob* pDstPixels,
68+
Diligent::ImageDesc* pDstImgDesc);
6769

6870
Diligent::ENCODE_JPEG_RESULT Diligent_EncodeJpeg(Diligent::Uint8* pSrcRGBData,
6971
Diligent::Uint32 Width,
@@ -79,11 +81,12 @@ extern "C"
7981
namespace Diligent
8082
{
8183

82-
DECODE_PNG_RESULT DecodePng(const IDataBlob* pSrcPngBits,
83-
IDataBlob* pDstPixels,
84-
ImageDesc* pDstImgDesc)
84+
DECODE_PNG_RESULT DecodePng(const void* pSrcPngBits,
85+
size_t PngDataSize,
86+
IDataBlob* pDstPixels,
87+
ImageDesc* pDstImgDesc)
8588
{
86-
return Diligent_DecodePng(pSrcPngBits, pDstPixels, pDstImgDesc);
89+
return Diligent_DecodePng(pSrcPngBits, PngDataSize, pDstPixels, pDstImgDesc);
8790
}
8891

8992
ENCODE_PNG_RESULT EncodePng(const Uint8* pSrcPixels,
@@ -97,11 +100,12 @@ ENCODE_PNG_RESULT EncodePng(const Uint8* pSrcPixels,
97100
}
98101

99102

100-
DECODE_JPEG_RESULT DecodeJpeg(const IDataBlob* pSrcJpegBits,
101-
IDataBlob* pDstPixels,
102-
ImageDesc* pDstImgDesc)
103+
DECODE_JPEG_RESULT DecodeJpeg(const void* pSrcJpegBits,
104+
size_t JpegDataSize,
105+
IDataBlob* pDstPixels,
106+
ImageDesc* pDstImgDesc)
103107
{
104-
return Diligent_DecodeJpeg(pSrcJpegBits, pDstPixels, pDstImgDesc);
108+
return Diligent_DecodeJpeg(pSrcJpegBits, JpegDataSize, pDstPixels, pDstImgDesc);
105109
}
106110

107111
ENCODE_JPEG_RESULT EncodeJpeg(Uint8* pSrcRGBPixels,

0 commit comments

Comments
 (0)