Skip to content

Commit 20ade3a

Browse files
DecodePng: added option to only decode image description
1 parent ec826b4 commit 20ade3a

File tree

4 files changed

+74
-54
lines changed

4 files changed

+74
-54
lines changed

Tests/DiligentToolsTest/src/JPEGCodecTest.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,35 @@ TEST(Tools_TextureLoader, JPEGCodec)
6666
ImageDesc DecodedImgDesc;
6767
EXPECT_EQ(DecodeJpeg(pJpgData, nullptr, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
6868

69-
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
70-
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);
71-
ASSERT_EQ(DecodedImgDesc.NumComponents, NumComponents);
72-
ASSERT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
69+
EXPECT_EQ(DecodedImgDesc.Width, TestImgWidth);
70+
EXPECT_EQ(DecodedImgDesc.Height, TestImgHeight);
71+
EXPECT_EQ(DecodedImgDesc.NumComponents, NumComponents);
72+
EXPECT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
7373
}
7474

75-
RefCntAutoPtr<IDataBlob> pDecodedPixelsBlob = DataBlobImpl::Create();
75+
{
76+
RefCntAutoPtr<IDataBlob> pDecodedPixelsBlob = DataBlobImpl::Create();
7677

77-
ImageDesc DecodedImgDesc;
78-
EXPECT_EQ(DecodeJpeg(pJpgData, pDecodedPixelsBlob, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
78+
ImageDesc DecodedImgDesc;
79+
ASSERT_EQ(DecodeJpeg(pJpgData, pDecodedPixelsBlob, &DecodedImgDesc), DECODE_JPEG_RESULT_OK);
7980

80-
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
81-
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);
82-
ASSERT_EQ(DecodedImgDesc.NumComponents, NumComponents);
83-
ASSERT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
81+
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
82+
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);
83+
ASSERT_EQ(DecodedImgDesc.NumComponents, NumComponents);
84+
ASSERT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
8485

85-
const Uint8* pTestPixels = pDecodedPixelsBlob->GetConstDataPtr<Uint8>();
86-
for (Uint32 y = 0; y < TestImgHeight; ++y)
87-
{
88-
for (Uint32 x = 0; x < TestImgWidth; ++x)
86+
const Uint8* pTestPixels = pDecodedPixelsBlob->GetConstDataPtr<Uint8>();
87+
for (Uint32 y = 0; y < TestImgHeight; ++y)
8988
{
90-
for (Uint32 c = 0; c < NumComponents; ++c)
89+
for (Uint32 x = 0; x < TestImgWidth; ++x)
9190
{
92-
Uint8 RefVal = RefPixels[(x + y * TestImgWidth) * NumComponents + c];
93-
Uint8 TestVal = pTestPixels[x * DecodedImgDesc.NumComponents + c + y * DecodedImgDesc.RowStride];
94-
int Diff = std::abs(static_cast<int>(RefVal) - static_cast<int>(TestVal));
95-
EXPECT_LE(Diff, 1) << "[" << x << "," << y << "][" << c << "]: " << static_cast<int>(RefVal) << " vs " << static_cast<int>(TestVal);
91+
for (Uint32 c = 0; c < NumComponents; ++c)
92+
{
93+
Uint8 RefVal = RefPixels[(x + y * TestImgWidth) * NumComponents + c];
94+
Uint8 TestVal = pTestPixels[x * DecodedImgDesc.NumComponents + c + y * DecodedImgDesc.RowStride];
95+
int Diff = std::abs(static_cast<int>(RefVal) - static_cast<int>(TestVal));
96+
EXPECT_LE(Diff, 1) << "[" << x << "," << y << "][" << c << "]: " << static_cast<int>(RefVal) << " vs " << static_cast<int>(TestVal);
97+
}
9698
}
9799
}
98100
}

Tests/DiligentToolsTest/src/PNGCodecTest.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,45 @@ TEST(Tools_TextureLoader, PNGCodec)
6363
}
6464
}
6565

