Public Member Functions | |
| CDDSImage () | |
| ~CDDSImage () | |
| bool | load (std::string filename, bool flipImage=true) |
| void | clear () |
| operator char * () | |
| CTexture & | operator[] (int index) |
| int | get_num_images (void) |
| CTexture & | get_image (int index) |
| int | get_components () |
| int | get_format () |
| bool | is_compressed () |
| bool | is_cubemap () |
| bool | is_volume () |
| bool | is_valid () |
Private Member Functions | |
| int | clamp_size (int size) |
| int | get_line_width (int width, int bpp) |
| int | size_dxtc (int width, int height) |
| int | size_rgb (int width, int height) |
| void | swap_endian (void *val) |
| void | align_memory (CTexture *surface) |
| void | flip (char *image, int width, int height, int depth, int size) |
| void | swap (void *byte1, void *byte2, int size) |
| void | flip_blocks_dxtc1 (DXTColBlock *line, int numBlocks) |
| void | flip_blocks_dxtc3 (DXTColBlock *line, int numBlocks) |
| void | flip_blocks_dxtc5 (DXTColBlock *line, int numBlocks) |
| void | flip_dxt5_alpha (DXT5AlphaBlock *block) |
Private Attributes | |
| int | format |
| int | components |
| bool | compressed |
| bool | cubemap |
| bool | volume |
| bool | valid |
| std::vector< CTexture > | images |
Definition at line 221 of file OSGDDSImageFileType.cpp.
|
|
Definition at line 465 of file OSGDDSImageFileType.cpp. 00466 : format(0), 00467 components(0), 00468 compressed(false), 00469 cubemap(false), 00470 volume(false), 00471 valid(false) 00472 { 00473 }
|
|
|
Definition at line 475 of file OSGDDSImageFileType.cpp.
|
|
||||||||||||
|
Definition at line 484 of file OSGDDSImageFileType.cpp. References align_memory(), clamp_size(), clear(), components, compressed, cubemap, osg::endLog(), flip(), format, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, images, CSurface::size, size_dxtc(), size_rgb(), swap_endian(), SWARNING, valid, volume, and CSurface::width. Referenced by osg::DDSImageFileType::read(). 00485 { 00486 DDS_HEADER ddsh; 00487 char filecode[4]; 00488 FILE *fp; 00489 int width, height, depth; 00490 int (CDDSImage::*sizefunc)(int, int); 00491 00492 // clear any previously loaded images 00493 clear(); 00494 00495 // open file 00496 fp = fopen(filename.data(), "rb"); 00497 if (fp == NULL) 00498 return false; 00499 00500 // read in file marker, make sure its a DDS file 00501 fread(filecode, 1, 4, fp); 00502 if (strncmp(filecode, "DDS ", 4) != 0) 00503 { 00504 fclose(fp); 00505 return false; 00506 } 00507 00508 // read in DDS header 00509 fread(&ddsh, sizeof(ddsh), 1, fp); 00510 00511 swap_endian(&ddsh.dwSize); 00512 swap_endian(&ddsh.dwFlags); 00513 swap_endian(&ddsh.dwHeight); 00514 swap_endian(&ddsh.dwWidth); 00515 swap_endian(&ddsh.dwPitchOrLinearSize); 00516 swap_endian(&ddsh.dwMipMapCount); 00517 swap_endian(&ddsh.ddspf.dwSize); 00518 swap_endian(&ddsh.ddspf.dwFlags); 00519 swap_endian(&ddsh.ddspf.dwFourCC); 00520 swap_endian(&ddsh.ddspf.dwRGBBitCount); 00521 swap_endian(&ddsh.dwCaps1); 00522 swap_endian(&ddsh.dwCaps2); 00523 00524 // check if image is a cubempa 00525 if (ddsh.dwCaps2 & DDS_CUBEMAP) 00526 cubemap = true; 00527 00528 // check if image is a volume texture 00529 if ((ddsh.dwCaps2 & DDS_VOLUME) && (ddsh.dwDepth > 0)) 00530 volume = true; 00531 00532 // figure out what the image format is 00533 if (ddsh.ddspf.dwFlags & DDS_FOURCC) 00534 { 00535 switch(ddsh.ddspf.dwFourCC) 00536 { 00537 case FOURCC_DXT1: 00538 format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; 00539 components = 3; 00540 compressed = true; 00541 break; 00542 case FOURCC_DXT3: 00543 format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; 00544 components = 4; 00545 compressed = true; 00546 break; 00547 case FOURCC_DXT5: 00548 format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; 00549 components = 4; 00550 compressed = true; 00551 break; 00552 default: 00553 fclose(fp); 00554 return false; 00555 } 00556 } 00557 else if (ddsh.ddspf.dwFlags == DDS_RGBA && ddsh.ddspf.dwRGBBitCount == 32) 00558 { 00559 format = Image::OSG_BGRA_PF; 00560 compressed = false; 00561 components = 4; 00562 } 00563 else if (ddsh.ddspf.dwFlags == DDS_RGB && ddsh.ddspf.dwRGBBitCount == 32) 00564 { 00565 format = Image::OSG_BGRA_PF; 00566 compressed = false; 00567 components = 4; 00568 } 00569 else if (ddsh.ddspf.dwFlags == DDS_RGB && ddsh.ddspf.dwRGBBitCount == 24) 00570 { 00571 format = Image::OSG_BGR_PF; 00572 compressed = false; 00573 components = 3; 00574 } 00575 else if (/*ddsh.ddspf.dwFlags == 0x20000 &&*/ ddsh.ddspf.dwRGBBitCount == 8) 00576 { 00577 format = Image::OSG_L_PF; 00578 compressed = false; 00579 components = 1; 00580 } 00581 else 00582 { 00583 SWARNING << "ERROR: unknown image format!" << endLog; 00584 fclose(fp); 00585 return false; 00586 } 00587 00588 // store primary surface width/height/depth 00589 width = ddsh.dwWidth; 00590 height = ddsh.dwHeight; 00591 depth = clamp_size(ddsh.dwDepth); // set to 1 if 0 00592 00593 // use correct size calculation function depending on whether image is 00594 // compressed 00595 sizefunc = (compressed ? &CDDSImage::size_dxtc : &CDDSImage::size_rgb); 00596 00597 // load all surfaces for the image (6 surfaces for cubemaps) 00598 for (int n = 0; n < (cubemap ? 6 : 1); n++) 00599 { 00600 int size; 00601 00602 // calculate surface size 00603 size = (this->*sizefunc)(width, height)*depth; 00604 00605 // load surface 00606 CTexture img(width, height, depth, size); 00607 fread(img, 1, img.size, fp); 00608 00609 align_memory(&img); 00610 00611 if (flipImage && !cubemap) 00612 flip(img, img.width, img.height, img.depth, img.size); 00613 00614 int w = clamp_size(width >> 1); 00615 int h = clamp_size(height >> 1); 00616 int d = clamp_size(depth >> 1); 00617 00618 // store number of mipmaps 00619 int numMipmaps = ddsh.dwMipMapCount; 00620 00621 // number of mipmaps in file includes main surface so decrease count 00622 // by one 00623 if (numMipmaps != 0) 00624 numMipmaps--; 00625 00626 // load all mipmaps for current surface 00627 for (int i = 0; i < numMipmaps && (w || h); i++) 00628 { 00629 // calculate mipmap size 00630 size = (this->*sizefunc)(w, h)*d; 00631 00632 CSurface mipmap(w, h, d, size); 00633 fread(mipmap, 1, mipmap.size, fp); 00634 00635 if (flipImage && !cubemap) 00636 { 00637 flip(mipmap, mipmap.width, mipmap.height, mipmap.depth, 00638 mipmap.size); 00639 } 00640 00641 img.mipmaps.push_back(mipmap); 00642 00643 // shrink to next power of 2 00644 w = clamp_size(w >> 1); 00645 h = clamp_size(h >> 1); 00646 d = clamp_size(d >> 1); 00647 } 00648 00649 images.push_back(img); 00650 } 00651 00652 #if 0 00653 // swap cubemaps on y axis (since image is flipped in OGL) 00654 if (cubemap && flipImage) 00655 { 00656 CTexture tmp; 00657 tmp = images[3]; 00658 images[3] = images[2]; 00659 images[2] = tmp; 00660 } 00661 #endif 00662 00663 fclose(fp); 00664 00665 valid = true; 00666 00667 return true; 00668 }
|
|
|
Definition at line 672 of file OSGDDSImageFileType.cpp. References components, compressed, cubemap, format, images, valid, and volume. Referenced by load(). 00673 { 00674 components = 0; 00675 format = 0; 00676 compressed = false; 00677 cubemap = false; 00678 volume = false; 00679 valid = false; 00680 00681 images.clear(); 00682 }
|
|
|
Definition at line 698 of file OSGDDSImageFileType.cpp.
|
|
|
Definition at line 687 of file OSGDDSImageFileType.cpp. 00688 { 00689 // make sure an image has been loaded 00690 assert(valid); 00691 assert(index < (int)images.size()); 00692 00693 return images[index]; 00694 }
|
|
|
Definition at line 233 of file OSGDDSImageFileType.cpp. References images. Referenced by osg::DDSImageFileType::read(). 00233 { return (int)images.size(); }
|
|
|
Definition at line 234 of file OSGDDSImageFileType.cpp. References images.
|
|
|
Definition at line 240 of file OSGDDSImageFileType.cpp. References components. Referenced by osg::DDSImageFileType::read(). 00240 { return components; }
|
|
|
Definition at line 241 of file OSGDDSImageFileType.cpp. References format. Referenced by osg::DDSImageFileType::read(). 00241 { return format; }
|
|
|
Definition at line 243 of file OSGDDSImageFileType.cpp. References compressed. Referenced by osg::DDSImageFileType::read(). 00243 { return compressed; }
|
|
|
Definition at line 244 of file OSGDDSImageFileType.cpp. References cubemap. Referenced by osg::DDSImageFileType::read(). 00244 { return cubemap; }
|
|
|
Definition at line 245 of file OSGDDSImageFileType.cpp. References volume. Referenced by osg::DDSImageFileType::read(). 00245 { return volume; }
|
|
|
Definition at line 246 of file OSGDDSImageFileType.cpp. References valid. Referenced by osg::DDSImageFileType::read(). 00246 { return valid; }
|
|
|
Definition at line 708 of file OSGDDSImageFileType.cpp. Referenced by load().
|
|
||||||||||||
|
Definition at line 722 of file OSGDDSImageFileType.cpp. Referenced by align_memory().
|
|
||||||||||||
|
Definition at line 729 of file OSGDDSImageFileType.cpp. References format, and GL_COMPRESSED_RGBA_S3TC_DXT1_EXT. Referenced by load(). 00730 { 00731 return ((width+3)/4)*((height+3)/4)* 00732 (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ? 8 : 16); 00733 }
|
|
||||||||||||
|
Definition at line 737 of file OSGDDSImageFileType.cpp. References components. Referenced by load(). 00738 { 00739 return width*height*components; 00740 }
|
|
|
Definition at line 744 of file OSGDDSImageFileType.cpp. Referenced by load(). 00745 { 00746 #ifdef MACOS 00747 unsigned int *ival = (unsigned int *)val; 00748 00749 *ival = ((*ival >> 24) & 0x000000ff) | 00750 ((*ival >> 8) & 0x0000ff00) | 00751 ((*ival << 8) & 0x00ff0000) | 00752 ((*ival << 24) & 0xff000000); 00753 #endif 00754 }
|
|
|
Definition at line 758 of file OSGDDSImageFileType.cpp. References components, compressed, cubemap, CSurface::depth, get_line_width(), CSurface::height, CSurface::size, volume, and CSurface::width. Referenced by load(). 00759 { 00760 // don't bother with compressed images, volume textures, or cubemaps 00761 if (compressed || volume || cubemap) 00762 return; 00763 00764 // calculate new image size 00765 int linesize = get_line_width(surface->width, components*8); 00766 int imagesize = linesize*surface->height; 00767 00768 // exit if already aligned 00769 if (surface->size == imagesize) 00770 return; 00771 00772 // create new image of new size 00773 CTexture newSurface(surface->width, surface->height, surface->depth, 00774 imagesize); 00775 00776 // add pad bytes to end of each line 00777 char *srcimage = (char*)*surface; 00778 char *dstimage = (char*)newSurface; 00779 for (int n = 0; n < surface->depth; n++) 00780 { 00781 char *curline = srcimage; 00782 char *newline = dstimage; 00783 00784 int imsize = surface->size / surface->depth; 00785 int lnsize = imsize / surface->height; 00786 00787 for (int i = 0; i < surface->height; i++) 00788 { 00789 memcpy(newline, curline, lnsize); 00790 newline += linesize; 00791 curline += lnsize; 00792 } 00793 } 00794 00795 // save padded image 00796 *surface = newSurface; 00797 }
|
|
||||||||||||||||||||||||
|
Definition at line 801 of file OSGDDSImageFileType.cpp. References compressed, flip_blocks_dxtc1(), flip_blocks_dxtc3(), flip_blocks_dxtc5(), format, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, and swap(). Referenced by load(). 00802 { 00803 int linesize; 00804 int offset; 00805 00806 if (!compressed) 00807 { 00808 assert(depth > 0); 00809 00810 int imagesize = size/depth; 00811 linesize = imagesize / height; 00812 00813 for (int n = 0; n < depth; n++) 00814 { 00815 offset = imagesize*n; 00816 char *top = image + offset; 00817 char *bottom = top + (imagesize-linesize); 00818 00819 for (int i = 0; i < (height >> 1); i++) 00820 { 00821 swap(bottom, top, linesize); 00822 00823 top += linesize; 00824 bottom -= linesize; 00825 } 00826 } 00827 } 00828 else 00829 { 00830 void (CDDSImage::*flipblocks)(DXTColBlock*, int); 00831 int xblocks = width / 4; 00832 int yblocks = height / 4; 00833 int blocksize; 00834 00835 switch (format) 00836 { 00837 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 00838 blocksize = 8; 00839 flipblocks = &CDDSImage::flip_blocks_dxtc1; 00840 break; 00841 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: 00842 blocksize = 16; 00843 flipblocks = &CDDSImage::flip_blocks_dxtc3; 00844 break; 00845 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: 00846 blocksize = 16; 00847 flipblocks = &CDDSImage::flip_blocks_dxtc5; 00848 break; 00849 default: 00850 return; 00851 } 00852 00853 linesize = xblocks * blocksize; 00854 00855 DXTColBlock *top; 00856 DXTColBlock *bottom; 00857 00858 for (int j = 0; j < (yblocks >> 1); j++) 00859 { 00860 top = (DXTColBlock*)(image + j * linesize); 00861 bottom = (DXTColBlock*)(image + (((yblocks-j)-1) * linesize)); 00862 00863 (this->*flipblocks)(top, xblocks); 00864 (this->*flipblocks)(bottom, xblocks); 00865 00866 swap(bottom, top, linesize); 00867 } 00868 } 00869 }
|
|
||||||||||||||||
|
Definition at line 873 of file OSGDDSImageFileType.cpp. Referenced by flip(), flip_blocks_dxtc1(), flip_blocks_dxtc3(), and flip_blocks_dxtc5(). 00874 { 00875 unsigned char *tmp = new unsigned char[size]; 00876 00877 memcpy(tmp, byte1, size); 00878 memcpy(byte1, byte2, size); 00879 memcpy(byte2, tmp, size); 00880 00881 delete [] tmp; 00882 }
|
|
||||||||||||
|
Definition at line 886 of file OSGDDSImageFileType.cpp. References DXTColBlock::row, and swap(). Referenced by flip(). 00887 { 00888 DXTColBlock *curblock = line; 00889 00890 for (int i = 0; i < numBlocks; i++) 00891 { 00892 swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char)); 00893 swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char)); 00894 00895 curblock++; 00896 } 00897 }
|
|
||||||||||||
|
Definition at line 901 of file OSGDDSImageFileType.cpp. References DXTColBlock::row, DXT3AlphaBlock::row, and swap(). Referenced by flip(). 00902 { 00903 DXTColBlock *curblock = line; 00904 DXT3AlphaBlock *alphablock; 00905 00906 for (int i = 0; i < numBlocks; i++) 00907 { 00908 alphablock = (DXT3AlphaBlock*)curblock; 00909 00910 swap(&alphablock->row[0], &alphablock->row[3], sizeof(unsigned short)); 00911 swap(&alphablock->row[1], &alphablock->row[2], sizeof(unsigned short)); 00912 00913 curblock++; 00914 00915 swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char)); 00916 swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char)); 00917 00918 curblock++; 00919 } 00920 }
|
|
||||||||||||
|
Definition at line 1000 of file OSGDDSImageFileType.cpp. References flip_dxt5_alpha(), DXTColBlock::row, and swap(). Referenced by flip(). 01001 { 01002 DXTColBlock *curblock = line; 01003 DXT5AlphaBlock *alphablock; 01004 01005 for (int i = 0; i < numBlocks; i++) 01006 { 01007 alphablock = (DXT5AlphaBlock*)curblock; 01008 01009 flip_dxt5_alpha(alphablock); 01010 01011 curblock++; 01012 01013 swap(&curblock->row[0], &curblock->row[3], sizeof(unsigned char)); 01014 swap(&curblock->row[1], &curblock->row[2], sizeof(unsigned char)); 01015 01016 curblock++; 01017 } 01018 }
|
|
|
Definition at line 924 of file OSGDDSImageFileType.cpp. References DXT5AlphaBlock::row. Referenced by flip_blocks_dxtc5(). 00925 { 00926 unsigned char gBits[4][4]; 00927 00928 const unsigned long mask = 0x00000007; // bits = 00 00 01 11 00929 unsigned long bits = 0; 00930 memcpy(&bits, &block->row[0], sizeof(unsigned char) * 3); 00931 00932 gBits[0][0] = (unsigned char)(bits & mask); 00933 bits >>= 3; 00934 gBits[0][1] = (unsigned char)(bits & mask); 00935 bits >>= 3; 00936 gBits[0][2] = (unsigned char)(bits & mask); 00937 bits >>= 3; 00938 gBits[0][3] = (unsigned char)(bits & mask); 00939 bits >>= 3; 00940 gBits[1][0] = (unsigned char)(bits & mask); 00941 bits >>= 3; 00942 gBits[1][1] = (unsigned char)(bits & mask); 00943 bits >>= 3; 00944 gBits[1][2] = (unsigned char)(bits & mask); 00945 bits >>= 3; 00946 gBits[1][3] = (unsigned char)(bits & mask); 00947 00948 bits = 0; 00949 memcpy(&bits, &block->row[3], sizeof(unsigned char) * 3); 00950 00951 gBits[2][0] = (unsigned char)(bits & mask); 00952 bits >>= 3; 00953 gBits[2][1] = (unsigned char)(bits & mask); 00954 bits >>= 3; 00955 gBits[2][2] = (unsigned char)(bits & mask); 00956 bits >>= 3; 00957 gBits[2][3] = (unsigned char)(bits & mask); 00958 bits >>= 3; 00959 gBits[3][0] = (unsigned char)(bits & mask); 00960 bits >>= 3; 00961 gBits[3][1] = (unsigned char)(bits & mask); 00962 bits >>= 3; 00963 gBits[3][2] = (unsigned char)(bits & mask); 00964 bits >>= 3; 00965 gBits[3][3] = (unsigned char)(bits & mask); 00966 00967 unsigned long *pBits = ((unsigned long*) &(block->row[0])); 00968 00969 *pBits = *pBits | (gBits[3][0] << 0); 00970 *pBits = *pBits | (gBits[3][1] << 3); 00971 *pBits = *pBits | (gBits[3][2] << 6); 00972 *pBits = *pBits | (gBits[3][3] << 9); 00973 00974 *pBits = *pBits | (gBits[2][0] << 12); 00975 *pBits = *pBits | (gBits[2][1] << 15); 00976 *pBits = *pBits | (gBits[2][2] << 18); 00977 *pBits = *pBits | (gBits[2][3] << 21); 00978 00979 pBits = ((unsigned long*) &(block->row[3])); 00980 00981 #ifdef MACOS 00982 *pBits &= 0x000000ff; 00983 #else 00984 *pBits &= 0xff000000; 00985 #endif 00986 00987 *pBits = *pBits | (gBits[1][0] << 0); 00988 *pBits = *pBits | (gBits[1][1] << 3); 00989 *pBits = *pBits | (gBits[1][2] << 6); 00990 *pBits = *pBits | (gBits[1][3] << 9); 00991 00992 *pBits = *pBits | (gBits[0][0] << 12); 00993 *pBits = *pBits | (gBits[0][1] << 15); 00994 *pBits = *pBits | (gBits[0][2] << 18); 00995 *pBits = *pBits | (gBits[0][3] << 21); 00996 }
|
|
|
Definition at line 264 of file OSGDDSImageFileType.cpp. Referenced by clear(), flip(), get_format(), load(), and size_dxtc(). |
|
|
Definition at line 265 of file OSGDDSImageFileType.cpp. Referenced by align_memory(), clear(), get_components(), load(), and size_rgb(). |
|
|
Definition at line 266 of file OSGDDSImageFileType.cpp. Referenced by align_memory(), clear(), flip(), is_compressed(), and load(). |
|
|
Definition at line 267 of file OSGDDSImageFileType.cpp. Referenced by align_memory(), clear(), is_cubemap(), and load(). |
|
|
Definition at line 268 of file OSGDDSImageFileType.cpp. Referenced by align_memory(), clear(), is_volume(), and load(). |
|
|
Definition at line 269 of file OSGDDSImageFileType.cpp. Referenced by clear(), is_valid(), load(), operator char *(), and operator[](). |
|
|
Definition at line 271 of file OSGDDSImageFileType.cpp. Referenced by clear(), get_image(), get_num_images(), load(), operator char *(), and operator[](). |
1.4.3