colourset.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown [email protected]
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. #include "colourset.h"
  21. #include "squishMath.h"
  22. namespace squish {
  23. ColourSet::ColourSet( u8 const* rgba, int mask, int flags )
  24. : m_count( 0 ),
  25. m_transparent( false )
  26. {
  27. // check the compression mode for dxt1
  28. bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
  29. bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );
  30. // create the minimal set
  31. for( int i = 0; i < 16; ++i )
  32. {
  33. // check this pixel is enabled
  34. int bit = 1 << i;
  35. if( ( mask & bit ) == 0 )
  36. {
  37. m_remap[i] = -1;
  38. continue;
  39. }
  40. // check for transparent pixels when using dxt1
  41. if( isDxt1 && rgba[4*i + 3] < 128 )
  42. {
  43. m_remap[i] = -1;
  44. m_transparent = true;
  45. continue;
  46. }
  47. // loop over previous points for a match
  48. for( int j = 0;; ++j )
  49. {
  50. // allocate a new point
  51. if( j == i )
  52. {
  53. // normalise coordinates to [0,1]
  54. float x = ( float )rgba[4*i] / 255.0f;
  55. float y = ( float )rgba[4*i + 1] / 255.0f;
  56. float z = ( float )rgba[4*i + 2] / 255.0f;
  57. // ensure there is always non-zero weight even for zero alpha
  58. float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
  59. // add the point
  60. m_points[m_count] = Vec3( x, y, z );
  61. m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
  62. m_remap[i] = m_count;
  63. // advance
  64. ++m_count;
  65. break;
  66. }
  67. // check for a match
  68. int oldbit = 1 << j;
  69. bool match = ( ( mask & oldbit ) != 0 )
  70. && ( rgba[4*i] == rgba[4*j] )
  71. && ( rgba[4*i + 1] == rgba[4*j + 1] )
  72. && ( rgba[4*i + 2] == rgba[4*j + 2] )
  73. && ( rgba[4*j + 3] >= 128 || !isDxt1 );
  74. if( match )
  75. {
  76. // get the index of the match
  77. int index = m_remap[j];
  78. // ensure there is always non-zero weight even for zero alpha
  79. float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
  80. // map to this point and increase the weight
  81. m_weights[index] += ( weightByAlpha ? w : 1.0f );
  82. m_remap[i] = index;
  83. break;
  84. }
  85. }
  86. }
  87. // square root the weights
  88. for( int i = 0; i < m_count; ++i )
  89. m_weights[i] = SquishMath::sqrt( m_weights[i] );
  90. }
  91. void ColourSet::RemapIndices( u8 const* source, u8* target ) const
  92. {
  93. for( int i = 0; i < 16; ++i )
  94. {
  95. int j = m_remap[i];
  96. if( j == -1 )
  97. target[i] = 3;
  98. else
  99. target[i] = source[j];
  100. }
  101. }
  102. } // namespace squish