Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4Encoding_RGB8A1.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_RGB8A1.cpp contains:
19 Block4x4Encoding_RGB8A1
20 Block4x4Encoding_RGB8A1_Opaque
21 Block4x4Encoding_RGB8A1_Transparent
22
23 These encoders are used when targetting file format RGB8A1.
24
25 Block4x4Encoding_RGB8A1_Opaque is used when all pixels in the 4x4 block are opaque
26 Block4x4Encoding_RGB8A1_Transparent is used when all pixels in the 4x4 block are transparent
27 Block4x4Encoding_RGB8A1 is used when there is a mixture of alphas in the 4x4 block
28
29 */
30
31 #include "EtcConfig.h"
32 #include "EtcBlock4x4Encoding_RGB8A1.h"
33
34 #include "EtcBlock4x4.h"
35 #include "EtcBlock4x4EncodingBits.h"
36 #include "EtcBlock4x4Encoding_RGB8.h"
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <assert.h>
41
42 namespace Etc
43 {
44
45 // ####################################################################################################
46 // Block4x4Encoding_RGB8A1
47 // ####################################################################################################
48
49 float Block4x4Encoding_RGB8A1::s_aafCwOpaqueUnsetTable[CW_RANGES][SELECTORS] =
50 {
51 { 0.0f / 255.0f, 8.0f / 255.0f, 0.0f / 255.0f, -8.0f / 255.0f },
52 { 0.0f / 255.0f, 17.0f / 255.0f, 0.0f / 255.0f, -17.0f / 255.0f },
53 { 0.0f / 255.0f, 29.0f / 255.0f, 0.0f / 255.0f, -29.0f / 255.0f },
54 { 0.0f / 255.0f, 42.0f / 255.0f, 0.0f / 255.0f, -42.0f / 255.0f },
55 { 0.0f / 255.0f, 60.0f / 255.0f, 0.0f / 255.0f, -60.0f / 255.0f },
56 { 0.0f / 255.0f, 80.0f / 255.0f, 0.0f / 255.0f, -80.0f / 255.0f },
57 { 0.0f / 255.0f, 106.0f / 255.0f, 0.0f / 255.0f, -106.0f / 255.0f },
58 { 0.0f / 255.0f, 183.0f / 255.0f, 0.0f / 255.0f, -183.0f / 255.0f }
59 };
60
61 // ----------------------------------------------------------------------------------------------------
62 //
63 Block4x4Encoding_RGB8A1::Block4x4Encoding_RGB8A1(void)
64 {
65 m_pencodingbitsRGB8 = nullptr;
66 m_boolOpaque = false;
67 m_boolTransparent = false;
68 m_boolPunchThroughPixels = true;
69
70 }
71 Block4x4Encoding_RGB8A1::~Block4x4Encoding_RGB8A1(void) {}
72 // ----------------------------------------------------------------------------------------------------
73 // initialization prior to encoding
74 // a_pblockParent points to the block associated with this encoding
75 // a_errormetric is used to choose the best encoding
76 // a_pafrgbaSource points to a 4x4 block subset of the source image
77 // a_paucEncodingBits points to the final encoding bits
78 //
79 void Block4x4Encoding_RGB8A1::InitFromSource(Block4x4 *a_pblockParent,
80 ColorFloatRGBA *a_pafrgbaSource,
81 unsigned char *a_paucEncodingBits,
82 ErrorMetric a_errormetric)
83 {
84
85 Block4x4Encoding_RGB8::InitFromSource(a_pblockParent,
86 a_pafrgbaSource,
87 a_paucEncodingBits,
88 a_errormetric);
89
90 m_boolOpaque = a_pblockParent->GetSourceAlphaMix() == Block4x4::SourceAlphaMix::OPAQUE;
91 m_boolTransparent = a_pblockParent->GetSourceAlphaMix() == Block4x4::SourceAlphaMix::TRANSPARENT;
92 m_boolPunchThroughPixels = a_pblockParent->HasPunchThroughPixels();
93
94 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
95 {
96 if (m_pafrgbaSource[uiPixel].fA >= 0.5f)
97 {
98 m_afDecodedAlphas[uiPixel] = 1.0f;
99 }
100 else
101 {
102 m_afDecodedAlphas[uiPixel] = 0.0f;
103 }
104 }
105
106 }
107
108 // ----------------------------------------------------------------------------------------------------
109 // initialization from the encoding bits of a previous encoding
110 // a_pblockParent points to the block associated with this encoding
111 // a_errormetric is used to choose the best encoding
112 // a_pafrgbaSource points to a 4x4 block subset of the source image
113 // a_paucEncodingBits points to the final encoding bits of a previous encoding
114 //
115 void Block4x4Encoding_RGB8A1::InitFromEncodingBits(Block4x4 *a_pblockParent,
116 unsigned char *a_paucEncodingBits,
117 ColorFloatRGBA *a_pafrgbaSource,
118 ErrorMetric a_errormetric)
119 {
120
121
122 InitFromEncodingBits_ETC1(a_pblockParent,
123 a_paucEncodingBits,
124 a_pafrgbaSource,
125 a_errormetric);
126
127 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)a_paucEncodingBits;
128
129 // detect if there is a T, H or Planar mode present
130 int iRed1 = m_pencodingbitsRGB8->differential.red1;
131 int iDRed2 = m_pencodingbitsRGB8->differential.dred2;
132 int iRed2 = iRed1 + iDRed2;
133
134 int iGreen1 = m_pencodingbitsRGB8->differential.green1;
135 int iDGreen2 = m_pencodingbitsRGB8->differential.dgreen2;
136 int iGreen2 = iGreen1 + iDGreen2;
137
138 int iBlue1 = m_pencodingbitsRGB8->differential.blue1;
139 int iDBlue2 = m_pencodingbitsRGB8->differential.dblue2;
140 int iBlue2 = iBlue1 + iDBlue2;
141
142 if (iRed2 < 0 || iRed2 > 31)
143 {
144 InitFromEncodingBits_T();
145 }
146 else if (iGreen2 < 0 || iGreen2 > 31)
147 {
148 InitFromEncodingBits_H();
149 }
150 else if (iBlue2 < 0 || iBlue2 > 31)
151 {
152 Block4x4Encoding_RGB8::InitFromEncodingBits_Planar();
153 }
154
155 }
156
157 // ----------------------------------------------------------------------------------------------------
158 // initialization from the encoding bits of a previous encoding assuming the encoding is an ETC1 mode.
159 // if it isn't an ETC1 mode, this will be overwritten later
160 //
161 void Block4x4Encoding_RGB8A1::InitFromEncodingBits_ETC1(Block4x4 *a_pblockParent,
162 unsigned char *a_paucEncodingBits,
163 ColorFloatRGBA *a_pafrgbaSource,
164 ErrorMetric a_errormetric)
165 {
166 Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,
167 a_errormetric);
168
169 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)a_paucEncodingBits;
170
171 m_mode = MODE_ETC1;
172 m_boolDiff = true;
173 m_boolFlip = m_pencodingbitsRGB8->differential.flip;
174 m_boolOpaque = m_pencodingbitsRGB8->differential.diff;
175
176 int iR2 = m_pencodingbitsRGB8->differential.red1 + m_pencodingbitsRGB8->differential.dred2;
177 if (iR2 < 0)
178 {
179 iR2 = 0;
180 }
181 else if (iR2 > 31)
182 {
183 iR2 = 31;
184 }
185
186 int iG2 = m_pencodingbitsRGB8->differential.green1 + m_pencodingbitsRGB8->differential.dgreen2;
187 if (iG2 < 0)
188 {
189 iG2 = 0;
190 }
191 else if (iG2 > 31)
192 {
193 iG2 = 31;
194 }
195
196 int iB2 = m_pencodingbitsRGB8->differential.blue1 + m_pencodingbitsRGB8->differential.dblue2;
197 if (iB2 < 0)
198 {
199 iB2 = 0;
200 }
201 else if (iB2 > 31)
202 {
203 iB2 = 31;
204 }
205
206 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5(m_pencodingbitsRGB8->differential.red1, m_pencodingbitsRGB8->differential.green1, m_pencodingbitsRGB8->differential.blue1);
207 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iR2, (unsigned char)iG2, (unsigned char)iB2);
208
209 m_uiCW1 = m_pencodingbitsRGB8->differential.cw1;
210 m_uiCW2 = m_pencodingbitsRGB8->differential.cw2;
211
212 Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
213
214 Decode_ETC1();
215
216 CalcBlockError();
217
218 }
219
220 // ----------------------------------------------------------------------------------------------------
221 // initialization from the encoding bits of a previous encoding if T mode is detected
222 //
223 void Block4x4Encoding_RGB8A1::InitFromEncodingBits_T(void)
224 {
225 m_mode = MODE_T;
226
227 unsigned char ucRed1 = (unsigned char)((m_pencodingbitsRGB8->t.red1a << 2) +
228 m_pencodingbitsRGB8->t.red1b);
229 unsigned char ucGreen1 = m_pencodingbitsRGB8->t.green1;
230 unsigned char ucBlue1 = m_pencodingbitsRGB8->t.blue1;
231
232 unsigned char ucRed2 = m_pencodingbitsRGB8->t.red2;
233 unsigned char ucGreen2 = m_pencodingbitsRGB8->t.green2;
234 unsigned char ucBlue2 = m_pencodingbitsRGB8->t.blue2;
235
236 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4(ucRed1, ucGreen1, ucBlue1);
237 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4(ucRed2, ucGreen2, ucBlue2);
238
239 m_uiCW1 = (m_pencodingbitsRGB8->t.da << 1) + m_pencodingbitsRGB8->t.db;
240
241 Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
242
243 DecodePixels_T();
244
245 CalcBlockError();
246 }
247
248 // ----------------------------------------------------------------------------------------------------
249 // initialization from the encoding bits of a previous encoding if H mode is detected
250 //
251 void Block4x4Encoding_RGB8A1::InitFromEncodingBits_H(void)
252 {
253 m_mode = MODE_H;
254
255 unsigned char ucRed1 = m_pencodingbitsRGB8->h.red1;
256 unsigned char ucGreen1 = (unsigned char)((m_pencodingbitsRGB8->h.green1a << 1) +
257 m_pencodingbitsRGB8->h.green1b);
258 unsigned char ucBlue1 = (unsigned char)((m_pencodingbitsRGB8->h.blue1a << 3) +
259 (m_pencodingbitsRGB8->h.blue1b << 1) +
260 m_pencodingbitsRGB8->h.blue1c);
261
262 unsigned char ucRed2 = m_pencodingbitsRGB8->h.red2;
263 unsigned char ucGreen2 = (unsigned char)((m_pencodingbitsRGB8->h.green2a << 1) +
264 m_pencodingbitsRGB8->h.green2b);
265 unsigned char ucBlue2 = m_pencodingbitsRGB8->h.blue2;
266
267 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4(ucRed1, ucGreen1, ucBlue1);
268 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4(ucRed2, ucGreen2, ucBlue2);
269
270 // used to determine the LSB of the CW
271 unsigned int uiRGB1 = (unsigned int)(((int)ucRed1 << 16) + ((int)ucGreen1 << 8) + (int)ucBlue1);
272 unsigned int uiRGB2 = (unsigned int)(((int)ucRed2 << 16) + ((int)ucGreen2 << 8) + (int)ucBlue2);
273
274 m_uiCW1 = (m_pencodingbitsRGB8->h.da << 2) + (m_pencodingbitsRGB8->h.db << 1);
275 if (uiRGB1 >= uiRGB2)
276 {
277 m_uiCW1++;
278 }
279
280 Block4x4Encoding_ETC1::InitFromEncodingBits_Selectors();
281
282 DecodePixels_H();
283
284 CalcBlockError();
285 }
286
287 // ----------------------------------------------------------------------------------------------------
288 // for ETC1 modes, set the decoded colors and decoded alpha based on the encoding state
289 //
290 void Block4x4Encoding_RGB8A1::Decode_ETC1(void)
291 {
292
293 const unsigned int *pauiPixelOrder = m_boolFlip ? s_auiPixelOrderFlip1 : s_auiPixelOrderFlip0;
294
295 for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS; uiPixelOrder++)
296 {
297 ColorFloatRGBA *pfrgbaCenter = uiPixelOrder < 8 ? &m_frgbaColor1 : &m_frgbaColor2;
298 unsigned int uiCW = uiPixelOrder < 8 ? m_uiCW1 : m_uiCW2;
299
300 unsigned int uiPixel = pauiPixelOrder[uiPixelOrder];
301
302 float fDelta;
303 if (m_boolOpaque)
304 fDelta = Block4x4Encoding_ETC1::s_aafCwTable[uiCW][m_auiSelectors[uiPixel]];
305 else
306 fDelta = s_aafCwOpaqueUnsetTable[uiCW][m_auiSelectors[uiPixel]];
307
308 if (m_boolOpaque == false && m_auiSelectors[uiPixel] == TRANSPARENT_SELECTOR)
309 {
310 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
311 m_afDecodedAlphas[uiPixel] = 0.0f;
312 }
313 else
314 {
315 m_afrgbaDecodedColors[uiPixel] = (*pfrgbaCenter + fDelta).ClampRGB();
316 m_afDecodedAlphas[uiPixel] = 1.0f;
317 }
318 }
319
320 }
321
322 // ----------------------------------------------------------------------------------------------------
323 // for T mode, set the decoded colors and decoded alpha based on the encoding state
324 //
325 void Block4x4Encoding_RGB8A1::DecodePixels_T(void)
326 {
327
328 float fDistance = s_afTHDistanceTable[m_uiCW1];
329 ColorFloatRGBA frgbaDistance(fDistance, fDistance, fDistance, 0.0f);
330
331 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
332 {
333 switch (m_auiSelectors[uiPixel])
334 {
335 case 0:
336 m_afrgbaDecodedColors[uiPixel] = m_frgbaColor1;
337 m_afDecodedAlphas[uiPixel] = 1.0f;
338 break;
339
340 case 1:
341 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 + frgbaDistance).ClampRGB();
342 m_afDecodedAlphas[uiPixel] = 1.0f;
343 break;
344
345 case 2:
346 if (m_boolOpaque == false)
347 {
348 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
349 m_afDecodedAlphas[uiPixel] = 0.0f;
350 }
351 else
352 {
353 m_afrgbaDecodedColors[uiPixel] = m_frgbaColor2;
354 m_afDecodedAlphas[uiPixel] = 1.0f;
355 }
356 break;
357
358 case 3:
359 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 - frgbaDistance).ClampRGB();
360 m_afDecodedAlphas[uiPixel] = 1.0f;
361 break;
362 }
363
364 }
365
366 }
367
368 // ----------------------------------------------------------------------------------------------------
369 // for H mode, set the decoded colors and decoded alpha based on the encoding state
370 //
371 void Block4x4Encoding_RGB8A1::DecodePixels_H(void)
372 {
373
374 float fDistance = s_afTHDistanceTable[m_uiCW1];
375 ColorFloatRGBA frgbaDistance(fDistance, fDistance, fDistance, 0.0f);
376
377 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
378 {
379 switch (m_auiSelectors[uiPixel])
380 {
381 case 0:
382 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor1 + frgbaDistance).ClampRGB();
383 m_afDecodedAlphas[uiPixel] = 1.0f;
384 break;
385
386 case 1:
387 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor1 - frgbaDistance).ClampRGB();
388 m_afDecodedAlphas[uiPixel] = 1.0f;
389 break;
390
391 case 2:
392 if (m_boolOpaque == false)
393 {
394 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
395 m_afDecodedAlphas[uiPixel] = 0.0f;
396 }
397 else
398 {
399 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 + frgbaDistance).ClampRGB();
400 m_afDecodedAlphas[uiPixel] = 1.0f;
401 }
402 break;
403
404 case 3:
405 m_afrgbaDecodedColors[uiPixel] = (m_frgbaColor2 - frgbaDistance).ClampRGB();
406 m_afDecodedAlphas[uiPixel] = 1.0f;
407 break;
408 }
409
410 }
411
412 }
413
414
415 // ----------------------------------------------------------------------------------------------------
416 // perform a single encoding iteration
417 // replace the encoding if a better encoding was found
418 // subsequent iterations generally take longer for each iteration
419 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
420 //
421 // RGB8A1 can't use individual mode
422 // RGB8A1 with transparent pixels can't use planar mode
423 //
424 void Block4x4Encoding_RGB8A1::PerformIteration(float a_fEffort)
425 {
426 assert(!m_boolOpaque);
427 assert(!m_boolTransparent);
428 assert(!m_boolDone);
429
430 switch (m_uiEncodingIterations)
431 {
432 case 0:
433 PerformFirstIteration();
434 break;
435
436 case 1:
437 TryDifferential(m_boolMostLikelyFlip, 1, 0, 0);
438 break;
439
440 case 2:
441 TryDifferential(!m_boolMostLikelyFlip, 1, 0, 0);
442 if (a_fEffort <= 39.5f)
443 {
444 m_boolDone = true;
445 }
446 break;
447
448 case 3:
449 Block4x4Encoding_RGB8::CalculateBaseColorsForTAndH();
450 TryT(1);
451 TryH(1);
452 if (a_fEffort <= 49.5f)
453 {
454 m_boolDone = true;
455 }
456 break;
457
458 case 4:
459 TryDegenerates1();
460 if (a_fEffort <= 59.5f)
461 {
462 m_boolDone = true;
463 }
464 break;
465
466 case 5:
467 TryDegenerates2();
468 if (a_fEffort <= 69.5f)
469 {
470 m_boolDone = true;
471 }
472 break;
473
474 case 6:
475 TryDegenerates3();
476 if (a_fEffort <= 79.5f)
477 {
478 m_boolDone = true;
479 }
480 break;
481
482 case 7:
483 TryDegenerates4();
484 m_boolDone = true;
485 break;
486
487 default:
488 assert(0);
489 break;
490 }
491
492 m_uiEncodingIterations++;
493
494 SetDoneIfPerfect();
495
496 }
497
498 // ----------------------------------------------------------------------------------------------------
499 // find best initial encoding to ensure block has a valid encoding
500 //
501 void Block4x4Encoding_RGB8A1::PerformFirstIteration(void)
502 {
503 Block4x4Encoding_ETC1::CalculateMostLikelyFlip();
504
505 m_fError = FLT_MAX;
506
507 TryDifferential(m_boolMostLikelyFlip, 0, 0, 0);
508 SetDoneIfPerfect();
509 if (m_boolDone)
510 {
511 return;
512 }
513 TryDifferential(!m_boolMostLikelyFlip, 0, 0, 0);
514 SetDoneIfPerfect();
515
516 }
517
518 // ----------------------------------------------------------------------------------------------------
519 // mostly copied from ETC1
520 // differences:
521 // Block4x4Encoding_RGB8A1 encodingTry = *this;
522 //
523 void Block4x4Encoding_RGB8A1::TryDifferential(bool a_boolFlip, unsigned int a_uiRadius,
524 int a_iGrayOffset1, int a_iGrayOffset2)
525 {
526
527 ColorFloatRGBA frgbaColor1;
528 ColorFloatRGBA frgbaColor2;
529
530 const unsigned int *pauiPixelMapping1;
531 const unsigned int *pauiPixelMapping2;
532
533 if (a_boolFlip)
534 {
535 frgbaColor1 = m_frgbaSourceAverageTop;
536 frgbaColor2 = m_frgbaSourceAverageBottom;
537
538 pauiPixelMapping1 = s_auiTopPixelMapping;
539 pauiPixelMapping2 = s_auiBottomPixelMapping;
540 }
541 else
542 {
543 frgbaColor1 = m_frgbaSourceAverageLeft;
544 frgbaColor2 = m_frgbaSourceAverageRight;
545
546 pauiPixelMapping1 = s_auiLeftPixelMapping;
547 pauiPixelMapping2 = s_auiRightPixelMapping;
548 }
549
550 DifferentialTrys trys(frgbaColor1, frgbaColor2, pauiPixelMapping1, pauiPixelMapping2,
551 a_uiRadius, a_iGrayOffset1, a_iGrayOffset2);
552
553 Block4x4Encoding_RGB8A1 encodingTry = *this;
554 encodingTry.m_boolFlip = a_boolFlip;
555
556 encodingTry.TryDifferentialHalf(&trys.m_half1);
557 encodingTry.TryDifferentialHalf(&trys.m_half2);
558
559 // find best halves that are within differential range
560 DifferentialTrys::Try *ptryBest1 = nullptr;
561 DifferentialTrys::Try *ptryBest2 = nullptr;
562 encodingTry.m_fError = FLT_MAX;
563
564 // see if the best of each half are in differential range
565 int iDRed = trys.m_half2.m_ptryBest->m_iRed - trys.m_half1.m_ptryBest->m_iRed;
566 int iDGreen = trys.m_half2.m_ptryBest->m_iGreen - trys.m_half1.m_ptryBest->m_iGreen;
567 int iDBlue = trys.m_half2.m_ptryBest->m_iBlue - trys.m_half1.m_ptryBest->m_iBlue;
568 if (iDRed >= -4 && iDRed <= 3 && iDGreen >= -4 && iDGreen <= 3 && iDBlue >= -4 && iDBlue <= 3)
569 {
570 ptryBest1 = trys.m_half1.m_ptryBest;
571 ptryBest2 = trys.m_half2.m_ptryBest;
572 encodingTry.m_fError = trys.m_half1.m_ptryBest->m_fError + trys.m_half2.m_ptryBest->m_fError;
573 }
574 else
575 {
576 // else, find the next best halves that are in differential range
577 for (DifferentialTrys::Try *ptry1 = &trys.m_half1.m_atry[0];
578 ptry1 < &trys.m_half1.m_atry[trys.m_half1.m_uiTrys];
579 ptry1++)
580 {
581 for (DifferentialTrys::Try *ptry2 = &trys.m_half2.m_atry[0];
582 ptry2 < &trys.m_half2.m_atry[trys.m_half2.m_uiTrys];
583 ptry2++)
584 {
585 iDRed = ptry2->m_iRed - ptry1->m_iRed;
586 bool boolValidRedDelta = iDRed <= 3 && iDRed >= -4;
587 iDGreen = ptry2->m_iGreen - ptry1->m_iGreen;
588 bool boolValidGreenDelta = iDGreen <= 3 && iDGreen >= -4;
589 iDBlue = ptry2->m_iBlue - ptry1->m_iBlue;
590 bool boolValidBlueDelta = iDBlue <= 3 && iDBlue >= -4;
591
592 if (boolValidRedDelta && boolValidGreenDelta && boolValidBlueDelta)
593 {
594 float fError = ptry1->m_fError + ptry2->m_fError;
595
596 if (fError < encodingTry.m_fError)
597 {
598 encodingTry.m_fError = fError;
599
600 ptryBest1 = ptry1;
601 ptryBest2 = ptry2;
602 }
603 }
604
605 }
606 }
607 assert(encodingTry.m_fError < FLT_MAX);
608 assert(ptryBest1 != nullptr);
609 assert(ptryBest2 != nullptr);
610 }
611
612 if (encodingTry.m_fError < m_fError)
613 {
614 m_mode = MODE_ETC1;
615 m_boolDiff = true;
616 m_boolFlip = encodingTry.m_boolFlip;
617 m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest1->m_iRed, (unsigned char)ptryBest1->m_iGreen, (unsigned char)ptryBest1->m_iBlue);
618 m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB5((unsigned char)ptryBest2->m_iRed, (unsigned char)ptryBest2->m_iGreen, (unsigned char)ptryBest2->m_iBlue);
619 m_uiCW1 = ptryBest1->m_uiCW;
620 m_uiCW2 = ptryBest2->m_uiCW;
621
622 m_fError = 0.0f;
623 for (unsigned int uiPixelOrder = 0; uiPixelOrder < PIXELS / 2; uiPixelOrder++)
624 {
625 unsigned int uiPixel1 = pauiPixelMapping1[uiPixelOrder];
626 unsigned int uiPixel2 = pauiPixelMapping2[uiPixelOrder];
627
628 unsigned int uiSelector1 = ptryBest1->m_auiSelectors[uiPixelOrder];
629 unsigned int uiSelector2 = ptryBest2->m_auiSelectors[uiPixelOrder];
630
631 m_auiSelectors[uiPixel1] = uiSelector1;
632 m_auiSelectors[uiPixel2] = ptryBest2->m_auiSelectors[uiPixelOrder];
633
634 if (uiSelector1 == TRANSPARENT_SELECTOR)
635 {
636 m_afrgbaDecodedColors[uiPixel1] = ColorFloatRGBA();
637 m_afDecodedAlphas[uiPixel1] = 0.0f;
638 }
639 else
640 {
641 float fDeltaRGB1 = s_aafCwOpaqueUnsetTable[m_uiCW1][uiSelector1];
642 m_afrgbaDecodedColors[uiPixel1] = (m_frgbaColor1 + fDeltaRGB1).ClampRGB();
643 m_afDecodedAlphas[uiPixel1] = 1.0f;
644 }
645
646 if (uiSelector2 == TRANSPARENT_SELECTOR)
647 {
648 m_afrgbaDecodedColors[uiPixel2] = ColorFloatRGBA();
649 m_afDecodedAlphas[uiPixel2] = 0.0f;
650 }
651 else
652 {
653 float fDeltaRGB2 = s_aafCwOpaqueUnsetTable[m_uiCW2][uiSelector2];
654 m_afrgbaDecodedColors[uiPixel2] = (m_frgbaColor2 + fDeltaRGB2).ClampRGB();
655 m_afDecodedAlphas[uiPixel2] = 1.0f;
656 }
657
658 float fDeltaA1 = m_afDecodedAlphas[uiPixel1] - m_pafrgbaSource[uiPixel1].fA;
659 m_fError += fDeltaA1 * fDeltaA1;
660 float fDeltaA2 = m_afDecodedAlphas[uiPixel2] - m_pafrgbaSource[uiPixel2].fA;
661 m_fError += fDeltaA2 * fDeltaA2;
662 }
663
664 m_fError1 = ptryBest1->m_fError;
665 m_fError2 = ptryBest2->m_fError;
666 m_boolSeverelyBentDifferentialColors = trys.m_boolSeverelyBentColors;
667 m_fError = m_fError1 + m_fError2;
668
669 // sanity check
670 {
671 int iRed1 = m_frgbaColor1.IntRed(31.0f);
672 int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
673 int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
674
675 int iRed2 = m_frgbaColor2.IntRed(31.0f);
676 int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
677 int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
678
679 iDRed = iRed2 - iRed1;
680 iDGreen = iGreen2 - iGreen1;
681 iDBlue = iBlue2 - iBlue1;
682
683 assert(iDRed >= -4 && iDRed < 4);
684 assert(iDGreen >= -4 && iDGreen < 4);
685 assert(iDBlue >= -4 && iDBlue < 4);
686 }
687 }
688
689 }
690
691 // ----------------------------------------------------------------------------------------------------
692 // mostly copied from ETC1
693 // differences:
694 // uses s_aafCwOpaqueUnsetTable
695 // color for selector set to 0,0,0,0
696 //
697 void Block4x4Encoding_RGB8A1::TryDifferentialHalf(DifferentialTrys::Half *a_phalf)
698 {
699
700 a_phalf->m_ptryBest = nullptr;
701 float fBestTryError = FLT_MAX;
702
703 a_phalf->m_uiTrys = 0;
704 for (int iRed = a_phalf->m_iRed - (int)a_phalf->m_uiRadius;
705 iRed <= a_phalf->m_iRed + (int)a_phalf->m_uiRadius;
706 iRed++)
707 {
708 assert(iRed >= 0 && iRed <= 31);
709
710 for (int iGreen = a_phalf->m_iGreen - (int)a_phalf->m_uiRadius;
711 iGreen <= a_phalf->m_iGreen + (int)a_phalf->m_uiRadius;
712 iGreen++)
713 {
714 assert(iGreen >= 0 && iGreen <= 31);
715
716 for (int iBlue = a_phalf->m_iBlue - (int)a_phalf->m_uiRadius;
717 iBlue <= a_phalf->m_iBlue + (int)a_phalf->m_uiRadius;
718 iBlue++)
719 {
720 assert(iBlue >= 0 && iBlue <= 31);
721
722 DifferentialTrys::Try *ptry = &a_phalf->m_atry[a_phalf->m_uiTrys];
723 assert(ptry < &a_phalf->m_atry[DifferentialTrys::Half::MAX_TRYS]);
724
725 ptry->m_iRed = iRed;
726 ptry->m_iGreen = iGreen;
727 ptry->m_iBlue = iBlue;
728 ptry->m_fError = FLT_MAX;
729 ColorFloatRGBA frgbaColor = ColorFloatRGBA::ConvertFromRGB5((unsigned char)iRed, (unsigned char)iGreen, (unsigned char)iBlue);
730
731 // try each CW
732 for (unsigned int uiCW = 0; uiCW < CW_RANGES; uiCW++)
733 {
734 unsigned int auiPixelSelectors[PIXELS / 2];
735 ColorFloatRGBA afrgbaDecodedColors[PIXELS / 2];
736 float afPixelErrors[PIXELS / 2] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
737 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
738
739 // pre-compute decoded pixels for each selector
740 ColorFloatRGBA afrgbaSelectors[SELECTORS];
741 assert(SELECTORS == 4);
742 afrgbaSelectors[0] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][0]).ClampRGB();
743 afrgbaSelectors[1] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][1]).ClampRGB();
744 afrgbaSelectors[2] = ColorFloatRGBA();
745 afrgbaSelectors[3] = (frgbaColor + s_aafCwOpaqueUnsetTable[uiCW][3]).ClampRGB();
746
747 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
748 {
749 ColorFloatRGBA *pfrgbaSourcePixel = &m_pafrgbaSource[a_phalf->m_pauiPixelMapping[uiPixel]];
750 ColorFloatRGBA frgbaDecodedPixel;
751
752 for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
753 {
754 if (pfrgbaSourcePixel->fA < 0.5f)
755 {
756 uiSelector = TRANSPARENT_SELECTOR;
757 }
758 else if (uiSelector == TRANSPARENT_SELECTOR)
759 {
760 continue;
761 }
762
763 frgbaDecodedPixel = afrgbaSelectors[uiSelector];
764
765 float fPixelError;
766
767 fPixelError = CalcPixelError(frgbaDecodedPixel, m_afDecodedAlphas[a_phalf->m_pauiPixelMapping[uiPixel]],
768 *pfrgbaSourcePixel);
769
770 if (fPixelError < afPixelErrors[uiPixel])
771 {
772 auiPixelSelectors[uiPixel] = uiSelector;
773 afrgbaDecodedColors[uiPixel] = frgbaDecodedPixel;
774 afPixelErrors[uiPixel] = fPixelError;
775 }
776
777 if (uiSelector == TRANSPARENT_SELECTOR)
778 {
779 break;
780 }
781 }
782 }
783
784 // add up all pixel errors
785 float fCWError = 0.0f;
786 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
787 {
788 fCWError += afPixelErrors[uiPixel];
789 }
790
791 // if best CW so far
792 if (fCWError < ptry->m_fError)
793 {
794 ptry->m_uiCW = uiCW;
795 for (unsigned int uiPixel = 0; uiPixel < 8; uiPixel++)
796 {
797 ptry->m_auiSelectors[uiPixel] = auiPixelSelectors[uiPixel];
798 }
799 ptry->m_fError = fCWError;
800 }
801
802 }
803
804 if (ptry->m_fError < fBestTryError)
805 {
806 a_phalf->m_ptryBest = ptry;
807 fBestTryError = ptry->m_fError;
808 }
809
810 assert(ptry->m_fError < FLT_MAX);
811
812 a_phalf->m_uiTrys++;
813 }
814 }
815 }
816
817 }
818
819 // ----------------------------------------------------------------------------------------------------
820 // try encoding in T mode
821 // save this encoding if it improves the error
822 //
823 // since pixels that use base color1 don't use the distance table, color1 and color2 can be twiddled independently
824 // better encoding can be found if TWIDDLE_RADIUS is set to 2, but it will be much slower
825 //
826 void Block4x4Encoding_RGB8A1::TryT(unsigned int a_uiRadius)
827 {
828 Block4x4Encoding_RGB8A1 encodingTry = *this;
829
830 // init "try"
831 {
832 encodingTry.m_mode = MODE_T;
833 encodingTry.m_boolDiff = true;
834 encodingTry.m_boolFlip = false;
835 encodingTry.m_fError = FLT_MAX;
836 }
837
838 int iColor1Red = m_frgbaOriginalColor1_TAndH.IntRed(15.0f);
839 int iColor1Green = m_frgbaOriginalColor1_TAndH.IntGreen(15.0f);
840 int iColor1Blue = m_frgbaOriginalColor1_TAndH.IntBlue(15.0f);
841
842 int iMinRed1 = iColor1Red - (int)a_uiRadius;
843 if (iMinRed1 < 0)
844 {
845 iMinRed1 = 0;
846 }
847 int iMaxRed1 = iColor1Red + (int)a_uiRadius;
848 if (iMaxRed1 > 15)
849 {
850 iMinRed1 = 15;
851 }
852
853 int iMinGreen1 = iColor1Green - (int)a_uiRadius;
854 if (iMinGreen1 < 0)
855 {
856 iMinGreen1 = 0;
857 }
858 int iMaxGreen1 = iColor1Green + (int)a_uiRadius;
859 if (iMaxGreen1 > 15)
860 {
861 iMinGreen1 = 15;
862 }
863
864 int iMinBlue1 = iColor1Blue - (int)a_uiRadius;
865 if (iMinBlue1 < 0)
866 {
867 iMinBlue1 = 0;
868 }
869 int iMaxBlue1 = iColor1Blue + (int)a_uiRadius;
870 if (iMaxBlue1 > 15)
871 {
872 iMinBlue1 = 15;
873 }
874
875 int iColor2Red = m_frgbaOriginalColor2_TAndH.IntRed(15.0f);
876 int iColor2Green = m_frgbaOriginalColor2_TAndH.IntGreen(15.0f);
877 int iColor2Blue = m_frgbaOriginalColor2_TAndH.IntBlue(15.0f);
878
879 int iMinRed2 = iColor2Red - (int)a_uiRadius;
880 if (iMinRed2 < 0)
881 {
882 iMinRed2 = 0;
883 }
884 int iMaxRed2 = iColor2Red + (int)a_uiRadius;
885 if (iMaxRed2 > 15)
886 {
887 iMinRed2 = 15;
888 }
889
890 int iMinGreen2 = iColor2Green - (int)a_uiRadius;
891 if (iMinGreen2 < 0)
892 {
893 iMinGreen2 = 0;
894 }
895 int iMaxGreen2 = iColor2Green + (int)a_uiRadius;
896 if (iMaxGreen2 > 15)
897 {
898 iMinGreen2 = 15;
899 }
900
901 int iMinBlue2 = iColor2Blue - (int)a_uiRadius;
902 if (iMinBlue2 < 0)
903 {
904 iMinBlue2 = 0;
905 }
906 int iMaxBlue2 = iColor2Blue + (int)a_uiRadius;
907 if (iMaxBlue2 > 15)
908 {
909 iMinBlue2 = 15;
910 }
911
912 for (unsigned int uiDistance = 0; uiDistance < TH_DISTANCES; uiDistance++)
913 {
914 encodingTry.m_uiCW1 = uiDistance;
915
916 // twiddle m_frgbaOriginalColor2_TAndH
917 // twiddle color2 first, since it affects 3 selectors, while color1 only affects one selector
918 //
919 for (int iRed2 = iMinRed2; iRed2 <= iMaxRed2; iRed2++)
920 {
921 for (int iGreen2 = iMinGreen2; iGreen2 <= iMaxGreen2; iGreen2++)
922 {
923 for (int iBlue2 = iMinBlue2; iBlue2 <= iMaxBlue2; iBlue2++)
924 {
925 for (unsigned int uiBaseColorSwaps = 0; uiBaseColorSwaps < 2; uiBaseColorSwaps++)
926 {
927 if (uiBaseColorSwaps == 0)
928 {
929 encodingTry.m_frgbaColor1 = m_frgbaOriginalColor1_TAndH;
930 encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
931 }
932 else
933 {
934 encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
935 encodingTry.m_frgbaColor2 = m_frgbaOriginalColor1_TAndH;
936 }
937
938 encodingTry.TryT_BestSelectorCombination();
939
940 if (encodingTry.m_fError < m_fError)
941 {
942 m_mode = encodingTry.m_mode;
943 m_boolDiff = encodingTry.m_boolDiff;
944 m_boolFlip = encodingTry.m_boolFlip;
945
946 m_frgbaColor1 = encodingTry.m_frgbaColor1;
947 m_frgbaColor2 = encodingTry.m_frgbaColor2;
948 m_uiCW1 = encodingTry.m_uiCW1;
949
950 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
951 {
952 m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
953 m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
954 }
955
956 m_fError = encodingTry.m_fError;
957 }
958 }
959 }
960 }
961 }
962
963 // twiddle m_frgbaOriginalColor1_TAndH
964 for (int iRed1 = iMinRed1; iRed1 <= iMaxRed1; iRed1++)
965 {
966 for (int iGreen1 = iMinGreen1; iGreen1 <= iMaxGreen1; iGreen1++)
967 {
968 for (int iBlue1 = iMinBlue1; iBlue1 <= iMaxBlue1; iBlue1++)
969 {
970 for (unsigned int uiBaseColorSwaps = 0; uiBaseColorSwaps < 2; uiBaseColorSwaps++)
971 {
972 if (uiBaseColorSwaps == 0)
973 {
974 encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
975 encodingTry.m_frgbaColor2 = m_frgbaOriginalColor2_TAndH;
976 }
977 else
978 {
979 encodingTry.m_frgbaColor1 = m_frgbaOriginalColor2_TAndH;
980 encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
981 }
982
983 encodingTry.TryT_BestSelectorCombination();
984
985 if (encodingTry.m_fError < m_fError)
986 {
987 m_mode = encodingTry.m_mode;
988 m_boolDiff = encodingTry.m_boolDiff;
989 m_boolFlip = encodingTry.m_boolFlip;
990
991 m_frgbaColor1 = encodingTry.m_frgbaColor1;
992 m_frgbaColor2 = encodingTry.m_frgbaColor2;
993 m_uiCW1 = encodingTry.m_uiCW1;
994
995 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
996 {
997 m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
998 m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
999 }
1000
1001 m_fError = encodingTry.m_fError;
1002 }
1003 }
1004 }
1005 }
1006 }
1007
1008 }
1009
1010 }
1011
1012 // ----------------------------------------------------------------------------------------------------
1013 // find best selector combination for TryT
1014 // called on an encodingTry
1015 //
1016 void Block4x4Encoding_RGB8A1::TryT_BestSelectorCombination(void)
1017 {
1018
1019 float fDistance = s_afTHDistanceTable[m_uiCW1];
1020
1021 unsigned int auiBestPixelSelectors[PIXELS];
1022 float afBestPixelErrors[PIXELS] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
1023 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
1024 ColorFloatRGBA afrgbaBestDecodedPixels[PIXELS];
1025 ColorFloatRGBA afrgbaDecodedPixel[SELECTORS];
1026
1027 assert(SELECTORS == 4);
1028 afrgbaDecodedPixel[0] = m_frgbaColor1;
1029 afrgbaDecodedPixel[1] = (m_frgbaColor2 + fDistance).ClampRGB();
1030 afrgbaDecodedPixel[2] = ColorFloatRGBA();
1031 afrgbaDecodedPixel[3] = (m_frgbaColor2 - fDistance).ClampRGB();
1032
1033 // try each selector
1034 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1035 {
1036 unsigned int uiMinSelector = 0;
1037 unsigned int uiMaxSelector = SELECTORS - 1;
1038
1039 if (m_pafrgbaSource[uiPixel].fA < 0.5f)
1040 {
1041 uiMinSelector = 2;
1042 uiMaxSelector = 2;
1043 }
1044
1045 for (unsigned int uiSelector = uiMinSelector; uiSelector <= uiMaxSelector; uiSelector++)
1046 {
1047 float fPixelError = CalcPixelError(afrgbaDecodedPixel[uiSelector], m_afDecodedAlphas[uiPixel],
1048 m_pafrgbaSource[uiPixel]);
1049
1050 if (fPixelError < afBestPixelErrors[uiPixel])
1051 {
1052 afBestPixelErrors[uiPixel] = fPixelError;
1053 auiBestPixelSelectors[uiPixel] = uiSelector;
1054 afrgbaBestDecodedPixels[uiPixel] = afrgbaDecodedPixel[uiSelector];
1055 }
1056 }
1057 }
1058
1059
1060 // add up all of the pixel errors
1061 float fBlockError = 0.0f;
1062 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1063 {
1064 fBlockError += afBestPixelErrors[uiPixel];
1065 }
1066
1067 if (fBlockError < m_fError)
1068 {
1069 m_fError = fBlockError;
1070
1071 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1072 {
1073 m_auiSelectors[uiPixel] = auiBestPixelSelectors[uiPixel];
1074 m_afrgbaDecodedColors[uiPixel] = afrgbaBestDecodedPixels[uiPixel];
1075 }
1076 }
1077
1078 }
1079
1080 // ----------------------------------------------------------------------------------------------------
1081 // try encoding in H mode
1082 // save this encoding if it improves the error
1083 //
1084 // since all pixels use the distance table, color1 and color2 can NOT be twiddled independently
1085 // TWIDDLE_RADIUS of 2 is WAY too slow
1086 //
1087 void Block4x4Encoding_RGB8A1::TryH(unsigned int a_uiRadius)
1088 {
1089 Block4x4Encoding_RGB8A1 encodingTry = *this;
1090
1091 // init "try"
1092 {
1093 encodingTry.m_mode = MODE_H;
1094 encodingTry.m_boolDiff = true;
1095 encodingTry.m_boolFlip = false;
1096 encodingTry.m_fError = FLT_MAX;
1097 }
1098
1099 int iColor1Red = m_frgbaOriginalColor1_TAndH.IntRed(15.0f);
1100 int iColor1Green = m_frgbaOriginalColor1_TAndH.IntGreen(15.0f);
1101 int iColor1Blue = m_frgbaOriginalColor1_TAndH.IntBlue(15.0f);
1102
1103 int iMinRed1 = iColor1Red - (int)a_uiRadius;
1104 if (iMinRed1 < 0)
1105 {
1106 iMinRed1 = 0;
1107 }
1108 int iMaxRed1 = iColor1Red + (int)a_uiRadius;
1109 if (iMaxRed1 > 15)
1110 {
1111 iMinRed1 = 15;
1112 }
1113
1114 int iMinGreen1 = iColor1Green - (int)a_uiRadius;
1115 if (iMinGreen1 < 0)
1116 {
1117 iMinGreen1 = 0;
1118 }
1119 int iMaxGreen1 = iColor1Green + (int)a_uiRadius;
1120 if (iMaxGreen1 > 15)
1121 {
1122 iMinGreen1 = 15;
1123 }
1124
1125 int iMinBlue1 = iColor1Blue - (int)a_uiRadius;
1126 if (iMinBlue1 < 0)
1127 {
1128 iMinBlue1 = 0;
1129 }
1130 int iMaxBlue1 = iColor1Blue + (int)a_uiRadius;
1131 if (iMaxBlue1 > 15)
1132 {
1133 iMinBlue1 = 15;
1134 }
1135
1136 int iColor2Red = m_frgbaOriginalColor2_TAndH.IntRed(15.0f);
1137 int iColor2Green = m_frgbaOriginalColor2_TAndH.IntGreen(15.0f);
1138 int iColor2Blue = m_frgbaOriginalColor2_TAndH.IntBlue(15.0f);
1139
1140 int iMinRed2 = iColor2Red - (int)a_uiRadius;
1141 if (iMinRed2 < 0)
1142 {
1143 iMinRed2 = 0;
1144 }
1145 int iMaxRed2 = iColor2Red + (int)a_uiRadius;
1146 if (iMaxRed2 > 15)
1147 {
1148 iMinRed2 = 15;
1149 }
1150
1151 int iMinGreen2 = iColor2Green - (int)a_uiRadius;
1152 if (iMinGreen2 < 0)
1153 {
1154 iMinGreen2 = 0;
1155 }
1156 int iMaxGreen2 = iColor2Green + (int)a_uiRadius;
1157 if (iMaxGreen2 > 15)
1158 {
1159 iMinGreen2 = 15;
1160 }
1161
1162 int iMinBlue2 = iColor2Blue - (int)a_uiRadius;
1163 if (iMinBlue2 < 0)
1164 {
1165 iMinBlue2 = 0;
1166 }
1167 int iMaxBlue2 = iColor2Blue + (int)a_uiRadius;
1168 if (iMaxBlue2 > 15)
1169 {
1170 iMinBlue2 = 15;
1171 }
1172
1173 for (unsigned int uiDistance = 0; uiDistance < TH_DISTANCES; uiDistance++)
1174 {
1175 encodingTry.m_uiCW1 = uiDistance;
1176
1177 // twiddle m_frgbaOriginalColor1_TAndH
1178 for (int iRed1 = iMinRed1; iRed1 <= iMaxRed1; iRed1++)
1179 {
1180 for (int iGreen1 = iMinGreen1; iGreen1 <= iMaxGreen1; iGreen1++)
1181 {
1182 for (int iBlue1 = iMinBlue1; iBlue1 <= iMaxBlue1; iBlue1++)
1183 {
1184 encodingTry.m_frgbaColor1 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed1, (unsigned char)iGreen1, (unsigned char)iBlue1);
1185 encodingTry.m_frgbaColor2 = m_frgbaOriginalColor2_TAndH;
1186
1187 // if color1 == color2, H encoding issues can pop up, so abort
1188 if (iRed1 == iColor2Red && iGreen1 == iColor2Green && iBlue1 == iColor2Blue)
1189 {
1190 continue;
1191 }
1192
1193 encodingTry.TryH_BestSelectorCombination();
1194
1195 if (encodingTry.m_fError < m_fError)
1196 {
1197 m_mode = encodingTry.m_mode;
1198 m_boolDiff = encodingTry.m_boolDiff;
1199 m_boolFlip = encodingTry.m_boolFlip;
1200
1201 m_frgbaColor1 = encodingTry.m_frgbaColor1;
1202 m_frgbaColor2 = encodingTry.m_frgbaColor2;
1203 m_uiCW1 = encodingTry.m_uiCW1;
1204
1205 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1206 {
1207 m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
1208 m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
1209 }
1210
1211 m_fError = encodingTry.m_fError;
1212 }
1213 }
1214 }
1215 }
1216
1217 // twiddle m_frgbaOriginalColor2_TAndH
1218 for (int iRed2 = iMinRed2; iRed2 <= iMaxRed2; iRed2++)
1219 {
1220 for (int iGreen2 = iMinGreen2; iGreen2 <= iMaxGreen2; iGreen2++)
1221 {
1222 for (int iBlue2 = iMinBlue2; iBlue2 <= iMaxBlue2; iBlue2++)
1223 {
1224 encodingTry.m_frgbaColor1 = m_frgbaOriginalColor1_TAndH;
1225 encodingTry.m_frgbaColor2 = ColorFloatRGBA::ConvertFromRGB4((unsigned char)iRed2, (unsigned char)iGreen2, (unsigned char)iBlue2);
1226
1227 // if color1 == color2, H encoding issues can pop up, so abort
1228 if (iRed2 == iColor1Red && iGreen2 == iColor1Green && iBlue2 == iColor1Blue)
1229 {
1230 continue;
1231 }
1232
1233 encodingTry.TryH_BestSelectorCombination();
1234
1235 if (encodingTry.m_fError < m_fError)
1236 {
1237 m_mode = encodingTry.m_mode;
1238 m_boolDiff = encodingTry.m_boolDiff;
1239 m_boolFlip = encodingTry.m_boolFlip;
1240
1241 m_frgbaColor1 = encodingTry.m_frgbaColor1;
1242 m_frgbaColor2 = encodingTry.m_frgbaColor2;
1243 m_uiCW1 = encodingTry.m_uiCW1;
1244
1245 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1246 {
1247 m_auiSelectors[uiPixel] = encodingTry.m_auiSelectors[uiPixel];
1248 m_afrgbaDecodedColors[uiPixel] = encodingTry.m_afrgbaDecodedColors[uiPixel];
1249 }
1250
1251 m_fError = encodingTry.m_fError;
1252 }
1253 }
1254 }
1255 }
1256
1257 }
1258
1259 }
1260
1261 // ----------------------------------------------------------------------------------------------------
1262 // find best selector combination for TryH
1263 // called on an encodingTry
1264 //
1265 void Block4x4Encoding_RGB8A1::TryH_BestSelectorCombination(void)
1266 {
1267
1268 // abort if colors and CW will pose an encoding problem
1269 {
1270 unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(255.0f);
1271 unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(255.0f);
1272 unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(255.0f);
1273 unsigned int uiColorValue1 = (uiRed1 << 16) + (uiGreen1 << 8) + uiBlue1;
1274
1275 unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(255.0f);
1276 unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(255.0f);
1277 unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(255.0f);
1278 unsigned int uiColorValue2 = (uiRed2 << 16) + (uiGreen2 << 8) + uiBlue2;
1279
1280 unsigned int uiCWLsb = m_uiCW1 & 1;
1281
1282 if ((uiColorValue1 >= (uiColorValue2 & uiCWLsb)) == 0 ||
1283 (uiColorValue1 < (uiColorValue2 & uiCWLsb)) == 1)
1284 {
1285 return;
1286 }
1287 }
1288
1289 float fDistance = s_afTHDistanceTable[m_uiCW1];
1290
1291 unsigned int auiBestPixelSelectors[PIXELS];
1292 float afBestPixelErrors[PIXELS] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
1293 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX };
1294 ColorFloatRGBA afrgbaBestDecodedPixels[PIXELS];
1295 ColorFloatRGBA afrgbaDecodedPixel[SELECTORS];
1296
1297 assert(SELECTORS == 4);
1298 afrgbaDecodedPixel[0] = (m_frgbaColor1 + fDistance).ClampRGB();
1299 afrgbaDecodedPixel[1] = (m_frgbaColor1 - fDistance).ClampRGB();
1300 afrgbaDecodedPixel[2] = ColorFloatRGBA();;
1301 afrgbaDecodedPixel[3] = (m_frgbaColor2 - fDistance).ClampRGB();
1302
1303
1304 // try each selector
1305 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1306 {
1307 unsigned int uiMinSelector = 0;
1308 unsigned int uiMaxSelector = SELECTORS - 1;
1309
1310 if (m_pafrgbaSource[uiPixel].fA < 0.5f)
1311 {
1312 uiMinSelector = 2;
1313 uiMaxSelector = 2;
1314 }
1315
1316 for (unsigned int uiSelector = uiMinSelector; uiSelector <= uiMaxSelector; uiSelector++)
1317 {
1318 float fPixelError = CalcPixelError(afrgbaDecodedPixel[uiSelector], m_afDecodedAlphas[uiPixel],
1319 m_pafrgbaSource[uiPixel]);
1320
1321 if (fPixelError < afBestPixelErrors[uiPixel])
1322 {
1323 afBestPixelErrors[uiPixel] = fPixelError;
1324 auiBestPixelSelectors[uiPixel] = uiSelector;
1325 afrgbaBestDecodedPixels[uiPixel] = afrgbaDecodedPixel[uiSelector];
1326 }
1327 }
1328 }
1329
1330
1331 // add up all of the pixel errors
1332 float fBlockError = 0.0f;
1333 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1334 {
1335 fBlockError += afBestPixelErrors[uiPixel];
1336 }
1337
1338 if (fBlockError < m_fError)
1339 {
1340 m_fError = fBlockError;
1341
1342 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1343 {
1344 m_auiSelectors[uiPixel] = auiBestPixelSelectors[uiPixel];
1345 m_afrgbaDecodedColors[uiPixel] = afrgbaBestDecodedPixels[uiPixel];
1346 }
1347 }
1348
1349 }
1350
1351 // ----------------------------------------------------------------------------------------------------
1352 // try version 1 of the degenerate search
1353 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1354 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1355 // be successfull
1356 //
1357 void Block4x4Encoding_RGB8A1::TryDegenerates1(void)
1358 {
1359
1360 TryDifferential(m_boolMostLikelyFlip, 1, -2, 0);
1361 TryDifferential(m_boolMostLikelyFlip, 1, 2, 0);
1362 TryDifferential(m_boolMostLikelyFlip, 1, 0, 2);
1363 TryDifferential(m_boolMostLikelyFlip, 1, 0, -2);
1364
1365 }
1366
1367 // ----------------------------------------------------------------------------------------------------
1368 // try version 2 of the degenerate search
1369 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1370 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1371 // be successfull
1372 //
1373 void Block4x4Encoding_RGB8A1::TryDegenerates2(void)
1374 {
1375
1376 TryDifferential(!m_boolMostLikelyFlip, 1, -2, 0);
1377 TryDifferential(!m_boolMostLikelyFlip, 1, 2, 0);
1378 TryDifferential(!m_boolMostLikelyFlip, 1, 0, 2);
1379 TryDifferential(!m_boolMostLikelyFlip, 1, 0, -2);
1380
1381 }
1382
1383 // ----------------------------------------------------------------------------------------------------
1384 // try version 3 of the degenerate search
1385 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1386 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1387 // be successfull
1388 //
1389 void Block4x4Encoding_RGB8A1::TryDegenerates3(void)
1390 {
1391
1392 TryDifferential(m_boolMostLikelyFlip, 1, -2, -2);
1393 TryDifferential(m_boolMostLikelyFlip, 1, -2, 2);
1394 TryDifferential(m_boolMostLikelyFlip, 1, 2, -2);
1395 TryDifferential(m_boolMostLikelyFlip, 1, 2, 2);
1396
1397 }
1398
1399 // ----------------------------------------------------------------------------------------------------
1400 // try version 4 of the degenerate search
1401 // degenerate encodings use basecolor movement and a subset of the selectors to find useful encodings
1402 // each subsequent version of the degenerate search uses more basecolor movement and is less likely to
1403 // be successfull
1404 //
1405 void Block4x4Encoding_RGB8A1::TryDegenerates4(void)
1406 {
1407
1408 TryDifferential(m_boolMostLikelyFlip, 1, -4, 0);
1409 TryDifferential(m_boolMostLikelyFlip, 1, 4, 0);
1410 TryDifferential(m_boolMostLikelyFlip, 1, 0, 4);
1411 TryDifferential(m_boolMostLikelyFlip, 1, 0, -4);
1412
1413 }
1414
1415 // ----------------------------------------------------------------------------------------------------
1416 // set the encoding bits based on encoding state
1417 //
1418 void Block4x4Encoding_RGB8A1::SetEncodingBits(void)
1419 {
1420 switch (m_mode)
1421 {
1422 case MODE_ETC1:
1423 SetEncodingBits_ETC1();
1424 break;
1425
1426 case MODE_T:
1427 SetEncodingBits_T();
1428 break;
1429
1430 case MODE_H:
1431 SetEncodingBits_H();
1432 break;
1433
1434 case MODE_PLANAR:
1435 Block4x4Encoding_RGB8::SetEncodingBits_Planar();
1436 break;
1437
1438 default:
1439 assert(false);
1440 }
1441 }
1442
1443 // ----------------------------------------------------------------------------------------------------
1444 // set the encoding bits based on encoding state if ETC1 mode
1445 //
1446 void Block4x4Encoding_RGB8A1::SetEncodingBits_ETC1(void)
1447 {
1448
1449 // there is no individual mode in RGB8A1
1450 assert(m_boolDiff);
1451
1452 int iRed1 = m_frgbaColor1.IntRed(31.0f);
1453 int iGreen1 = m_frgbaColor1.IntGreen(31.0f);
1454 int iBlue1 = m_frgbaColor1.IntBlue(31.0f);
1455
1456 int iRed2 = m_frgbaColor2.IntRed(31.0f);
1457 int iGreen2 = m_frgbaColor2.IntGreen(31.0f);
1458 int iBlue2 = m_frgbaColor2.IntBlue(31.0f);
1459
1460 int iDRed2 = iRed2 - iRed1;
1461 int iDGreen2 = iGreen2 - iGreen1;
1462 int iDBlue2 = iBlue2 - iBlue1;
1463
1464 assert(iDRed2 >= -4 && iDRed2 < 4);
1465 assert(iDGreen2 >= -4 && iDGreen2 < 4);
1466 assert(iDBlue2 >= -4 && iDBlue2 < 4);
1467
1468 m_pencodingbitsRGB8->differential.red1 = iRed1;
1469 m_pencodingbitsRGB8->differential.green1 = iGreen1;
1470 m_pencodingbitsRGB8->differential.blue1 = iBlue1;
1471
1472 m_pencodingbitsRGB8->differential.dred2 = iDRed2;
1473 m_pencodingbitsRGB8->differential.dgreen2 = iDGreen2;
1474 m_pencodingbitsRGB8->differential.dblue2 = iDBlue2;
1475
1476 m_pencodingbitsRGB8->individual.cw1 = m_uiCW1;
1477 m_pencodingbitsRGB8->individual.cw2 = m_uiCW2;
1478
1479 SetEncodingBits_Selectors();
1480
1481 // in RGB8A1 encoding bits, opaque replaces differential
1482 m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
1483
1484 m_pencodingbitsRGB8->individual.flip = m_boolFlip;
1485
1486 }
1487
1488 // ----------------------------------------------------------------------------------------------------
1489 // set the encoding bits based on encoding state if T mode
1490 //
1491 void Block4x4Encoding_RGB8A1::SetEncodingBits_T(void)
1492 {
1493 static const bool SANITY_CHECK = true;
1494
1495 assert(m_mode == MODE_T);
1496 assert(m_boolDiff == true);
1497
1498 unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(15.0f);
1499 unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(15.0f);
1500 unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(15.0f);
1501
1502 unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(15.0f);
1503 unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(15.0f);
1504 unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(15.0f);
1505
1506 m_pencodingbitsRGB8->t.red1a = uiRed1 >> 2;
1507 m_pencodingbitsRGB8->t.red1b = uiRed1;
1508 m_pencodingbitsRGB8->t.green1 = uiGreen1;
1509 m_pencodingbitsRGB8->t.blue1 = uiBlue1;
1510
1511 m_pencodingbitsRGB8->t.red2 = uiRed2;
1512 m_pencodingbitsRGB8->t.green2 = uiGreen2;
1513 m_pencodingbitsRGB8->t.blue2 = uiBlue2;
1514
1515 m_pencodingbitsRGB8->t.da = m_uiCW1 >> 1;
1516 m_pencodingbitsRGB8->t.db = m_uiCW1;
1517
1518 // in RGB8A1 encoding bits, opaque replaces differential
1519 m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
1520
1521 Block4x4Encoding_ETC1::SetEncodingBits_Selectors();
1522
1523 // create an invalid R differential to trigger T mode
1524 m_pencodingbitsRGB8->t.detect1 = 0;
1525 m_pencodingbitsRGB8->t.detect2 = 0;
1526 int iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
1527 if (iRed2 >= 4)
1528 {
1529 m_pencodingbitsRGB8->t.detect1 = 7;
1530 m_pencodingbitsRGB8->t.detect2 = 0;
1531 }
1532 else
1533 {
1534 m_pencodingbitsRGB8->t.detect1 = 0;
1535 m_pencodingbitsRGB8->t.detect2 = 1;
1536 }
1537
1538 if (SANITY_CHECK)
1539 {
1540 iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
1541
1542 // make sure red overflows
1543 assert(iRed2 < 0 || iRed2 > 31);
1544 }
1545
1546 }
1547
1548 // ----------------------------------------------------------------------------------------------------
1549 // set the encoding bits based on encoding state if H mode
1550 //
1551 // colors and selectors may need to swap in order to generate lsb of distance index
1552 //
1553 void Block4x4Encoding_RGB8A1::SetEncodingBits_H(void)
1554 {
1555 static const bool SANITY_CHECK = true;
1556
1557 assert(m_mode == MODE_H);
1558 assert(m_boolDiff == true);
1559
1560 unsigned int uiRed1 = (unsigned int)m_frgbaColor1.IntRed(15.0f);
1561 unsigned int uiGreen1 = (unsigned int)m_frgbaColor1.IntGreen(15.0f);
1562 unsigned int uiBlue1 = (unsigned int)m_frgbaColor1.IntBlue(15.0f);
1563
1564 unsigned int uiRed2 = (unsigned int)m_frgbaColor2.IntRed(15.0f);
1565 unsigned int uiGreen2 = (unsigned int)m_frgbaColor2.IntGreen(15.0f);
1566 unsigned int uiBlue2 = (unsigned int)m_frgbaColor2.IntBlue(15.0f);
1567
1568 unsigned int uiColor1 = (uiRed1 << 16) + (uiGreen1 << 8) + uiBlue1;
1569 unsigned int uiColor2 = (uiRed2 << 16) + (uiGreen2 << 8) + uiBlue2;
1570
1571 bool boolOddDistance = m_uiCW1 & 1;
1572 bool boolSwapColors = (uiColor1 < uiColor2) ^ !boolOddDistance;
1573
1574 if (boolSwapColors)
1575 {
1576 m_pencodingbitsRGB8->h.red1 = uiRed2;
1577 m_pencodingbitsRGB8->h.green1a = uiGreen2 >> 1;
1578 m_pencodingbitsRGB8->h.green1b = uiGreen2;
1579 m_pencodingbitsRGB8->h.blue1a = uiBlue2 >> 3;
1580 m_pencodingbitsRGB8->h.blue1b = uiBlue2 >> 1;
1581 m_pencodingbitsRGB8->h.blue1c = uiBlue2;
1582
1583 m_pencodingbitsRGB8->h.red2 = uiRed1;
1584 m_pencodingbitsRGB8->h.green2a = uiGreen1 >> 1;
1585 m_pencodingbitsRGB8->h.green2b = uiGreen1;
1586 m_pencodingbitsRGB8->h.blue2 = uiBlue1;
1587
1588 m_pencodingbitsRGB8->h.da = m_uiCW1 >> 2;
1589 m_pencodingbitsRGB8->h.db = m_uiCW1 >> 1;
1590 }
1591 else
1592 {
1593 m_pencodingbitsRGB8->h.red1 = uiRed1;
1594 m_pencodingbitsRGB8->h.green1a = uiGreen1 >> 1;
1595 m_pencodingbitsRGB8->h.green1b = uiGreen1;
1596 m_pencodingbitsRGB8->h.blue1a = uiBlue1 >> 3;
1597 m_pencodingbitsRGB8->h.blue1b = uiBlue1 >> 1;
1598 m_pencodingbitsRGB8->h.blue1c = uiBlue1;
1599
1600 m_pencodingbitsRGB8->h.red2 = uiRed2;
1601 m_pencodingbitsRGB8->h.green2a = uiGreen2 >> 1;
1602 m_pencodingbitsRGB8->h.green2b = uiGreen2;
1603 m_pencodingbitsRGB8->h.blue2 = uiBlue2;
1604
1605 m_pencodingbitsRGB8->h.da = m_uiCW1 >> 2;
1606 m_pencodingbitsRGB8->h.db = m_uiCW1 >> 1;
1607 }
1608
1609 // in RGB8A1 encoding bits, opaque replaces differential
1610 m_pencodingbitsRGB8->differential.diff = !m_boolPunchThroughPixels;
1611
1612 Block4x4Encoding_ETC1::SetEncodingBits_Selectors();
1613
1614 if (boolSwapColors)
1615 {
1616 m_pencodingbitsRGB8->h.selectors ^= 0x0000FFFF;
1617 }
1618
1619 // create an invalid R differential to trigger T mode
1620 m_pencodingbitsRGB8->h.detect1 = 0;
1621 m_pencodingbitsRGB8->h.detect2 = 0;
1622 m_pencodingbitsRGB8->h.detect3 = 0;
1623 int iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
1624 int iGreen2 = (int)m_pencodingbitsRGB8->differential.green1 + (int)m_pencodingbitsRGB8->differential.dgreen2;
1625 if (iRed2 < 0 || iRed2 > 31)
1626 {
1627 m_pencodingbitsRGB8->h.detect1 = 1;
1628 }
1629 if (iGreen2 >= 4)
1630 {
1631 m_pencodingbitsRGB8->h.detect2 = 7;
1632 m_pencodingbitsRGB8->h.detect3 = 0;
1633 }
1634 else
1635 {
1636 m_pencodingbitsRGB8->h.detect2 = 0;
1637 m_pencodingbitsRGB8->h.detect3 = 1;
1638 }
1639
1640 if (SANITY_CHECK)
1641 {
1642 iRed2 = (int)m_pencodingbitsRGB8->differential.red1 + (int)m_pencodingbitsRGB8->differential.dred2;
1643 iGreen2 = (int)m_pencodingbitsRGB8->differential.green1 + (int)m_pencodingbitsRGB8->differential.dgreen2;
1644
1645 // make sure red doesn't overflow and green does
1646 assert(iRed2 >= 0 && iRed2 <= 31);
1647 assert(iGreen2 < 0 || iGreen2 > 31);
1648 }
1649
1650 }
1651
1652 // ####################################################################################################
1653 // Block4x4Encoding_RGB8A1_Opaque
1654 // ####################################################################################################
1655
1656 // ----------------------------------------------------------------------------------------------------
1657 // perform a single encoding iteration
1658 // replace the encoding if a better encoding was found
1659 // subsequent iterations generally take longer for each iteration
1660 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
1661 //
1662 void Block4x4Encoding_RGB8A1_Opaque::PerformIteration(float a_fEffort)
1663 {
1664 assert(!m_boolPunchThroughPixels);
1665 assert(!m_boolTransparent);
1666 assert(!m_boolDone);
1667
1668 switch (m_uiEncodingIterations)
1669 {
1670 case 0:
1671 PerformFirstIteration();
1672 break;
1673
1674 case 1:
1675 Block4x4Encoding_ETC1::TryDifferential(m_boolMostLikelyFlip, 1, 0, 0);
1676 break;
1677
1678 case 2:
1679 Block4x4Encoding_ETC1::TryDifferential(!m_boolMostLikelyFlip, 1, 0, 0);
1680 break;
1681
1682 case 3:
1683 Block4x4Encoding_RGB8::TryPlanar(1);
1684 break;
1685
1686 case 4:
1687 Block4x4Encoding_RGB8::TryTAndH(1);
1688 if (a_fEffort <= 49.5f)
1689 {
1690 m_boolDone = true;
1691 }
1692 break;
1693
1694 case 5:
1695 Block4x4Encoding_ETC1::TryDegenerates1();
1696 if (a_fEffort <= 59.5f)
1697 {
1698 m_boolDone = true;
1699 }
1700 break;
1701
1702 case 6:
1703 Block4x4Encoding_ETC1::TryDegenerates2();
1704 if (a_fEffort <= 69.5f)
1705 {
1706 m_boolDone = true;
1707 }
1708 break;
1709
1710 case 7:
1711 Block4x4Encoding_ETC1::TryDegenerates3();
1712 if (a_fEffort <= 79.5f)
1713 {
1714 m_boolDone = true;
1715 }
1716 break;
1717
1718 case 8:
1719 Block4x4Encoding_ETC1::TryDegenerates4();
1720 m_boolDone = true;
1721 break;
1722
1723 default:
1724 assert(0);
1725 break;
1726 }
1727
1728 m_uiEncodingIterations++;
1729 SetDoneIfPerfect();
1730 }
1731
1732 // ----------------------------------------------------------------------------------------------------
1733 // find best initial encoding to ensure block has a valid encoding
1734 //
1735 void Block4x4Encoding_RGB8A1_Opaque::PerformFirstIteration(void)
1736 {
1737
1738 // set decoded alphas
1739 // calculate alpha error
1740 m_fError = 0.0f;
1741 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1742 {
1743 m_afDecodedAlphas[uiPixel] = 1.0f;
1744
1745 float fDeltaA = 1.0f - m_pafrgbaSource[uiPixel].fA;
1746 m_fError += fDeltaA * fDeltaA;
1747 }
1748
1749 CalculateMostLikelyFlip();
1750
1751 m_fError = FLT_MAX;
1752
1753 Block4x4Encoding_ETC1::TryDifferential(m_boolMostLikelyFlip, 0, 0, 0);
1754 SetDoneIfPerfect();
1755 if (m_boolDone)
1756 {
1757 return;
1758 }
1759 Block4x4Encoding_ETC1::TryDifferential(!m_boolMostLikelyFlip, 0, 0, 0);
1760 SetDoneIfPerfect();
1761 if (m_boolDone)
1762 {
1763 return;
1764 }
1765 Block4x4Encoding_RGB8::TryPlanar(0);
1766 SetDoneIfPerfect();
1767 if (m_boolDone)
1768 {
1769 return;
1770 }
1771 Block4x4Encoding_RGB8::TryTAndH(0);
1772 SetDoneIfPerfect();
1773 }
1774
1775 // ####################################################################################################
1776 // Block4x4Encoding_RGB8A1_Transparent
1777 // ####################################################################################################
1778
1779 // ----------------------------------------------------------------------------------------------------
1780 // perform a single encoding iteration
1781 // replace the encoding if a better encoding was found
1782 // subsequent iterations generally take longer for each iteration
1783 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
1784 //
1785 void Block4x4Encoding_RGB8A1_Transparent::PerformIteration(float )
1786 {
1787 assert(!m_boolOpaque);
1788 assert(m_boolTransparent);
1789 assert(!m_boolDone);
1790 assert(m_uiEncodingIterations == 0);
1791
1792 m_mode = MODE_ETC1;
1793 m_boolDiff = true;
1794 m_boolFlip = false;
1795
1796 m_uiCW1 = 0;
1797 m_uiCW2 = 0;
1798
1799 m_frgbaColor1 = ColorFloatRGBA();
1800 m_frgbaColor2 = ColorFloatRGBA();
1801
1802 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
1803 {
1804 m_auiSelectors[uiPixel] = TRANSPARENT_SELECTOR;
1805
1806 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
1807 m_afDecodedAlphas[uiPixel] = 0.0f;
1808 }
1809
1810 CalcBlockError();
1811
1812 m_boolDone = true;
1813 m_uiEncodingIterations++;
1814
1815 }
1816
1817 // ----------------------------------------------------------------------------------------------------
1818 //
1819 }