From a9546da36837df760b30a0a0c1432e707b8655de Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Sat, 17 Aug 2019 22:05:38 +0200 Subject: [PATCH 1/2] optimize conversion with a union --- src/osgPlugins/dds/ReaderWriterDDS.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/osgPlugins/dds/ReaderWriterDDS.cpp b/src/osgPlugins/dds/ReaderWriterDDS.cpp index c4658a5e0..569e62013 100644 --- a/src/osgPlugins/dds/ReaderWriterDDS.cpp +++ b/src/osgPlugins/dds/ReaderWriterDDS.cpp @@ -989,12 +989,17 @@ osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead) } } - OSG_INFO<<"ReadDDS, dataType = 0x"<c8, 1024)) { OSG_WARN << "ReadDDSFile warning: couldn't read palette" << std::endl; return NULL; @@ -1030,20 +1035,16 @@ osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead) if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) { // Now we need to substitute the indexed image data with full RGBA image data. - unsigned char* convertedData = new unsigned char [sizeWithMipmaps * 4]; + PaletteWord* convertedData = new PaletteWord [sizeWithMipmaps]; + PaletteWord* pconvertedData = convertedData; for (unsigned int i = 0; i < sizeWithMipmaps; i++) - { - convertedData[i * 4 + 0] = palette[imageData[i] * 4 + 0]; - convertedData[i * 4 + 1] = palette[imageData[i] * 4 + 1]; - convertedData[i * 4 + 2] = palette[imageData[i] * 4 + 2]; - convertedData[i * 4 + 3] = palette[imageData[i] * 4 + 3]; - } + pconvertedData++->f32 = palette[imageData[i]].f32; delete [] imageData; for (unsigned int i = 0; i < mipmap_offsets.size(); i++) mipmap_offsets[i] *= 4; internalFormat = GL_RGBA; pixelFormat = GL_RGBA; - osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, convertedData, osg::Image::USE_NEW_DELETE, packing); + osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, convertedData->c8, osg::Image::USE_NEW_DELETE, packing); } else { From 5516f86f9eede6bfe4f8e6aa9e0e10f332fdcf4d Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Sun, 18 Aug 2019 17:56:51 +0200 Subject: [PATCH 2/2] use memcpy instead of union --- src/osgPlugins/dds/ReaderWriterDDS.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/osgPlugins/dds/ReaderWriterDDS.cpp b/src/osgPlugins/dds/ReaderWriterDDS.cpp index 569e62013..b854b887d 100644 --- a/src/osgPlugins/dds/ReaderWriterDDS.cpp +++ b/src/osgPlugins/dds/ReaderWriterDDS.cpp @@ -990,16 +990,12 @@ osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead) } OSG_INFO<<"ReadDDS, dataType = 0x"<c8, 1024)) + if (!_istream.read((char*)palette, 1024)) { OSG_WARN << "ReadDDSFile warning: couldn't read palette" << std::endl; return NULL; @@ -1035,16 +1031,19 @@ osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead) if (ddsd.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) { // Now we need to substitute the indexed image data with full RGBA image data. - PaletteWord* convertedData = new PaletteWord [sizeWithMipmaps]; - PaletteWord* pconvertedData = convertedData; + unsigned char * convertedData = new unsigned char [sizeWithMipmaps * 4]; + unsigned char * pconvertedData = convertedData; for (unsigned int i = 0; i < sizeWithMipmaps; i++) - pconvertedData++->f32 = palette[imageData[i]].f32; + { + memcpy(pconvertedData, &palette[ imageData[i] * 4], sizeof(unsigned char) * 4 ); + pconvertedData += 4; + } delete [] imageData; for (unsigned int i = 0; i < mipmap_offsets.size(); i++) mipmap_offsets[i] *= 4; internalFormat = GL_RGBA; pixelFormat = GL_RGBA; - osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, convertedData->c8, osg::Image::USE_NEW_DELETE, packing); + osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, convertedData, osg::Image::USE_NEW_DELETE, packing); } else {