- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/dds/ReaderWriterDDS.cpp
r12912 r13041 15 15 * Modified 13.05.2004 16 16 * by George Tarantilis, gtaranti@nps.navy.mil 17 * Modified 22.05.2009 18 * Wojtek Lewandowski, lewandowski@ai.com.pl 17 * Modified 22.05.2009 18 * Wojtek Lewandowski, lewandowski@ai.com.pl 19 19 * 20 * WARNING: 21 * Bit Masks in the WrtiteDDS are set for 8 bit components 22 * write with 4 or 16 bit components will 23 * probably produce corrupted file 24 * Wojtek Lewandowski 2009-05-22 20 * WARNING: 21 * Bit Masks in the WrtiteDDS are set for 8 bit components 22 * write with 4 or 16 bit components will 23 * probably produce corrupted file 24 * Wojtek Lewandowski 2009-05-22 25 25 * 26 26 **********************************************************************/ … … 57 57 dwColorSpaceLowValue(0), 58 58 dwColorSpaceHighValue(0) {} 59 59 60 60 UI32 dwColorSpaceLowValue; 61 61 UI32 dwColorSpaceHighValue; … … 74 74 dwBBitMask(0), 75 75 dwRGBAlphaBitMask(0) {} 76 76 77 77 78 78 UI32 dwSize; … … 135 135 dwFlags(0), 136 136 dwHeight(0), 137 dwWidth(0), 137 dwWidth(0), 138 138 lPitch(0), 139 139 dwBackBufferCount(0), 140 140 dwMipMapCount(0), 141 141 dwAlphaBitDepth(0), 142 dwReserved(0), 143 lpSurface(0), 144 dwTextureStage(0) {} 145 142 dwReserved(0), 143 lpSurface(0), 144 dwTextureStage(0) {} 145 146 146 147 147 UI32 dwSize; 148 148 UI32 dwFlags; 149 149 UI32 dwHeight; 150 UI32 dwWidth; 151 union 150 UI32 dwWidth; 151 union 152 152 { 153 153 I32 lPitch; … … 157 157 { 158 158 UI32 dwBackBufferCount; 159 UI32 dwDepth; 159 UI32 dwDepth; 160 160 }; 161 161 union … … 165 165 }; 166 166 UI32 dwAlphaBitDepth; 167 UI32 dwReserved; 168 UI32 lpSurface; //Fred Marmond: removed from pointer type to UI32 for 64bits compatibility. it is unused data 169 DDCOLORKEY ddckCKDestOverlay; 170 DDCOLORKEY ddckCKDestBlt; 171 DDCOLORKEY ddckCKSrcOverlay; 172 DDCOLORKEY ddckCKSrcBlt; 173 DDPIXELFORMAT ddpfPixelFormat; 174 DDSCAPS2 ddsCaps; 175 UI32 dwTextureStage; 167 UI32 dwReserved; 168 UI32 lpSurface; //Fred Marmond: removed from pointer type to UI32 for 64bits compatibility. it is unused data 169 DDCOLORKEY ddckCKDestOverlay; 170 DDCOLORKEY ddckCKDestBlt; 171 DDCOLORKEY ddckCKSrcOverlay; 172 DDCOLORKEY ddckCKSrcBlt; 173 DDPIXELFORMAT ddpfPixelFormat; 174 DDSCAPS2 ddsCaps; 175 UI32 dwTextureStage; 176 176 }; 177 177 … … 204 204 // 205 205 #define DDPF_ALPHAPIXELS 0x00000001l 206 #define DDPF_FOURCC 0x00000004l // Compressed formats 206 #define DDPF_FOURCC 0x00000004l // Compressed formats 207 207 #define DDPF_RGB 0x00000040l // Uncompressed formats 208 208 #define DDPF_ALPHA 0x00000002l … … 251 251 if( depth < 1 ) depth = 1; 252 252 253 // Taking advantage of the fact that 253 // Taking advantage of the fact that 254 254 // DXT formats are defined as 4 successive numbers: 255 255 // GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 … … 296 296 297 297 char filecode[4]; 298 298 299 299 _istream.read(filecode, 4); 300 300 if (strncmp(filecode, "DDS ", 4) != 0) { … … 304 304 _istream.read((char*)(&ddsd), sizeof(ddsd)); 305 305 306 osg::ref_ptr<osg::Image> osgImage = new osg::Image(); 307 306 osg::ref_ptr<osg::Image> osgImage = new osg::Image(); 307 308 308 //Check valid structure sizes 309 309 if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32) … … 323 323 int s = ddsd.dwWidth; 324 324 int t = ddsd.dwHeight; 325 int r = depth; 325 int r = depth; 326 326 unsigned int dataType = GL_UNSIGNED_BYTE; 327 327 unsigned int pixelFormat = 0; … … 329 329 330 330 // Handle some esoteric formats 331 if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPDUDV) 331 if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPDUDV) 332 332 { 333 333 OSG_WARN << "ReadDDSFile warning: DDPF_BUMPDUDV format is not supported" << std::endl; … … 340 340 // // A2W10U10V10 as RGBA (dwFlags == DDPF_BUMPDUDV + DDPF_ALPHAPIXELS) 341 341 } 342 if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPLUMINANCE) 342 if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPLUMINANCE) 343 343 { 344 344 OSG_WARN << "ReadDDSFile warning: DDPF_BUMPLUMINANCE format is not supported" << std::endl; … … 349 349 // // X8L8V8U8 -- just as RGB 350 350 } 351 351 352 352 // Uncompressed formats will usually use DDPF_RGB to indicate an RGB format, 353 353 // while compressed formats will use DDPF_FOURCC with a four-character code. 354 354 355 355 bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS; 356 356 … … 373 373 const unsigned int UNSUPPORTED = 0; 374 374 375 static const RGBFormat rgbFormats[] = 375 static const RGBFormat rgbFormats[] = 376 376 { 377 377 { "R3G3B2" , 8, 0xe0, 0x1c, 0x03, 0x00, … … 392 392 393 393 { "R8G8B8", 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, 394 GL_RGB , GL_BGR , GL_UNSIGNED_BYTE }, 394 GL_RGB , GL_BGR , GL_UNSIGNED_BYTE }, 395 395 396 396 { "B8G8R8", 24, 0x0000ff, 0x00ff00, 0xff0000, 0x000000, 397 GL_RGB , GL_RGB , GL_UNSIGNED_BYTE }, 397 GL_RGB , GL_RGB , GL_UNSIGNED_BYTE }, 398 398 399 399 { "A8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, 400 GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE }, 400 GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE }, 401 401 { "X8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, 402 GL_RGB , GL_BGRA, GL_UNSIGNED_BYTE }, 402 GL_RGB , GL_BGRA, GL_UNSIGNED_BYTE }, 403 403 { "A8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, 404 GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE }, 404 GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE }, 405 405 { "X8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, 406 GL_RGB , GL_RGBA, GL_UNSIGNED_BYTE }, 406 GL_RGB , GL_RGBA, GL_UNSIGNED_BYTE }, 407 407 { "A2R10G10B10", 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, 408 GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV }, 408 GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV }, 409 409 { "A2B10G10R10", 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, 410 GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV }, 410 GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV }, 411 411 { "G16R16", 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, 412 GL_RGB, UNSUPPORTED, GL_UNSIGNED_SHORT }, 412 GL_RGB, UNSUPPORTED, GL_UNSIGNED_SHORT }, 413 413 }; 414 414 … … 439 439 OSG_INFO << "ReadDDSFile info : " << f.name 440 440 << " format is not supported" << std::endl; 441 return NULL; 441 return NULL; 442 442 } 443 } 443 } 444 444 } 445 445 … … 450 450 << ddsd.ddpfPixelFormat.dwRGBBitCount << std::endl; 451 451 OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRBitMask = 0x" 452 << std::hex << std::setw(8) << std::setfill('0') 452 << std::hex << std::setw(8) << std::setfill('0') 453 453 << ddsd.ddpfPixelFormat.dwRBitMask << std::endl; 454 454 OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwGBitMask = 0x" 455 << std::hex << std::setw(8) << std::setfill('0') 455 << std::hex << std::setw(8) << std::setfill('0') 456 456 << ddsd.ddpfPixelFormat.dwGBitMask << std::endl; 457 457 OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwBBitMask = 0x" 458 << std::hex << std::setw(8) << std::setfill('0') 458 << std::hex << std::setw(8) << std::setfill('0') 459 459 << ddsd.ddpfPixelFormat.dwBBitMask << std::endl; 460 460 OSG_INFO << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0x" 461 << std::hex << std::setw(8) << std::setfill('0') 461 << std::hex << std::setw(8) << std::setfill('0') 462 462 << ddsd.ddpfPixelFormat.dwRGBAlphaBitMask << std::dec << std::endl; 463 463 return NULL; … … 500 500 OSG_INFO << "ReadDDSFile info : format = ALPHA" << std::endl; 501 501 internalFormat = GL_ALPHA; 502 pixelFormat = GL_ALPHA; 502 pixelFormat = GL_ALPHA; 503 503 } 504 504 // Compressed formats … … 539 539 internalFormat = GL_COMPRESSED_RED_GREEN_RGTC2_EXT; 540 540 pixelFormat = GL_COMPRESSED_RED_GREEN_RGTC2_EXT; 541 break; 541 break; 542 542 case 0x00000024: // A16B16G16R16 543 543 OSG_INFO << "ReadDDSFile info : format = A16B16G16R16" << std::endl; … … 550 550 internalFormat = GL_RGBA; // why no transparency? 551 551 pixelFormat = GL_RGBA; 552 dataType = GL_HALF_FLOAT_NV; 552 dataType = GL_HALF_FLOAT_NV; 553 553 break; 554 554 case 0x0000006E: // Q16W16V16U16 … … 611 611 << ddsd.ddpfPixelFormat.dwFourCC << std::dec 612 612 << ") in dds file, image not loaded." << std::endl; 613 return NULL; 614 } 615 } 616 else 613 return NULL; 614 } 615 } 616 else 617 617 { 618 618 OSG_WARN << "ReadDDSFile warning: unhandled pixel format (ddsd.ddpfPixelFormat.dwFlags" … … 629 629 osg::Image::MipmapDataType mipmap_offsets; 630 630 if ( ddsd.dwMipMapCount>1 ) 631 { 631 { 632 632 unsigned numMipmaps = osg::Image::computeNumberOfMipmapLevels( s, t, r ); 633 633 if( numMipmaps > ddsd.dwMipMapCount ) numMipmaps = ddsd.dwMipMapCount; … … 636 636 637 637 int width = s; 638 int height = t; 638 int height = t; 639 639 int depth = r; 640 640 … … 647 647 depth = osg::maximum( depth >> 1, 1 ); 648 648 649 sizeWithMipmaps += 649 sizeWithMipmaps += 650 650 ComputeImageSizeInBytes( width, height, depth, pixelFormat, dataType ); 651 651 } 652 652 } 653 653 654 654 unsigned char* imageData = new unsigned char [sizeWithMipmaps]; 655 655 if(!imageData) … … 658 658 return NULL; 659 659 } 660 661 // Read pixels in two chunks. First main image, next mipmaps. 660 661 // Read pixels in two chunks. First main image, next mipmaps. 662 662 if ( !_istream.read( (char*)imageData, size ) ) 663 663 { … … 689 689 { 690 690 691 // Initialize ddsd structure and its members 691 // Initialize ddsd structure and its members 692 692 DDSURFACEDESC2 ddsd; 693 693 memset( &ddsd, 0, sizeof( ddsd ) ); … … 701 701 memset( &ddsCaps, 0, sizeof( ddsCaps ) ); 702 702 703 ddsd.dwSize = sizeof(ddsd); 703 ddsd.dwSize = sizeof(ddsd); 704 704 ddpf.dwSize = sizeof(ddpf); 705 705 706 706 // Default values and initialization of structures' flags 707 707 unsigned int SD_flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT; 708 unsigned int CAPS_flags = DDSCAPS_TEXTURE; 708 unsigned int CAPS_flags = DDSCAPS_TEXTURE; 709 709 unsigned int PF_flags = 0; 710 710 unsigned int CAPS2_flags = 0; … … 738 738 ddpf.dwRBitMask = 0x000000ff; 739 739 ddpf.dwGBitMask = 0x0000ff00; 740 ddpf.dwBBitMask = 0x00ff0000; 740 ddpf.dwBBitMask = 0x00ff0000; 741 741 ddpf.dwRGBAlphaBitMask = 0xff000000; 742 742 PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); 743 ddpf.dwRGBBitCount = pixelSize; 743 ddpf.dwRGBBitCount = pixelSize; 744 744 ddsd.lPitch = img->getRowSizeInBytes(); 745 745 SD_flags |= DDSD_PITCH; … … 750 750 ddpf.dwBBitMask = 0x000000ff; 751 751 ddpf.dwGBitMask = 0x0000ff00; 752 ddpf.dwRBitMask = 0x00ff0000; 752 ddpf.dwRBitMask = 0x00ff0000; 753 753 ddpf.dwRGBAlphaBitMask = 0xff000000; 754 754 PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); 755 ddpf.dwRGBBitCount = pixelSize; 755 ddpf.dwRGBBitCount = pixelSize; 756 756 ddsd.lPitch = img->getRowSizeInBytes(); 757 757 SD_flags |= DDSD_PITCH; … … 762 762 ddpf.dwRBitMask = 0x000000ff; 763 763 ddpf.dwRGBAlphaBitMask = 0x0000ff00; 764 PF_flags |= (DDPF_ALPHAPIXELS | DDPF_LUMINANCE); 765 ddpf.dwRGBBitCount = pixelSize; 764 PF_flags |= (DDPF_ALPHAPIXELS | DDPF_LUMINANCE); 765 ddpf.dwRGBBitCount = pixelSize; 766 766 ddsd.lPitch = img->getRowSizeInBytes(); 767 767 SD_flags |= DDSD_PITCH; … … 772 772 ddpf.dwRBitMask = 0x000000ff; 773 773 ddpf.dwGBitMask = 0x0000ff00; 774 ddpf.dwBBitMask = 0x00ff0000; 774 ddpf.dwBBitMask = 0x00ff0000; 775 775 PF_flags |= DDPF_RGB; 776 776 ddpf.dwRGBBitCount = pixelSize; … … 783 783 ddpf.dwBBitMask = 0x000000ff; 784 784 ddpf.dwGBitMask = 0x0000ff00; 785 ddpf.dwRBitMask = 0x00ff0000; 785 ddpf.dwRBitMask = 0x00ff0000; 786 786 PF_flags |= DDPF_RGB; 787 787 ddpf.dwRGBBitCount = pixelSize; … … 857 857 SD_flags |= DDSD_LINEARSIZE; 858 858 } 859 break; 859 break; 860 860 case GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT: 861 861 { … … 865 865 SD_flags |= DDSD_LINEARSIZE; 866 866 } 867 break; 867 break; 868 868 case GL_COMPRESSED_RED_GREEN_RGTC2_EXT: 869 869 { … … 873 873 SD_flags |= DDSD_LINEARSIZE; 874 874 } 875 break; 875 break; 876 876 default: 877 877 OSG_WARN<<"Warning:: unhandled pixel format in image, file cannot be written."<<std::endl; … … 892 892 SD_flags |= DDSD_MIPMAPCOUNT; 893 893 CAPS_flags |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP; 894 894 895 895 ddsd.dwMipMapCount = img->getNumMipmapLevels(); 896 896 … … 921 921 922 922 // If we get that far the file was saved properly // 923 return true; 923 return true; 924 924 } 925 925 … … 939 939 940 940 virtual const char* className() const 941 { 942 return "DDS Image Reader/Writer"; 941 { 942 return "DDS Image Reader/Writer"; 943 943 } 944 944 … … 959 959 960 960 std::string fileName = osgDB::findDataFile( file, options ); 961 961 962 962 if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 963 963 964 964 osgDB::ifstream stream(fileName.c_str(), std::ios::in | std::ios::binary); 965 965 if(!stream) return ReadResult::FILE_NOT_HANDLED; … … 1016 1016 osgImage->flipVertical(); 1017 1017 } 1018 1018 1019 1019 return osgImage; 1020 1020 }
