| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include <osg/Texture> |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/Registry> |
|---|
| 21 | #include <osgDB/FileNameUtils> |
|---|
| 22 | #include <osgDB/FileUtils> |
|---|
| 23 | |
|---|
| 24 | #include <iomanip> |
|---|
| 25 | #include <stdio.h> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | enum VTFFlags |
|---|
| 29 | { |
|---|
| 30 | VTF_FLAGS_POINTSAMPLE = 0x00000001, |
|---|
| 31 | VTF_FLAGS_TRILINEAR = 0x00000002, |
|---|
| 32 | VTF_FLAGS_CLAMP_S = 0x00000004, |
|---|
| 33 | VTF_FLAGS_CLAMP_T = 0x00000008, |
|---|
| 34 | VTF_FLAGS_ANISOTROPIC = 0x00000010, |
|---|
| 35 | VTF_FLAGS_HINT_DXT5 = 0x00000020, |
|---|
| 36 | VTF_FLAGS_NOCOMPRESS = 0x00000040, |
|---|
| 37 | VTF_FLAGS_NORMAL = 0x00000080, |
|---|
| 38 | VTF_FLAGS_NOMIP = 0x00000100, |
|---|
| 39 | VTF_FLAGS_NOLOD = 0x00000200, |
|---|
| 40 | VTF_FLAGS_MINMIP = 0x00000400, |
|---|
| 41 | VTF_FLAGS_PROCEDURAL = 0x00000800, |
|---|
| 42 | VTF_FLAGS_ONEBITALPHA = 0x00001000, |
|---|
| 43 | VTF_FLAGS_EIGHTBITALPHA = 0x00002000, |
|---|
| 44 | VTF_FLAGS_ENVMAP = 0x00004000, |
|---|
| 45 | VTF_FLAGS_RENDERTARGET = 0x00008000, |
|---|
| 46 | VTF_FLAGS_DEPTHRENDERTARGET = 0x00010000, |
|---|
| 47 | VTF_FLAGS_NODEBUGOVERRIDE = 0x00020000, |
|---|
| 48 | VTF_FLAGS_SINGLECOPY = 0x00040000, |
|---|
| 49 | VTF_FLAGS_ONEOVERMIPLEVELINALPHA = 0x00080000, |
|---|
| 50 | VTF_FLAGS_PREMULTCOLORBYONEOVERMIPLEVEL = 0x00100000, |
|---|
| 51 | VTF_FLAGS_NORMALTODUDV = 0x00200000, |
|---|
| 52 | VTF_FLAGS_ALPHATESTMIPGENERATION = 0x00400000, |
|---|
| 53 | VTF_FLAGS_NODEPTHBUFFER = 0x00800000, |
|---|
| 54 | VTF_FLAGS_NICEFILTERED = 0x01000000, |
|---|
| 55 | VTF_FLAGS_CLAMP_U = 0x02000000, |
|---|
| 56 | VTF_FLAGS_PRESWIZZLED = 0x04000000, |
|---|
| 57 | VTF_FLAGS_CACHEABLE = 0x08000000, |
|---|
| 58 | VTF_FLAGS_UNFILTERABLE_OK = 0x10000000, |
|---|
| 59 | VTF_FLAGS_LASTFLAG = 0x10000000 |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | enum VTFCubeMapFaceIndex |
|---|
| 64 | { |
|---|
| 65 | VTF_CUBEMAP_FACE_RIGHT = 0, |
|---|
| 66 | VTF_CUBEMAP_FACE_LEFT, |
|---|
| 67 | VTF_CUBEMAP_FACE_BACK, |
|---|
| 68 | VTF_CUBEMAP_FACE_FRONT, |
|---|
| 69 | VTF_CUBEMAP_FACE_UP, |
|---|
| 70 | VTF_CUBEMAP_FACE_DOWN, |
|---|
| 71 | VTF_CUBEMAP_FACE_SPHEREMAP, |
|---|
| 72 | VTF_CUBEMAP_FACE_COUNT |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | enum VTFLookDir |
|---|
| 77 | { |
|---|
| 78 | VTF_LOOK_DOWN_X = 0, |
|---|
| 79 | VTF_LOOK_DOWN_NEGX, |
|---|
| 80 | VTF_LOOK_DOWN_Y = 0, |
|---|
| 81 | VTF_LOOK_DOWN_NEGY, |
|---|
| 82 | VTF_LOOK_DOWN_Z = 0, |
|---|
| 83 | VTF_LOOK_DOWN_NEGZ |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | enum VTFImageFormat |
|---|
| 88 | { |
|---|
| 89 | VTF_FORMAT_UNKNOWN = -1, |
|---|
| 90 | VTF_FORMAT_RGBA8888 = 0, |
|---|
| 91 | VTF_FORMAT_ABGR8888, |
|---|
| 92 | VTF_FORMAT_RGB888, |
|---|
| 93 | VTF_FORMAT_BGR888, |
|---|
| 94 | VTF_FORMAT_RGB565, |
|---|
| 95 | VTF_FORMAT_I8, |
|---|
| 96 | VTF_FORMAT_IA88, |
|---|
| 97 | VTF_FORMAT_P8, |
|---|
| 98 | VTF_FORMAT_A8, |
|---|
| 99 | VTF_FORMAT_RGB888_BLUESCREEN, |
|---|
| 100 | VTF_FORMAT_BGR888_BLUESCREEN, |
|---|
| 101 | VTF_FORMAT_ARGB8888, |
|---|
| 102 | VTF_FORMAT_BGRA8888, |
|---|
| 103 | VTF_FORMAT_DXT1, |
|---|
| 104 | VTF_FORMAT_DXT3, |
|---|
| 105 | VTF_FORMAT_DXT5, |
|---|
| 106 | VTF_FORMAT_BGRX8888, |
|---|
| 107 | VTF_FORMAT_BGR565, |
|---|
| 108 | VTF_FORMAT_BGRX5551, |
|---|
| 109 | VTF_FORMAT_BGRA4444, |
|---|
| 110 | VTF_FORMAT_DXT1_ONEBITALPHA, |
|---|
| 111 | VTF_FORMAT_BGRA5551, |
|---|
| 112 | VTF_FORMAT_UV88, |
|---|
| 113 | VTF_FORMAT_UVWQ8888, |
|---|
| 114 | VTF_FORMAT_RGBA16161616F, |
|---|
| 115 | VTF_FORMAT_RGBA16161616, |
|---|
| 116 | VTF_FORMAT_UVLX8888, |
|---|
| 117 | VTF_FORMAT_R32F, |
|---|
| 118 | VTF_FORMAT_RGB323232F, |
|---|
| 119 | VTF_FORMAT_RGBA32323232F, |
|---|
| 120 | VTF_NUM_IMAGE_FORMATS |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | #define VTF_FORMAT_DEFAULT ((VTFImageFormat)-2) |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | struct VTFFileHeader |
|---|
| 128 | { |
|---|
| 129 | char magic_number[4]; |
|---|
| 130 | unsigned int file_version[2]; |
|---|
| 131 | unsigned int header_size; |
|---|
| 132 | unsigned short image_width; |
|---|
| 133 | unsigned short image_height; |
|---|
| 134 | unsigned int image_flags; |
|---|
| 135 | unsigned short num_frames; |
|---|
| 136 | unsigned short start_frame; |
|---|
| 137 | |
|---|
| 138 | unsigned char padding_0[4]; |
|---|
| 139 | osg::Vec3f reflectivity_value; |
|---|
| 140 | unsigned char padding_1[4]; |
|---|
| 141 | |
|---|
| 142 | float bump_scale; |
|---|
| 143 | unsigned int image_format; |
|---|
| 144 | unsigned char num_mip_levels; |
|---|
| 145 | unsigned char low_res_image_format; |
|---|
| 146 | unsigned char padding_2[3]; |
|---|
| 147 | unsigned char low_res_image_width; |
|---|
| 148 | unsigned char low_res_image_height; |
|---|
| 149 | unsigned short image_depth; |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | struct DXT1TexelsBlock |
|---|
| 158 | { |
|---|
| 159 | unsigned short color_0; |
|---|
| 160 | unsigned short color_1; |
|---|
| 161 | unsigned int texels4x4; |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | bool ConvertImageFormat(unsigned int vtfFormat, int& internalFormat, |
|---|
| 166 | int& pixelFormat, int& dataType) |
|---|
| 167 | { |
|---|
| 168 | bool supported; |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | supported = true; |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | switch (vtfFormat) |
|---|
| 175 | { |
|---|
| 176 | case VTF_FORMAT_DEFAULT: |
|---|
| 177 | supported = false; |
|---|
| 178 | break; |
|---|
| 179 | |
|---|
| 180 | case VTF_FORMAT_UNKNOWN: |
|---|
| 181 | supported = false; |
|---|
| 182 | break; |
|---|
| 183 | |
|---|
| 184 | case VTF_FORMAT_RGBA8888: |
|---|
| 185 | internalFormat = GL_RGBA; |
|---|
| 186 | pixelFormat = GL_RGBA; |
|---|
| 187 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 188 | break; |
|---|
| 189 | |
|---|
| 190 | case VTF_FORMAT_ABGR8888: |
|---|
| 191 | internalFormat = GL_RGBA; |
|---|
| 192 | pixelFormat = GL_ABGR_EXT; |
|---|
| 193 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 194 | break; |
|---|
| 195 | |
|---|
| 196 | case VTF_FORMAT_RGB888: |
|---|
| 197 | internalFormat = GL_RGB; |
|---|
| 198 | pixelFormat = GL_RGB; |
|---|
| 199 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 200 | break; |
|---|
| 201 | |
|---|
| 202 | case VTF_FORMAT_BGR888: |
|---|
| 203 | internalFormat = GL_RGB; |
|---|
| 204 | pixelFormat = GL_BGR; |
|---|
| 205 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 206 | break; |
|---|
| 207 | |
|---|
| 208 | case VTF_FORMAT_RGB565: |
|---|
| 209 | internalFormat = GL_RGB; |
|---|
| 210 | pixelFormat = GL_RGB; |
|---|
| 211 | dataType = GL_UNSIGNED_SHORT_5_6_5; |
|---|
| 212 | break; |
|---|
| 213 | |
|---|
| 214 | case VTF_FORMAT_I8: |
|---|
| 215 | internalFormat = GL_LUMINANCE; |
|---|
| 216 | pixelFormat = GL_LUMINANCE; |
|---|
| 217 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 218 | break; |
|---|
| 219 | |
|---|
| 220 | case VTF_FORMAT_IA88: |
|---|
| 221 | internalFormat = GL_LUMINANCE_ALPHA; |
|---|
| 222 | pixelFormat = GL_LUMINANCE_ALPHA; |
|---|
| 223 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 224 | break; |
|---|
| 225 | |
|---|
| 226 | case VTF_FORMAT_P8: |
|---|
| 227 | |
|---|
| 228 | supported = false; |
|---|
| 229 | break; |
|---|
| 230 | |
|---|
| 231 | case VTF_FORMAT_A8: |
|---|
| 232 | internalFormat = GL_ALPHA; |
|---|
| 233 | pixelFormat = GL_ALPHA; |
|---|
| 234 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 235 | break; |
|---|
| 236 | |
|---|
| 237 | case VTF_FORMAT_RGB888_BLUESCREEN: |
|---|
| 238 | |
|---|
| 239 | internalFormat = GL_RGB; |
|---|
| 240 | pixelFormat = GL_RGB; |
|---|
| 241 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 242 | break; |
|---|
| 243 | |
|---|
| 244 | case VTF_FORMAT_BGR888_BLUESCREEN: |
|---|
| 245 | |
|---|
| 246 | internalFormat = GL_RGB; |
|---|
| 247 | pixelFormat = GL_BGR; |
|---|
| 248 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 249 | break; |
|---|
| 250 | |
|---|
| 251 | case VTF_FORMAT_ARGB8888: |
|---|
| 252 | |
|---|
| 253 | supported = false; |
|---|
| 254 | break; |
|---|
| 255 | |
|---|
| 256 | case VTF_FORMAT_BGRA8888: |
|---|
| 257 | internalFormat = GL_RGBA; |
|---|
| 258 | pixelFormat = GL_BGRA; |
|---|
| 259 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 260 | break; |
|---|
| 261 | |
|---|
| 262 | case VTF_FORMAT_DXT1: |
|---|
| 263 | internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
|---|
| 264 | pixelFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
|---|
| 265 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 266 | break; |
|---|
| 267 | |
|---|
| 268 | case VTF_FORMAT_DXT3: |
|---|
| 269 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
|---|
| 270 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
|---|
| 271 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 272 | break; |
|---|
| 273 | |
|---|
| 274 | case VTF_FORMAT_DXT5: |
|---|
| 275 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
|---|
| 276 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
|---|
| 277 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 278 | break; |
|---|
| 279 | |
|---|
| 280 | case VTF_FORMAT_BGRX8888: |
|---|
| 281 | internalFormat = GL_RGB; |
|---|
| 282 | pixelFormat = GL_BGRA; |
|---|
| 283 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 284 | break; |
|---|
| 285 | |
|---|
| 286 | case VTF_FORMAT_BGR565: |
|---|
| 287 | internalFormat = GL_RGB; |
|---|
| 288 | pixelFormat = GL_BGR; |
|---|
| 289 | dataType = GL_UNSIGNED_SHORT_5_6_5_REV; |
|---|
| 290 | break; |
|---|
| 291 | |
|---|
| 292 | case VTF_FORMAT_BGRX5551: |
|---|
| 293 | internalFormat = GL_RGB; |
|---|
| 294 | pixelFormat = GL_BGRA; |
|---|
| 295 | dataType = GL_UNSIGNED_SHORT_5_5_5_1; |
|---|
| 296 | break; |
|---|
| 297 | |
|---|
| 298 | case VTF_FORMAT_BGRA4444: |
|---|
| 299 | internalFormat = GL_RGBA; |
|---|
| 300 | pixelFormat = GL_BGRA; |
|---|
| 301 | dataType = GL_UNSIGNED_SHORT_4_4_4_4; |
|---|
| 302 | break; |
|---|
| 303 | |
|---|
| 304 | case VTF_FORMAT_DXT1_ONEBITALPHA: |
|---|
| 305 | internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 306 | pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
|---|
| 307 | dataType = GL_UNSIGNED_BYTE; |
|---|
| 308 | break; |
|---|
| 309 | |
|---|
| 310 | case VTF_FORMAT_BGRA5551: |
|---|
| 311 | internalFormat = GL_RGBA; |
|---|
| 312 | pixelFormat = GL_BGRA; |
|---|
| 313 | dataType = GL_UNSIGNED_SHORT_5_5_5_1; |
|---|
| 314 | break; |
|---|
| 315 | |
|---|
| 316 | case VTF_FORMAT_UV88: |
|---|
| 317 | supported = false; |
|---|
| 318 | break; |
|---|
| 319 | |
|---|
| 320 | case VTF_FORMAT_UVWQ8888: |
|---|
| 321 | supported = false; |
|---|
| 322 | break; |
|---|
| 323 | |
|---|
| 324 | case VTF_FORMAT_RGBA16161616F: |
|---|
| 325 | internalFormat = GL_RGBA; |
|---|
| 326 | pixelFormat = GL_RGBA; |
|---|
| 327 | dataType = GL_HALF_FLOAT_NV; |
|---|
| 328 | break; |
|---|
| 329 | |
|---|
| 330 | case VTF_FORMAT_RGBA16161616: |
|---|
| 331 | internalFormat = GL_RGBA; |
|---|
| 332 | pixelFormat = GL_RGBA; |
|---|
| 333 | dataType = GL_UNSIGNED_SHORT; |
|---|
| 334 | break; |
|---|
| 335 | |
|---|
| 336 | case VTF_FORMAT_UVLX8888: |
|---|
| 337 | supported = false; |
|---|
| 338 | break; |
|---|
| 339 | |
|---|
| 340 | default: |
|---|
| 341 | supported = false; |
|---|
| 342 | break; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | return supported; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | osg::Image* ReadVTFFile(std::istream& _istream) |
|---|
| 351 | { |
|---|
| 352 | VTFFileHeader vtf_header; |
|---|
| 353 | bool supported; |
|---|
| 354 | int internalFormat; |
|---|
| 355 | int pixelFormat; |
|---|
| 356 | int dataType; |
|---|
| 357 | int s, t, r; |
|---|
| 358 | unsigned int lrSize; |
|---|
| 359 | unsigned char * imageData; |
|---|
| 360 | unsigned int size; |
|---|
| 361 | int mip; |
|---|
| 362 | int mipSize; |
|---|
| 363 | int mipOffset; |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | _istream.read(&vtf_header.magic_number[0], 4); |
|---|
| 367 | if ((vtf_header.magic_number[0] != 'V') || |
|---|
| 368 | (vtf_header.magic_number[1] != 'T') || |
|---|
| 369 | (vtf_header.magic_number[2] != 'F') || |
|---|
| 370 | (vtf_header.magic_number[3] != 0)) |
|---|
| 371 | { |
|---|
| 372 | |
|---|
| 373 | OSG_WARN << "VTF file is invalid" << std::endl; |
|---|
| 374 | return NULL; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | _istream.read((char *)&vtf_header.file_version[0], 8); |
|---|
| 379 | _istream.read((char *)&vtf_header.header_size, 4); |
|---|
| 380 | _istream.read((char *)&vtf_header.image_width, 2); |
|---|
| 381 | _istream.read((char *)&vtf_header.image_height, 2); |
|---|
| 382 | _istream.read((char *)&vtf_header.image_flags, 4); |
|---|
| 383 | _istream.read((char *)&vtf_header.num_frames, 2); |
|---|
| 384 | _istream.read((char *)&vtf_header.start_frame, 2); |
|---|
| 385 | _istream.ignore(4); |
|---|
| 386 | _istream.read((char *)&vtf_header.reflectivity_value, 12); |
|---|
| 387 | _istream.ignore(4); |
|---|
| 388 | _istream.read((char *)&vtf_header.bump_scale, 4); |
|---|
| 389 | _istream.read((char *)&vtf_header.image_format, 4); |
|---|
| 390 | _istream.read((char *)&vtf_header.num_mip_levels, 1); |
|---|
| 391 | _istream.read((char *)&vtf_header.low_res_image_format, 4); |
|---|
| 392 | _istream.read((char *)&vtf_header.low_res_image_width, 1); |
|---|
| 393 | _istream.read((char *)&vtf_header.low_res_image_height, 1); |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | if ((vtf_header.file_version[0] < 7) || |
|---|
| 397 | ((vtf_header.file_version[0] == 7) && |
|---|
| 398 | (vtf_header.file_version[1] < 2))) |
|---|
| 399 | { |
|---|
| 400 | |
|---|
| 401 | vtf_header.image_depth = 1; |
|---|
| 402 | } |
|---|
| 403 | else |
|---|
| 404 | { |
|---|
| 405 | |
|---|
| 406 | _istream.read((char *)&vtf_header.image_depth, 2); |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | std::streampos filePos = _istream.tellg(); |
|---|
| 411 | _istream.ignore(vtf_header.header_size - filePos); |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | if (vtf_header.image_flags & VTF_FLAGS_ENVMAP) |
|---|
| 415 | { |
|---|
| 416 | OSG_WARN << "VTF Environment maps not supported"; |
|---|
| 417 | OSG_WARN << std::endl; |
|---|
| 418 | return NULL; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | OSG_INFO << "VTF Header: (" << sizeof(VTFFileHeader); |
|---|
| 422 | OSG_INFO << " bytes)" << std::endl; |
|---|
| 423 | OSG_INFO << " magic_number = "; |
|---|
| 424 | OSG_INFO << vtf_header.magic_number[0]; |
|---|
| 425 | OSG_INFO << vtf_header.magic_number[1]; |
|---|
| 426 | OSG_INFO << vtf_header.magic_number[2]; |
|---|
| 427 | OSG_INFO << vtf_header.magic_number[3] << std:: endl; |
|---|
| 428 | OSG_INFO << " file_version = "; |
|---|
| 429 | OSG_INFO << vtf_header.file_version[0] << "."; |
|---|
| 430 | OSG_INFO << vtf_header.file_version[1] << std:: endl; |
|---|
| 431 | OSG_INFO << " header_size = "; |
|---|
| 432 | OSG_INFO << vtf_header.header_size << std::endl; |
|---|
| 433 | OSG_INFO << " image_width = "; |
|---|
| 434 | OSG_INFO << vtf_header.image_width << std::endl; |
|---|
| 435 | OSG_INFO << " image_height = "; |
|---|
| 436 | OSG_INFO << vtf_header.image_height << std::endl; |
|---|
| 437 | OSG_INFO << " num_frames = "; |
|---|
| 438 | OSG_INFO << vtf_header.num_frames << std::endl; |
|---|
| 439 | OSG_INFO << " start_frame = "; |
|---|
| 440 | OSG_INFO << vtf_header.start_frame << std::endl; |
|---|
| 441 | OSG_INFO << " reflectivity = "; |
|---|
| 442 | OSG_INFO << vtf_header.reflectivity_value.x() << ", "; |
|---|
| 443 | OSG_INFO << vtf_header.reflectivity_value.y() << ", "; |
|---|
| 444 | OSG_INFO << vtf_header.reflectivity_value.z() << std::endl; |
|---|
| 445 | OSG_INFO << " bump_scale = "; |
|---|
| 446 | OSG_INFO << vtf_header.bump_scale << std::endl; |
|---|
| 447 | OSG_INFO << " image_format = "; |
|---|
| 448 | OSG_INFO << vtf_header.image_format << std::endl; |
|---|
| 449 | OSG_INFO << " num_mip_lvls = "; |
|---|
| 450 | OSG_INFO << (int)vtf_header.num_mip_levels << std::endl; |
|---|
| 451 | OSG_INFO << " lr_image_fmt = "; |
|---|
| 452 | OSG_INFO << (int)vtf_header.low_res_image_format << std::endl; |
|---|
| 453 | OSG_INFO << " lr_width = "; |
|---|
| 454 | OSG_INFO << (int)vtf_header.low_res_image_width << std::endl; |
|---|
| 455 | OSG_INFO << " lr_height = "; |
|---|
| 456 | OSG_INFO << (int)vtf_header.low_res_image_height << std::endl; |
|---|
| 457 | OSG_INFO << " image_depth = "; |
|---|
| 458 | OSG_INFO << (int)vtf_header.image_depth << std::endl; |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | s = vtf_header.low_res_image_width; |
|---|
| 464 | t = vtf_header.low_res_image_height; |
|---|
| 465 | r = 1; |
|---|
| 466 | OSG_INFO << "Low-res s = " << s << std::endl; |
|---|
| 467 | OSG_INFO << "Low-res t = " << t << std::endl; |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | lrSize = 0; |
|---|
| 471 | if ((s > 0) && (t > 0)) |
|---|
| 472 | { |
|---|
| 473 | supported = ConvertImageFormat(vtf_header.low_res_image_format, |
|---|
| 474 | internalFormat, pixelFormat, dataType); |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | if (!supported) |
|---|
| 479 | { |
|---|
| 480 | OSG_WARN << "Low-res image format is not supported"; |
|---|
| 481 | OSG_WARN << " (" << vtf_header.low_res_image_format; |
|---|
| 482 | OSG_WARN << ")" << std::endl; |
|---|
| 483 | return NULL; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | osg::ref_ptr<osg::Image> loResImage = new osg::Image(); |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | loResImage->setImage(s, t, r, internalFormat, pixelFormat, dataType, |
|---|
| 491 | 0, osg::Image::USE_NEW_DELETE); |
|---|
| 492 | lrSize = loResImage->getTotalSizeInBytes(); |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | OSG_INFO << "Low-res size = " << lrSize << std::endl; |
|---|
| 496 | _istream.ignore(lrSize); |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | supported = ConvertImageFormat(vtf_header.image_format, internalFormat, |
|---|
| 505 | pixelFormat, dataType); |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | if (!supported) |
|---|
| 509 | { |
|---|
| 510 | OSG_WARN << "Image format is not supported ("; |
|---|
| 511 | OSG_WARN << vtf_header.image_format << ")"; |
|---|
| 512 | OSG_WARN << std::endl; |
|---|
| 513 | return NULL; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | s = vtf_header.image_width; |
|---|
| 518 | t = vtf_header.image_height; |
|---|
| 519 | r = vtf_header.image_depth; |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | if (r == 0) |
|---|
| 523 | r = 1; |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | osg::Image::MipmapDataType mipmaps; |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | if (vtf_header.num_mip_levels > 1) |
|---|
| 534 | { |
|---|
| 535 | |
|---|
| 536 | float power2_s = logf((float)s)/logf((float)2); |
|---|
| 537 | float power2_t = logf((float)t)/logf((float)2); |
|---|
| 538 | mipmaps.resize((unsigned int)osg::maximum(power2_s,power2_t),0); |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | if ((vtf_header.image_format == VTF_FORMAT_DXT1) || |
|---|
| 542 | (vtf_header.image_format == VTF_FORMAT_DXT1_ONEBITALPHA) || |
|---|
| 543 | (vtf_header.image_format == VTF_FORMAT_DXT3) || |
|---|
| 544 | (vtf_header.image_format == VTF_FORMAT_DXT5)) |
|---|
| 545 | { |
|---|
| 546 | |
|---|
| 547 | int width = vtf_header.image_width; |
|---|
| 548 | int height = vtf_header.image_height; |
|---|
| 549 | int blockSize; |
|---|
| 550 | |
|---|
| 551 | if ((vtf_header.image_format == VTF_FORMAT_DXT1) || |
|---|
| 552 | (vtf_header.image_format == VTF_FORMAT_DXT1_ONEBITALPHA)) |
|---|
| 553 | blockSize = 8; |
|---|
| 554 | else |
|---|
| 555 | blockSize = 16; |
|---|
| 556 | |
|---|
| 557 | int offset = 0; |
|---|
| 558 | for (unsigned int k = 1; |
|---|
| 559 | (k < vtf_header.num_mip_levels) && (width || height); |
|---|
| 560 | ++k) |
|---|
| 561 | { |
|---|
| 562 | |
|---|
| 563 | if (width == 0) |
|---|
| 564 | width = 1; |
|---|
| 565 | if (height == 0) |
|---|
| 566 | height = 1; |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | offset += (((width+3)/4) * ((height+3)/4) * blockSize); |
|---|
| 570 | mipmaps[k-1] = offset; |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | width >>= 1; |
|---|
| 574 | height >>= 1; |
|---|
| 575 | } |
|---|
| 576 | } |
|---|
| 577 | else |
|---|
| 578 | { |
|---|
| 579 | |
|---|
| 580 | int offset = 0; |
|---|
| 581 | int width = vtf_header.image_width; |
|---|
| 582 | int height = vtf_header.image_height; |
|---|
| 583 | int depth = vtf_header.image_depth; |
|---|
| 584 | for (unsigned int k = 1; |
|---|
| 585 | (k < vtf_header.num_mip_levels) && (width || height || depth); |
|---|
| 586 | ++k) |
|---|
| 587 | { |
|---|
| 588 | if (width == 0) |
|---|
| 589 | width = 1; |
|---|
| 590 | if (height == 0) |
|---|
| 591 | height = 1; |
|---|
| 592 | if (depth == 0) |
|---|
| 593 | depth = 1; |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | offset += depth * height * |
|---|
| 597 | osg::Image::computeRowWidthInBytes(width, pixelFormat, |
|---|
| 598 | dataType, 1 ); |
|---|
| 599 | mipmaps[k-1] = offset; |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | width >>= 1; |
|---|
| 603 | height >>= 1; |
|---|
| 604 | depth >>= 1; |
|---|
| 605 | } |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | |
|---|
| 610 | osg::ref_ptr<osg::Image> osgImage = new osg::Image(); |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | osgImage->setImage(s, t, r, internalFormat, pixelFormat, dataType, |
|---|
| 617 | 0, osg::Image::USE_NEW_DELETE); |
|---|
| 618 | if (mipmaps.size() > 0) |
|---|
| 619 | osgImage->setMipmapLevels(mipmaps); |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | size = osgImage->getTotalSizeInBytesIncludingMipmaps(); |
|---|
| 623 | OSG_INFO << "ReadVTFFile info : size = " << size << std::endl; |
|---|
| 624 | if(size <= 0) |
|---|
| 625 | { |
|---|
| 626 | OSG_WARN << "ReadVTFFile warning: size <= 0" << std::endl; |
|---|
| 627 | return NULL; |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | imageData = new unsigned char [size]; |
|---|
| 632 | if(!imageData) |
|---|
| 633 | { |
|---|
| 634 | OSG_WARN << "ReadVTFFile warning: imageData == NULL"; |
|---|
| 635 | OSG_WARN << std::endl; |
|---|
| 636 | return NULL; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | if (vtf_header.num_mip_levels > 1) |
|---|
| 641 | { |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | for (mip = vtf_header.num_mip_levels - 2; mip >= 0; mip--) |
|---|
| 646 | { |
|---|
| 647 | |
|---|
| 648 | mipOffset = mipmaps[mip]; |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | if (mip == vtf_header.num_mip_levels-2) |
|---|
| 652 | mipSize = size - mipOffset; |
|---|
| 653 | else |
|---|
| 654 | mipSize = mipmaps[mip+1] - mipOffset; |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | _istream.read((char*)&imageData[mipOffset], mipSize); |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | mipSize = mipmaps[0]; |
|---|
| 663 | _istream.read((char*)imageData, mipSize); |
|---|
| 664 | } |
|---|
| 665 | else |
|---|
| 666 | { |
|---|
| 667 | |
|---|
| 668 | _istream.read((char*)imageData, size); |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType, |
|---|
| 697 | imageData, osg::Image::USE_NEW_DELETE); |
|---|
| 698 | if (mipmaps.size()>0) osgImage->setMipmapLevels(mipmaps); |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | return osgImage.release(); |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | bool WriteVTFFile(const osg::Image *img, std::ostream& fout) |
|---|
| 706 | { |
|---|
| 707 | |
|---|
| 708 | return false; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | class ReaderWriterVTF : public osgDB::ReaderWriter |
|---|
| 713 | { |
|---|
| 714 | public: |
|---|
| 715 | virtual const char* className() const |
|---|
| 716 | { |
|---|
| 717 | return "VTF Image Reader/Writer"; |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | virtual bool acceptsExtension(const std::string& extension) const |
|---|
| 721 | { |
|---|
| 722 | return osgDB::equalCaseInsensitive(extension, "vtf"); |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 726 | { |
|---|
| 727 | return readImage(file,options); |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | virtual ReadResult readObject(std::istream& fin, const Options* options) const |
|---|
| 731 | { |
|---|
| 732 | return readImage(fin,options); |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 736 | { |
|---|
| 737 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 738 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 739 | |
|---|
| 740 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 741 | |
|---|
| 742 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 743 | |
|---|
| 744 | osgDB::ifstream stream(fileName.c_str(), std::ios::in | std::ios::binary); |
|---|
| 745 | if(!stream) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 746 | ReadResult rr = readImage(stream, options); |
|---|
| 747 | if(rr.validImage()) rr.getImage()->setFileName(file); |
|---|
| 748 | return rr; |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | virtual ReadResult readImage(std::istream& fin, const Options* options) const |
|---|
| 752 | { |
|---|
| 753 | osg::Image* osgImage = ReadVTFFile(fin); |
|---|
| 754 | if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 755 | |
|---|
| 756 | if (options && options->getOptionString().find("vtf_flip")!=std::string::npos) |
|---|
| 757 | { |
|---|
| 758 | osgImage->flipVertical(); |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | return osgImage; |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | virtual WriteResult writeObject(const osg::Object& object,const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 765 | { |
|---|
| 766 | const osg::Image* image = dynamic_cast<const osg::Image*>(&object); |
|---|
| 767 | if (!image) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 768 | |
|---|
| 769 | return writeImage(*image,file,options); |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | virtual WriteResult writeObject(const osg::Object& object,std::ostream& fout,const Options* options) const |
|---|
| 773 | { |
|---|
| 774 | const osg::Image* image = dynamic_cast<const osg::Image*>(&object); |
|---|
| 775 | if (!image) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 776 | |
|---|
| 777 | return writeImage(*image,fout,options); |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | virtual WriteResult writeImage(const osg::Image &image,const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 782 | { |
|---|
| 783 | std::string ext = osgDB::getFileExtension(file); |
|---|
| 784 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 785 | |
|---|
| 786 | osgDB::ofstream fout(file.c_str(), std::ios::out | std::ios::binary); |
|---|
| 787 | if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 788 | |
|---|
| 789 | return writeImage(image,fout,options); |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const Options*) const |
|---|
| 793 | { |
|---|
| 794 | bool success = WriteVTFFile(&image, fout); |
|---|
| 795 | |
|---|
| 796 | if(success) |
|---|
| 797 | return WriteResult::FILE_SAVED; |
|---|
| 798 | else |
|---|
| 799 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 800 | } |
|---|
| 801 | }; |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | REGISTER_OSGPLUGIN(vtf, ReaderWriterVTF) |
|---|