| [1794] | 1 | |
|---|
| [2995] | 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| [3017] | 15 | |
|---|
| 16 | |
|---|
| [2995] | 17 | |
|---|
| 18 | |
|---|
| [1794] | 19 | |
|---|
| 20 | #include <osg/Texture> |
|---|
| [1795] | 21 | #include <osg/Notify> |
|---|
| [1794] | 22 | |
|---|
| 23 | #include <osgDB/Registry> |
|---|
| 24 | #include <osgDB/FileNameUtils> |
|---|
| [2501] | 25 | #include <osgDB/FileUtils> |
|---|
| [9124] | 26 | #include <osgDB/fstream> |
|---|
| [1794] | 27 | |
|---|
| [6929] | 28 | #include <iomanip> |
|---|
| [1794] | 29 | #include <stdio.h> |
|---|
| [9343] | 30 | #include <string.h> |
|---|
| [1794] | 31 | |
|---|
| [2995] | 32 | |
|---|
| [1794] | 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| [3772] | 36 | |
|---|
| 37 | typedef unsigned int UI32; |
|---|
| 38 | typedef int I32; |
|---|
| 39 | |
|---|
| [3017] | 40 | struct DDCOLORKEY |
|---|
| [1794] | 41 | { |
|---|
| [3017] | 42 | DDCOLORKEY(): |
|---|
| 43 | dwColorSpaceLowValue(0), |
|---|
| 44 | dwColorSpaceHighValue(0) {} |
|---|
| 45 | |
|---|
| [3772] | 46 | UI32 dwColorSpaceLowValue; |
|---|
| 47 | UI32 dwColorSpaceHighValue; |
|---|
| [3017] | 48 | }; |
|---|
| [1794] | 49 | |
|---|
| [3017] | 50 | struct DDPIXELFORMAT |
|---|
| [1794] | 51 | { |
|---|
| [3017] | 52 | |
|---|
| 53 | DDPIXELFORMAT(): |
|---|
| 54 | dwSize(0), |
|---|
| 55 | dwFlags(0), |
|---|
| 56 | dwFourCC(0), |
|---|
| 57 | dwRGBBitCount(0), |
|---|
| 58 | dwRBitMask(0), |
|---|
| 59 | dwGBitMask(0), |
|---|
| 60 | dwBBitMask(0), |
|---|
| 61 | dwRGBAlphaBitMask(0) {} |
|---|
| 62 | |
|---|
| 63 | |
|---|
| [3772] | 64 | UI32 dwSize; |
|---|
| 65 | UI32 dwFlags; |
|---|
| 66 | UI32 dwFourCC; |
|---|
| [1794] | 67 | union |
|---|
| 68 | { |
|---|
| [3772] | 69 | UI32 dwRGBBitCount; |
|---|
| 70 | UI32 dwYUVBitCount; |
|---|
| 71 | UI32 dwZBufferBitDepth; |
|---|
| 72 | UI32 dwAlphaBitDepth; |
|---|
| [6929] | 73 | UI32 dwLuminanceBitDepth; |
|---|
| [1794] | 74 | }; |
|---|
| 75 | union |
|---|
| 76 | { |
|---|
| [3772] | 77 | UI32 dwRBitMask; |
|---|
| 78 | UI32 dwYBitMask; |
|---|
| [1794] | 79 | }; |
|---|
| 80 | union |
|---|
| 81 | { |
|---|
| [3772] | 82 | UI32 dwGBitMask; |
|---|
| 83 | UI32 dwUBitMask; |
|---|
| [1794] | 84 | }; |
|---|
| 85 | union |
|---|
| 86 | { |
|---|
| [3772] | 87 | UI32 dwBBitMask; |
|---|
| 88 | UI32 dwVBitMask; |
|---|
| [1794] | 89 | }; |
|---|
| 90 | union |
|---|
| 91 | { |
|---|
| [3772] | 92 | UI32 dwRGBAlphaBitMask; |
|---|
| 93 | UI32 dwYUVAlphaBitMask; |
|---|
| 94 | UI32 dwRGBZBitMask; |
|---|
| 95 | UI32 dwYUVZBitMask; |
|---|
| [1794] | 96 | }; |
|---|
| [3017] | 97 | }; |
|---|
| [1794] | 98 | |
|---|
| [3017] | 99 | struct DDSCAPS2 |
|---|
| [1794] | 100 | { |
|---|
| [3017] | 101 | DDSCAPS2(): |
|---|
| 102 | dwCaps(0), |
|---|
| 103 | dwCaps2(0), |
|---|
| 104 | dwCaps3(0), |
|---|
| 105 | dwCaps4(0) {} |
|---|
| 106 | |
|---|
| [3772] | 107 | UI32 dwCaps; |
|---|
| 108 | UI32 dwCaps2; |
|---|
| 109 | UI32 dwCaps3; |
|---|
| [1794] | 110 | union |
|---|
| 111 | { |
|---|
| [3772] | 112 | UI32 dwCaps4; |
|---|
| 113 | UI32 dwVolumeDepth; |
|---|
| [1794] | 114 | }; |
|---|
| [3017] | 115 | }; |
|---|
| [1794] | 116 | |
|---|
| [3017] | 117 | struct DDSURFACEDESC2 |
|---|
| [1795] | 118 | { |
|---|
| [3017] | 119 | DDSURFACEDESC2(): |
|---|
| 120 | dwSize(0), |
|---|
| 121 | dwFlags(0), |
|---|
| 122 | dwHeight(0), |
|---|
| 123 | dwWidth(0), |
|---|
| 124 | lPitch(0), |
|---|
| 125 | dwBackBufferCount(0), |
|---|
| 126 | dwMipMapCount(0), |
|---|
| 127 | dwAlphaBitDepth(0), |
|---|
| 128 | dwReserved(0), |
|---|
| 129 | lpSurface(0), |
|---|
| 130 | dwTextureStage(0) {} |
|---|
| 131 | |
|---|
| 132 | |
|---|
| [3772] | 133 | UI32 dwSize; |
|---|
| 134 | UI32 dwFlags; |
|---|
| 135 | UI32 dwHeight; |
|---|
| 136 | UI32 dwWidth; |
|---|
| [2995] | 137 | union |
|---|
| [1794] | 138 | { |
|---|
| [3772] | 139 | I32 lPitch; |
|---|
| 140 | UI32 dwLinearSize; |
|---|
| [1794] | 141 | }; |
|---|
| 142 | union |
|---|
| 143 | { |
|---|
| [3772] | 144 | UI32 dwBackBufferCount; |
|---|
| 145 | UI32 dwDepth; |
|---|
| [2995] | 146 | }; |
|---|
| 147 | union |
|---|
| 148 | { |
|---|
| [3772] | 149 | UI32 dwMipMapCount; |
|---|
| 150 | UI32 dwRefreshRate; |
|---|
| [1794] | 151 | }; |
|---|
| [3772] | 152 | UI32 dwAlphaBitDepth; |
|---|
| 153 | UI32 dwReserved; |
|---|
| [4801] | 154 | UI32 lpSurface; |
|---|
| [2995] | 155 | DDCOLORKEY ddckCKDestOverlay; |
|---|
| 156 | DDCOLORKEY ddckCKDestBlt; |
|---|
| 157 | DDCOLORKEY ddckCKSrcOverlay; |
|---|
| 158 | DDCOLORKEY ddckCKSrcBlt; |
|---|
| 159 | DDPIXELFORMAT ddpfPixelFormat; |
|---|
| 160 | DDSCAPS2 ddsCaps; |
|---|
| [3772] | 161 | UI32 dwTextureStage; |
|---|
| [5755] | 162 | }; |
|---|
| [1794] | 163 | |
|---|
| [2995] | 164 | |
|---|
| [5755] | 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | struct DXT1TexelsBlock |
|---|
| 169 | { |
|---|
| 170 | unsigned short color_0; |
|---|
| 171 | unsigned short color_1; |
|---|
| 172 | unsigned int texels4x4; |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | |
|---|
| [2995] | 176 | |
|---|
| 177 | |
|---|
| 178 | #define DDSD_CAPS 0x00000001l // default |
|---|
| [3017] | 179 | #define DDSD_HEIGHT 0x00000002l // default |
|---|
| 180 | #define DDSD_WIDTH 0x00000004l // default |
|---|
| 181 | #define DDSD_PIXELFORMAT 0x00001000l // default |
|---|
| [2995] | 182 | #define DDSD_PITCH 0x00000008l // For uncompressed formats |
|---|
| 183 | #define DDSD_MIPMAPCOUNT 0x00020000l |
|---|
| 184 | #define DDSD_LINEARSIZE 0x00080000l // For compressed formats |
|---|
| [3017] | 185 | #define DDSD_DEPTH 0x00800000l // Volume Textures |
|---|
| [2995] | 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| [1801] | 190 | #define DDPF_ALPHAPIXELS 0x00000001l |
|---|
| [3017] | 191 | #define DDPF_FOURCC 0x00000004l // Compressed formats |
|---|
| 192 | #define DDPF_RGB 0x00000040l // Uncompressed formats |
|---|
| [2995] | 193 | #define DDPF_ALPHA 0x00000002l |
|---|
| 194 | #define DDPF_COMPRESSED 0x00000080l |
|---|
| 195 | #define DDPF_LUMINANCE 0x00020000l |
|---|
| [6929] | 196 | #define DDPF_BUMPLUMINANCE 0x00040000l // L,U,V |
|---|
| 197 | #define DDPF_BUMPDUDV 0x00080000l // U,V |
|---|
| [1801] | 198 | |
|---|
| [2995] | 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | #define DDSCAPS_TEXTURE 0x00001000l // default |
|---|
| 203 | #define DDSCAPS_COMPLEX 0x00000008l |
|---|
| 204 | #define DDSCAPS_MIPMAP 0x00400000l |
|---|
| 205 | #define DDSCAPS2_VOLUME 0x00200000l |
|---|
| 206 | |
|---|
| 207 | |
|---|
| [1794] | 208 | #ifndef MAKEFOURCC |
|---|
| [2995] | 209 | #define MAKEFOURCC(ch0, ch1, ch2, ch3) \ |
|---|
| [3772] | 210 | ((UI32)(char)(ch0) | ((UI32)(char)(ch1) << 8) | \ |
|---|
| 211 | ((UI32)(char)(ch2) << 16) | ((UI32)(char)(ch3) << 24 )) |
|---|
| [1794] | 212 | #endif //defined(MAKEFOURCC) |
|---|
| 213 | |
|---|
| 214 | |
|---|
| [2995] | 215 | |
|---|
| 216 | |
|---|
| [1794] | 217 | #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1')) |
|---|
| 218 | #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2')) |
|---|
| 219 | #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3')) |
|---|
| 220 | #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4')) |
|---|
| 221 | #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5')) |
|---|
| 222 | |
|---|
| [2995] | 223 | |
|---|
| [3014] | 224 | osg::Image* ReadDDSFile(std::istream& _istream) |
|---|
| [1795] | 225 | { |
|---|
| [1794] | 226 | |
|---|
| [1795] | 227 | DDSURFACEDESC2 ddsd; |
|---|
| [1794] | 228 | |
|---|
| [1795] | 229 | char filecode[4]; |
|---|
| [3014] | 230 | |
|---|
| [3017] | 231 | _istream.read(filecode, 4); |
|---|
| [1795] | 232 | if (strncmp(filecode, "DDS ", 4) != 0) { |
|---|
| 233 | return NULL; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| [3014] | 236 | _istream.read((char*)(&ddsd), sizeof(ddsd)); |
|---|
| [1794] | 237 | |
|---|
| [3014] | 238 | |
|---|
| [2995] | 239 | |
|---|
| [3088] | 240 | |
|---|
| 241 | |
|---|
| [1983] | 242 | |
|---|
| [2840] | 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| [2488] | 246 | |
|---|
| [3014] | 247 | |
|---|
| [2995] | 248 | |
|---|
| [2840] | 249 | osg::ref_ptr<osg::Image> osgImage = new osg::Image(); |
|---|
| [3014] | 250 | |
|---|
| [3088] | 251 | |
|---|
| [2995] | 252 | if(ddsd.dwSize != 124 && ddsd.ddpfPixelFormat.dwSize != 32) |
|---|
| 253 | { |
|---|
| 254 | return NULL; |
|---|
| 255 | } |
|---|
| [1794] | 256 | |
|---|
| [2995] | 257 | bool is3dImage = false; |
|---|
| 258 | int depth = 1; |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | if( ddsd.dwDepth > 0 && (ddsd.dwFlags & DDSD_DEPTH)) |
|---|
| 262 | { |
|---|
| 263 | is3dImage = true; |
|---|
| 264 | depth = ddsd.dwDepth; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| [1795] | 267 | |
|---|
| 268 | int s = ddsd.dwWidth; |
|---|
| 269 | int t = ddsd.dwHeight; |
|---|
| [2995] | 270 | int r = depth; |
|---|
| [1795] | 271 | unsigned int dataType = GL_UNSIGNED_BYTE; |
|---|
| [1801] | 272 | unsigned int pixelFormat = 0; |
|---|
| 273 | unsigned int internalFormat = 0; |
|---|
| 274 | |
|---|
| [6929] | 275 | |
|---|
| 276 | if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPDUDV) |
|---|
| 277 | { |
|---|
| 278 | osg::notify(osg::WARN) << "ReadDDSFile warning: DDPF_BUMPDUDV format is not supported" << std::endl; |
|---|
| 279 | return NULL; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | } |
|---|
| 287 | if(ddsd.ddpfPixelFormat.dwFlags & DDPF_BUMPLUMINANCE) |
|---|
| 288 | { |
|---|
| 289 | osg::notify(osg::WARN) << "ReadDDSFile warning: DDPF_BUMPLUMINANCE format is not supported" << std::endl; |
|---|
| 290 | return NULL; |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| [1801] | 297 | |
|---|
| 298 | |
|---|
| [2995] | 299 | |
|---|
| 300 | bool usingAlpha = ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS; |
|---|
| [5755] | 301 | bool checkIfUsingOneBitAlpha = false; |
|---|
| [1801] | 302 | |
|---|
| 303 | |
|---|
| 304 | if(ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB) |
|---|
| [1795] | 305 | { |
|---|
| [6929] | 306 | struct RGBFormat |
|---|
| [1801] | 307 | { |
|---|
| [6929] | 308 | const char* name; |
|---|
| 309 | UI32 bitCount; |
|---|
| 310 | UI32 rBitMask; |
|---|
| 311 | UI32 gBitMask; |
|---|
| 312 | UI32 bBitMask; |
|---|
| 313 | UI32 aBitMask; |
|---|
| 314 | unsigned int internalFormat; |
|---|
| 315 | unsigned int pixelFormat; |
|---|
| 316 | unsigned int dataType; |
|---|
| 317 | }; |
|---|
| 318 | |
|---|
| [7375] | 319 | const unsigned int UNSUPPORTED = 0; |
|---|
| [6929] | 320 | |
|---|
| 321 | static const RGBFormat rgbFormats[] = |
|---|
| 322 | { |
|---|
| 323 | { "R3G3B2" , 8, 0xe0, 0x1c, 0x03, 0x00, |
|---|
| 324 | GL_RGB , GL_RGB , GL_UNSIGNED_BYTE_3_3_2 }, |
|---|
| 325 | |
|---|
| 326 | { "R5G6B5" , 16, 0xf800, 0x07e0, 0x001f, 0x0000, |
|---|
| 327 | GL_RGB , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 }, |
|---|
| 328 | { "A1R5G5B5" , 16, 0x7c00, 0x03e0, 0x001f, 0x8000, |
|---|
| 329 | GL_RGBA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV }, |
|---|
| 330 | { "X1R5G5B5" , 16, 0x7c00, 0x03e0, 0x001f, 0x0000, |
|---|
| 331 | GL_RGB , GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV }, |
|---|
| 332 | { "A4R4G4B4" , 16, 0x0f00, 0x00f0, 0x000f, 0xf000, |
|---|
| 333 | GL_RGBA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV }, |
|---|
| 334 | { "X4R4G4B4" , 16, 0x0f00, 0x00f0, 0x000f, 0x0000, |
|---|
| 335 | GL_RGB , GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV }, |
|---|
| 336 | { "A8R3G3B2" , 16, 0x00e0, 0x001c, 0x0003, 0xff00, |
|---|
| 337 | GL_RGBA, GL_BGRA, UNSUPPORTED }, |
|---|
| 338 | |
|---|
| 339 | { "R8G8B8", 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, |
|---|
| 340 | GL_RGB , GL_BGR , GL_UNSIGNED_BYTE }, |
|---|
| 341 | |
|---|
| [8033] | 342 | { "B8G8R8", 24, 0x0000ff, 0x00ff00, 0xff0000, 0x000000, |
|---|
| 343 | GL_RGB , GL_RGB , GL_UNSIGNED_BYTE }, |
|---|
| 344 | |
|---|
| [6929] | 345 | { "A8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, |
|---|
| 346 | GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE }, |
|---|
| 347 | { "X8R8G8B8", 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, |
|---|
| 348 | GL_RGB , GL_BGRA, GL_UNSIGNED_BYTE }, |
|---|
| 349 | { "A8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, |
|---|
| 350 | GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE }, |
|---|
| 351 | { "X8B8G8R8", 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, |
|---|
| 352 | GL_RGB , GL_RGBA, GL_UNSIGNED_BYTE }, |
|---|
| 353 | { "A2R10G10B10", 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, |
|---|
| 354 | GL_RGBA, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV }, |
|---|
| 355 | { "A2B10G10R10", 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, |
|---|
| 356 | GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV }, |
|---|
| 357 | { "G16R16", 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, |
|---|
| 358 | GL_RGB, UNSUPPORTED, GL_UNSIGNED_SHORT }, |
|---|
| 359 | }; |
|---|
| 360 | |
|---|
| 361 | bool found = false; |
|---|
| 362 | |
|---|
| [7375] | 363 | for ( unsigned int i = 0; i < sizeof ( rgbFormats ) / sizeof ( RGBFormat ); i++ ) |
|---|
| [6929] | 364 | { |
|---|
| 365 | const RGBFormat& f = rgbFormats[ i ]; |
|---|
| 366 | if ( ddsd.ddpfPixelFormat.dwRGBBitCount == f.bitCount && |
|---|
| 367 | ddsd.ddpfPixelFormat.dwRBitMask == f.rBitMask && |
|---|
| 368 | ddsd.ddpfPixelFormat.dwGBitMask == f.gBitMask && |
|---|
| 369 | ddsd.ddpfPixelFormat.dwBBitMask == f.bBitMask && |
|---|
| 370 | ddsd.ddpfPixelFormat.dwRGBAlphaBitMask == f.aBitMask ) |
|---|
| 371 | { |
|---|
| 372 | if ( f.internalFormat != UNSUPPORTED && |
|---|
| 373 | f.pixelFormat != UNSUPPORTED && |
|---|
| 374 | f.dataType != UNSUPPORTED ) |
|---|
| 375 | { |
|---|
| 376 | osg::notify(osg::INFO) << "ReadDDSFile info : format = " << f.name << std::endl; |
|---|
| 377 | internalFormat = f.internalFormat; |
|---|
| 378 | pixelFormat = f.pixelFormat; |
|---|
| 379 | dataType = f.dataType; |
|---|
| 380 | found = true; |
|---|
| 381 | break; |
|---|
| 382 | } |
|---|
| 383 | else |
|---|
| 384 | { |
|---|
| 385 | osg::notify(osg::INFO) << "ReadDDSFile info : " << f.name |
|---|
| 386 | << " format is not supported" << std::endl; |
|---|
| 387 | return NULL; |
|---|
| 388 | } |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | if ( !found ) |
|---|
| 393 | { |
|---|
| 394 | osg::notify(osg::WARN) << "ReadDDSFile warning: unhandled RGB pixel format in dds file, image not loaded" << std::endl; |
|---|
| 395 | osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRGBBitCount = " |
|---|
| 396 | << ddsd.ddpfPixelFormat.dwRGBBitCount << std::endl; |
|---|
| 397 | osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRBitMask = 0x" |
|---|
| 398 | << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 399 | << ddsd.ddpfPixelFormat.dwRBitMask << std::endl; |
|---|
| 400 | osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwGBitMask = 0x" |
|---|
| 401 | << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 402 | << ddsd.ddpfPixelFormat.dwGBitMask << std::endl; |
|---|
| 403 | osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwBBitMask = 0x" |
|---|
| 404 | << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 405 | << ddsd.ddpfPixelFormat.dwBBitMask << std::endl; |
|---|
| 406 | osg::notify(osg::INFO) << "ReadDDSFile info : ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0x" |
|---|
| 407 | << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 408 | << ddsd.ddpfPixelFormat.dwRGBAlphaBitMask << std::dec << std::endl; |
|---|
| [2995] | 409 | return NULL; |
|---|
| [1801] | 410 | } |
|---|
| [1795] | 411 | } |
|---|
| [2995] | 412 | else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_LUMINANCE) |
|---|
| 413 | { |
|---|
| 414 | internalFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE; |
|---|
| [6929] | 415 | pixelFormat = usingAlpha ? GL_LUMINANCE_ALPHA : GL_LUMINANCE; |
|---|
| 416 | if ( usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 8 ) |
|---|
| 417 | { |
|---|
| 418 | osg::notify(osg::INFO) << "ReadDDSFile info : format = L4A4" << std::endl; |
|---|
| 419 | pixelFormat = GL_LUMINANCE4_ALPHA4; |
|---|
| 420 | } |
|---|
| 421 | else if ( usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 32 ) |
|---|
| 422 | { |
|---|
| 423 | osg::notify(osg::INFO) << "ReadDDSFile info : format = L16A16" << std::endl; |
|---|
| 424 | dataType = GL_UNSIGNED_SHORT; |
|---|
| 425 | } |
|---|
| 426 | else if ( !usingAlpha && ddsd.ddpfPixelFormat.dwLuminanceBitDepth == 16 ) |
|---|
| 427 | { |
|---|
| 428 | osg::notify(osg::INFO) << "ReadDDSFile info : format = L16" << std::endl; |
|---|
| 429 | dataType = GL_UNSIGNED_SHORT; |
|---|
| 430 | } |
|---|
| 431 | else if ( usingAlpha ) |
|---|
| 432 | { |
|---|
| 433 | osg::notify(osg::INFO) << "ReadDDSFile info : format = L8A8" << std::endl; |
|---|
| 434 | } |
|---|
| 435 | else |
|---|
| 436 | { |
|---|
| 437 | osg::notify(osg::INFO) << "ReadDDSFile info : format = L8" << std::endl; |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| [2995] | 443 | } |
|---|
| [6929] | 444 | else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_ALPHA) |
|---|
| [3020] | 445 | { |
|---|
| [6929] | 446 | osg::notify(osg::INFO) << "ReadDDSFile info : format = ALPHA" << std::endl; |
|---|
| [3020] | 447 | internalFormat = GL_ALPHA; |
|---|
| 448 | pixelFormat = GL_ALPHA; |
|---|
| 449 | } |
|---|
| [2995] | 450 | |
|---|
| [1801] | 451 | else if(ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) |
|---|
| 452 | { |
|---|
| [6929] | 453 | |
|---|
| [1801] | 454 | switch(ddsd.ddpfPixelFormat.dwFourCC) |
|---|
| 455 | { |
|---|
| [2995] | 456 | case FOURCC_DXT1: |
|---|
| [6929] | 457 | osg::notify(osg::INFO) << "ReadDDSFile info : format = DXT1" << std::endl; |
|---|
| [2995] | 458 | if (usingAlpha) |
|---|
| 459 | { |
|---|
| 460 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 461 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 462 | } |
|---|
| 463 | else |
|---|
| 464 | { |
|---|
| 465 | internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
|---|
| 466 | pixelFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
|---|
| [5755] | 467 | checkIfUsingOneBitAlpha = true; |
|---|
| [2995] | 468 | } |
|---|
| 469 | break; |
|---|
| 470 | case FOURCC_DXT3: |
|---|
| [6929] | 471 | osg::notify(osg::INFO) << "ReadDDSFile info : format = DXT3" << std::endl; |
|---|
| [2995] | 472 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
|---|
| 473 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
|---|
| 474 | break; |
|---|
| 475 | case FOURCC_DXT5: |
|---|
| [6929] | 476 | osg::notify(osg::INFO) << "ReadDDSFile info : format = DXT5" << std::endl; |
|---|
| [2995] | 477 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
|---|
| 478 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
|---|
| 479 | break; |
|---|
| [6929] | 480 | case 0x00000024: |
|---|
| 481 | osg::notify(osg::INFO) << "ReadDDSFile info : format = A16B16G16R16" << std::endl; |
|---|
| 482 | internalFormat = GL_RGBA; |
|---|
| 483 | pixelFormat = GL_RGBA; |
|---|
| 484 | dataType = GL_UNSIGNED_SHORT; |
|---|
| 485 | break; |
|---|
| 486 | case 0x00000071: |
|---|
| 487 | osg::notify(osg::INFO) << "ReadDDSFile info : format = A16B16G16R16F" << std::endl; |
|---|
| 488 | internalFormat = GL_RGBA; |
|---|
| 489 | pixelFormat = GL_RGBA; |
|---|
| 490 | dataType = GL_HALF_FLOAT_NV; |
|---|
| 491 | break; |
|---|
| 492 | case 0x0000006E: |
|---|
| 493 | osg::notify(osg::INFO) << "ReadDDSFile info : format = Q16W16V16U16" << std::endl; |
|---|
| 494 | internalFormat = GL_RGBA; |
|---|
| 495 | pixelFormat = GL_RGBA; |
|---|
| 496 | dataType = GL_UNSIGNED_SHORT; |
|---|
| 497 | break; |
|---|
| 498 | case 0x00000070: |
|---|
| 499 | osg::notify(osg::INFO) << "ReadDDSFile info : G16R16F format is not supported" |
|---|
| 500 | << std::endl; |
|---|
| 501 | return NULL; |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | break; |
|---|
| 506 | case 0x00000073: |
|---|
| 507 | osg::notify(osg::INFO) << "ReadDDSFile info : G32R32F format is not supported" |
|---|
| 508 | << std::endl; |
|---|
| 509 | return NULL; |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | break; |
|---|
| 514 | case 0x00000072: |
|---|
| 515 | osg::notify(osg::INFO) << "ReadDDSFile info : format = R32F" << std::endl; |
|---|
| 516 | internalFormat = GL_RGB; |
|---|
| 517 | pixelFormat = GL_RED; |
|---|
| 518 | dataType = GL_FLOAT; |
|---|
| 519 | break; |
|---|
| 520 | case 0x0000006F: |
|---|
| 521 | osg::notify(osg::INFO) << "ReadDDSFile info : format = R16F" << std::endl; |
|---|
| 522 | internalFormat = GL_RGB; |
|---|
| 523 | pixelFormat = GL_RED; |
|---|
| 524 | dataType = GL_HALF_FLOAT_NV; |
|---|
| 525 | break; |
|---|
| 526 | case 0x00000074: |
|---|
| 527 | osg::notify(osg::INFO) << "ReadDDSFile info : format = A32B32G32R32F" << std::endl; |
|---|
| 528 | internalFormat = GL_RGBA; |
|---|
| 529 | pixelFormat = GL_RGBA; |
|---|
| 530 | dataType = GL_FLOAT; |
|---|
| 531 | break; |
|---|
| 532 | case 0x00000075: |
|---|
| 533 | osg::notify(osg::INFO) << "ReadDDSFile info : CxV8U8 format is not supported" << std::endl; |
|---|
| 534 | return NULL; |
|---|
| 535 | |
|---|
| 536 | case MAKEFOURCC( 'U', 'Y', 'V', 'Y' ): |
|---|
| 537 | case MAKEFOURCC( 'U', 'Y', 'V', '2' ): |
|---|
| 538 | case MAKEFOURCC( 'R', 'G', 'B', 'G' ): |
|---|
| 539 | case MAKEFOURCC( 'G', 'R', 'G', 'B' ): |
|---|
| 540 | |
|---|
| 541 | |
|---|
| [2995] | 542 | default: |
|---|
| [6929] | 543 | osg::notify(osg::WARN) << "ReadDDSFile warning: unhandled FOURCC pixel format (" |
|---|
| 544 | << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x000000ff)) |
|---|
| 545 | << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x0000ff00) >> 8) |
|---|
| 546 | << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0x00ff0000) >> 16) |
|---|
| 547 | << (char)((ddsd.ddpfPixelFormat.dwFourCC & 0xff000000) >> 24) |
|---|
| 548 | << " = 0x" << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 549 | << ddsd.ddpfPixelFormat.dwFourCC << std::dec |
|---|
| 550 | << ") in dds file, image not loaded." << std::endl; |
|---|
| [2995] | 551 | return NULL; |
|---|
| [1801] | 552 | } |
|---|
| 553 | } |
|---|
| [3014] | 554 | else |
|---|
| [1801] | 555 | { |
|---|
| [6929] | 556 | osg::notify(osg::WARN) << "ReadDDSFile warning: unhandled pixel format (ddsd.ddpfPixelFormat.dwFlags" |
|---|
| 557 | << " = 0x" << std::hex << std::setw(8) << std::setfill('0') |
|---|
| 558 | << ddsd.ddpfPixelFormat.dwFlags << std::dec |
|---|
| 559 | << ") in dds file, image not loaded."<<std::endl; |
|---|
| [1801] | 560 | return NULL; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| [3088] | 563 | |
|---|
| [3014] | 564 | |
|---|
| [2995] | 565 | |
|---|
| 566 | |
|---|
| [3014] | 567 | |
|---|
| [2995] | 568 | |
|---|
| 569 | |
|---|
| [3014] | 570 | |
|---|
| [3088] | 571 | |
|---|
| [3014] | 572 | |
|---|
| [3088] | 573 | |
|---|
| [3014] | 574 | |
|---|
| [2995] | 575 | |
|---|
| [3014] | 576 | |
|---|
| [2995] | 577 | |
|---|
| [3088] | 578 | |
|---|
| 579 | |
|---|
| [1823] | 580 | |
|---|
| 581 | |
|---|
| 582 | |
|---|
| [3088] | 583 | |
|---|
| [1823] | 584 | |
|---|
| [3014] | 585 | |
|---|
| [3088] | 586 | |
|---|
| [3014] | 587 | osg::Image::MipmapDataType mipmaps; |
|---|
| [1823] | 588 | |
|---|
| [1801] | 589 | |
|---|
| [1795] | 590 | if (ddsd.dwMipMapCount>1) |
|---|
| 591 | { |
|---|
| [1801] | 592 | |
|---|
| [3014] | 593 | |
|---|
| [1903] | 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | float power2_s = logf((float)s)/logf((float)2); |
|---|
| 599 | float power2_t = logf((float)t)/logf((float)2); |
|---|
| [2995] | 600 | |
|---|
| [9234] | 601 | osg::notify(osg::INFO) << "ReadDDSFile INFO : ddsd.dwMipMapCount = "<<ddsd.dwMipMapCount<<std::endl; |
|---|
| 602 | osg::notify(osg::INFO) << "ReadDDSFile INFO : s = "<<s<<std::endl; |
|---|
| 603 | osg::notify(osg::INFO) << "ReadDDSFile INFO : t = "<<t<<std::endl; |
|---|
| 604 | osg::notify(osg::INFO) << "ReadDDSFile INFO : power2_s="<<power2_s<<std::endl; |
|---|
| 605 | osg::notify(osg::INFO) << "ReadDDSFile INFO : power2_t="<<power2_t<<std::endl; |
|---|
| [2995] | 606 | |
|---|
| [1905] | 607 | mipmaps.resize((unsigned int)osg::maximum(power2_s,power2_t),0); |
|---|
| [1903] | 608 | |
|---|
| [6929] | 609 | |
|---|
| 610 | if( (ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) |
|---|
| 611 | && |
|---|
| 612 | (ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1 || |
|---|
| 613 | ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT3 || |
|---|
| 614 | ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT5)) |
|---|
| [1801] | 615 | { |
|---|
| 616 | int width = ddsd.dwWidth; |
|---|
| 617 | int height = ddsd.dwHeight; |
|---|
| [2995] | 618 | int blockSize = (ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1) ? 8 : 16; |
|---|
| [1801] | 619 | int offset = 0; |
|---|
| 620 | for (unsigned int k = 1; k < ddsd.dwMipMapCount && (width || height); ++k) |
|---|
| 621 | { |
|---|
| 622 | if (width == 0) |
|---|
| 623 | width = 1; |
|---|
| 624 | if (height == 0) |
|---|
| 625 | height = 1; |
|---|
| [3014] | 626 | offset += (((width+3)/4) * ((height+3)/4) * blockSize); |
|---|
| [1801] | 627 | mipmaps[k-1] = offset; |
|---|
| 628 | width >>= 1; |
|---|
| 629 | height >>= 1; |
|---|
| 630 | } |
|---|
| [3014] | 631 | |
|---|
| [1801] | 632 | } |
|---|
| 633 | |
|---|
| [6929] | 634 | else |
|---|
| [1801] | 635 | { |
|---|
| 636 | int offset = 0; |
|---|
| 637 | int width = ddsd.dwWidth; |
|---|
| 638 | int height = ddsd.dwHeight; |
|---|
| 639 | for (unsigned int k = 1; k < ddsd.dwMipMapCount && (width || height); ++k) |
|---|
| 640 | { |
|---|
| 641 | if (width == 0) |
|---|
| 642 | width = 1; |
|---|
| 643 | if (height == 0) |
|---|
| 644 | height = 1; |
|---|
| [6929] | 645 | offset += height * |
|---|
| 646 | osg::Image::computeRowWidthInBytes( width, pixelFormat, dataType, 1 ); |
|---|
| [1801] | 647 | mipmaps[k-1] = offset; |
|---|
| 648 | width >>= 1; |
|---|
| 649 | height >>= 1; |
|---|
| 650 | } |
|---|
| [3014] | 651 | |
|---|
| [1801] | 652 | } |
|---|
| [1795] | 653 | } |
|---|
| 654 | |
|---|
| [3017] | 655 | osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, 0, osg::Image::USE_NEW_DELETE); |
|---|
| [3755] | 656 | if (mipmaps.size()>0) osgImage->setMipmapLevels(mipmaps); |
|---|
| [3017] | 657 | unsigned int size = osgImage->getTotalSizeInBytesIncludingMipmaps(); |
|---|
| [3014] | 658 | |
|---|
| [9234] | 659 | osg::notify(osg::INFO) << "ReadDDSFile INFO : size = " << size << std::endl; |
|---|
| [6929] | 660 | |
|---|
| [3014] | 661 | if(size <= 0) |
|---|
| 662 | { |
|---|
| [6929] | 663 | osg::notify(osg::WARN) << "ReadDDSFile warning: size <= 0" << std::endl; |
|---|
| [3014] | 664 | return NULL; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | unsigned char* imageData = new unsigned char [size]; |
|---|
| 668 | if(!imageData) |
|---|
| [3088] | 669 | { |
|---|
| [6929] | 670 | osg::notify(osg::WARN) << "ReadDDSFile warning: imageData == NULL" << std::endl; |
|---|
| [3014] | 671 | return NULL; |
|---|
| [3088] | 672 | } |
|---|
| [3014] | 673 | |
|---|
| 674 | |
|---|
| 675 | _istream.read((char*)imageData, size); |
|---|
| 676 | |
|---|
| [5755] | 677 | |
|---|
| 678 | if (checkIfUsingOneBitAlpha) |
|---|
| 679 | { |
|---|
| 680 | const DXT1TexelsBlock *texelsBlock = reinterpret_cast<const DXT1TexelsBlock*>(imageData); |
|---|
| [3014] | 681 | |
|---|
| [5755] | 682 | |
|---|
| 683 | unsigned int numBlocks = mipmaps.size()>0 ? mipmaps[0] / 8 : size / 8; |
|---|
| [3014] | 684 | |
|---|
| [5755] | 685 | for (int i=numBlocks; i>0; --i, ++texelsBlock) |
|---|
| 686 | { |
|---|
| 687 | if (texelsBlock->color_0<=texelsBlock->color_1) |
|---|
| 688 | { |
|---|
| 689 | |
|---|
| 690 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 691 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 692 | break; |
|---|
| 693 | } |
|---|
| 694 | } |
|---|
| 695 | } |
|---|
| [3014] | 696 | |
|---|
| [5755] | 697 | osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, imageData, osg::Image::USE_NEW_DELETE); |
|---|
| 698 | if (mipmaps.size()>0) osgImage->setMipmapLevels(mipmaps); |
|---|
| 699 | |
|---|
| [3088] | 700 | |
|---|
| [2840] | 701 | return osgImage.release(); |
|---|
| [1794] | 702 | } |
|---|
| 703 | |
|---|
| [3014] | 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | |
|---|
| [3088] | 710 | |
|---|
| 711 | |
|---|
| [3014] | 712 | |
|---|
| [3088] | 713 | |
|---|
| 714 | |
|---|
| [3014] | 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | |
|---|
| 722 | |
|---|
| [3609] | 723 | bool WriteDDSFile(const osg::Image *img, std::ostream& fout) |
|---|
| [1794] | 724 | { |
|---|
| [2995] | 725 | |
|---|
| 726 | |
|---|
| [3017] | 727 | DDSURFACEDESC2 ddsd; |
|---|
| 728 | DDPIXELFORMAT ddpf; |
|---|
| [3755] | 729 | |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | |
|---|
| [3017] | 733 | DDSCAPS2 ddsCaps; |
|---|
| [2995] | 734 | |
|---|
| 735 | ddsd.dwSize = sizeof(ddsd); |
|---|
| 736 | ddpf.dwSize = sizeof(ddpf); |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | unsigned int SD_flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT; |
|---|
| 740 | unsigned int CAPS_flags = DDSCAPS_TEXTURE; |
|---|
| 741 | unsigned int PF_flags = 0; |
|---|
| 742 | unsigned int CAPS2_flags = 0; |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | unsigned int dataType = img->getDataType(); |
|---|
| 746 | unsigned int pixelFormat = img->getPixelFormat(); |
|---|
| [3088] | 747 | |
|---|
| 748 | |
|---|
| [2995] | 749 | unsigned int pixelSize = osg::Image::computePixelSizeInBits(pixelFormat, dataType); |
|---|
| [3017] | 750 | unsigned int imageSize = img->getImageSizeInBytes(); |
|---|
| [2995] | 751 | bool is3dImage = false; |
|---|
| 752 | |
|---|
| [3088] | 753 | ddsd.dwWidth = img->s(); |
|---|
| 754 | ddsd.dwHeight = img->t(); |
|---|
| [2995] | 755 | int r = img->r(); |
|---|
| 756 | |
|---|
| 757 | if(r > 1) |
|---|
| 758 | { |
|---|
| 759 | is3dImage = true; |
|---|
| 760 | ddsd.dwDepth = r; |
|---|
| 761 | SD_flags |= DDSD_DEPTH; |
|---|
| 762 | CAPS_flags |= DDSCAPS_COMPLEX; |
|---|
| 763 | CAPS2_flags |= DDSCAPS2_VOLUME; |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | switch (pixelFormat) |
|---|
| 768 | { |
|---|
| 769 | |
|---|
| 770 | case GL_RGBA: |
|---|
| 771 | { |
|---|
| [7427] | 772 | ddpf.dwRBitMask = 0x000000ff; |
|---|
| [2995] | 773 | ddpf.dwGBitMask = 0x0000ff00; |
|---|
| [7427] | 774 | ddpf.dwBBitMask = 0x00ff0000; |
|---|
| [2995] | 775 | ddpf.dwRGBAlphaBitMask = 0xff000000; |
|---|
| 776 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); |
|---|
| 777 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 778 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 779 | SD_flags |= DDSD_PITCH; |
|---|
| [1795] | 780 | } |
|---|
| [2995] | 781 | break; |
|---|
| 782 | case GL_BGRA: |
|---|
| 783 | { |
|---|
| [7427] | 784 | ddpf.dwBBitMask = 0x000000ff; |
|---|
| [2995] | 785 | ddpf.dwGBitMask = 0x0000ff00; |
|---|
| [7427] | 786 | ddpf.dwRBitMask = 0x00ff0000; |
|---|
| [2995] | 787 | ddpf.dwRGBAlphaBitMask = 0xff000000; |
|---|
| 788 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_RGB); |
|---|
| 789 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 790 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 791 | SD_flags |= DDSD_PITCH; |
|---|
| 792 | } |
|---|
| 793 | break; |
|---|
| 794 | case GL_LUMINANCE_ALPHA: |
|---|
| 795 | { |
|---|
| 796 | ddpf.dwRBitMask = 0x00ff0000; |
|---|
| 797 | ddpf.dwRGBAlphaBitMask = 0x000000ff; |
|---|
| [3017] | 798 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_LUMINANCE); |
|---|
| [2995] | 799 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 800 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 801 | SD_flags |= DDSD_PITCH; |
|---|
| 802 | } |
|---|
| 803 | break; |
|---|
| 804 | case GL_RGB: |
|---|
| 805 | { |
|---|
| [7427] | 806 | ddpf.dwRBitMask = 0x000000ff; |
|---|
| 807 | ddpf.dwGBitMask = 0x0000ff00; |
|---|
| 808 | ddpf.dwBBitMask = 0x00ff0000; |
|---|
| [2995] | 809 | PF_flags |= DDPF_RGB; |
|---|
| 810 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 811 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 812 | SD_flags |= DDSD_PITCH; |
|---|
| [1795] | 813 | } |
|---|
| [2995] | 814 | break; |
|---|
| 815 | case GL_LUMINANCE: |
|---|
| 816 | { |
|---|
| 817 | ddpf.dwRBitMask = 0x00ff0000; |
|---|
| 818 | PF_flags |= DDPF_LUMINANCE; |
|---|
| 819 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 820 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 821 | SD_flags |= DDSD_PITCH; |
|---|
| 822 | } |
|---|
| 823 | break; |
|---|
| [3017] | 824 | case GL_ALPHA: |
|---|
| 825 | { |
|---|
| 826 | ddpf.dwRGBAlphaBitMask = 0x000000ff; |
|---|
| 827 | PF_flags |= DDPF_ALPHA; |
|---|
| 828 | ddpf.dwRGBBitCount = pixelSize; |
|---|
| 829 | ddsd.lPitch = img->getRowSizeInBytes(); |
|---|
| 830 | SD_flags |= DDSD_PITCH; |
|---|
| 831 | } |
|---|
| 832 | break; |
|---|
| [1794] | 833 | |
|---|
| [2995] | 834 | |
|---|
| 835 | case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
|---|
| [1795] | 836 | { |
|---|
| [2995] | 837 | ddpf.dwFourCC = FOURCC_DXT1; |
|---|
| 838 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); |
|---|
| 839 | ddsd.dwLinearSize = imageSize; |
|---|
| 840 | SD_flags |= DDSD_LINEARSIZE; |
|---|
| 841 | } |
|---|
| 842 | break; |
|---|
| 843 | case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: |
|---|
| 844 | { |
|---|
| 845 | ddpf.dwFourCC = FOURCC_DXT3; |
|---|
| 846 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); |
|---|
| 847 | ddsd.dwLinearSize = imageSize; |
|---|
| 848 | SD_flags |= DDSD_LINEARSIZE; |
|---|
| 849 | } |
|---|
| 850 | break; |
|---|
| 851 | case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: |
|---|
| 852 | { |
|---|
| 853 | ddpf.dwFourCC = FOURCC_DXT5; |
|---|
| 854 | PF_flags |= (DDPF_ALPHAPIXELS | DDPF_FOURCC); |
|---|
| 855 | ddsd.dwLinearSize = imageSize; |
|---|
| 856 | SD_flags |= DDSD_LINEARSIZE; |
|---|
| 857 | } |
|---|
| 858 | break; |
|---|
| 859 | case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
|---|
| 860 | { |
|---|
| 861 | ddpf.dwFourCC = FOURCC_DXT1; |
|---|
| 862 | PF_flags |= DDPF_FOURCC; |
|---|
| 863 | ddsd.dwLinearSize = imageSize; |
|---|
| 864 | SD_flags |= DDSD_LINEARSIZE; |
|---|
| 865 | } |
|---|
| 866 | break; |
|---|
| 867 | default: |
|---|
| 868 | osg::notify(osg::WARN)<<"Warning:: unhandled pixel format in image, file cannot be written."<<std::endl; |
|---|
| 869 | return false; |
|---|
| 870 | } |
|---|
| [2501] | 871 | |
|---|
| [3017] | 872 | |
|---|
| [2995] | 873 | |
|---|
| 874 | if(img->isMipmap() && !is3dImage) |
|---|
| 875 | { |
|---|
| 876 | SD_flags |= DDSD_MIPMAPCOUNT; |
|---|
| 877 | CAPS_flags |= DDSCAPS_COMPLEX | DDSCAPS_MIPMAP; |
|---|
| 878 | ddsd.dwMipMapCount = img->getNumMipmapLevels(); |
|---|
| [9037] | 879 | |
|---|
| [9234] | 880 | osg::notify(osg::INFO)<<"writing out with mipmaps ddsd.dwMipMapCount"<<ddsd.dwMipMapCount<<std::endl; |
|---|
| [2995] | 881 | } |
|---|
| [9037] | 882 | else |
|---|
| 883 | { |
|---|
| [9234] | 884 | osg::notify(osg::INFO)<<"no mipmaps to write out."<<std::endl; |
|---|
| [9037] | 885 | } |
|---|
| [1794] | 886 | |
|---|
| [2995] | 887 | |
|---|
| 888 | |
|---|
| 889 | ddsd.dwFlags = SD_flags; |
|---|
| 890 | ddpf.dwFlags = PF_flags; |
|---|
| 891 | ddsCaps.dwCaps = CAPS_flags; |
|---|
| 892 | ddsCaps.dwCaps2 = CAPS2_flags; |
|---|
| 893 | |
|---|
| 894 | ddsd.ddpfPixelFormat = ddpf; |
|---|
| 895 | ddsd.ddsCaps = ddsCaps; |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | |
|---|
| [5468] | 899 | fout.write("DDS ", 4); |
|---|
| 900 | fout.write(reinterpret_cast<char*>(&ddsd), sizeof(ddsd)); |
|---|
| [2995] | 901 | |
|---|
| [5468] | 902 | |
|---|
| [2995] | 903 | if(!is3dImage) |
|---|
| 904 | { |
|---|
| [5468] | 905 | fout.write(reinterpret_cast<const char*>(img->data()), img->getTotalSizeInBytesIncludingMipmaps()); |
|---|
| [2995] | 906 | } |
|---|
| 907 | else |
|---|
| 908 | { |
|---|
| 909 | for(int i = 0; i < r; ++i) |
|---|
| 910 | { |
|---|
| [5468] | 911 | fout.write(reinterpret_cast<const char*>(img->data(0, 0, i)), imageSize); |
|---|
| [2995] | 912 | } |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | |
|---|
| [3609] | 916 | if (fout.fail()) |
|---|
| [2995] | 917 | { |
|---|
| 918 | return false; |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | return true; |
|---|
| 923 | } |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | class ReaderWriterDDS : public osgDB::ReaderWriter |
|---|
| 927 | { |
|---|
| 928 | public: |
|---|
| [8578] | 929 | |
|---|
| 930 | ReaderWriterDDS() |
|---|
| 931 | { |
|---|
| 932 | supportsExtension("dds","DDS image format"); |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| [3539] | 935 | virtual const char* className() const |
|---|
| [2995] | 936 | { |
|---|
| 937 | return "DDS Image Reader/Writer"; |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| [3694] | 940 | virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| [3609] | 941 | { |
|---|
| 942 | return readImage(file,options); |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| [3694] | 945 | virtual ReadResult readObject(std::istream& fin, const Options* options) const |
|---|
| [3609] | 946 | { |
|---|
| 947 | return readImage(fin,options); |
|---|
| 948 | } |
|---|
| 949 | |
|---|
| [3694] | 950 | virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| [2995] | 951 | { |
|---|
| 952 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 953 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 954 | |
|---|
| [3691] | 955 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| [5468] | 956 | |
|---|
| [2995] | 957 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 958 | |
|---|
| [9124] | 959 | osgDB::ifstream stream(fileName.c_str(), std::ios::in | std::ios::binary); |
|---|
| [3088] | 960 | if(!stream) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 961 | ReadResult rr = readImage(stream, options); |
|---|
| 962 | if(rr.validImage()) rr.getImage()->setFileName(file); |
|---|
| 963 | return rr; |
|---|
| [3014] | 964 | } |
|---|
| 965 | |
|---|
| [3694] | 966 | virtual ReadResult readImage(std::istream& fin, const Options* options) const |
|---|
| [3088] | 967 | { |
|---|
| [3014] | 968 | osg::Image* osgImage = ReadDDSFile(fin); |
|---|
| [2995] | 969 | if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED; |
|---|
| [3291] | 970 | |
|---|
| [3294] | 971 | if (options && options->getOptionString().find("dds_flip")!=std::string::npos) |
|---|
| [3291] | 972 | { |
|---|
| 973 | osgImage->flipVertical(); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| [2995] | 976 | return osgImage; |
|---|
| [3088] | 977 | } |
|---|
| [2995] | 978 | |
|---|
| [3694] | 979 | virtual WriteResult writeObject(const osg::Object& object,const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| [2995] | 980 | { |
|---|
| [3609] | 981 | const osg::Image* image = dynamic_cast<const osg::Image*>(&object); |
|---|
| 982 | if (!image) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 983 | |
|---|
| 984 | return writeImage(*image,file,options); |
|---|
| 985 | } |
|---|
| 986 | |
|---|
| [3694] | 987 | virtual WriteResult writeObject(const osg::Object& object,std::ostream& fout,const Options* options) const |
|---|
| [3609] | 988 | { |
|---|
| 989 | const osg::Image* image = dynamic_cast<const osg::Image*>(&object); |
|---|
| 990 | if (!image) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 991 | |
|---|
| 992 | return writeImage(*image,fout,options); |
|---|
| 993 | } |
|---|
| 994 | |
|---|
| 995 | |
|---|
| [3694] | 996 | virtual WriteResult writeImage(const osg::Image &image,const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| [3609] | 997 | { |
|---|
| [2995] | 998 | std::string ext = osgDB::getFileExtension(file); |
|---|
| 999 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 1000 | |
|---|
| [9124] | 1001 | osgDB::ofstream fout(file.c_str(), std::ios::out | std::ios::binary); |
|---|
| [3609] | 1002 | if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| [2995] | 1003 | |
|---|
| [3609] | 1004 | return writeImage(image,fout,options); |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| [3694] | 1007 | virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const Options*) const |
|---|
| [3609] | 1008 | { |
|---|
| 1009 | bool success = WriteDDSFile(&image, fout); |
|---|
| 1010 | |
|---|
| [2995] | 1011 | if(success) |
|---|
| 1012 | return WriteResult::FILE_SAVED; |
|---|
| 1013 | else |
|---|
| 1014 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 1015 | } |
|---|
| [1794] | 1016 | }; |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| [7076] | 1020 | REGISTER_OSGPLUGIN(dds, ReaderWriterDDS) |
|---|