diff --git a/src/osgPlugins/dds/ReaderWriterDDS.cpp b/src/osgPlugins/dds/ReaderWriterDDS.cpp index 00d55c977..6489e4a25 100644 --- a/src/osgPlugins/dds/ReaderWriterDDS.cpp +++ b/src/osgPlugins/dds/ReaderWriterDDS.cpp @@ -24,6 +24,7 @@ #include #include +#include #include @@ -67,6 +68,7 @@ struct DDPIXELFORMAT UI32 dwYUVBitCount; UI32 dwZBufferBitDepth; UI32 dwAlphaBitDepth; + UI32 dwLuminanceBitDepth; }; union { @@ -189,6 +191,8 @@ struct DXT1TexelsBlock #define DDPF_ALPHA 0x00000002l #define DDPF_COMPRESSED 0x00000080l #define DDPF_LUMINANCE 0x00020000l +#define DDPF_BUMPLUMINANCE 0x00040000l // L,U,V +#define DDPF_BUMPDUDV 0x00080000l // U,V // // DDSCAPS flags @@ -266,6 +270,28 @@ osg::Image* ReadDDSFile(std::istream& _istream) unsigned int pixelFormat = 0; unsigned int internalFormat = 0; + // Handle some esoteric formats + if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPDUDV) + { + osg::notify(osg::WARN) << "ReadDDSFile warning: DDPF_BUMPDUDV format is not supported" << std::endl; + return NULL; +// ddsd.ddpfPixelFormat.dwFlags = +// DDPF_LUMINANCE + DDPF_ALPHAPIXELS; +// // handle V8U8 as A8L8 +// // handle V16U16 as A16L16 +// // but Q8W8U8L8 as RGB? +// // A2W10U10V10 as RGBA (dwFlags == DDPF_BUMPDUDV + DDPF_ALPHAPIXELS) + } + if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPLUMINANCE) + { + osg::notify(osg::WARN) << "ReadDDSFile warning: DDPF_BUMPLUMINANCE format is not supported" << std::endl; + return NULL; +// ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB; +// // handle as RGB +// // L6V5U5 -- 655 is not supported data type in GL +// // X8L8V8U8 -- just as RGB + } + // Uncompressed formats will usually use DDPF_RGB to indicate an RGB format, // while compressed formats will use DDPF_FOURCC with a four-character code. @@ -275,38 +301,155 @@ osg::Image* ReadDDSFile(std::istream& _istream) // Uncompressed formats. if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB) { - switch(ddsd.ddpfPixelFormat.dwRGBBitCount) + struct RGBFormat { - case 32: - internalFormat = 4; - pixelFormat = GL_RGBA; - break; - case 24: - internalFormat = 3; - pixelFormat = GL_RGB; - break; - case 16: - default: - osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded"<> 8) + << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x00ff0000) >> 16) + << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0xff000000) >> 24) + << " = 0x" << std::hex << std::setw(8) << std::setfill('0') + << ddsd.ddpfPixelFormat.dwFourCC << std::dec + << ") in dds file, image not loaded." << std::endl; return NULL; } } else { - osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in dds file, image not loaded."<setMipmapData(mipmaps); } // Handle uncompressed mipmaps - if(ddsd.ddpfPixelFormat.dwFlags & (DDPF_RGB | DDPF_LUMINANCE | DDPF_ALPHA)) + else { int offset = 0; int width = ddsd.dwWidth; @@ -416,7 +637,8 @@ osg::Image* ReadDDSFile(std::istream& _istream) width = 1; if (height == 0) height = 1; - offset += (width*height*(ddsd.ddpfPixelFormat.dwRGBBitCount/8)); + offset += height * + osg::Image::computeRowWidthInBytes( width, pixelFormat, dataType, 1 ); mipmaps[k-1] = offset; width >>= 1; height >>= 1; @@ -425,19 +647,22 @@ osg::Image* ReadDDSFile(std::istream& _istream) } } - osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, 0, osg::Image::USE_NEW_DELETE); if (mipmaps.size()>0) osgImage->setMipmapLevels(mipmaps); unsigned int size = osgImage->getTotalSizeInBytesIncludingMipmaps(); + osg::notify(osg::INFO) << "ReadDDSFile info : size = " << size << std::endl; + if(size <= 0) { + osg::notify(osg::WARN) << "ReadDDSFile warning: size <= 0" << std::endl; return NULL; } unsigned char* imageData = new unsigned char [size]; if(!imageData) { + osg::notify(osg::WARN) << "ReadDDSFile warning: imageData == NULL" << std::endl; return NULL; }