ddsUtils.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "squish/squish.h"
  23. #include "gfx/bitmap/ddsFile.h"
  24. #include "gfx/bitmap/ddsUtils.h"
  25. //------------------------------------------------------------------------------
  26. // If false is returned, from this method, the source DDS is not modified
  27. bool DDSUtil::squishDDS( DDSFile *srcDDS, const GFXFormat dxtFormat )
  28. {
  29. // Sanity check
  30. if( srcDDS->mBytesPerPixel != 4 )
  31. {
  32. AssertFatal( false, "Squish wants 32-bit source data" );
  33. return false;
  34. }
  35. // Build flags, start with fast compress
  36. U32 squishFlags = squish::kColourRangeFit;
  37. // Flag which format we are using
  38. switch( dxtFormat )
  39. {
  40. case GFXFormatDXT1:
  41. squishFlags |= squish::kDxt1;
  42. break;
  43. case GFXFormatDXT2:
  44. case GFXFormatDXT3:
  45. squishFlags |= squish::kDxt3;
  46. break;
  47. case GFXFormatDXT4:
  48. case GFXFormatDXT5:
  49. squishFlags |= squish::kDxt5;
  50. break;
  51. default:
  52. AssertFatal( false, "Assumption failed" );
  53. return false;
  54. break;
  55. }
  56. // We got this far, so assume we can finish (gosh I hope so)
  57. srcDDS->mFormat = dxtFormat;
  58. srcDDS->mFlags.set( DDSFile::CompressedData );
  59. // If this has alpha, set the flag
  60. if( srcDDS->mFormat == GFXFormatR8G8B8A8 )
  61. squishFlags |= squish::kWeightColourByAlpha;
  62. // The source surface is the original surface of the file
  63. DDSFile::SurfaceData *srcSurface = srcDDS->mSurfaces.last();
  64. // Create a new surface, this will be the DXT compressed surface. Once we
  65. // are done, we can discard the old surface, and replace it with this one.
  66. DDSFile::SurfaceData *newSurface = new DDSFile::SurfaceData();
  67. for( S32 i = 0; i < srcDDS->mMipMapCount; i++ )
  68. {
  69. const U8 *srcBits = srcSurface->mMips[i];
  70. const U32 mipSz = srcDDS->getSurfaceSize(i);
  71. U8 *dstBits = new U8[mipSz];
  72. newSurface->mMips.push_back( dstBits );
  73. PROFILE_START(SQUISH_DXT_COMPRESS);
  74. // Compress with Squish
  75. squish::CompressImage( srcBits, srcDDS->getWidth(i), srcDDS->getHeight(i),
  76. dstBits, squishFlags );
  77. PROFILE_END();
  78. }
  79. // Now delete the source surface, and return.
  80. srcDDS->mSurfaces.pop_back();
  81. delete srcSurface;
  82. srcDDS->mSurfaces.push_back( newSurface );
  83. return true;
  84. }
  85. //------------------------------------------------------------------------------
  86. void DDSUtil::swizzleDDS( DDSFile *srcDDS, const Swizzle<U8, 4> &swizzle )
  87. {
  88. for( S32 i = 0; i < srcDDS->mMipMapCount; i++ )
  89. {
  90. swizzle.InPlace( srcDDS->mSurfaces.last()->mMips[i], srcDDS->getSurfaceSize( i ) );
  91. }
  92. }