EtcDifferentialTrys.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. EtcDifferentialTrys.cpp
  18. Gathers the results of the various encoding trys for both halves of a 4x4 block for Differential mode
  19. */
  20. #include "EtcConfig.h"
  21. #include "EtcDifferentialTrys.h"
  22. #include <assert.h>
  23. namespace Etc
  24. {
  25. // ----------------------------------------------------------------------------------------------------
  26. // construct a list of trys (encoding attempts)
  27. //
  28. // a_frgbaColor1 is the basecolor for the first half
  29. // a_frgbaColor2 is the basecolor for the second half
  30. // a_pauiPixelMapping1 is the pixel order for the first half
  31. // a_pauiPixelMapping2 is the pixel order for the second half
  32. // a_uiRadius is the amount to vary the base colors
  33. //
  34. DifferentialTrys::DifferentialTrys(ColorFloatRGBA a_frgbaColor1, ColorFloatRGBA a_frgbaColor2,
  35. const unsigned int *a_pauiPixelMapping1,
  36. const unsigned int *a_pauiPixelMapping2,
  37. unsigned int a_uiRadius,
  38. int a_iGrayOffset1, int a_iGrayOffset2)
  39. {
  40. assert(a_uiRadius <= MAX_RADIUS);
  41. m_boolSeverelyBentColors = false;
  42. ColorFloatRGBA frgbaQuantizedColor1 = a_frgbaColor1.QuantizeR5G5B5();
  43. ColorFloatRGBA frgbaQuantizedColor2 = a_frgbaColor2.QuantizeR5G5B5();
  44. // quantize base colors
  45. // ensure that trys with a_uiRadius don't overflow
  46. int iRed1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntRed(31.0f)+a_iGrayOffset1, a_uiRadius);
  47. int iGreen1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntGreen(31.0f) + a_iGrayOffset1, a_uiRadius);
  48. int iBlue1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntBlue(31.0f) + a_iGrayOffset1, a_uiRadius);
  49. int iRed2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntRed(31.0f) + a_iGrayOffset2, a_uiRadius);
  50. int iGreen2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntGreen(31.0f) + a_iGrayOffset2, a_uiRadius);
  51. int iBlue2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntBlue(31.0f) + a_iGrayOffset2, a_uiRadius);
  52. int iDeltaRed = iRed2 - iRed1;
  53. int iDeltaGreen = iGreen2 - iGreen1;
  54. int iDeltaBlue = iBlue2 - iBlue1;
  55. // make sure components are within range
  56. {
  57. if (iDeltaRed > 3)
  58. {
  59. if (iDeltaRed > 7)
  60. {
  61. m_boolSeverelyBentColors = true;
  62. }
  63. iRed1 += (iDeltaRed - 3) / 2;
  64. iRed2 = iRed1 + 3;
  65. iDeltaRed = 3;
  66. }
  67. else if (iDeltaRed < -4)
  68. {
  69. if (iDeltaRed < -8)
  70. {
  71. m_boolSeverelyBentColors = true;
  72. }
  73. iRed1 += (iDeltaRed + 4) / 2;
  74. iRed2 = iRed1 - 4;
  75. iDeltaRed = -4;
  76. }
  77. assert(iRed1 >= (signed)(0 + a_uiRadius) && iRed1 <= (signed)(31 - a_uiRadius));
  78. assert(iRed2 >= (signed)(0 + a_uiRadius) && iRed2 <= (signed)(31 - a_uiRadius));
  79. assert(iDeltaRed >= -4 && iDeltaRed <= 3);
  80. if (iDeltaGreen > 3)
  81. {
  82. if (iDeltaGreen > 7)
  83. {
  84. m_boolSeverelyBentColors = true;
  85. }
  86. iGreen1 += (iDeltaGreen - 3) / 2;
  87. iGreen2 = iGreen1 + 3;
  88. iDeltaGreen = 3;
  89. }
  90. else if (iDeltaGreen < -4)
  91. {
  92. if (iDeltaGreen < -8)
  93. {
  94. m_boolSeverelyBentColors = true;
  95. }
  96. iGreen1 += (iDeltaGreen + 4) / 2;
  97. iGreen2 = iGreen1 - 4;
  98. iDeltaGreen = -4;
  99. }
  100. assert(iGreen1 >= (signed)(0 + a_uiRadius) && iGreen1 <= (signed)(31 - a_uiRadius));
  101. assert(iGreen2 >= (signed)(0 + a_uiRadius) && iGreen2 <= (signed)(31 - a_uiRadius));
  102. assert(iDeltaGreen >= -4 && iDeltaGreen <= 3);
  103. if (iDeltaBlue > 3)
  104. {
  105. if (iDeltaBlue > 7)
  106. {
  107. m_boolSeverelyBentColors = true;
  108. }
  109. iBlue1 += (iDeltaBlue - 3) / 2;
  110. iBlue2 = iBlue1 + 3;
  111. iDeltaBlue = 3;
  112. }
  113. else if (iDeltaBlue < -4)
  114. {
  115. if (iDeltaBlue < -8)
  116. {
  117. m_boolSeverelyBentColors = true;
  118. }
  119. iBlue1 += (iDeltaBlue + 4) / 2;
  120. iBlue2 = iBlue1 - 4;
  121. iDeltaBlue = -4;
  122. }
  123. assert(iBlue1 >= (signed)(0+a_uiRadius) && iBlue1 <= (signed)(31 - a_uiRadius));
  124. assert(iBlue2 >= (signed)(0 + a_uiRadius) && iBlue2 <= (signed)(31 - a_uiRadius));
  125. assert(iDeltaBlue >= -4 && iDeltaBlue <= 3);
  126. }
  127. m_half1.Init(iRed1, iGreen1, iBlue1, a_pauiPixelMapping1, a_uiRadius);
  128. m_half2.Init(iRed2, iGreen2, iBlue2, a_pauiPixelMapping2, a_uiRadius);
  129. }
  130. // ----------------------------------------------------------------------------------------------------
  131. //
  132. void DifferentialTrys::Half::Init(int a_iRed, int a_iGreen, int a_iBlue,
  133. const unsigned int *a_pauiPixelMapping, unsigned int a_uiRadius)
  134. {
  135. m_iRed = a_iRed;
  136. m_iGreen = a_iGreen;
  137. m_iBlue = a_iBlue;
  138. m_pauiPixelMapping = a_pauiPixelMapping;
  139. m_uiRadius = a_uiRadius;
  140. m_uiTrys = 0;
  141. }
  142. // ----------------------------------------------------------------------------------------------------
  143. //
  144. } // namespace Etc