EtcBlock4x4Encoding_ETC1.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #pragma once
  17. #include "EtcBlock4x4Encoding.h"
  18. #include "EtcBlock4x4EncodingBits.h"
  19. #include "EtcDifferentialTrys.h"
  20. #include "EtcIndividualTrys.h"
  21. namespace Etc
  22. {
  23. // base class for Block4x4Encoding_RGB8
  24. class Block4x4Encoding_ETC1 : public Block4x4Encoding
  25. {
  26. public:
  27. Block4x4Encoding_ETC1(void);
  28. virtual ~Block4x4Encoding_ETC1(void);
  29. virtual void InitFromSource(Block4x4 *a_pblockParent,
  30. ColorFloatRGBA *a_pafrgbaSource,
  31. unsigned char *a_paucEncodingBits,
  32. ErrorMetric a_errormetric);
  33. virtual void InitFromEncodingBits(Block4x4 *a_pblockParent,
  34. unsigned char *a_paucEncodingBits,
  35. ColorFloatRGBA *a_pafrgbaSource,
  36. ErrorMetric a_errormetric);
  37. virtual void PerformIteration(float a_fEffort);
  38. inline virtual bool GetFlip(void)
  39. {
  40. return m_boolFlip;
  41. }
  42. inline virtual bool IsDifferential(void)
  43. {
  44. return m_boolDiff;
  45. }
  46. virtual void SetEncodingBits(void);
  47. void Decode(void);
  48. inline ColorFloatRGBA GetColor1(void) const
  49. {
  50. return m_frgbaColor1;
  51. }
  52. inline ColorFloatRGBA GetColor2(void) const
  53. {
  54. return m_frgbaColor2;
  55. }
  56. inline const unsigned int * GetSelectors(void) const
  57. {
  58. return m_auiSelectors;
  59. }
  60. inline unsigned int GetCW1(void) const
  61. {
  62. return m_uiCW1;
  63. }
  64. inline unsigned int GetCW2(void) const
  65. {
  66. return m_uiCW2;
  67. }
  68. inline bool HasSeverelyBentDifferentialColors(void) const
  69. {
  70. return m_boolSeverelyBentDifferentialColors;
  71. }
  72. protected:
  73. static const unsigned int s_auiPixelOrderFlip0[PIXELS];
  74. static const unsigned int s_auiPixelOrderFlip1[PIXELS];
  75. static const unsigned int s_auiPixelOrderHScan[PIXELS];
  76. static const unsigned int s_auiLeftPixelMapping[8];
  77. static const unsigned int s_auiRightPixelMapping[8];
  78. static const unsigned int s_auiTopPixelMapping[8];
  79. static const unsigned int s_auiBottomPixelMapping[8];
  80. static const unsigned int SELECTOR_BITS = 2;
  81. static const unsigned int SELECTORS = 1 << SELECTOR_BITS;
  82. static const unsigned int CW_BITS = 3;
  83. static const unsigned int CW_RANGES = 1 << CW_BITS;
  84. static float s_aafCwTable[CW_RANGES][SELECTORS];
  85. static unsigned char s_aucDifferentialCwRange[256];
  86. static const int MAX_DIFFERENTIAL = 3;
  87. static const int MIN_DIFFERENTIAL = -4;
  88. void InitFromEncodingBits_Selectors(void);
  89. void PerformFirstIteration(void);
  90. void CalculateMostLikelyFlip(void);
  91. void TryDifferential(bool a_boolFlip, unsigned int a_uiRadius,
  92. int a_iGrayOffset1, int a_iGrayOffset2);
  93. void TryDifferentialHalf(DifferentialTrys::Half *a_phalf);
  94. void TryIndividual(bool a_boolFlip, unsigned int a_uiRadius);
  95. void TryIndividualHalf(IndividualTrys::Half *a_phalf);
  96. void TryDegenerates1(void);
  97. void TryDegenerates2(void);
  98. void TryDegenerates3(void);
  99. void TryDegenerates4(void);
  100. void CalculateSelectors();
  101. void CalculateHalfOfTheSelectors(unsigned int a_uiHalf,
  102. const unsigned int *pauiPixelMapping);
  103. // calculate the distance2 of r_frgbaPixel from r_frgbaTarget's gray line
  104. inline float CalcGrayDistance2(ColorFloatRGBA &r_frgbaPixel,
  105. ColorFloatRGBA &r_frgbaTarget)
  106. {
  107. float fDeltaGray = ((r_frgbaPixel.fR - r_frgbaTarget.fR) +
  108. (r_frgbaPixel.fG - r_frgbaTarget.fG) +
  109. (r_frgbaPixel.fB - r_frgbaTarget.fB)) / 3.0f;
  110. ColorFloatRGBA frgbaPointOnGrayLine = (r_frgbaTarget + fDeltaGray).ClampRGB();
  111. float fDR = r_frgbaPixel.fR - frgbaPointOnGrayLine.fR;
  112. float fDG = r_frgbaPixel.fG - frgbaPointOnGrayLine.fG;
  113. float fDB = r_frgbaPixel.fB - frgbaPointOnGrayLine.fB;
  114. return (fDR*fDR) + (fDG*fDG) + (fDB*fDB);
  115. }
  116. void SetEncodingBits_Selectors(void);
  117. // intermediate encoding
  118. bool m_boolDiff;
  119. bool m_boolFlip;
  120. ColorFloatRGBA m_frgbaColor1;
  121. ColorFloatRGBA m_frgbaColor2;
  122. unsigned int m_uiCW1;
  123. unsigned int m_uiCW2;
  124. unsigned int m_auiSelectors[PIXELS];
  125. // state shared between iterations
  126. ColorFloatRGBA m_frgbaSourceAverageLeft;
  127. ColorFloatRGBA m_frgbaSourceAverageRight;
  128. ColorFloatRGBA m_frgbaSourceAverageTop;
  129. ColorFloatRGBA m_frgbaSourceAverageBottom;
  130. bool m_boolMostLikelyFlip;
  131. // stats
  132. float m_fError1; // error for Etc1 half 1
  133. float m_fError2; // error for Etc1 half 2
  134. bool m_boolSeverelyBentDifferentialColors; // only valid if m_boolDiff;
  135. // final encoding
  136. Block4x4EncodingBits_RGB8 *m_pencodingbitsRGB8; // or RGB8 portion of Block4x4EncodingBits_RGB8A8
  137. private:
  138. void CalculateSourceAverages(void);
  139. };
  140. } // namespace Etc