singlecolourfit.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "singlecolourfit.h"
  21. #include "colourset.h"
  22. #include "colourblock.h"
  23. #include <limits.h>
  24. namespace squish {
  25. struct SourceBlock
  26. {
  27. u8 start;
  28. u8 end;
  29. u8 error;
  30. };
  31. struct SingleColourLookup
  32. {
  33. SourceBlock sources[2];
  34. };
  35. #include "singlecolourlookup.inl"
  36. static int FloatToInt( float a, int limit )
  37. {
  38. // use ANSI round-to-zero behaviour to get round-to-nearest
  39. int i = ( int )( a + 0.5f );
  40. // clamp to the limit
  41. if( i < 0 )
  42. i = 0;
  43. else if( i > limit )
  44. i = limit;
  45. // done
  46. return i;
  47. }
  48. SingleColourFit::SingleColourFit( ColourSet const* colours, int flags )
  49. : ColourFit( colours, flags )
  50. {
  51. // grab the single colour
  52. Vec3 const* values = m_colours->GetPoints();
  53. m_colour[0] = ( u8 )FloatToInt( 255.0f*values->X(), 255 );
  54. m_colour[1] = ( u8 )FloatToInt( 255.0f*values->Y(), 255 );
  55. m_colour[2] = ( u8 )FloatToInt( 255.0f*values->Z(), 255 );
  56. // initialise the best error
  57. m_besterror = INT_MAX;
  58. }
  59. void SingleColourFit::Compress3( void* block )
  60. {
  61. // build the table of lookups
  62. SingleColourLookup const* const lookups[] =
  63. {
  64. lookup_5_3,
  65. lookup_6_3,
  66. lookup_5_3
  67. };
  68. // find the best end-points and index
  69. ComputeEndPoints( lookups );
  70. // build the block if we win
  71. if( m_error < m_besterror )
  72. {
  73. // remap the indices
  74. u8 indices[16];
  75. m_colours->RemapIndices( &m_index, indices );
  76. // save the block
  77. WriteColourBlock3( m_start, m_end, indices, block );
  78. // save the error
  79. m_besterror = m_error;
  80. }
  81. }
  82. void SingleColourFit::Compress4( void* block )
  83. {
  84. // build the table of lookups
  85. SingleColourLookup const* const lookups[] =
  86. {
  87. lookup_5_4,
  88. lookup_6_4,
  89. lookup_5_4
  90. };
  91. // find the best end-points and index
  92. ComputeEndPoints( lookups );
  93. // build the block if we win
  94. if( m_error < m_besterror )
  95. {
  96. // remap the indices
  97. u8 indices[16];
  98. m_colours->RemapIndices( &m_index, indices );
  99. // save the block
  100. WriteColourBlock4( m_start, m_end, indices, block );
  101. // save the error
  102. m_besterror = m_error;
  103. }
  104. }
  105. void SingleColourFit::ComputeEndPoints( SingleColourLookup const* const* lookups )
  106. {
  107. // check each index combination (endpoint or intermediate)
  108. m_error = INT_MAX;
  109. for( int index = 0; index < 2; ++index )
  110. {
  111. // check the error for this codebook index
  112. SourceBlock const* sources[3];
  113. int error = 0;
  114. for( int channel = 0; channel < 3; ++channel )
  115. {
  116. // grab the lookup table and index for this channel
  117. SingleColourLookup const* lookup = lookups[channel];
  118. int target = m_colour[channel];
  119. // store a pointer to the source for this channel
  120. sources[channel] = lookup[target].sources + index;
  121. // accumulate the error
  122. int diff = sources[channel]->error;
  123. error += diff*diff;
  124. }
  125. // keep it if the error is lower
  126. if( error < m_error )
  127. {
  128. m_start = Vec3(
  129. ( float )sources[0]->start/31.0f,
  130. ( float )sources[1]->start/63.0f,
  131. ( float )sources[2]->start/31.0f
  132. );
  133. m_end = Vec3(
  134. ( float )sources[0]->end/31.0f,
  135. ( float )sources[1]->end/63.0f,
  136. ( float )sources[2]->end/31.0f
  137. );
  138. m_index = ( u8 )( 2*index );
  139. m_error = error;
  140. }
  141. }
  142. }
  143. } // namespace squish