Debian Codegraph @

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