Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4Encoding_RGBA8.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_RGBA8.cpp contains:
19 Block4x4Encoding_RGBA8
20 Block4x4Encoding_RGBA8_Opaque
21 Block4x4Encoding_RGBA8_Transparent
22
23 These encoders are used when targetting file format RGBA8.
24
25 Block4x4Encoding_RGBA8_Opaque is used when all pixels in the 4x4 block are opaque
26 Block4x4Encoding_RGBA8_Transparent is used when all pixels in the 4x4 block are transparent
27 Block4x4Encoding_RGBA8 is used when there is a mixture of alphas in the 4x4 block
28
29 */
30
31 #include "EtcConfig.h"
32 #include "EtcBlock4x4Encoding_RGBA8.h"
33
34 #include "EtcBlock4x4EncodingBits.h"
35 #include "EtcBlock4x4.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <float.h>
41 #include <limits>
42
43 namespace Etc
44 {
45
46 // ####################################################################################################
47 // Block4x4Encoding_RGBA8
48 // ####################################################################################################
49
50 float Block4x4Encoding_RGBA8::s_aafModifierTable[MODIFIER_TABLE_ENTRYS][ALPHA_SELECTORS]
51 {
52 { -3.0f / 255.0f, -6.0f / 255.0f, -9.0f / 255.0f, -15.0f / 255.0f, 2.0f / 255.0f, 5.0f / 255.0f, 8.0f / 255.0f, 14.0f / 255.0f },
53 { -3.0f / 255.0f, -7.0f / 255.0f, -10.0f / 255.0f, -13.0f / 255.0f, 2.0f / 255.0f, 6.0f / 255.0f, 9.0f / 255.0f, 12.0f / 255.0f },
54 { -2.0f / 255.0f, -5.0f / 255.0f, -8.0f / 255.0f, -13.0f / 255.0f, 1.0f / 255.0f, 4.0f / 255.0f, 7.0f / 255.0f, 12.0f / 255.0f },
55 { -2.0f / 255.0f, -4.0f / 255.0f, -6.0f / 255.0f, -13.0f / 255.0f, 1.0f / 255.0f, 3.0f / 255.0f, 5.0f / 255.0f, 12.0f / 255.0f },
56
57 { -3.0f / 255.0f, -6.0f / 255.0f, -8.0f / 255.0f, -12.0f / 255.0f, 2.0f / 255.0f, 5.0f / 255.0f, 7.0f / 255.0f, 11.0f / 255.0f },
58 { -3.0f / 255.0f, -7.0f / 255.0f, -9.0f / 255.0f, -11.0f / 255.0f, 2.0f / 255.0f, 6.0f / 255.0f, 8.0f / 255.0f, 10.0f / 255.0f },
59 { -4.0f / 255.0f, -7.0f / 255.0f, -8.0f / 255.0f, -11.0f / 255.0f, 3.0f / 255.0f, 6.0f / 255.0f, 7.0f / 255.0f, 10.0f / 255.0f },
60 { -3.0f / 255.0f, -5.0f / 255.0f, -8.0f / 255.0f, -11.0f / 255.0f, 2.0f / 255.0f, 4.0f / 255.0f, 7.0f / 255.0f, 10.0f / 255.0f },
61
62 { -2.0f / 255.0f, -6.0f / 255.0f, -8.0f / 255.0f, -10.0f / 255.0f, 1.0f / 255.0f, 5.0f / 255.0f, 7.0f / 255.0f, 9.0f / 255.0f },
63 { -2.0f / 255.0f, -5.0f / 255.0f, -8.0f / 255.0f, -10.0f / 255.0f, 1.0f / 255.0f, 4.0f / 255.0f, 7.0f / 255.0f, 9.0f / 255.0f },
64 { -2.0f / 255.0f, -4.0f / 255.0f, -8.0f / 255.0f, -10.0f / 255.0f, 1.0f / 255.0f, 3.0f / 255.0f, 7.0f / 255.0f, 9.0f / 255.0f },
65 { -2.0f / 255.0f, -5.0f / 255.0f, -7.0f / 255.0f, -10.0f / 255.0f, 1.0f / 255.0f, 4.0f / 255.0f, 6.0f / 255.0f, 9.0f / 255.0f },
66
67 { -3.0f / 255.0f, -4.0f / 255.0f, -7.0f / 255.0f, -10.0f / 255.0f, 2.0f / 255.0f, 3.0f / 255.0f, 6.0f / 255.0f, 9.0f / 255.0f },
68 { -1.0f / 255.0f, -2.0f / 255.0f, -3.0f / 255.0f, -10.0f / 255.0f, 0.0f / 255.0f, 1.0f / 255.0f, 2.0f / 255.0f, 9.0f / 255.0f },
69 { -4.0f / 255.0f, -6.0f / 255.0f, -8.0f / 255.0f, -9.0f / 255.0f, 3.0f / 255.0f, 5.0f / 255.0f, 7.0f / 255.0f, 8.0f / 255.0f },
70 { -3.0f / 255.0f, -5.0f / 255.0f, -7.0f / 255.0f, -9.0f / 255.0f, 2.0f / 255.0f, 4.0f / 255.0f, 6.0f / 255.0f, 8.0f / 255.0f }
71 };
72
73 // ----------------------------------------------------------------------------------------------------
74 //
75 Block4x4Encoding_RGBA8::Block4x4Encoding_RGBA8(void)
76 {
77
78 m_pencodingbitsA8 = nullptr;
79
80 }
81 Block4x4Encoding_RGBA8::~Block4x4Encoding_RGBA8(void) {}
82 // ----------------------------------------------------------------------------------------------------
83 // initialization prior to encoding
84 // a_pblockParent points to the block associated with this encoding
85 // a_errormetric is used to choose the best encoding
86 // a_pafrgbaSource points to a 4x4 block subset of the source image
87 // a_paucEncodingBits points to the final encoding bits
88 //
89 void Block4x4Encoding_RGBA8::InitFromSource(Block4x4 *a_pblockParent,
90 ColorFloatRGBA *a_pafrgbaSource,
91 unsigned char *a_paucEncodingBits, ErrorMetric a_errormetric)
92 {
93 Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);
94
95 m_pencodingbitsA8 = (Block4x4EncodingBits_A8 *)a_paucEncodingBits;
96 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)(a_paucEncodingBits + sizeof(Block4x4EncodingBits_A8));
97
98 }
99
100 // ----------------------------------------------------------------------------------------------------
101 // initialization from the encoding bits of a previous encoding
102 // a_pblockParent points to the block associated with this encoding
103 // a_errormetric is used to choose the best encoding
104 // a_pafrgbaSource points to a 4x4 block subset of the source image
105 // a_paucEncodingBits points to the final encoding bits of a previous encoding
106 //
107 void Block4x4Encoding_RGBA8::InitFromEncodingBits(Block4x4 *a_pblockParent,
108 unsigned char *a_paucEncodingBits,
109 ColorFloatRGBA *a_pafrgbaSource,
110 ErrorMetric a_errormetric)
111 {
112
113 m_pencodingbitsA8 = (Block4x4EncodingBits_A8 *)a_paucEncodingBits;
114 m_pencodingbitsRGB8 = (Block4x4EncodingBits_RGB8 *)(a_paucEncodingBits + sizeof(Block4x4EncodingBits_A8));
115
116 // init RGB portion
117 Block4x4Encoding_RGB8::InitFromEncodingBits(a_pblockParent,
118 (unsigned char *) m_pencodingbitsRGB8,
119 a_pafrgbaSource,
120 a_errormetric);
121
122 // init A8 portion
123 // has to be done after InitFromEncodingBits()
124 {
125 m_fBase = m_pencodingbitsA8->data.base / 255.0f;
126 m_fMultiplier = (float)m_pencodingbitsA8->data.multiplier;
127 m_uiModifierTableIndex = m_pencodingbitsA8->data.table;
128
129 unsigned long long int ulliSelectorBits = 0;
130 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors0 << 40;
131 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors1 << 32;
132 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors2 << 24;
133 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors3 << 16;
134 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors4 << 8;
135 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsA8->data.selectors5;
136 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
137 {
138 unsigned int uiShift = 45 - (3 * uiPixel);
139 m_auiAlphaSelectors[uiPixel] = (ulliSelectorBits >> uiShift) & (ALPHA_SELECTORS - 1);
140 }
141
142 // decode the alphas
143 // calc alpha error
144 m_fError = 0.0f;
145 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
146 {
147 m_afDecodedAlphas[uiPixel] = DecodePixelAlpha(m_fBase, m_fMultiplier,
148 m_uiModifierTableIndex,
149 m_auiAlphaSelectors[uiPixel]);
150
151 float fDeltaAlpha = m_afDecodedAlphas[uiPixel] - m_pafrgbaSource[uiPixel].fA;
152 m_fError += fDeltaAlpha * fDeltaAlpha;
153 }
154 }
155
156 // redo error calc to include alpha
157 CalcBlockError();
158
159 }
160
161 // ----------------------------------------------------------------------------------------------------
162 // perform a single encoding iteration
163 // replace the encoding if a better encoding was found
164 // subsequent iterations generally take longer for each iteration
165 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
166 //
167 // similar to Block4x4Encoding_RGB8_Base::Encode_RGB8(), but with alpha added
168 //
169 void Block4x4Encoding_RGBA8::PerformIteration(float a_fEffort)
170 {
171 assert(!m_boolDone);
172
173 if (m_uiEncodingIterations == 0)
174 {
175 if (a_fEffort < 24.9f)
176 {
177 CalculateA8(0.0f);
178 }
179 else if (a_fEffort < 49.9f)
180 {
181 CalculateA8(1.0f);
182 }
183 else
184 {
185 CalculateA8(2.0f);
186 }
187 }
188
189 Block4x4Encoding_RGB8::PerformIteration(a_fEffort);
190
191 }
192
193 // ----------------------------------------------------------------------------------------------------
194 // find the best combination of base alpga, multiplier and selectors
195 //
196 // a_fRadius limits the range of base alpha to try
197 //
198 void Block4x4Encoding_RGBA8::CalculateA8(float a_fRadius)
199 {
200
201 // find min/max alpha
202 float fMinAlpha = 1.0f;
203 float fMaxAlpha = 0.0f;
204 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
205 {
206 float fAlpha = m_pafrgbaSource[uiPixel].fA;
207
208 // ignore border pixels
209 if (isnan(fAlpha))
210 {
211 continue;
212 }
213
214 if (fAlpha < fMinAlpha)
215 {
216 fMinAlpha = fAlpha;
217 }
218 if (fAlpha > fMaxAlpha)
219 {
220 fMaxAlpha = fAlpha;
221 }
222 }
223 assert(fMinAlpha <= fMaxAlpha);
224
225 float fAlphaRange = fMaxAlpha - fMinAlpha;
226
227 // try each modifier table entry
228 m_fError = FLT_MAX; // artificially high value
229 for (unsigned int uiTableEntry = 0; uiTableEntry < MODIFIER_TABLE_ENTRYS; uiTableEntry++)
230 {
231 static const unsigned int MIN_VALUE_SELECTOR = 3;
232 static const unsigned int MAX_VALUE_SELECTOR = 7;
233
234 float fTableEntryCenter = -s_aafModifierTable[uiTableEntry][MIN_VALUE_SELECTOR];
235
236 float fTableEntryRange = s_aafModifierTable[uiTableEntry][MAX_VALUE_SELECTOR] -
237 s_aafModifierTable[uiTableEntry][MIN_VALUE_SELECTOR];
238
239 float fCenterRatio = fTableEntryCenter / fTableEntryRange;
240
241 float fCenter = fMinAlpha + fCenterRatio*fAlphaRange;
242 fCenter = roundf(255.0f * fCenter) / 255.0f;
243
244 float fMinBase = fCenter - (a_fRadius / 255.0f);
245 if (fMinBase < 0.0f)
246 {
247 fMinBase = 0.0f;
248 }
249
250 float fMaxBase = fCenter + (a_fRadius / 255.0f);
251 if (fMaxBase > 1.0f)
252 {
253 fMaxBase = 1.0f;
254 }
255
256 for (float fBase = fMinBase; fBase <= fMaxBase; fBase += (0.999999f / 255.0f))
257 {
258
259 float fRangeMultiplier = roundf(fAlphaRange / fTableEntryRange);
260
261 float fMinMultiplier = fRangeMultiplier - a_fRadius;
262 if (fMinMultiplier < 1.0f)
263 {
264 fMinMultiplier = 1.0f;
265 }
266 else if (fMinMultiplier > 15.0f)
267 {
268 fMinMultiplier = 15.0f;
269 }
270
271 float fMaxMultiplier = fRangeMultiplier + a_fRadius;
272 if (fMaxMultiplier < 1.0f)
273 {
274 fMaxMultiplier = 1.0f;
275 }
276 else if (fMaxMultiplier > 15.0f)
277 {
278 fMaxMultiplier = 15.0f;
279 }
280
281 for (float fMultiplier = fMinMultiplier; fMultiplier <= fMaxMultiplier; fMultiplier += 1.0f)
282 {
283 // find best selector for each pixel
284 unsigned int auiBestSelectors[PIXELS];
285 float afBestAlphaError[PIXELS];
286 float afBestDecodedAlphas[PIXELS];
287 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
288 {
289 float fBestPixelAlphaError = FLT_MAX;
290 for (unsigned int uiSelector = 0; uiSelector < ALPHA_SELECTORS; uiSelector++)
291 {
292 float fDecodedAlpha = DecodePixelAlpha(fBase, fMultiplier, uiTableEntry, uiSelector);
293
294 // border pixels (NAN) should have zero error
295 float fPixelDeltaAlpha = isnan(m_pafrgbaSource[uiPixel].fA) ?
296 0.0f :
297 fDecodedAlpha - m_pafrgbaSource[uiPixel].fA;
298
299 float fPixelAlphaError = fPixelDeltaAlpha * fPixelDeltaAlpha;
300
301 if (fPixelAlphaError < fBestPixelAlphaError)
302 {
303 fBestPixelAlphaError = fPixelAlphaError;
304 auiBestSelectors[uiPixel] = uiSelector;
305 afBestAlphaError[uiPixel] = fBestPixelAlphaError;
306 afBestDecodedAlphas[uiPixel] = fDecodedAlpha;
307 }
308 }
309 }
310
311 float fBlockError = 0.0f;
312 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
313 {
314 fBlockError += afBestAlphaError[uiPixel];
315 }
316
317 if (fBlockError < m_fError)
318 {
319 m_fError = fBlockError;
320
321 m_fBase = fBase;
322 m_fMultiplier = fMultiplier;
323 m_uiModifierTableIndex = uiTableEntry;
324 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
325 {
326 m_auiAlphaSelectors[uiPixel] = auiBestSelectors[uiPixel];
327 m_afDecodedAlphas[uiPixel] = afBestDecodedAlphas[uiPixel];
328 }
329 }
330 }
331 }
332
333 }
334
335 }
336
337 // ----------------------------------------------------------------------------------------------------
338 // set the encoding bits based on encoding state
339 //
340 void Block4x4Encoding_RGBA8::SetEncodingBits(void)
341 {
342
343 // set the RGB8 portion
344 Block4x4Encoding_RGB8::SetEncodingBits();
345
346 // set the A8 portion
347 {
348 m_pencodingbitsA8->data.base = (unsigned char)roundf(255.0f * m_fBase);
349 m_pencodingbitsA8->data.table = m_uiModifierTableIndex;
350 m_pencodingbitsA8->data.multiplier = (unsigned char)roundf(m_fMultiplier);
351
352 unsigned long long int ulliSelectorBits = 0;
353 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
354 {
355 unsigned int uiShift = 45 - (3 * uiPixel);
356 ulliSelectorBits |= ((unsigned long long int)m_auiAlphaSelectors[uiPixel]) << uiShift;
357 }
358
359 m_pencodingbitsA8->data.selectors0 = ulliSelectorBits >> 40;
360 m_pencodingbitsA8->data.selectors1 = ulliSelectorBits >> 32;
361 m_pencodingbitsA8->data.selectors2 = ulliSelectorBits >> 24;
362 m_pencodingbitsA8->data.selectors3 = ulliSelectorBits >> 16;
363 m_pencodingbitsA8->data.selectors4 = ulliSelectorBits >> 8;
364 m_pencodingbitsA8->data.selectors5 = ulliSelectorBits;
365 }
366
367 }
368
369 // ####################################################################################################
370 // Block4x4Encoding_RGBA8_Opaque
371 // ####################################################################################################
372
373 // ----------------------------------------------------------------------------------------------------
374 // perform a single encoding iteration
375 // replace the encoding if a better encoding was found
376 // subsequent iterations generally take longer for each iteration
377 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
378 //
379 void Block4x4Encoding_RGBA8_Opaque::PerformIteration(float a_fEffort)
380 {
381 assert(!m_boolDone);
382
383 if (m_uiEncodingIterations == 0)
384 {
385 m_fError = 0.0f;
386
387 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
388 {
389 m_afDecodedAlphas[uiPixel] = 1.0f;
390 }
391 }
392
393 Block4x4Encoding_RGB8::PerformIteration(a_fEffort);
394
395 }
396
397 // ----------------------------------------------------------------------------------------------------
398 // set the encoding bits based on encoding state
399 //
400 void Block4x4Encoding_RGBA8_Opaque::SetEncodingBits(void)
401 {
402
403 // set the RGB8 portion
404 Block4x4Encoding_RGB8::SetEncodingBits();
405
406 // set the A8 portion
407 m_pencodingbitsA8->data.base = 255;
408 m_pencodingbitsA8->data.table = 15;
409 m_pencodingbitsA8->data.multiplier = 15;
410 m_pencodingbitsA8->data.selectors0 = 0xFF;
411 m_pencodingbitsA8->data.selectors1 = 0xFF;
412 m_pencodingbitsA8->data.selectors2 = 0xFF;
413 m_pencodingbitsA8->data.selectors3 = 0xFF;
414 m_pencodingbitsA8->data.selectors4 = 0xFF;
415 m_pencodingbitsA8->data.selectors5 = 0xFF;
416
417 }
418
419 // ####################################################################################################
420 // Block4x4Encoding_RGBA8_Transparent
421 // ####################################################################################################
422
423 // ----------------------------------------------------------------------------------------------------
424 // perform a single encoding iteration
425 // replace the encoding if a better encoding was found
426 // subsequent iterations generally take longer for each iteration
427 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
428 //
429 void Block4x4Encoding_RGBA8_Transparent::PerformIteration(float )
430 {
431 assert(!m_boolDone);
432 assert(m_uiEncodingIterations == 0);
433
434 m_mode = MODE_ETC1;
435 m_boolDiff = true;
436 m_boolFlip = false;
437
438 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
439 {
440 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA();
441 m_afDecodedAlphas[uiPixel] = 0.0f;
442 }
443
444 m_fError = 0.0f;
445
446 m_boolDone = true;
447 m_uiEncodingIterations++;
448
449 }
450
451 // ----------------------------------------------------------------------------------------------------
452 // set the encoding bits based on encoding state
453 //
454 void Block4x4Encoding_RGBA8_Transparent::SetEncodingBits(void)
455 {
456
457 Block4x4Encoding_RGB8::SetEncodingBits();
458
459 // set the A8 portion
460 m_pencodingbitsA8->data.base = 0;
461 m_pencodingbitsA8->data.table = 0;
462 m_pencodingbitsA8->data.multiplier = 1;
463 m_pencodingbitsA8->data.selectors0 = 0;
464 m_pencodingbitsA8->data.selectors1 = 0;
465 m_pencodingbitsA8->data.selectors2 = 0;
466 m_pencodingbitsA8->data.selectors3 = 0;
467 m_pencodingbitsA8->data.selectors4 = 0;
468 m_pencodingbitsA8->data.selectors5 = 0;
469
470 }
471
472 // ----------------------------------------------------------------------------------------------------
473 //
474 }