From Marcin Prus and Robert Osfield, moved the key parts of the .dds plugins computeRowWidthInBytes implementation into the osg::Image::computeImageSizeInBytes(..) to

address bugs in the computation of image size.
This commit is contained in:
Robert Osfield 2013-05-28 14:14:45 +00:00
parent d024444140
commit 47f574429a
4 changed files with 48 additions and 50 deletions

View File

@ -170,6 +170,7 @@ osgDB::ReaderWriter::ReadResult ImageReaderWriter::local_readNode(const std::str
// set up the texture. // set up the texture.
osg::Texture2D* texture = new osg::Texture2D; osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image); texture->setImage(image);
texture->setResizeNonPowerOfTwoHint(false);
texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);

View File

@ -365,7 +365,7 @@ class OSG_EXPORT Image : public BufferData
static unsigned int computeNumComponents(GLenum pixelFormat); static unsigned int computeNumComponents(GLenum pixelFormat);
static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type); static unsigned int computePixelSizeInBits(GLenum pixelFormat,GLenum type);
static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing); static unsigned int computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum type,int packing);
static unsigned int computeImageSizeInBytes(int width,int height, int depth, GLenum pixelFormat,GLenum type,int packing); static unsigned int computeImageSizeInBytes(int width,int height, int depth, GLenum pixelFormat, GLenum type, int packing = 1, int slice_packing = 1, int image_packing = 1);
static int computeNearestPowerOfTwo(int s,float bias=0.5f); static int computeNearestPowerOfTwo(int s,float bias=0.5f);
static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1); static int computeNumberOfMipmapLevels(int s,int t = 1, int r = 1);

View File

@ -722,14 +722,49 @@ unsigned int Image::computeRowWidthInBytes(int width,GLenum pixelFormat,GLenum t
return (widthInBits/packingInBits + ((widthInBits%packingInBits)?1:0))*packing; return (widthInBits/packingInBits + ((widthInBits%packingInBits)?1:0))*packing;
} }
unsigned int Image::computeImageSizeInBytes(int width,int height, int depth, GLenum pixelFormat,GLenum type,int packing) unsigned int Image::computeImageSizeInBytes(int width,int height, int depth, GLenum pixelFormat,GLenum type,int packing, int slice_packing, int image_packing)
{ {
if (width==0 || height==0 || depth==0) return 0; if (width<=0 || height<=0 || depth<=0) return 0;
return osg::maximum( // Taking advantage of the fact that
Image::computeRowWidthInBytes(width,pixelFormat,type,packing)*height*depth, // DXT formats are defined as 4 successive numbers:
computeBlockSize(pixelFormat, packing) // GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
); // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
// GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
// GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
if( pixelFormat >= GL_COMPRESSED_RGB_S3TC_DXT1_EXT &&
pixelFormat <= GL_COMPRESSED_RGBA_S3TC_DXT5_EXT )
{
width = (width + 3) & ~3;
height = (height + 3) & ~3;
}
// 3dc ATI formats
// GL_COMPRESSED_RED_RGTC1_EXT 0x8DBB
// GL_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
// GL_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
// GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
if( pixelFormat >= GL_COMPRESSED_RED_RGTC1_EXT &&
pixelFormat <= GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT )
{
width = (width + 3) & ~3;
height = (height + 3) & ~3;
}
// compute size of one row
unsigned int size = osg::Image::computeRowWidthInBytes( width, pixelFormat, type, packing );
// now compute size of slice
size *= height;
size += slice_packing - 1;
size -= size % slice_packing;
// compute size of whole image
size *= depth;
size += image_packing - 1;
size -= size % image_packing;
return osg::maximum( size, computeBlockSize(pixelFormat, packing) );
} }
int Image::computeNearestPowerOfTwo(int s,float bias) int Image::computeNearestPowerOfTwo(int s,float bias)

View File

@ -400,53 +400,15 @@ typedef struct {
UI32 reserved; UI32 reserved;
} OSG_DDS_HEADER_DXT10; } OSG_DDS_HEADER_DXT10;
static unsigned int ComputeImageSizeInBytes static unsigned int ComputeImageSizeInBytes( int width, int height, int depth,
( int width, int height, int depth, unsigned int pixelFormat, unsigned int pixelType,
unsigned int pixelFormat, unsigned int pixelType, int packing = 1, int slice_packing = 1, int image_packing = 1 )
int packing = 1, int slice_packing = 1, int image_packing = 1 )
{ {
if( width < 1 ) width = 1; if( width < 1 ) width = 1;
if( height < 1 ) height = 1; if( height < 1 ) height = 1;
if( depth < 1 ) depth = 1; if( depth < 1 ) depth = 1;
// Taking advantage of the fact that return osg::Image::computeImageSizeInBytes(width, height, depth, packing, slice_packing, image_packing);
// DXT formats are defined as 4 successive numbers:
// GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
// GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
// GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
// GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
if( pixelFormat >= GL_COMPRESSED_RGB_S3TC_DXT1_EXT &&
pixelFormat <= GL_COMPRESSED_RGBA_S3TC_DXT5_EXT )
{
width = (width + 3) & ~3;
height = (height + 3) & ~3;
}
// 3dc ATI formats
// GL_COMPRESSED_RED_RGTC1_EXT 0x8DBB
// GL_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
// GL_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
// GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
if( pixelFormat >= GL_COMPRESSED_RED_RGTC1_EXT &&
pixelFormat <= GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT )
{
width = (width + 3) & ~3;
height = (height + 3) & ~3;
}
// compute size of one row
unsigned int size = osg::Image::computeRowWidthInBytes
( width, pixelFormat, pixelType, packing );
// now compute size of slice
size *= height;
size += slice_packing - 1;
size -= size % slice_packing;
// compute size of whole image
size *= depth;
size += image_packing - 1;
size -= size % image_packing;
return size;
} }
osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead) osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead)