From 31c54aa3e45f1a44da93b98de066d535853681a3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 21 Nov 2013 13:58:36 +0000 Subject: [PATCH] From Farshid Lashkari, "I recently encountered an issue attempting to load an IVE file generated by an older version of OSG. The file contained dxt1 compressed image data with mipmaps. The loaded model would cause crashes when passing the mipmap data to glCompressedTexImage2D. It seems that the size of the data array within the IVE file did not match the computed size from Image::getTotalSizeInBytesIncludingMipmaps(). This essentially made the mipmap offsets invalid within the Image object. I'm not sure if the IVE was simply generated incorrectly, or if the Image::getTotalSizeInBytesIncludingMipmaps() was modified since the file was generated. Either way, I added a simple check in the IVE loader so that it clears the mipmap offsets if the actual data size does not match the computed data size. This seems like a safe fallback since the mipmap data can be automatically generated, and it fixes the problem in my case. Also, while looking into this issue, I noticed that the osgDB::InputStream class applies the serialized image allocation mode. However, since the serializer is allocating the image data itself, it seems like it should force the allocation mode to USE_NEW_DELETE. " --- src/osgDB/InputStream.cpp | 2 +- src/osgPlugins/ive/Image.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/osgDB/InputStream.cpp b/src/osgDB/InputStream.cpp index e30c95e3a..605962aad 100644 --- a/src/osgDB/InputStream.cpp +++ b/src/osgDB/InputStream.cpp @@ -639,7 +639,7 @@ osg::Image* InputStream::readImage(bool readFromExternal) image = new osg::Image; image->setOrigin( (osg::Image::Origin)origin ); image->setImage( s, t, r, internalFormat, pixelFormat, dataType, - (unsigned char*)data, (osg::Image::AllocationMode)mode, packing ); + (unsigned char*)data, osg::Image::USE_NEW_DELETE, packing ); } // _mipmapData diff --git a/src/osgPlugins/ive/Image.cpp b/src/osgPlugins/ive/Image.cpp index d9b1a4580..e0358e8c4 100644 --- a/src/osgPlugins/ive/Image.cpp +++ b/src/osgPlugins/ive/Image.cpp @@ -133,9 +133,10 @@ void Image::read(DataInputStream* in) // Read image data if any + unsigned int dataSize = 0; if(in->readBool()) { - unsigned int dataSize = (unsigned int)in->readInt(); + dataSize = (unsigned int)in->readInt(); //static int totalSize = 0; @@ -154,6 +155,12 @@ void Image::read(DataInputStream* in) } _mipmapData.swap(mipmapData); + + // If data size does not match computed image size, then clear mipmaps since we probably have invalid offsets + if (dataSize > 0 && dataSize != getTotalSizeInBytesIncludingMipmaps()) + { + _mipmapData.clear(); + } } else{ in_THROW_EXCEPTION("Image::read(): Expected Image identification.");