Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4Encoding_ETC1.h

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