colourset.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. bool toLinear = ((flags & kToLinear) != 0);
  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 = toLinear ?
  55. SRGBToLinear(( float )rgba[4*i] / 255.0f) :
  56. ( float )rgba[4*i] / 255.0f;
  57. float y = toLinear ?
  58. SRGBToLinear(( float )rgba[4*i + 1] / 255.0f) :
  59. ( float )rgba[4*i + 1] / 255.0f;
  60. float z = toLinear ?
  61. SRGBToLinear(( float )rgba[4*i + 2] / 255.0f) :
  62. ( float )rgba[4*i + 2] / 255.0f;
  63. // ensure there is always non-zero weight even for zero alpha
  64. float w = toLinear != 0 ?
  65. SRGBToLinear(( float )rgba[4*i + 3] / 256.0f) :
  66. ( float )rgba[4*i + 3] / 256.0f;
  67. // add the point
  68. m_points[m_count] = Vec3( x, y, z );
  69. m_weights[m_count] = ( weightByAlpha ? w : 1.0f );
  70. m_remap[i] = m_count;
  71. // advance
  72. ++m_count;
  73. break;
  74. }
  75. // check for a match
  76. int oldbit = 1 << j;
  77. bool match = ( ( mask & oldbit ) != 0 )
  78. && ( rgba[4*i] == rgba[4*j] )
  79. && ( rgba[4*i + 1] == rgba[4*j + 1] )
  80. && ( rgba[4*i + 2] == rgba[4*j + 2] )
  81. && ( rgba[4*j + 3] >= 128 || !isDxt1 );
  82. if( match )
  83. {
  84. // get the index of the match
  85. int index = m_remap[j];
  86. // ensure there is always non-zero weight even for zero alpha
  87. float w = toLinear != 0 ?
  88. SRGBToLinear(( float )( rgba[4*i + 3] + 1 ) / 256.0f) :
  89. ( float )( rgba[4*i + 3] + 1 )/ 256.0f;
  90. // map to this point and increase the weight
  91. m_weights[index] += ( weightByAlpha ? w : 1.0f );
  92. m_remap[i] = index;
  93. break;
  94. }
  95. }
  96. }
  97. // square root the weights
  98. for( int i = 0; i < m_count; ++i )
  99. m_weights[i] = std::sqrt( m_weights[i] );
  100. }
  101. void ColourSet::RemapIndices( u8 const* source, u8* target ) const
  102. {
  103. for( int i = 0; i < 16; ++i )
  104. {
  105. int j = m_remap[i];
  106. if( j == -1 )
  107. target[i] = 3;
  108. else
  109. target[i] = source[j];
  110. }
  111. }
  112. } // namespace squish