rangefit.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "rangefit.h"
  21. #include "colourset.h"
  22. #include "colourblock.h"
  23. #include <cfloat>
  24. namespace squish {
  25. RangeFit::RangeFit( ColourSet const* colours, int flags, float* metric )
  26. : ColourFit( colours, flags )
  27. {
  28. // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
  29. if( metric )
  30. m_metric = Vec3( metric[0], metric[1], metric[2] );
  31. else
  32. m_metric = Vec3( 1.0f );
  33. // initialise the best error
  34. m_besterror = FLT_MAX;
  35. // cache some values
  36. int const count = m_colours->GetCount();
  37. Vec3 const* values = m_colours->GetPoints();
  38. float const* weights = m_colours->GetWeights();
  39. // get the covariance matrix
  40. Sym3x3 covariance = ComputeWeightedCovariance( count, values, weights );
  41. // compute the principle component
  42. Vec3 principle = ComputePrincipleComponent( covariance );
  43. // get the min and max range as the codebook endpoints
  44. Vec3 start( 0.0f );
  45. Vec3 end( 0.0f );
  46. if( count > 0 )
  47. {
  48. float min, max;
  49. // compute the range
  50. start = end = values[0];
  51. min = max = Dot( values[0], principle );
  52. for( int i = 1; i < count; ++i )
  53. {
  54. float val = Dot( values[i], principle );
  55. if( val < min )
  56. {
  57. start = values[i];
  58. min = val;
  59. }
  60. else if( val > max )
  61. {
  62. end = values[i];
  63. max = val;
  64. }
  65. }
  66. }
  67. // clamp the output to [0, 1]
  68. Vec3 const one( 1.0f );
  69. Vec3 const zero( 0.0f );
  70. start = Min( one, Max( zero, start ) );
  71. end = Min( one, Max( zero, end ) );
  72. // clamp to the grid and save
  73. Vec3 const grid( 31.0f, 63.0f, 31.0f );
  74. Vec3 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f );
  75. Vec3 const half( 0.5f );
  76. m_start = Truncate( grid*start + half )*gridrcp;
  77. m_end = Truncate( grid*end + half )*gridrcp;
  78. }
  79. void RangeFit::Compress3( void* block )
  80. {
  81. // cache some values
  82. int const count = m_colours->GetCount();
  83. Vec3 const* values = m_colours->GetPoints();
  84. // create a codebook
  85. Vec3 codes[3];
  86. codes[0] = m_start;
  87. codes[1] = m_end;
  88. codes[2] = 0.5f*m_start + 0.5f*m_end;
  89. // match each point to the closest code
  90. u8 closest[16];
  91. float error = 0.0f;
  92. for( int i = 0; i < count; ++i )
  93. {
  94. // find the closest code
  95. float dist = FLT_MAX;
  96. int idx = 0;
  97. for( int j = 0; j < 3; ++j )
  98. {
  99. float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
  100. if( d < dist )
  101. {
  102. dist = d;
  103. idx = j;
  104. }
  105. }
  106. // save the index
  107. closest[i] = ( u8 )idx;
  108. // accumulate the error
  109. error += dist;
  110. }
  111. // save this scheme if it wins
  112. if( error < m_besterror )
  113. {
  114. // remap the indices
  115. u8 indices[16];
  116. m_colours->RemapIndices( closest, indices );
  117. // save the block
  118. WriteColourBlock3( m_start, m_end, indices, block );
  119. // save the error
  120. m_besterror = error;
  121. }
  122. }
  123. void RangeFit::Compress4( void* block )
  124. {
  125. // cache some values
  126. int const count = m_colours->GetCount();
  127. Vec3 const* values = m_colours->GetPoints();
  128. // create a codebook
  129. Vec3 codes[4];
  130. codes[0] = m_start;
  131. codes[1] = m_end;
  132. codes[2] = ( 2.0f/3.0f )*m_start + ( 1.0f/3.0f )*m_end;
  133. codes[3] = ( 1.0f/3.0f )*m_start + ( 2.0f/3.0f )*m_end;
  134. // match each point to the closest code
  135. u8 closest[16];
  136. float error = 0.0f;
  137. for( int i = 0; i < count; ++i )
  138. {
  139. // find the closest code
  140. float dist = FLT_MAX;
  141. int idx = 0;
  142. for( int j = 0; j < 4; ++j )
  143. {
  144. float d = LengthSquared( m_metric*( values[i] - codes[j] ) );
  145. if( d < dist )
  146. {
  147. dist = d;
  148. idx = j;
  149. }
  150. }
  151. // save the index
  152. closest[i] = ( u8 )idx;
  153. // accumulate the error
  154. error += dist;
  155. }
  156. // save this scheme if it wins
  157. if( error < m_besterror )
  158. {
  159. // remap the indices
  160. u8 indices[16];
  161. m_colours->RemapIndices( closest, indices );
  162. // save the block
  163. WriteColourBlock4( m_start, m_end, indices, block );
  164. // save the error
  165. m_besterror = error;
  166. }
  167. }
  168. } // namespace squish