Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4Encoding_ETC1.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 EtcBlock4x4Encoding_ETC1.cpp
19
20 Block4x4Encoding_ETC1 is the encoder to use when targetting file format ETC1. This encoder is also
21 used for the ETC1 subset of file format RGB8, RGBA8 and RGB8A1
22
23 */
24
25 #include "EtcConfig.h"
26 #include "EtcBlock4x4Encoding_ETC1.h"
27
28 #include "EtcBlock4x4.h"
29 #include "EtcBlock4x4EncodingBits.h"
30 #include "EtcDifferentialTrys.h"
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <float.h>
36 #include <limits>
37
38 namespace Etc
39 {
40
41 // pixel processing order if the flip bit = 0 (horizontal split)
42 const unsigned int Block4x4Encoding_ETC1::s_auiPixelOrderFlip0[PIXELS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
43
44 // pixel processing order if the flip bit = 1 (vertical split)
45 const unsigned int Block4x4Encoding_ETC1::s_auiPixelOrderFlip1[PIXELS] = { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 };
46
47 // pixel processing order for horizontal scan (ETC normally does a vertical scan)
48 const unsigned int Block4x4Encoding_ETC1::s_auiPixelOrderHScan[PIXELS] = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
49
50 // pixel indices for different block halves
51 const unsigned int Block4x4Encoding_ETC1::s_auiLeftPixelMapping[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
52 const unsigned int Block4x4Encoding_ETC1::s_auiRightPixelMapping[8] = { 8, 9, 10, 11, 12, 13, 14, 15 };
53 const unsigned int Block4x4Encoding_ETC1::s_auiTopPixelMapping[8] = { 0, 1, 4, 5, 8, 9, 12, 13 };
54 const unsigned int Block4x4Encoding_ETC1::s_auiBottomPixelMapping[8] = { 2, 3, 6, 7, 10, 11, 14, 15 };
55
56 // CW ranges that the ETC1 decoders use
57 // CW is basically a contrast for the different selector bits, since these values are offsets to the base color
58 // the first axis in the array is indexed by the CW in the encoding bits
59 // the second axis in the array is indexed by the selector bits
60 float Block4x4Encoding_ETC1::s_aafCwTable[CW_RANGES][SELECTORS] =
61 {
62 { 2.0f / 255.0f, 8.0f / 255.0f, -2.0f / 255.0f, -8.0f / 255.0f },
63 { 5.0f / 255.0f, 17.0f / 255.0f, -5.0f / 255.0f, -17.0f / 255.0f },
64 { 9.0f / 255.0f, 29.0f / 255.0f, -9.0f / 255.0f, -29.0f / 255.0f },
65 { 13.0f / 255.0f, 42.0f / 255.0f, -13.0f / 255.0f, -42.0f / 255.0f },
66 { 18.0f / 255.0f, 60.0f / 255.0f, -18.0f / 255.0f, -60.0f / 255.0f },
67 { 24.0f / 255.0f, 80.0f / 255.0f, -24.0f / 255.0f, -80.0f / 255.0f },
68 { 33.0f / 255.0f, 106.0f / 255.0f, -33.0f / 255.0f, -106.0f / 255.0f },
69 { 47.0f / 255.0f, 183.0f / 255.0f, -47.0f / 255.0f, -183.0f / 255.0f }
70 };
71
72 // ----------------------------------------------------------------------------------------------------
73 //
74 Block4x4Encoding_ETC1::Block4x4Encoding_ETC1(void)
75 {
76 m_mode = MODE_ETC1;
77 m_boolDiff = false;
78 m_boolFlip = false;
79 m_frgbaColor1 = ColorFloatRGBA();
80 m_frgbaColor2 = ColorFloatRGBA();
81 m_uiCW1 = 0;
82 m_uiCW2 = 0;
83 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
84 {
85 m_auiSelectors[uiPixel] = 0;
86 m_afDecodedAlphas[uiPixel] = 1.0f;
87 }
88
89 m_boolMostLikelyFlip = false;
90
91 m_fError = -1.0f;
92
93 m_fError1 = -1.0f;
94 m_fError2 = -1.0f;
95 m_boolSeverelyBentDifferentialColors = false;
96
97 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
98 {
99 m_afDecodedAlphas[uiPixel] = 1.0f;
100 }
101
102 }
103
104 Block4x4Encoding_ETC1::~Block4x4Encoding_ETC1(void) {}
105
106 // ----------------------------------------------------------------------------------------------------
107 // initialization prior to encoding
108 // a_pblockParent points to the block associated with this encoding
109 // a_errormetric is used to choose the best encoding
110 // a_pafrgbaSource points to a 4x4 block subset of the source image
111 // a_paucEncodingBits points to the final encoding bits
112 //
113 void Block4x4Encoding_ETC1::InitFromSource(Block4x4 *a_pblockParent,
114 ColorFloatRGBA *a_pafrgbaSource,
115 unsigned char *a_paucEncodingBits, ErrorMetric a_errormetric)
116 {
117
118 Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);
119
120 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
121 {
122 m_afDecodedAlphas[uiPixel] = 1.0f;
123 }
124
125 m_fError = -1.0f;
126
127 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)(a_paucEncodingBits);
128
129 }
130
131 // ----------------------------------------------------------------------------------------------------
132 // initialization from the encoding bits of a previous encoding
133 // a_pblockParent points to the block associated with this encoding
134 // a_errormetric is used to choose the best encoding
135 // a_pafrgbaSource points to a 4x4 block subset of the source image
136 // a_paucEncodingBits points to the final encoding bits of a previous encoding
137 //
138 void Block4x4Encoding_ETC1::InitFromEncodingBits(Block4x4 *a_pblockParent,
139 unsigned char *a_paucEncodingBits,
140 ColorFloatRGBA *a_pafrgbaSource,
141 ErrorMetric a_errormetric)
142 {
143
144 Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);
145 m_fError = -1.0f;
146
147 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)a_paucEncodingBits;
148
149 m_mode = MODE_ETC1;
150 m_boolDiff = m_pencodingbitsRGB8->individual.diff;
151 m_boolFlip = m_pencodingbitsRGB8->individual.flip;
152 if (m_boolDiff)
153 {
154 int iR2 = (int)(m_pencodingbitsRGB8->differential.red1 + m_pencodingbitsRGB8->differential.dred2);
155 if (iR2 < 0)
156 {
157 iR2 = 0;
158 }
159 else if (iR2 > 31)
160 {
161 iR2 = 31;
162 }
163
164 int iG2 = (int)(m_pencodingbitsRGB8->differential.green1 + m_pencodingbitsRGB8->differential.dgreen2);
165 if (iG2 < 0)
166 {
167 iG2 = 0;
168 }
169 else if (iG2 > 31)
170 {
171 iG2 = 31;
172 }
173
174 int iB2 = (int)(m_pencodingbitsRGB8->differential.blue1 + m_pencodingbitsRGB8->differential.dblue2);
175 if (iB2 < 0)
176 {
177 iB2 = 0;
178 }
179 else if (iB2 > 31)
180 {
181 iB2 = 31;
182 }
183
184 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5(m_pencodingbitsRGB8->differential.red1, m_pencodingbitsRGB8->differential.green1, m_pencodingbitsRGB8->differential.blue1);
185 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iR2, (unsigned char)iG2, (unsigned char)iB2);
186
187 }
188 else
189 {
190 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4(m_pencodingbitsRGB8->individual.red1, m_pencodingbitsRGB8->individual.green1, m_pencodingbitsRGB8->individual.blue1);
191 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4(m_pencodingbitsRGB8->individual.red2, m_pencodingbitsRGB8->individual.green2, m_pencodingbitsRGB8->individual.blue2);
192 }
193
194 m_uiCW1 = m_pencodingbitsRGB8->individual.cw1;
195 m_uiCW2 = m_pencodingbitsRGB8->individual.cw2;
196
197 InitFromEncodingBits_Selectors();
198
199 Decode();
200
201 CalcBlockError();
202 }
203
204 // ----------------------------------------------------------------------------------------------------
205 // init the selectors from a prior encoding
206 //
207 void Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors(void)
208 {
209
210 unsigned char *paucSelectors = (unsigned char *)&m_pencodingbitsRGB8->individual.selectors;
211
212 for (unsigned int iPixel = 0; iPixel < PIXELS; iPixel++)
213 {
214 unsigned int uiByteMSB = (unsigned int)(1 - (iPixel / 8));
215 unsigned int uiByteLSB = (unsigned int)(3 - (iPixel / 8));
216 unsigned int uiShift = (unsigned int)(iPixel & 7);
217
218 unsigned int uiSelectorMSB = (unsigned int)((paucSelectors[uiByteMSB] >> uiShift) & 1);
219 unsigned int uiSelectorLSB = (unsigned int)((paucSelectors[uiByteLSB] >> uiShift) & 1);
220
221 m_auiSelectors[iPixel] = (uiSelectorMSB << 1) + uiSelectorLSB;
222 }
223
224 }
225
226 // ----------------------------------------------------------------------------------------------------
227 // perform a single encoding iteration
228 // replace the encoding if a better encoding was found
229 // subsequent iterations generally take longer for each iteration
230 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
231 //
232 void Block4x4Encoding_ETC1::PerformIteration(float a_fEffort)
233 {
234 assert(!m_boolDone);
235
236 switch (m_uiEncodingIterations)
237 {
238 case 0:
239 PerformFirstIteration();
240 break;
241
242 case 1:
243 TryDifferential(m_boolMostLikelyFlip, 1, 0, 0);
244 break;
245
246 case 2:
247 TryIndividual(m_boolMostLikelyFlip, 1);
248 if (a_fEffort <= 49.5f)
249 {
250 m_boolDone = true;
251 }
252 break;
253
254 case 3:
255 TryDifferential(!m_boolMostLikelyFlip, 1, 0, 0);
256 if (a_fEffort <= 59.5f)
257 {
258 m_boolDone = true;
259 }
260 break;
261
262 case 4:
263 TryIndividual(!m_boolMostLikelyFlip, 1);
264 if (a_fEffort <= 69.5f)
265 {
266 m_boolDone = true;
267 }
268 break;
269
270 case 5:
271 TryDegenerates1();
272 if (a_fEffort <= 79.5f)
273 {
274 m_boolDone = true;
275 }
276 break;
277
278 case 6:
279 TryDegenerates2();
280 if (a_fEffort <= 89.5f)
281 {
282 m_boolDone = true;
283 }
284 break;
285
286 case 7:
287 TryDegenerates3();
288 if (a_fEffort <= 99.5f)
289 {
290 m_boolDone = true;
291 }
292 break;
293
294 case 8:
295 TryDegenerates4();
296 m_boolDone = true;
297 break;
298
299 default:
300 assert(0);
301 break;
302 }
303
304 m_uiEncodingIterations++;
305 SetDoneIfPerfect();
306 }
307
308 // ----------------------------------------------------------------------------------------------------
309 // find best initial encoding to ensure block has a valid encoding
310 //
311 void Block4x4Encoding_ETC1::PerformFirstIteration(void)
312 {
313 CalculateMostLikelyFlip();
314
315 m_fError = FLT_MAX;
316
317 TryDifferential(m_boolMostLikelyFlip, 0, 0, 0);
318 SetDoneIfPerfect();
319 if (m_boolDone)
320 {
321 return;
322 }
323
324 TryIndividual(m_boolMostLikelyFlip, 0);
325 SetDoneIfPerfect();
326 if (m_boolDone)
327 {
328 return;
329 }
330 TryDifferential(!m_boolMostLikelyFlip, 0, 0, 0);
331 SetDoneIfPerfect();
332 if (m_boolDone)
333 {
334 return;
335 }
336 TryIndividual(!m_boolMostLikelyFlip, 0);
337
338 }
339
340 // ----------------------------------------------------------------------------------------------------
341 // algorithm:
342 // create a source average color for the Left, Right, Top and Bottom halves using the 8 pixels in each half
343 // note: the "gray line" is the line of equal delta RGB that goes thru the average color
344 // for each half:
345 // see how close each of the 8 pixels are to the "gray line" that goes thru the source average color
346 // create an error value that is the sum of the distances from the gray line
347 // h_error is the sum of Left and Right errors
348 // v_error is the sum of Top and Bottom errors
349 //
350 void Block4x4Encoding_ETC1::CalculateMostLikelyFlip(void)
351 {
352 static const bool DEBUG_PRINT = false;
353
354 CalculateSourceAverages();
355
356 float fLeftGrayErrorSum = 0.0f;
357 float fRightGrayErrorSum = 0.0f;
358 float fTopGrayErrorSum = 0.0f;
359 float fBottomGrayErrorSum = 0.0f;
360
361 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
362 {
363 ColorFloatRGBA *pfrgbaLeft = &m_pafrgbaSource[uiPixel];
364 ColorFloatRGBA *pfrgbaRight = &m_pafrgbaSource[uiPixel + 8];
365 ColorFloatRGBA *pfrgbaTop = &m_pafrgbaSource[s_auiTopPixelMapping[uiPixel]];
366 ColorFloatRGBA *pfrgbaBottom = &m_pafrgbaSource[s_auiBottomPixelMapping[uiPixel]];
367
368 float fLeftGrayError = CalcGrayDistance2(*pfrgbaLeft, m_frgbaSourceAverageLeft);
369 float fRightGrayError = CalcGrayDistance2(*pfrgbaRight, m_frgbaSourceAverageRight);
370 float fTopGrayError = CalcGrayDistance2(*pfrgbaTop, m_frgbaSourceAverageTop);
371 float fBottomGrayError = CalcGrayDistance2(*pfrgbaBottom, m_frgbaSourceAverageBottom);
372
373 fLeftGrayErrorSum += fLeftGrayError;
374 fRightGrayErrorSum += fRightGrayError;
375 fTopGrayErrorSum += fTopGrayError;
376 fBottomGrayErrorSum += fBottomGrayError;
377 }
378
379 if (DEBUG_PRINT)
380 {
381 printf("\n%.2f %.2f\n", fLeftGrayErrorSum + fRightGrayErrorSum, fTopGrayErrorSum + fBottomGrayErrorSum);
382 }
383
384 m_boolMostLikelyFlip = (fTopGrayErrorSum + fBottomGrayErrorSum) < (fLeftGrayErrorSum + fRightGrayErrorSum);
385
386 }
387
388 // ----------------------------------------------------------------------------------------------------
389 // calculate source pixel averages for each 2x2 quadrant in a 4x4 block
390 // these are used to determine the averages for each of the 4 different halves (left, right, top, bottom)
391 // ignore pixels that have alpha == NAN (these are border pixels outside of the source image)
392 // weight the averages based on a pixel's alpha
393 //
394 void Block4x4Encoding_ETC1::CalculateSourceAverages(void)
395 {
396 static const bool DEBUG_PRINT = false;
397
398 bool boolRGBX = m_pblockParent->GetImageSource()->GetErrorMetric() == ErrorMetric::RGBX;
399
400 if (m_pblockParent->GetSourceAlphaMix() == Block4x4::SourceAlphaMix::OPAQUE || boolRGBX)
401 {
402 ColorFloatRGBA frgbaSumUL = m_pafrgbaSource[0] + m_pafrgbaSource[1] + m_pafrgbaSource[4] + m_pafrgbaSource[5];
403 ColorFloatRGBA frgbaSumLL = m_pafrgbaSource[2] + m_pafrgbaSource[3] + m_pafrgbaSource[6] + m_pafrgbaSource[7];
404 ColorFloatRGBA frgbaSumUR = m_pafrgbaSource[8] + m_pafrgbaSource[9] + m_pafrgbaSource[12] + m_pafrgbaSource[13];
405 ColorFloatRGBA frgbaSumLR = m_pafrgbaSource[10] + m_pafrgbaSource[11] + m_pafrgbaSource[14] + m_pafrgbaSource[15];
406
407 m_frgbaSourceAverageLeft = (frgbaSumUL + frgbaSumLL) * 0.125f;
408 m_frgbaSourceAverageRight = (frgbaSumUR + frgbaSumLR) * 0.125f;
409 m_frgbaSourceAverageTop = (frgbaSumUL + frgbaSumUR) * 0.125f;
410 m_frgbaSourceAverageBottom = (frgbaSumLL + frgbaSumLR) * 0.125f;
411 }
412 else
413 {
414 float afSourceAlpha[PIXELS];
415
416 // treat alpha NAN as 0.0f
417 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
418 {
419 afSourceAlpha[uiPixel] = isnan(m_pafrgbaSource[uiPixel].fA) ?
420 0.0f :
421 m_pafrgbaSource[uiPixel].fA;
422 }
423
424 ColorFloatRGBA afrgbaAlphaWeightedSource[PIXELS];
425 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
426 {
427 afrgbaAlphaWeightedSource[uiPixel] = m_pafrgbaSource[uiPixel] * afSourceAlpha[uiPixel];
428 }
429
430 ColorFloatRGBA frgbaSumUL = afrgbaAlphaWeightedSource[0] +
431 afrgbaAlphaWeightedSource[1] +
432 afrgbaAlphaWeightedSource[4] +
433 afrgbaAlphaWeightedSource[5];
434
435 ColorFloatRGBA frgbaSumLL = afrgbaAlphaWeightedSource[2] +
436 afrgbaAlphaWeightedSource[3] +
437 afrgbaAlphaWeightedSource[6] +
438 afrgbaAlphaWeightedSource[7];
439
440 ColorFloatRGBA frgbaSumUR = afrgbaAlphaWeightedSource[8] +
441 afrgbaAlphaWeightedSource[9] +
442 afrgbaAlphaWeightedSource[12] +
443 afrgbaAlphaWeightedSource[13];
444
445 ColorFloatRGBA frgbaSumLR = afrgbaAlphaWeightedSource[10] +
446 afrgbaAlphaWeightedSource[11] +
447 afrgbaAlphaWeightedSource[14] +
448 afrgbaAlphaWeightedSource[15];
449
450 float fWeightSumUL = afSourceAlpha[0] +
451 afSourceAlpha[1] +
452 afSourceAlpha[4] +
453 afSourceAlpha[5];
454
455 float fWeightSumLL = afSourceAlpha[2] +
456 afSourceAlpha[3] +
457 afSourceAlpha[6] +
458 afSourceAlpha[7];
459
460 float fWeightSumUR = afSourceAlpha[8] +
461 afSourceAlpha[9] +
462 afSourceAlpha[12] +
463 afSourceAlpha[13];
464
465 float fWeightSumLR = afSourceAlpha[10] +
466 afSourceAlpha[11] +
467 afSourceAlpha[14] +
468 afSourceAlpha[15];
469
470 ColorFloatRGBA frgbaSumLeft = frgbaSumUL + frgbaSumLL;
471 ColorFloatRGBA frgbaSumRight = frgbaSumUR + frgbaSumLR;
472 ColorFloatRGBA frgbaSumTop = frgbaSumUL + frgbaSumUR;
473 ColorFloatRGBA frgbaSumBottom = frgbaSumLL + frgbaSumLR;
474
475 float fWeightSumLeft = fWeightSumUL + fWeightSumLL;
476 float fWeightSumRight = fWeightSumUR + fWeightSumLR;
477 float fWeightSumTop = fWeightSumUL + fWeightSumUR;
478 float fWeightSumBottom = fWeightSumLL + fWeightSumLR;
479
480 // check to see if there is at least 1 pixel with non-zero alpha
481 // completely transparent block should not make it to this code
482 assert((fWeightSumLeft + fWeightSumRight) > 0.0f);
483 assert((fWeightSumTop + fWeightSumBottom) > 0.0f);
484
485 if (fWeightSumLeft > 0.0f)
486 {
487 m_frgbaSourceAverageLeft = frgbaSumLeft * (1.0f/fWeightSumLeft);
488 }
489 if (fWeightSumRight > 0.0f)
490 {
491 m_frgbaSourceAverageRight = frgbaSumRight * (1.0f/fWeightSumRight);
492 }
493 if (fWeightSumTop > 0.0f)
494 {
495 m_frgbaSourceAverageTop = frgbaSumTop * (1.0f/fWeightSumTop);
496 }
497 if (fWeightSumBottom > 0.0f)
498 {
499 m_frgbaSourceAverageBottom = frgbaSumBottom * (1.0f/fWeightSumBottom);
500 }
501
502 if (fWeightSumLeft == 0.0f)
503 {
504 assert(fWeightSumRight > 0.0f);
505 m_frgbaSourceAverageLeft = m_frgbaSourceAverageRight;
506 }
507 if (fWeightSumRight == 0.0f)
508 {
509 assert(fWeightSumLeft > 0.0f);
510 m_frgbaSourceAverageRight = m_frgbaSourceAverageLeft;
511 }
512 if (fWeightSumTop == 0.0f)
513 {
514 assert(fWeightSumBottom > 0.0f);
515 m_frgbaSourceAverageTop = m_frgbaSourceAverageBottom;
516 }
517 if (fWeightSumBottom == 0.0f)
518 {
519 assert(fWeightSumTop > 0.0f);
520 m_frgbaSourceAverageBottom = m_frgbaSourceAverageTop;
521 }
522 }
523
524
525
526 if (DEBUG_PRINT)
527 {
528 printf("\ntarget: [%.2f,%.2f,%.2f] [%.2f,%.2f,%.2f] [%.2f,%.2f,%.2f] [%.2f,%.2f,%.2f]\n",
529 m_frgbaSourceAverageLeft.fR, m_frgbaSourceAverageLeft.fG, m_frgbaSourceAverageLeft.fB,
530 m_frgbaSourceAverageRight.fR, m_frgbaSourceAverageRight.fG, m_frgbaSourceAverageRight.fB,
531 m_frgbaSourceAverageTop.fR, m_frgbaSourceAverageTop.fG, m_frgbaSourceAverageTop.fB,
532 m_frgbaSourceAverageBottom.fR, m_frgbaSourceAverageBottom.fG, m_frgbaSourceAverageBottom.fB);
533 }
534
535 }
536
537 // ----------------------------------------------------------------------------------------------------
538 // try an ETC1 differential mode encoding
539 // use a_boolFlip to set the encoding F bit
540 // use a_uiRadius to alter basecolor components in the range[-a_uiRadius:a_uiRadius]
541 // use a_iGrayOffset1 and a_iGrayOffset2 to offset the basecolor to search for degenerate encodings
542 // replace the encoding if the encoding error is less than previous encoding
543 //
544 void Block4x4Encoding_ETC1::TryDifferential(bool a_boolFlip, unsigned int a_uiRadius,
545 int a_iGrayOffset1, int a_iGrayOffset2)
546 {
547
548 ColorFloatRGBA frgbaColor1;
549 ColorFloatRGBA frgbaColor2;
550
551 const unsigned int *pauiPixelMapping1;
552 const unsigned int *pauiPixelMapping2;
553
554 if (a_boolFlip)
555 {
556 frgbaColor1 = m_frgbaSourceAverageTop;
557 frgbaColor2 = m_frgbaSourceAverageBottom;
558
559 pauiPixelMapping1 = s_auiTopPixelMapping;
560 pauiPixelMapping2 = s_auiBottomPixelMapping;
561 }
562 else
563 {
564 frgbaColor1 = m_frgbaSourceAverageLeft;
565 frgbaColor2 = m_frgbaSourceAverageRight;
566
567 pauiPixelMapping1 = s_auiLeftPixelMapping;
568 pauiPixelMapping2 = s_auiRightPixelMapping;
569 }
570
571 DifferentialTrys trys(frgbaColor1, frgbaColor2, pauiPixelMapping1, pauiPixelMapping2,
572 a_uiRadius, a_iGrayOffset1, a_iGrayOffset2);
573
574 Block4x4Encoding_ETC1 encodingTry = *this;
575 encodingTry.m_boolFlip = a_boolFlip;
576
577 encodingTry.TryDifferentialHalf(&trys.m_half1);
578 encodingTry.TryDifferentialHalf(&trys.m_half2);
579
580 // find best halves that are within differential range
581 DifferentialTrys::Try *ptryBest1 = nullptr;
582 DifferentialTrys::Try *ptryBest2 = nullptr;
583 encodingTry.m_fError = FLT_MAX;
584
585 // see if the best of each half are in differential range
586 int iDRed = trys.m_half2.m_ptryBest->m_iRed - trys.m_half1.m_ptryBest->m_iRed;
587 int iDGreen = trys.m_half2.m_ptryBest->m_iGreen - trys.m_half1.m_ptryBest->m_iGreen;
588 int iDBlue = trys.m_half2.m_ptryBest->m_iBlue - trys.m_half1.m_ptryBest->m_iBlue;
589 if (iDRed >= -4 && iDRed <= 3 && iDGreen >= -4 && iDGreen <= 3 && iDBlue >= -4 && iDBlue <= 3)
590 {
591 ptryBest1 = trys.m_half1.m_ptryBest;
592 ptryBest2 = trys.m_half2.m_ptryBest;
593 encodingTry.m_fError = trys.m_half1.m_ptryBest->m_fError + trys.m_half2.m_ptryBest->m_fError;
594 }
595 else
596 {
597 // else, find the next best halves that are in differential range
598 for (DifferentialTrys::Try *ptry1 = &trys.m_half1.m_atry[0];
599 ptry1 < &trys.m_half1.m_atry[trys.m_half1.m_uiTrys];
600 ptry1++)
601 {
602 for (DifferentialTrys::Try *ptry2 = &trys.m_half2.m_atry[0];
603 ptry2 < &trys.m_half2.m_atry[trys.m_half2.m_uiTrys];
604 ptry2++)
605 {
606 iDRed = ptry2->m_iRed - ptry1->m_iRed;
607 bool boolValidRedDelta = iDRed <= 3 && iDRed >= -4;
608 iDGreen = ptry2->m_iGreen - ptry1->m_iGreen;
609 bool boolValidGreenDelta = iDGreen <= 3 && iDGreen >= -4;
610 iDBlue = ptry2->m_iBlue - ptry1->m_iBlue;
611 bool boolValidBlueDelta = iDBlue <= 3 && iDBlue >= -4;
612
613 if (boolValidRedDelta && boolValidGreenDelta && boolValidBlueDelta)
614 {
615 float fError = ptry1->m_fError + ptry2->m_fError;
616
617 if (fError < encodingTry.m_fError)
618 {
619 encodingTry.m_fError = fError;
620
621 ptryBest1 = ptry1;
622 ptryBest2 = ptry2;
623 }
624 }
625
626 }
627 }
628 assert(encodingTry.m_fError < FLT_MAX);
629 assert(ptryBest1 != nullptr);
630 assert(ptryBest2 != nullptr);
631 }
632
633 if (encodingTry.m_fError < m_fError)
634 {
635 m_mode = MODE_ETC1;
636 m_boolDiff = true;
637 m_boolFlip = encodingTry.m_boolFlip;
638 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest1->m_iRed, (unsigned char)ptryBest1->m_iGreen, (unsigned char)ptryBest1->m_iBlue);
639 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest2->m_iRed, (unsigned char)ptryBest2->m_iGreen, (unsigned char)ptryBest2->m_iBlue);
640 m_uiCW1 = ptryBest1->m_uiCW;
641 m_uiCW2 = ptryBest2->m_uiCW;
642
643 for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS / 2; uiPixelOrder++)
644 {
645 unsigned int uiPixel1 = pauiPixelMapping1[uiPixelOrder];
646 unsigned int uiPixel2 = pauiPixelMapping2[uiPixelOrder];
647
648 unsigned int uiSelector1 = ptryBest1->m_auiSelectors[uiPixelOrder];
649 unsigned int uiSelector2 = ptryBest2->m_auiSelectors[uiPixelOrder];
650
651 m_auiSelectors[uiPixel1] = uiSelector1;
652 m_auiSelectors[uiPixel2] = ptryBest2->m_auiSelectors[uiPixelOrder];
653
654 float fDeltaRGB1 = s_aafCwTable[m_uiCW1][uiSelector1];
655 float fDeltaRGB2 = s_aafCwTable[m_uiCW2][uiSelector2];
656
657 m_afrgbaDecodedColors[uiPixel1] = (m_frgbaColor1 + fDeltaRGB1).ClampRGB();
658 m_afrgbaDecodedColors[uiPixel2] = (m_frgbaColor2 + fDeltaRGB2).ClampRGB();
659 }
660
661 m_fError1 = ptryBest1->m_fError;
662 m_fError2 = ptryBest2->m_fError;
663 m_boolSeverelyBentDifferentialColors = trys.m_boolSeverelyBentColors;
664 m_fError = m_fError1 + m_fError2;
665
666 // sanity check
667 {
668 int iRed1 = m_frgbaColor1.IntRed(31.0f);
669 int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
670 int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
671
672 int iRed2 = m_frgbaColor2.IntRed(31.0f);
673 int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
674 int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
675
676 iDRed = iRed2 - iRed1;
677 iDGreen = iGreen2 - iGreen1;
678 iDBlue = iBlue2 - iBlue1;
679
680 assert(iDRed >= -4 && iDRed < 4);
681 assert(iDGreen >= -4 && iDGreen < 4);
682 assert(iDBlue >= -4 && iDBlue < 4);
683 }
684 }
685
686 }
687
688 // ----------------------------------------------------------------------------------------------------
689 // try an ETC1 differential mode encoding for a half of a 4x4 block
690 // vary the basecolor components using a radius
691 //
692 void Block4x4Encoding_ETC1::TryDifferentialHalf(DifferentialTrys::Half *a_phalf)
693 {
694
695 a_phalf->m_ptryBest = nullptr;
696 float fBestTryError = FLT_MAX;
697
698 a_phalf->m_uiTrys = 0;
699 for (int iRed = a_phalf->m_iRed - (int)a_phalf->m_uiRadius;
700 iRed <= a_phalf->m_iRed + (int)a_phalf->m_uiRadius;
701 iRed++)
702 {
703 assert(iRed >= 0 && iRed <= 31);
704
705 for (int iGreen = a_phalf->m_iGreen - (int)a_phalf->m_uiRadius;
706 iGreen <= a_phalf->m_iGreen + (int)a_phalf->m_uiRadius;
707 iGreen++)
708 {
709 assert(iGreen >= 0 && iGreen <= 31);
710
711 for (int iBlue = a_phalf->m_iBlue - (int)a_phalf->m_uiRadius;
712 iBlue <= a_phalf->m_iBlue + (int)a_phalf->m_uiRadius;
713 iBlue++)
714 {
715 assert(iBlue >= 0 && iBlue <= 31);
716
717 DifferentialTrys::Try *ptry = &a_phalf->m_atry[a_phalf->m_uiTrys];
718 assert(ptry < &a_phalf->m_atry[DifferentialTrys::Half::MAX_TRYS]);
719
720 ptry->m_iRed = iRed;
721 ptry->m_iGreen = iGreen;
722 ptry->m_iBlue = iBlue;
723 ptry->m_fError = FLT_MAX;
724 ColorFloatRGBA frgbaColor = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iRed, (unsigned char)iGreen, (unsigned char)iBlue);
725
726 // try each CW
727 for (unsigned int uiCW = 0; uiCW < CW_RANGES; uiCW++)
728 {
729 unsigned int auiPixelSelectors[PIXELS / 2];
730 ColorFloatRGBA afrgbaDecodedPixels[PIXELS / 2];
731 float afPixelErrors[PIXELS / 2] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
732 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
733
734 // pre-compute decoded pixels for each selector
735 ColorFloatRGBA afrgbaSelectors[SELECTORS];
736 assert(SELECTORS == 4);
737 afrgbaSelectors[0] = (frgbaColor + s_aafCwTable[uiCW][0]).ClampRGB();
738 afrgbaSelectors[1] = (frgbaColor + s_aafCwTable[uiCW][1]).ClampRGB();
739 afrgbaSelectors[2] = (frgbaColor + s_aafCwTable[uiCW][2]).ClampRGB();
740 afrgbaSelectors[3] = (frgbaColor + s_aafCwTable[uiCW][3]).ClampRGB();
741
742 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
743 {
744 ColorFloatRGBA *pfrgbaSourcePixel = &m_pafrgbaSource[a_phalf->m_pauiPixelMapping[uiPixel]];
745 ColorFloatRGBA frgbaDecodedPixel;
746
747 for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
748 {
749 frgbaDecodedPixel = afrgbaSelectors[uiSelector];
750
751 float fPixelError;
752
753 fPixelError = CalcPixelError(frgbaDecodedPixel, m_afDecodedAlphas[a_phalf->m_pauiPixelMapping[uiPixel]],
754 *pfrgbaSourcePixel);
755
756 if (fPixelError < afPixelErrors[uiPixel])
757 {
758 auiPixelSelectors[uiPixel] = uiSelector;
759 afrgbaDecodedPixels[uiPixel] = frgbaDecodedPixel;
760 afPixelErrors[uiPixel] = fPixelError;
761 }
762
763 }
764 }
765
766 // add up all pixel errors
767 float fCWError = 0.0f;
768 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
769 {
770 fCWError += afPixelErrors[uiPixel];
771 }
772
773 // if best CW so far
774 if (fCWError < ptry->m_fError)
775 {
776 ptry->m_uiCW = uiCW;
777 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
778 {
779 ptry->m_auiSelectors[uiPixel] = auiPixelSelectors[uiPixel];
780 }
781 ptry->m_fError = fCWError;
782 }
783
784 }
785
786 if (ptry->m_fError < fBestTryError)
787 {
788 a_phalf->m_ptryBest = ptry;
789 fBestTryError = ptry->m_fError;
790 }
791
792 assert(ptry->m_fError < FLT_MAX);
793
794 a_phalf->m_uiTrys++;
795 }
796 }
797 }
798
799 }
800
801 // ----------------------------------------------------------------------------------------------------
802 // try an ETC1 individual mode encoding
803 // use a_boolFlip to set the encoding F bit
804 // use a_uiRadius to alter basecolor components in the range[-a_uiRadius:a_uiRadius]
805 // replace the encoding if the encoding error is less than previous encoding
806 //
807 void Block4x4Encoding_ETC1::TryIndividual(bool a_boolFlip, unsigned int a_uiRadius)
808 {
809
810 ColorFloatRGBA frgbaColor1;
811 ColorFloatRGBA frgbaColor2;
812
813 const unsigned int *pauiPixelMapping1;
814 const unsigned int *pauiPixelMapping2;
815
816 if (a_boolFlip)
817 {
818 frgbaColor1 = m_frgbaSourceAverageTop;
819 frgbaColor2 = m_frgbaSourceAverageBottom;
820
821 pauiPixelMapping1 = s_auiTopPixelMapping;
822 pauiPixelMapping2 = s_auiBottomPixelMapping;
823 }
824 else
825 {
826 frgbaColor1 = m_frgbaSourceAverageLeft;
827 frgbaColor2 = m_frgbaSourceAverageRight;
828
829 pauiPixelMapping1 = s_auiLeftPixelMapping;
830 pauiPixelMapping2 = s_auiRightPixelMapping;
831 }
832
833 IndividualTrys trys(frgbaColor1, frgbaColor2, pauiPixelMapping1, pauiPixelMapping2, a_uiRadius);
834
835 Block4x4Encoding_ETC1 encodingTry = *this;
836 encodingTry.m_boolFlip = a_boolFlip;
837
838 encodingTry.TryIndividualHalf(&trys.m_half1);
839 encodingTry.TryIndividualHalf(&trys.m_half2);
840
841 // use the best of each half
842 IndividualTrys::Try *ptryBest1 = trys.m_half1.m_ptryBest;
843 IndividualTrys::Try *ptryBest2 = trys.m_half2.m_ptryBest;
844 encodingTry.m_fError = trys.m_half1.m_ptryBest->m_fError + trys.m_half2.m_ptryBest->m_fError;
845
846 if (encodingTry.m_fError < m_fError)
847 {
848 m_mode = MODE_ETC1;
849 m_boolDiff = false;
850 m_boolFlip = encodingTry.m_boolFlip;
851 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)ptryBest1->m_iRed, (unsigned char)ptryBest1->m_iGreen, (unsigned char)ptryBest1->m_iBlue);
852 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)ptryBest2->m_iRed, (unsigned char)ptryBest2->m_iGreen, (unsigned char)ptryBest2->m_iBlue);
853 m_uiCW1 = ptryBest1->m_uiCW;
854 m_uiCW2 = ptryBest2->m_uiCW;
855
856 for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS / 2; uiPixelOrder++)
857 {
858 unsigned int uiPixel1 = pauiPixelMapping1[uiPixelOrder];
859 unsigned int uiPixel2 = pauiPixelMapping2[uiPixelOrder];
860
861 unsigned int uiSelector1 = ptryBest1->m_auiSelectors[uiPixelOrder];
862 unsigned int uiSelector2 = ptryBest2->m_auiSelectors[uiPixelOrder];
863
864 m_auiSelectors[uiPixel1] = uiSelector1;
865 m_auiSelectors[uiPixel2] = ptryBest2->m_auiSelectors[uiPixelOrder];
866
867 float fDeltaRGB1 = s_aafCwTable[m_uiCW1][uiSelector1];
868 float fDeltaRGB2 = s_aafCwTable[m_uiCW2][uiSelector2];
869
870 m_afrgbaDecodedColors[uiPixel1] = (m_frgbaColor1 + fDeltaRGB1).ClampRGB();
871 m_afrgbaDecodedColors[uiPixel2] = (m_frgbaColor2 + fDeltaRGB2).ClampRGB();
872 }
873
874 m_fError1 = ptryBest1->m_fError;
875 m_fError2 = ptryBest2->m_fError;
876 m_fError = m_fError1 + m_fError2;
877 }
878
879 }
880
881 // ----------------------------------------------------------------------------------------------------
882 // try an ETC1 differential mode encoding for a half of a 4x4 block
883 // vary the basecolor components using a radius
884 //
885 void Block4x4Encoding_ETC1::TryIndividualHalf(IndividualTrys::Half *a_phalf)
886 {
887
888 a_phalf->m_ptryBest = nullptr;
889 float fBestTryError = FLT_MAX;
890
891 a_phalf->m_uiTrys = 0;
892 for (int iRed = a_phalf->m_iRed - (int)a_phalf->m_uiRadius;
893 iRed <= a_phalf->m_iRed + (int)a_phalf->m_uiRadius;
894 iRed++)
895 {
896 assert(iRed >= 0 && iRed <= 15);
897
898 for (int iGreen = a_phalf->m_iGreen - (int)a_phalf->m_uiRadius;
899 iGreen <= a_phalf->m_iGreen + (int)a_phalf->m_uiRadius;
900 iGreen++)
901 {
902 assert(iGreen >= 0 && iGreen <= 15);
903
904 for (int iBlue = a_phalf->m_iBlue - (int)a_phalf->m_uiRadius;
905 iBlue <= a_phalf->m_iBlue + (int)a_phalf->m_uiRadius;
906 iBlue++)
907 {
908 assert(iBlue >= 0 && iBlue <= 15);
909
910 IndividualTrys::Try *ptry = &a_phalf->m_atry[a_phalf->m_uiTrys];
911 assert(ptry < &a_phalf->m_atry[IndividualTrys::Half::MAX_TRYS]);
912
913 ptry->m_iRed = iRed;
914 ptry->m_iGreen = iGreen;
915 ptry->m_iBlue = iBlue;
916 ptry->m_fError = FLT_MAX;
917 ColorFloatRGBA frgbaColor = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed, (unsigned char)iGreen, (unsigned char)iBlue);
918
919 // try each CW
920 for (unsigned int uiCW = 0; uiCW < CW_RANGES; uiCW++)
921 {
922 unsigned int auiPixelSelectors[PIXELS / 2];
923 ColorFloatRGBA afrgbaDecodedPixels[PIXELS / 2];
924 float afPixelErrors[PIXELS / 2] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
925 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
926
927 // pre-compute decoded pixels for each selector
928 ColorFloatRGBA afrgbaSelectors[SELECTORS];
929 assert(SELECTORS == 4);
930 afrgbaSelectors[0] = (frgbaColor + s_aafCwTable[uiCW][0]).ClampRGB();
931 afrgbaSelectors[1] = (frgbaColor + s_aafCwTable[uiCW][1]).ClampRGB();
932 afrgbaSelectors[2] = (frgbaColor + s_aafCwTable[uiCW][2]).ClampRGB();
933 afrgbaSelectors[3] = (frgbaColor + s_aafCwTable[uiCW][3]).ClampRGB();
934
935 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
936 {
937 ColorFloatRGBA *pfrgbaSourcePixel = &m_pafrgbaSource[a_phalf->m_pauiPixelMapping[uiPixel]];
938 ColorFloatRGBA frgbaDecodedPixel;
939
940 for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
941 {
942 frgbaDecodedPixel = afrgbaSelectors[uiSelector];
943
944 float fPixelError;
945
946 fPixelError = CalcPixelError(frgbaDecodedPixel, m_afDecodedAlphas[a_phalf->m_pauiPixelMapping[uiPixel]],
947 *pfrgbaSourcePixel);
948
949 if (fPixelError < afPixelErrors[uiPixel])
950 {
951 auiPixelSelectors[uiPixel] = uiSelector;
952 afrgbaDecodedPixels[uiPixel] = frgbaDecodedPixel;
953 afPixelErrors[uiPixel] = fPixelError;
954 }
955
956 }
957 }
958
959 // add up all pixel errors
960 float fCWError = 0.0f;
961 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
962 {
963 fCWError += afPixelErrors[uiPixel];
964 }
965
966 // if best CW so far
967 if (fCWError < ptry->m_fError)
968 {
969 ptry->m_uiCW = uiCW;
970 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
971 {
972 ptry->m_auiSelectors[uiPixel] = auiPixelSelectors[uiPixel];
973 }
974 ptry->m_fError = fCWError;
975 }
976
977 }
978
979 if (ptry->m_fError < fBestTryError)
980 {
981 a_phalf->m_ptryBest = ptry;
982 fBestTryError = ptry->m_fError;
983 }
984
985 assert(ptry->m_fError < FLT_MAX);
986
987 a_phalf->m_uiTrys++;
988 }
989 }
990 }
991
992 }
993
994 // ----------------------------------------------------------------------------------------------------
995 // try version 1 of the degenerate search
996 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
997 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
998 // be successfull
999 //
1000 void Block4x4Encoding_ETC1::TryDegenerates1(void)
1001 {
1002
1003 TryDifferential(m_boolMostLikelyFlip, 1, -2, 0);
1004 TryDifferential(m_boolMostLikelyFlip, 1, 2, 0);
1005 TryDifferential(m_boolMostLikelyFlip, 1, 0, 2);
1006 TryDifferential(m_boolMostLikelyFlip, 1, 0, -2);
1007
1008 }
1009
1010 // ----------------------------------------------------------------------------------------------------
1011 // try version 2 of the degenerate search
1012 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1013 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1014 // be successfull
1015 //
1016 void Block4x4Encoding_ETC1::TryDegenerates2(void)
1017 {
1018
1019 TryDifferential(!m_boolMostLikelyFlip, 1, -2, 0);
1020 TryDifferential(!m_boolMostLikelyFlip, 1, 2, 0);
1021 TryDifferential(!m_boolMostLikelyFlip, 1, 0, 2);
1022 TryDifferential(!m_boolMostLikelyFlip, 1, 0, -2);
1023
1024 }
1025
1026 // ----------------------------------------------------------------------------------------------------
1027 // try version 3 of the degenerate search
1028 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1029 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1030 // be successfull
1031 //
1032 void Block4x4Encoding_ETC1::TryDegenerates3(void)
1033 {
1034
1035 TryDifferential(m_boolMostLikelyFlip, 1, -2, -2);
1036 TryDifferential(m_boolMostLikelyFlip, 1, -2, 2);
1037 TryDifferential(m_boolMostLikelyFlip, 1, 2, -2);
1038 TryDifferential(m_boolMostLikelyFlip, 1, 2, 2);
1039
1040 }
1041
1042 // ----------------------------------------------------------------------------------------------------
1043 // try version 4 of the degenerate search
1044 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1045 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1046 // be successfull
1047 //
1048 void Block4x4Encoding_ETC1::TryDegenerates4(void)
1049 {
1050
1051 TryDifferential(m_boolMostLikelyFlip, 1, -4, 0);
1052 TryDifferential(m_boolMostLikelyFlip, 1, 4, 0);
1053 TryDifferential(m_boolMostLikelyFlip, 1, 0, 4);
1054 TryDifferential(m_boolMostLikelyFlip, 1, 0, -4);
1055
1056 }
1057
1058 // ----------------------------------------------------------------------------------------------------
1059 // find the best selector for each pixel based on a particular basecolor and CW that have been previously set
1060 // calculate the selectors for each half of the block separately
1061 // set the block error as the sum of each half's error
1062 //
1063 void Block4x4Encoding_ETC1::CalculateSelectors()
1064 {
1065 if (m_boolFlip)
1066 {
1067 CalculateHalfOfTheSelectors(0, s_auiTopPixelMapping);
1068 CalculateHalfOfTheSelectors(1, s_auiBottomPixelMapping);
1069 }
1070 else
1071 {
1072 CalculateHalfOfTheSelectors(0, s_auiLeftPixelMapping);
1073 CalculateHalfOfTheSelectors(1, s_auiRightPixelMapping);
1074 }
1075
1076 m_fError = m_fError1 + m_fError2;
1077 }
1078
1079 // ----------------------------------------------------------------------------------------------------
1080 // choose best selectors for half of the block
1081 // calculate the error for half of the block
1082 //
1083 void Block4x4Encoding_ETC1::CalculateHalfOfTheSelectors(unsigned int a_uiHalf,
1084 const unsigned int *pauiPixelMapping)
1085 {
1086 static const bool DEBUG_PRINT = false;
1087
1088 ColorFloatRGBA *pfrgbaColor = a_uiHalf ? &m_frgbaColor2 : &m_frgbaColor1;
1089 unsigned int *puiCW = a_uiHalf ? &m_uiCW2 : &m_uiCW1;
1090
1091 float *pfHalfError = a_uiHalf ? &m_fError2 : &m_fError1;
1092 *pfHalfError = FLT_MAX;
1093
1094 // try each CW
1095 for (unsigned int uiCW = 0; uiCW < CW_RANGES; uiCW++)
1096 {
1097 if (DEBUG_PRINT)
1098 {
1099 printf("\ncw=%u\n", uiCW);
1100 }
1101
1102 unsigned int auiPixelSelectors[PIXELS / 2];
1103 ColorFloatRGBA afrgbaDecodedPixels[PIXELS / 2];
1104 float afPixelErrors[PIXELS / 2] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
1105
1106 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
1107 {
1108 if (DEBUG_PRINT)
1109 {
1110 printf("\tsource [%.2f,%.2f,%.2f]\n", m_pafrgbaSource[pauiPixelMapping[uiPixel]].fR,
1111 m_pafrgbaSource[pauiPixelMapping[uiPixel]].fG, m_pafrgbaSource[pauiPixelMapping[uiPixel]].fB);
1112 }
1113
1114 ColorFloatRGBA *pfrgbaSourcePixel = &m_pafrgbaSource[pauiPixelMapping[uiPixel]];
1115 ColorFloatRGBA frgbaDecodedPixel;
1116
1117 for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
1118 {
1119 float fDeltaRGB = s_aafCwTable[uiCW][uiSelector];
1120
1121 frgbaDecodedPixel = (*pfrgbaColor + fDeltaRGB).ClampRGB();
1122
1123 float fPixelError;
1124
1125 fPixelError = CalcPixelError(frgbaDecodedPixel, m_afDecodedAlphas[pauiPixelMapping[uiPixel]],
1126 *pfrgbaSourcePixel);
1127
1128 if (DEBUG_PRINT)
1129 {
1130 printf("\tpixel %u, index %u [%.2f,%.2f,%.2f], error %.2f", uiPixel, uiSelector,
1131 frgbaDecodedPixel.fR,
1132 frgbaDecodedPixel.fG,
1133 frgbaDecodedPixel.fB,
1134 fPixelError);
1135 }
1136
1137 if (fPixelError < afPixelErrors[uiPixel])
1138 {
1139 if (DEBUG_PRINT)
1140 {
1141 printf(" *");
1142 }
1143
1144 auiPixelSelectors[uiPixel] = uiSelector;
1145 afrgbaDecodedPixels[uiPixel] = frgbaDecodedPixel;
1146 afPixelErrors[uiPixel] = fPixelError;
1147 }
1148
1149 if (DEBUG_PRINT)
1150 {
1151 printf("\n");
1152 }
1153 }
1154 }
1155
1156 // add up all pixel errors
1157 float fCWError = 0.0f;
1158 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
1159 {
1160 fCWError += afPixelErrors[uiPixel];
1161 }
1162 if (DEBUG_PRINT)
1163 {
1164 printf("\terror %.2f\n", fCWError);
1165 }
1166
1167 // if best CW so far
1168 if (fCWError < *pfHalfError)
1169 {
1170 *pfHalfError = fCWError;
1171 *puiCW = uiCW;
1172 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
1173 {
1174 m_auiSelectors[pauiPixelMapping[uiPixel]] = auiPixelSelectors[uiPixel];
1175 m_afrgbaDecodedColors[pauiPixelMapping[uiPixel]] = afrgbaDecodedPixels[uiPixel];
1176 }
1177 }
1178 }
1179
1180 }
1181
1182 // ----------------------------------------------------------------------------------------------------
1183 // set the encoding bits based on encoding state
1184 //
1185 void Block4x4Encoding_ETC1::SetEncodingBits(void)
1186 {
1187 assert(m_mode == MODE_ETC1);
1188
1189 if (m_boolDiff)
1190 {
1191 int iRed1 = m_frgbaColor1.IntRed(31.0f);
1192 int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
1193 int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
1194
1195 int iRed2 = m_frgbaColor2.IntRed(31.0f);
1196 int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
1197 int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
1198
1199 int iDRed2 = iRed2 - iRed1;
1200 int iDGreen2 = iGreen2 - iGreen1;
1201 int iDBlue2 = iBlue2 - iBlue1;
1202
1203 assert(iDRed2 >= -4 && iDRed2 < 4);
1204 assert(iDGreen2 >= -4 && iDGreen2 < 4);
1205 assert(iDBlue2 >= -4 && iDBlue2 < 4);
1206
1207 m_pencodingbitsRGB8->differential.red1 = (unsigned int)iRed1;
1208 m_pencodingbitsRGB8->differential.green1 = (unsigned int)iGreen1;
1209 m_pencodingbitsRGB8->differential.blue1 = (unsigned int)iBlue1;
1210
1211 m_pencodingbitsRGB8->differential.dred2 = iDRed2;
1212 m_pencodingbitsRGB8->differential.dgreen2 = iDGreen2;
1213 m_pencodingbitsRGB8->differential.dblue2 = iDBlue2;
1214 }
1215 else
1216 {
1217 m_pencodingbitsRGB8->individual.red1 = (unsigned int)m_frgbaColor1.IntRed(15.0f);
1218 m_pencodingbitsRGB8->individual.green1 = (unsigned int)m_frgbaColor1.IntGreen(15.0f);
1219 m_pencodingbitsRGB8->individual.blue1 = (unsigned int)m_frgbaColor1.IntBlue(15.0f);
1220
1221 m_pencodingbitsRGB8->individual.red2 = (unsigned int)m_frgbaColor2.IntRed(15.0f);
1222 m_pencodingbitsRGB8->individual.green2 = (unsigned int)m_frgbaColor2.IntGreen(15.0f);
1223 m_pencodingbitsRGB8->individual.blue2 = (unsigned int)m_frgbaColor2.IntBlue(15.0f);
1224 }
1225
1226 m_pencodingbitsRGB8->individual.cw1 = m_uiCW1;
1227 m_pencodingbitsRGB8->individual.cw2 = m_uiCW2;
1228
1229 SetEncodingBits_Selectors();
1230
1231 m_pencodingbitsRGB8->individual.diff = (unsigned int)m_boolDiff;
1232 m_pencodingbitsRGB8->individual.flip = (unsigned int)m_boolFlip;
1233
1234 }
1235
1236 // ----------------------------------------------------------------------------------------------------
1237 // set the selectors in the encoding bits
1238 //
1239 void Block4x4Encoding_ETC1::SetEncodingBits_Selectors(void)
1240 {
1241
1242 m_pencodingbitsRGB8->individual.selectors = 0;
1243 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1244 {
1245 unsigned int uiSelector = m_auiSelectors[uiPixel];
1246
1247 // set index msb
1248 m_pencodingbitsRGB8->individual.selectors |= (uiSelector >> 1) << (uiPixel ^ 8);
1249
1250 // set index lsb
1251 m_pencodingbitsRGB8->individual.selectors |= (uiSelector & 1) << ((16 + uiPixel) ^ 8);
1252 }
1253
1254 }
1255
1256 // ----------------------------------------------------------------------------------------------------
1257 // set the decoded colors and decoded alpha based on the encoding state
1258 //
1259 void Block4x4Encoding_ETC1::Decode(void)
1260 {
1261
1262 const unsigned int *pauiPixelOrder = m_boolFlip ? s_auiPixelOrderFlip1 : s_auiPixelOrderFlip0;
1263
1264 for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS; uiPixelOrder++)
1265 {
1266 ColorFloatRGBA *pfrgbaCenter = uiPixelOrder < 8 ? &m_frgbaColor1 : &m_frgbaColor2;
1267 unsigned int uiCW = uiPixelOrder < 8 ? m_uiCW1 : m_uiCW2;
1268
1269 unsigned int uiPixel = pauiPixelOrder[uiPixelOrder];
1270
1271 float fDelta = s_aafCwTable[uiCW][m_auiSelectors[uiPixel]];
1272 m_afrgbaDecodedColors[uiPixel] = (*pfrgbaCenter + fDelta).ClampRGB();
1273 m_afDecodedAlphas[uiPixel] = 1.0f;
1274 }
1275
1276 }
1277
1278 // ----------------------------------------------------------------------------------------------------
1279 //
1280
1281 } // namespace Etc