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.
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
texture->setResizeNonPowerOfTwoHint(false);
texture->setFilter(osg::Texture::MIN_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 computePixelSizeInBits(GLenum pixelFormat,GLenum type);
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 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;
}
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(
Image::computeRowWidthInBytes(width,pixelFormat,type,packing)*height*depth,
computeBlockSize(pixelFormat, packing)
);
// Taking advantage of the fact that
// 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, 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)

View File

@ -400,8 +400,7 @@ typedef struct {
UI32 reserved;
} OSG_DDS_HEADER_DXT10;
static unsigned int ComputeImageSizeInBytes
( int width, int height, int depth,
static unsigned int ComputeImageSizeInBytes( int width, int height, int depth,
unsigned int pixelFormat, unsigned int pixelType,
int packing = 1, int slice_packing = 1, int image_packing = 1 )
{
@ -409,44 +408,7 @@ static unsigned int ComputeImageSizeInBytes
if( height < 1 ) height = 1;
if( depth < 1 ) depth = 1;
// Taking advantage of the fact that
// 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;
return osg::Image::computeImageSizeInBytes(width, height, depth, packing, slice_packing, image_packing);
}
osg::Image* ReadDDSFile(std::istream& _istream, bool flipDDSRead)