colourset.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  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. namespace squish {
  22. ColourSet::ColourSet( u8 const* rgba, int mask, int flags )
  23. : m_count( 0 ),
  24. m_transparent( false )
  25. {
  26. // check the compression mode for dxt1
  27. bool isDxt1 = ( ( flags & kDxt1 ) != 0 );
  28. bool weightByAlpha = ( ( flags & kWeightColourByAlpha ) != 0 );
  29. // create the minimal set
  30. for( int i = 0; i < 16; ++i )
  31. {
  32. // check this pixel is enabled
  33. int bit = 1 << i;
  34. if( ( mask & bit ) == 0 )
  35. {
  36. m_remap[i] = -1;
  37. continue;
  38. }
  39. // check for transparent pixels when using dxt1
  40. if( isDxt1 && rgba[4*i + 3] < 128 )
  41. {
  42. m_remap[i] = -1;
  43. m_transparent = true;
  44. continue;
  45. }
  46. // loop over previous points for a match
  47. for( int j = 0;; ++j )
  48. {
  49. // allocate a new point
  50. if( j == i )
  51. {
  52. // normalise coordinates to [0,1]
  53. float x = ( float )rgba[4*i] / 255.0f;
  54. float y = ( float )rgba[4*i + 1] / 255.0f;
  55. float z = ( float )rgba[4*i + 2] / 255.0f;
  56. // ensure there is always non-zero weight even for zero alpha
  57. float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
  58. // add the point
  59. m_points[m_count] = Vec3( x, y, z );
  60. m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
  61. m_remap[i] = m_count;
  62. // advance
  63. ++m_count;
  64. break;
  65. }
  66. // check for a match
  67. int oldbit = 1 << j;
  68. bool match = ( ( mask & oldbit ) != 0 )
  69. && ( rgba[4*i] == rgba[4*j] )
  70. && ( rgba[4*i + 1] == rgba[4*j + 1] )
  71. && ( rgba[4*i + 2] == rgba[4*j + 2] )
  72. && ( rgba[4*j + 3] >= 128 || !isDxt1 );
  73. if( match )
  74. {
  75. // get the index of the match
  76. int index = m_remap[j];
  77. // ensure there is always non-zero weight even for zero alpha
  78. float w = ( float )( rgba[4*i + 3] + 1 ) / 256.0f;
  79. // map to this point and increase the weight
  80. m_weights[index] += ( weightByAlpha ? w : 1.0f );
  81. m_remap[i] = index;
  82. break;
  83. }
  84. }
  85. }
  86. // square root the weights
  87. for( int i = 0; i < m_count; ++i )
  88. m_weights[i] = std::sqrt( m_weights[i] );
  89. }
  90. void ColourSet::RemapIndices( u8 const* source, u8* target ) const
  91. {
  92. for( int i = 0; i < 16; ++i )
  93. {
  94. int j = m_remap[i];
  95. if( j == -1 )
  96. target[i] = 3;
  97. else
  98. target[i] = source[j];
  99. }
  100. }
  101. } // namespace squish