Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcDifferentialTrys.cpp

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 /*
18 EtcDifferentialTrys.cpp
19
20 Gathers the results of the various encoding trys for both halves of a 4x4 block for Differential mode
21
22 */
23
24 #include "EtcConfig.h"
25 #include "EtcDifferentialTrys.h"
26
27 #include <assert.h>
28
29 namespace Etc
30 {
31
32 // ----------------------------------------------------------------------------------------------------
33 // construct a list of trys (encoding attempts)
34 //
35 // a_frgbaColor1 is the basecolor for the first half
36 // a_frgbaColor2 is the basecolor for the second half
37 // a_pauiPixelMapping1 is the pixel order for the first half
38 // a_pauiPixelMapping2 is the pixel order for the second half
39 // a_uiRadius is the amount to vary the base colors
40 //
41 DifferentialTrys::DifferentialTrys(ColorFloatRGBA a_frgbaColor1, ColorFloatRGBA a_frgbaColor2,
42 const unsigned int *a_pauiPixelMapping1,
43 const unsigned int *a_pauiPixelMapping2,
44 unsigned int a_uiRadius,
45 int a_iGrayOffset1, int a_iGrayOffset2)
46 {
47 assert(a_uiRadius <= MAX_RADIUS);
48
49 m_boolSeverelyBentColors = false;
50
51 ColorFloatRGBA frgbaQuantizedColor1 = a_frgbaColor1.QuantizeR5G5B5();
52 ColorFloatRGBA frgbaQuantizedColor2 = a_frgbaColor2.QuantizeR5G5B5();
53
54 // quantize base colors
55 // ensure that trys with a_uiRadius don't overflow
56 int iRed1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntRed(31.0f)+a_iGrayOffset1, a_uiRadius);
57 int iGreen1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntGreen(31.0f) + a_iGrayOffset1, a_uiRadius);
58 int iBlue1 = MoveAwayFromEdge(frgbaQuantizedColor1.IntBlue(31.0f) + a_iGrayOffset1, a_uiRadius);
59 int iRed2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntRed(31.0f) + a_iGrayOffset2, a_uiRadius);
60 int iGreen2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntGreen(31.0f) + a_iGrayOffset2, a_uiRadius);
61 int iBlue2 = MoveAwayFromEdge(frgbaQuantizedColor2.IntBlue(31.0f) + a_iGrayOffset2, a_uiRadius);
62
63 int iDeltaRed = iRed2 - iRed1;
64 int iDeltaGreen = iGreen2 - iGreen1;
65 int iDeltaBlue = iBlue2 - iBlue1;
66
67 // make sure components are within range
68 {
69 if (iDeltaRed > 3)
70 {
71 if (iDeltaRed > 7)
72 {
73 m_boolSeverelyBentColors = true;
74 }
75
76 iRed1 += (iDeltaRed - 3) / 2;
77 iRed2 = iRed1 + 3;
78 iDeltaRed = 3;
79 }
80 else if (iDeltaRed < -4)
81 {
82 if (iDeltaRed < -8)
83 {
84 m_boolSeverelyBentColors = true;
85 }
86
87 iRed1 += (iDeltaRed + 4) / 2;
88 iRed2 = iRed1 - 4;
89 iDeltaRed = -4;
90 }
91 assert(iRed1 >= (signed)(0 + a_uiRadius) && iRed1 <= (signed)(31 - a_uiRadius));
92 assert(iRed2 >= (signed)(0 + a_uiRadius) && iRed2 <= (signed)(31 - a_uiRadius));
93 assert(iDeltaRed >= -4 && iDeltaRed <= 3);
94
95 if (iDeltaGreen > 3)
96 {
97 if (iDeltaGreen > 7)
98 {
99 m_boolSeverelyBentColors = true;
100 }
101
102 iGreen1 += (iDeltaGreen - 3) / 2;
103 iGreen2 = iGreen1 + 3;
104 iDeltaGreen = 3;
105 }
106 else if (iDeltaGreen < -4)
107 {
108 if (iDeltaGreen < -8)
109 {
110 m_boolSeverelyBentColors = true;
111 }
112
113 iGreen1 += (iDeltaGreen + 4) / 2;
114 iGreen2 = iGreen1 - 4;
115 iDeltaGreen = -4;
116 }
117 assert(iGreen1 >= (signed)(0 + a_uiRadius) && iGreen1 <= (signed)(31 - a_uiRadius));
118 assert(iGreen2 >= (signed)(0 + a_uiRadius) && iGreen2 <= (signed)(31 - a_uiRadius));
119 assert(iDeltaGreen >= -4 && iDeltaGreen <= 3);
120
121 if (iDeltaBlue > 3)
122 {
123 if (iDeltaBlue > 7)
124 {
125 m_boolSeverelyBentColors = true;
126 }
127
128 iBlue1 += (iDeltaBlue - 3) / 2;
129 iBlue2 = iBlue1 + 3;
130 iDeltaBlue = 3;
131 }
132 else if (iDeltaBlue < -4)
133 {
134 if (iDeltaBlue < -8)
135 {
136 m_boolSeverelyBentColors = true;
137 }
138
139 iBlue1 += (iDeltaBlue + 4) / 2;
140 iBlue2 = iBlue1 - 4;
141 iDeltaBlue = -4;
142 }
143 assert(iBlue1 >= (signed)(0+a_uiRadius) && iBlue1 <= (signed)(31 - a_uiRadius));
144 assert(iBlue2 >= (signed)(0 + a_uiRadius) && iBlue2 <= (signed)(31 - a_uiRadius));
145 assert(iDeltaBlue >= -4 && iDeltaBlue <= 3);
146 }
147
148 m_half1.Init(iRed1, iGreen1, iBlue1, a_pauiPixelMapping1, a_uiRadius);
149 m_half2.Init(iRed2, iGreen2, iBlue2, a_pauiPixelMapping2, a_uiRadius);
150
151 }
152
153 // ----------------------------------------------------------------------------------------------------
154 //
155 void DifferentialTrys::Half::Init(int a_iRed, int a_iGreen, int a_iBlue,
156 const unsigned int *a_pauiPixelMapping, unsigned int a_uiRadius)
157 {
158
159 m_iRed = a_iRed;
160 m_iGreen = a_iGreen;
161 m_iBlue = a_iBlue;
162
163 m_pauiPixelMapping = a_pauiPixelMapping;
164 m_uiRadius = a_uiRadius;
165
166 m_uiTrys = 0;
167
168 }
169
170 // ----------------------------------------------------------------------------------------------------
171 //
172
173 } // namespace Etc