singlecolourfit.cpp 4.8 KB

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