66-
auto pPngData = DataBlobImpl::Create();
66+
RefCntAutoPtr<IDataBlob> pPngData = DataBlobImpl::Create();
6767

68-
auto Res = EncodePng(RefPixels.data(), TestImgWidth, TestImgHeight, TestImgWidth * NumComponents,
69-
EncodeAlpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
70-
pPngData);
68+
ENCODE_PNG_RESULT Res = EncodePng(RefPixels.data(), TestImgWidth, TestImgHeight, TestImgWidth * NumComponents,
69+
EncodeAlpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
70+
pPngData);
7171
ASSERT_EQ(Res, ENCODE_PNG_RESULT_OK);
7272

73-
auto pDecodedPixelsBlob = DataBlobImpl::Create();
74-
75-
ImageDesc DecodedImgDesc;
76-
DecodePng(pPngData, pDecodedPixelsBlob, &DecodedImgDesc);
73+
{
74+
ImageDesc DecodedImgDesc;
75+
EXPECT_EQ(DecodePng(pPngData, nullptr, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
7776

78-
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
79-
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);
80-
ASSERT_EQ(DecodedImgDesc.NumComponents, NumComponents);
81-
ASSERT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
77+
EXPECT_EQ(DecodedImgDesc.Width, TestImgWidth);
78+
EXPECT_EQ(DecodedImgDesc.Height, TestImgHeight);
79+
EXPECT_EQ(DecodedImgDesc.NumComponents, NumComponents);
80+
EXPECT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
81+
}
8282

83-
const Uint8* pTestPixels = pDecodedPixelsBlob->GetConstDataPtr<Uint8>();
84-
for (Uint32 y = 0; y < TestImgHeight; ++y)
8583
{
86-
for (Uint32 x = 0; x < TestImgWidth; ++x)
84+
RefCntAutoPtr<IDataBlob> pDecodedPixelsBlob = DataBlobImpl::Create();
85+
86+
ImageDesc DecodedImgDesc;
87+
ASSERT_EQ(DecodePng(pPngData, pDecodedPixelsBlob, &DecodedImgDesc), DECODE_PNG_RESULT_OK);
88+
89+
ASSERT_EQ(DecodedImgDesc.Width, TestImgWidth);
90+
ASSERT_EQ(DecodedImgDesc.Height, TestImgHeight);
91+
ASSERT_EQ(DecodedImgDesc.NumComponents, NumComponents);
92+
ASSERT_EQ(DecodedImgDesc.ComponentType, VT_UINT8);
93+
94+
const Uint8* pTestPixels = pDecodedPixelsBlob->GetConstDataPtr<Uint8>();
95+
for (Uint32 y = 0; y < TestImgHeight; ++y)
8796
{
88-
for (Uint32 c = 0; c < NumComponents; ++c)
97+
for (Uint32 x = 0; x < TestImgWidth; ++x)
8998
{
90-
auto RefVal = RefPixels[(x + y * TestImgWidth) * NumComponents + c];
91-
auto TestVal = pTestPixels[x * DecodedImgDesc.NumComponents + c + y * DecodedImgDesc.RowStride];
92-
EXPECT_EQ(static_cast<Uint32>(RefVal), static_cast<Uint32>(TestVal)) << "[" << x << "," << y << "][" << c << "]";
99+
for (Uint32 c = 0; c < NumComponents; ++c)
100+
{
101+
Uint8 RefVal = RefPixels[(x + y * TestImgWidth) * NumComponents + c];
102+
Uint8 TestVal = pTestPixels[x * DecodedImgDesc.NumComponents + c + y * DecodedImgDesc.RowStride];
103+
EXPECT_EQ(static_cast<Uint32>(RefVal), static_cast<Uint32>(TestVal)) << "[" << x << "," << y << "][" << c << "]";
104+
}
93105
}
94106
}
95107
}

TextureLoader/interface/PNGCodec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ DILIGENT_TYPED_ENUM(ENCODE_PNG_RESULT, Uint32)
7575
/// (for instance, components of 3-channel image will be written as |r|g|b|r|g|b|r|g|b|...).
7676
/// \param [out] pDstImgDesc - Decoded image description.
7777
/// \return Decoding result, see Diligent::DECODE_PNG_RESULT.
78+
///
79+
/// \remarks If pDstPixels is null, the function will only decode the image description.
7880
DECODE_PNG_RESULT DILIGENT_GLOBAL_FUNCTION(DecodePng)(const IDataBlob* pSrcPngBits,
7981
IDataBlob* pDstPixels,
8082
ImageDesc* pDstImgDesc);

TextureLoader/src/PNGCodec.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ DECODE_PNG_RESULT Diligent_DecodePng(const IDataBlob* pSrcPngBits,
5252
IDataBlob* pDstPixels,
5353
ImageDesc* pDstImgDesc)
5454
{
55-
if (!pSrcPngBits || !pDstPixels || !pDstImgDesc)
55+
if (!pSrcPngBits || !pDstImgDesc)
5656
return DECODE_PNG_RESULT_INVALID_ARGUMENTS;
5757

5858
// http://www.piko3d.net/tutorials/libpng-tutorial-loading-png-files-from-streams/
@@ -159,24 +159,28 @@ DECODE_PNG_RESULT Diligent_DecodePng(const IDataBlob* pSrcPngBits,
159159
}
160160
}
161161

162-
//Array of row pointers. One for every row.
163-
rowPtrs = malloc(sizeof(png_bytep) * pDstImgDesc->Height);
162+
if (pDstPixels != NULL)
163+
{
164+
//Array of row pointers. One for every row.
165+
rowPtrs = malloc(sizeof(png_bytep) * pDstImgDesc->Height);
164166

165-
//Allocate a buffer with enough space.
166-
pDstImgDesc->RowStride = pDstImgDesc->Width * (Uint32)bit_depth * pDstImgDesc->NumComponents / 8u;
167-
// Align stride to 4 bytes
168-
pDstImgDesc->RowStride = (pDstImgDesc->RowStride + 3u) & ~3u;
167+
//Allocate a buffer with enough space.
168+
pDstImgDesc->RowStride = pDstImgDesc->Width * (Uint32)bit_depth * pDstImgDesc->NumComponents / 8u;
169+
// Align stride to 4 bytes
170+
pDstImgDesc->RowStride = (pDstImgDesc->RowStride + 3u) & ~3u;
169171

170-
IDataBlob_Resize(pDstPixels, pDstImgDesc->Height * (size_t)pDstImgDesc->RowStride);
171-
png_bytep pRow0 = IDataBlob_GetDataPtr(pDstPixels, 0);
172-
for (size_t i = 0; i < pDstImgDesc->Height; i++)
173-
rowPtrs[i] = pRow0 + i * pDstImgDesc->RowStride;
172+
IDataBlob_Resize(pDstPixels, pDstImgDesc->Height * (size_t)pDstImgDesc->RowStride);
173+
png_bytep pRow0 = IDataBlob_GetDataPtr(pDstPixels, 0);
174+
for (size_t i = 0; i < pDstImgDesc->Height; i++)
175+
rowPtrs[i] = pRow0 + i * pDstImgDesc->RowStride;
174176

175-
//Read the imagedata and write it to the addresses pointed to
176-
//by rowptrs (in other words: our image databuffer)
177-
png_read_image(png, rowPtrs);
177+
//Read the imagedata and write it to the addresses pointed to
178+
//by rowptrs (in other words: our image databuffer)
179+
png_read_image(png, rowPtrs);
180+
181+
free(rowPtrs);
182+
}
178183

179-
free(rowPtrs);
180184
png_destroy_read_struct(&png, &info, (png_infopp)0);
181185

182186
return DECODE_PNG_RESULT_OK;

0 commit comments

Comments
 (0)