Debian Codegraph @

third_party/etc2comp/EtcLib/EtcCodec/EtcBlock4x4Encoding_R11.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_R11.cpp
19
20 Block4x4Encoding_R11 is the encoder to use when targetting file format R11 and SR11 (signed R11).
21
22 */
23
24 #include "EtcConfig.h"
25 #include "EtcBlock4x4Encoding_R11.h"
26
27 #include "EtcBlock4x4EncodingBits.h"
28 #include "EtcBlock4x4.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <float.h>
34 #include <limits>
35
36 namespace Etc
37 {
38
39 // modifier values to use for R11, SR11, RG11 and SRG11
40 float Block4x4Encoding_R11::s_aafModifierTable[MODIFIER_TABLE_ENTRYS][SELECTORS]
41 {
42 { -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 },
43 { -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 },
44 { -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 },
45 { -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 },
46
47 { -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 },
48 { -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 },
49 { -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 },
50 { -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 },
51
52 { -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 },
53 { -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 },
54 { -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 },
55 { -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 },
56
57 { -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 },
58 { -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 },
59 { -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 },
60 { -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 }
61 };
62
63 // ----------------------------------------------------------------------------------------------------
64 //
65 Block4x4Encoding_R11::Block4x4Encoding_R11(void)
66 {
67
68 m_pencodingbitsR11 = nullptr;
69
70 }
71
72 Block4x4Encoding_R11::~Block4x4Encoding_R11(void) {}
73 // ----------------------------------------------------------------------------------------------------
74 // initialization prior to encoding
75 // a_pblockParent points to the block associated with this encoding
76 // a_errormetric is used to choose the best encoding
77 // a_pafrgbaSource points to a 4x4 block subset of the source image
78 // a_paucEncodingBits points to the final encoding bits
79 //
80 void Block4x4Encoding_R11::InitFromSource(Block4x4 *a_pblockParent,
81 ColorFloatRGBA *a_pafrgbaSource,
82 unsigned char *a_paucEncodingBits, ErrorMetric a_errormetric)
83 {
84 Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);
85
86 m_pencodingbitsR11 = (Block4x4EncodingBits_R11 *)a_paucEncodingBits;
87 }
88
89 // ----------------------------------------------------------------------------------------------------
90 // initialization from the encoding bits of a previous encoding
91 // a_pblockParent points to the block associated with this encoding
92 // a_errormetric is used to choose the best encoding
93 // a_pafrgbaSource points to a 4x4 block subset of the source image
94 // a_paucEncodingBits points to the final encoding bits of a previous encoding
95 //
96 void Block4x4Encoding_R11::InitFromEncodingBits(Block4x4 *a_pblockParent,
97 unsigned char *a_paucEncodingBits,
98 ColorFloatRGBA *a_pafrgbaSource,
99 ErrorMetric a_errormetric)
100 {
101 m_pencodingbitsR11 = (Block4x4EncodingBits_R11 *)a_paucEncodingBits;
102
103 // init RGB portion
104 Block4x4Encoding_RGB8::InitFromEncodingBits(a_pblockParent,
105 (unsigned char *)m_pencodingbitsR11,
106 a_pafrgbaSource,
107 a_errormetric);
108
109 // init R11 portion
110 {
111 m_mode = MODE_R11;
112 if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_R11 || a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
113 {
114 m_fRedBase = (float)(signed char)m_pencodingbitsR11->data.base;
115 }
116 else
117 {
118 m_fRedBase = (float)(unsigned char)m_pencodingbitsR11->data.base;
119 }
120 m_fRedMultiplier = (float)m_pencodingbitsR11->data.multiplier;
121 m_uiRedModifierTableIndex = m_pencodingbitsR11->data.table;
122
123 unsigned long long int ulliSelectorBits = 0;
124 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors0 << 40;
125 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors1 << 32;
126 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors2 << 24;
127 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors3 << 16;
128 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors4 << 8;
129 ulliSelectorBits |= (unsigned long long int)m_pencodingbitsR11->data.selectors5;
130 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
131 {
132 unsigned int uiShift = 45 - (3 * uiPixel);
133 m_auiRedSelectors[uiPixel] = (ulliSelectorBits >> uiShift) & (SELECTORS - 1);
134 }
135
136 // decode the red channel
137 // calc red error
138 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
139 {
140 float fDecodedPixelData = 0.0f;
141 if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::R11 || a_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
142 {
143 fDecodedPixelData = DecodePixelRed(m_fRedBase, m_fRedMultiplier,
144 m_uiRedModifierTableIndex,
145 m_auiRedSelectors[uiPixel]);
146 }
147 else if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_R11 || a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
148 {
149 fDecodedPixelData = DecodePixelRed(m_fRedBase + 128, m_fRedMultiplier,
150 m_uiRedModifierTableIndex,
151 m_auiRedSelectors[uiPixel]);
152 }
153 else
154 {
155 assert(0);
156 }
157 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(fDecodedPixelData, 0.0f, 0.0f, 1.0f);
158 }
159 CalcBlockError();
160 }
161
162 }
163
164 // ----------------------------------------------------------------------------------------------------
165 // perform a single encoding iteration
166 // replace the encoding if a better encoding was found
167 // subsequent iterations generally take longer for each iteration
168 // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
169 //
170 void Block4x4Encoding_R11::PerformIteration(float a_fEffort)
171 {
172 assert(!m_boolDone);
173 m_mode = MODE_R11;
174
175 switch (m_uiEncodingIterations)
176 {
177 case 0:
178 m_fError = FLT_MAX;
179 m_fRedBlockError = FLT_MAX; // artificially high value
180 CalculateR11(8, 0.0f, 0.0f);
181 m_fError = m_fRedBlockError;
182 break;
183
184 case 1:
185 CalculateR11(8, 2.0f, 1.0f);
186 m_fError = m_fRedBlockError;
187 if (a_fEffort <= 24.5f)
188 {
189 m_boolDone = true;
190 }
191 break;
192
193 case 2:
194 CalculateR11(8, 12.0f, 1.0f);
195 m_fError = m_fRedBlockError;
196 if (a_fEffort <= 49.5f)
197 {
198 m_boolDone = true;
199 }
200 break;
201
202 case 3:
203 CalculateR11(7, 6.0f, 1.0f);
204 m_fError = m_fRedBlockError;
205 break;
206
207 case 4:
208 CalculateR11(6, 3.0f, 1.0f);
209 m_fError = m_fRedBlockError;
210 break;
211
212 case 5:
213 CalculateR11(5, 1.0f, 0.0f);
214 m_fError = m_fRedBlockError;
215 m_boolDone = true;
216 break;
217
218 default:
219 assert(0);
220 break;
221 }
222
223 m_uiEncodingIterations++;
224 SetDoneIfPerfect();
225 }
226
227 // ----------------------------------------------------------------------------------------------------
228 // find the best combination of base color, multiplier and selectors
229 //
230 // a_uiSelectorsUsed limits the number of selector combinations to try
231 // a_fBaseRadius limits the range of base colors to try
232 // a_fMultiplierRadius limits the range of multipliers to try
233 //
234 void Block4x4Encoding_R11::CalculateR11(unsigned int a_uiSelectorsUsed,
235 float a_fBaseRadius, float a_fMultiplierRadius)
236 {
237 // maps from virtual (monotonic) selector to ETC selector
238 static const unsigned int auiVirtualSelectorMap[8] = {3, 2, 1, 0, 4, 5, 6, 7};
239
240 // find min/max red
241 float fMinRed = 1.0f;
242 float fMaxRed = 0.0f;
243 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
244 {
245 // ignore border pixels
246 float fAlpha = m_pafrgbaSource[uiPixel].fA;
247 if (isnan(fAlpha))
248 {
249 continue;
250 }
251
252 float fRed = m_pafrgbaSource[uiPixel].fR;
253
254 if (fRed < fMinRed)
255 {
256 fMinRed = fRed;
257 }
258 if (fRed > fMaxRed)
259 {
260 fMaxRed = fRed;
261 }
262 }
263 assert(fMinRed <= fMaxRed);
264
265 float fRedRange = (fMaxRed - fMinRed);
266
267 // try each modifier table entry
268 for (unsigned int uiTableEntry = 0; uiTableEntry < MODIFIER_TABLE_ENTRYS; uiTableEntry++)
269 {
270 for (unsigned int uiMinVirtualSelector = 0;
271 uiMinVirtualSelector <= (8- a_uiSelectorsUsed);
272 uiMinVirtualSelector++)
273 {
274 unsigned int uiMaxVirtualSelector = uiMinVirtualSelector + a_uiSelectorsUsed - 1;
275
276 unsigned int uiMinSelector = auiVirtualSelectorMap[uiMinVirtualSelector];
277 unsigned int uiMaxSelector = auiVirtualSelectorMap[uiMaxVirtualSelector];
278
279 float fTableEntryCenter = -s_aafModifierTable[uiTableEntry][uiMinSelector];
280
281 float fTableEntryRange = s_aafModifierTable[uiTableEntry][uiMaxSelector] -
282 s_aafModifierTable[uiTableEntry][uiMinSelector];
283
284 float fCenterRatio = fTableEntryCenter / fTableEntryRange;
285
286 float fCenter = fMinRed + fCenterRatio*fRedRange;
287 fCenter = roundf(255.0f * fCenter) / 255.0f;
288
289 float fMinBase = fCenter - (a_fBaseRadius / 255.0f);
290 if (fMinBase < 0.0f)
291 {
292 fMinBase = 0.0f;
293 }
294
295 float fMaxBase = fCenter + (a_fBaseRadius / 255.0f);
296 if (fMaxBase > 1.0f)
297 {
298 fMaxBase = 1.0f;
299 }
300
301 for (float fBase = fMinBase; fBase <= fMaxBase; fBase += (0.999999f / 255.0f))
302 {
303 float fRangeMultiplier = roundf(fRedRange / fTableEntryRange);
304
305 float fMinMultiplier = fRangeMultiplier - a_fMultiplierRadius;
306 if (fMinMultiplier < 1.0f)
307 {
308 fMinMultiplier = 0.0f;
309 }
310 else if (fMinMultiplier > 15.0f)
311 {
312 fMinMultiplier = 15.0f;
313 }
314
315 float fMaxMultiplier = fRangeMultiplier + a_fMultiplierRadius;
316 if (fMaxMultiplier < 1.0f)
317 {
318 fMaxMultiplier = 1.0f;
319 }
320 else if (fMaxMultiplier > 15.0f)
321 {
322 fMaxMultiplier = 15.0f;
323 }
324
325 for (float fMultiplier = fMinMultiplier; fMultiplier <= fMaxMultiplier; fMultiplier += 1.0f)
326 {
327 // find best selector for each pixel
328 unsigned int auiBestSelectors[PIXELS];
329 float afBestRedError[PIXELS];
330 float afBestPixelRed[PIXELS];
331
332 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
333 {
334 float fBestPixelRedError = FLT_MAX;
335
336 for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
337 {
338 float fPixelRed = DecodePixelRed(fBase * 255.0f, fMultiplier, uiTableEntry, uiSelector);
339
340 ColorFloatRGBA frgba(fPixelRed, m_pafrgbaSource[uiPixel].fG,0.0f,1.0f);
341
342 float fPixelRedError = CalcPixelError(frgba, 1.0f, m_pafrgbaSource[uiPixel]);
343
344 if (fPixelRedError < fBestPixelRedError)
345 {
346 fBestPixelRedError = fPixelRedError;
347 auiBestSelectors[uiPixel] = uiSelector;
348 afBestRedError[uiPixel] = fBestPixelRedError;
349 afBestPixelRed[uiPixel] = fPixelRed;
350 }
351 }
352 }
353 float fBlockError = 0.0f;
354 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
355 {
356 fBlockError += afBestRedError[uiPixel];
357 }
358 if (fBlockError < m_fRedBlockError)
359 {
360 m_fRedBlockError = fBlockError;
361
362 if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::R11 || m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
363 {
364 m_fRedBase = 255.0f * fBase;
365 }
366 else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_R11 || m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
367 {
368 m_fRedBase = (fBase * 255) - 128;
369 }
370 else
371 {
372 assert(0);
373 }
374 m_fRedMultiplier = fMultiplier;
375 m_uiRedModifierTableIndex = uiTableEntry;
376
377 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
378 {
379 m_auiRedSelectors[uiPixel] = auiBestSelectors[uiPixel];
380 float fBestPixelRed = afBestPixelRed[uiPixel];
381 m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(fBestPixelRed, 0.0f, 0.0f, 1.0f);
382 m_afDecodedAlphas[uiPixel] = 1.0f;
383 }
384 }
385 }
386 }
387
388 }
389 }
390 }
391
392 // ----------------------------------------------------------------------------------------------------
393 // set the encoding bits based on encoding state
394 //
395 void Block4x4Encoding_R11::SetEncodingBits(void)
396 {
397 if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::R11 || m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
398 {
399 m_pencodingbitsR11->data.base = (unsigned char)roundf(m_fRedBase);
400 }
401 else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_R11 || m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
402 {
403 m_pencodingbitsR11->data.base = (signed char)roundf(m_fRedBase);
404 }
405 else
406 {
407 assert(0);
408 }
409 m_pencodingbitsR11->data.table = m_uiRedModifierTableIndex;
410 m_pencodingbitsR11->data.multiplier = (unsigned char)roundf(m_fRedMultiplier);
411
412 unsigned long long int ulliSelectorBits = 0;
413 for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
414 {
415 unsigned int uiShift = 45 - (3 * uiPixel);
416 ulliSelectorBits |= ((unsigned long long int)m_auiRedSelectors[uiPixel]) << uiShift;
417 }
418
419 m_pencodingbitsR11->data.selectors0 = ulliSelectorBits >> 40;
420 m_pencodingbitsR11->data.selectors1 = ulliSelectorBits >> 32;
421 m_pencodingbitsR11->data.selectors2 = ulliSelectorBits >> 24;
422 m_pencodingbitsR11->data.selectors3 = ulliSelectorBits >> 16;
423 m_pencodingbitsR11->data.selectors4 = ulliSelectorBits >> 8;
424 m_pencodingbitsR11->data.selectors5 = ulliSelectorBits;
425 }
426
427 // ----------------------------------------------------------------------------------------------------
428 //
429 }