Debian Codegraph @

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