Skip to content

Commit a1f55fc

Browse files
Fixed issue with row stride in TIFF loader
1 parent de7d055 commit a1f55fc

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

TextureLoader/src/Image.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ void Image::LoadTiffFile(IDataBlob* pFileData, const ImageLoadInfo& LoadInfo)
208208
LOG_ERROR_AND_THROW("Unknown sample format: ", Uint32{SampleFormat});
209209
}
210210

211-
auto ScanlineSize = TIFFScanlineSize(TiffFile);
212-
m_Desc.RowStride = AlignUp(m_Desc.Width * m_Desc.NumComponents * (BitsPerSample / 8), 4u);
211+
size_t ScanlineSize = TIFFScanlineSize(TiffFile);
212+
m_Desc.RowStride = AlignUp(m_Desc.Width * m_Desc.NumComponents * (BitsPerSample / 8), 4u);
213213
m_pData->Resize(size_t{m_Desc.Height} * size_t{m_Desc.RowStride});
214214

215215
Uint16 PlanarConfig = 0;
216216
TIFFGetField(TiffFile, TIFFTAG_PLANARCONFIG, &PlanarConfig);
217217
if (PlanarConfig == PLANARCONFIG_CONTIG || m_Desc.NumComponents == 1)
218218
{
219-
VERIFY_EXPR(m_Desc.RowStride >= static_cast<Uint32>(ScanlineSize));
219+
VERIFY_EXPR(m_Desc.RowStride >= ScanlineSize);
220220
auto* pDataPtr = reinterpret_cast<Uint8*>(m_pData->GetDataPtr());
221221
for (Uint32 row = 0; row < m_Desc.Height; row++, pDataPtr += m_Desc.RowStride)
222222
{
@@ -226,32 +226,33 @@ void Image::LoadTiffFile(IDataBlob* pFileData, const ImageLoadInfo& LoadInfo)
226226
else if (PlanarConfig == PLANARCONFIG_SEPARATE)
227227
{
228228
std::vector<Uint8> ScanlineData(ScanlineSize);
229-
for (Uint16 comp = 0; comp < m_Desc.NumComponents; ++comp)
229+
for (Uint32 row = 0; row < m_Desc.Height; ++row)
230230
{
231-
for (Uint32 row = 0; row < m_Desc.Height; ++row)
231+
for (Uint16 comp = 0; comp < m_Desc.NumComponents; ++comp)
232232
{
233+
auto* const pDstRow = reinterpret_cast<Uint8*>(m_pData->GetDataPtr()) + m_Desc.RowStride * row + comp;
234+
233235
TIFFReadScanline(TiffFile, ScanlineData.data(), row, comp);
234236

235-
auto CopyComponet = [&](const auto* pSrc, auto* pDst) {
236-
pDst += row * m_Desc.Width * m_Desc.NumComponents + comp;
237-
for (Uint32 x = 0; x < m_Desc.Width; ++x)
237+
auto CopyComponet = [Width = m_Desc.Width, NumComp = m_Desc.NumComponents](const auto* pSrc, auto* pDst) {
238+
for (Uint32 x = 0; x < Width; ++x)
238239
{
239-
pDst[x * m_Desc.NumComponents] = pSrc[x];
240+
pDst[x * NumComp] = pSrc[x];
240241
}
241242
};
242243

243244
switch (BitsPerSample)
244245
{
245246
case 8:
246-
CopyComponet(reinterpret_cast<const Uint8*>(ScanlineData.data()), reinterpret_cast<Uint8*>(m_pData->GetDataPtr()));
247+
CopyComponet(reinterpret_cast<const Uint8*>(ScanlineData.data()), reinterpret_cast<Uint8*>(pDstRow));
247248
break;
248249

249250
case 16:
250-
CopyComponet(reinterpret_cast<const Uint16*>(ScanlineData.data()), reinterpret_cast<Uint16*>(m_pData->GetDataPtr()));
251+
CopyComponet(reinterpret_cast<const Uint16*>(ScanlineData.data()), reinterpret_cast<Uint16*>(pDstRow));
251252
break;
252253

253254
case 32:
254-
CopyComponet(reinterpret_cast<const Uint32*>(ScanlineData.data()), reinterpret_cast<Uint32*>(m_pData->GetDataPtr()));
255+
CopyComponet(reinterpret_cast<const Uint32*>(ScanlineData.data()), reinterpret_cast<Uint32*>(pDstRow));
255256
break;
256257

257258
default:

0 commit comments

Comments
 (0)