summaryrefslogtreecommitdiff
path: root/thirdparty/etc2comp/EtcBlock4x4Encoding_RG11.cpp
blob: 417835db5192cc424ea2b0cb3d6fb503165ec732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Copyright 2015 The Etc2Comp Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
EtcBlock4x4Encoding_RG11.cpp

Block4x4Encoding_RG11 is the encoder to use when targetting file format RG11 and SRG11 (signed RG11).

*/

#include "EtcConfig.h"
#include "EtcBlock4x4Encoding_RG11.h"

#include "EtcBlock4x4EncodingBits.h"
#include "EtcBlock4x4.h"

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <float.h>
#include <limits>

namespace Etc
{
	// ----------------------------------------------------------------------------------------------------
	//
	Block4x4Encoding_RG11::Block4x4Encoding_RG11(void)
	{
		m_pencodingbitsRG11 = nullptr;
	}

	Block4x4Encoding_RG11::~Block4x4Encoding_RG11(void) {}
	// ----------------------------------------------------------------------------------------------------
	// initialization prior to encoding
	// a_pblockParent points to the block associated with this encoding
	// a_errormetric is used to choose the best encoding
	// a_pafrgbaSource points to a 4x4 block subset of the source image
	// a_paucEncodingBits points to the final encoding bits
	//
	void Block4x4Encoding_RG11::InitFromSource(Block4x4 *a_pblockParent,
		ColorFloatRGBA *a_pafrgbaSource,
		unsigned char *a_paucEncodingBits, ErrorMetric a_errormetric)
	{
		Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);

		m_pencodingbitsRG11 = (Block4x4EncodingBits_RG11 *)a_paucEncodingBits;
	}

	// ----------------------------------------------------------------------------------------------------
	// initialization from the encoding bits of a previous encoding
	// a_pblockParent points to the block associated with this encoding
	// a_errormetric is used to choose the best encoding
	// a_pafrgbaSource points to a 4x4 block subset of the source image
	// a_paucEncodingBits points to the final encoding bits of a previous encoding
	//
	void Block4x4Encoding_RG11::InitFromEncodingBits(Block4x4 *a_pblockParent,
		unsigned char *a_paucEncodingBits,
		ColorFloatRGBA *a_pafrgbaSource,
		ErrorMetric a_errormetric)
	{

		m_pencodingbitsRG11 = (Block4x4EncodingBits_RG11 *)a_paucEncodingBits;

		// init RGB portion
		Block4x4Encoding_RGB8::InitFromEncodingBits(a_pblockParent,
			(unsigned char *)m_pencodingbitsRG11,
			a_pafrgbaSource,
			a_errormetric);
		m_fError = 0.0f;

		{
			m_mode = MODE_RG11;
			if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
			{
				m_fRedBase = (float)(signed char)m_pencodingbitsRG11->data.baseR;
				m_fGrnBase = (float)(signed char)m_pencodingbitsRG11->data.baseG;
			}
			else
			{
				m_fRedBase = (float)(unsigned char)m_pencodingbitsRG11->data.baseR;
				m_fGrnBase = (float)(unsigned char)m_pencodingbitsRG11->data.baseG;
			}
			m_fRedMultiplier = (float)m_pencodingbitsRG11->data.multiplierR;
			m_fGrnMultiplier = (float)m_pencodingbitsRG11->data.multiplierG;
			m_uiRedModifierTableIndex = m_pencodingbitsRG11->data.tableIndexR;
			m_uiGrnModifierTableIndex = m_pencodingbitsRG11->data.tableIndexG;

			unsigned long long int ulliSelectorBitsR = 0;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR0 << 40;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR1 << 32;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR2 << 24;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR3 << 16;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR4 << 8;
			ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR5;

			unsigned long long int ulliSelectorBitsG = 0;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG0 << 40;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG1 << 32;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG2 << 24;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG3 << 16;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG4 << 8;
			ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG5;

			
			for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
			{
				unsigned int uiShift = 45 - (3 * uiPixel);
				m_auiRedSelectors[uiPixel] = (ulliSelectorBitsR >> uiShift) & (SELECTORS - 1);
				m_auiGrnSelectors[uiPixel] = (ulliSelectorBitsG >> uiShift) & (SELECTORS - 1);
			}

			
			for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
			{
				float fRedDecodedData = 0.0f;
				float fGrnDecodedData = 0.0f;
				if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
				{
					fRedDecodedData = DecodePixelRed(m_fRedBase, m_fRedMultiplier, m_uiRedModifierTableIndex, m_auiRedSelectors[uiPixel]);
					fGrnDecodedData = DecodePixelRed(m_fGrnBase, m_fGrnMultiplier, m_uiGrnModifierTableIndex, m_auiGrnSelectors[uiPixel]);
				}
				else if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
				{
					fRedDecodedData = DecodePixelRed(m_fRedBase + 128, m_fRedMultiplier, m_uiRedModifierTableIndex, m_auiRedSelectors[uiPixel]);
					fGrnDecodedData = DecodePixelRed(m_fGrnBase + 128, m_fGrnMultiplier, m_uiGrnModifierTableIndex, m_auiGrnSelectors[uiPixel]);
				}
				else
				{
					assert(0);
				}
				m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(fRedDecodedData, fGrnDecodedData, 0.0f, 1.0f);
			}

		}

		CalcBlockError();
 	}

	// ----------------------------------------------------------------------------------------------------
	// perform a single encoding iteration
	// replace the encoding if a better encoding was found
	// subsequent iterations generally take longer for each iteration
	// set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
	//
	void Block4x4Encoding_RG11::PerformIteration(float a_fEffort)
	{
		assert(!m_boolDone);

		switch (m_uiEncodingIterations)
		{
		case 0:
			m_fError = FLT_MAX;
			m_fGrnBlockError = FLT_MAX;		// artificially high value
			m_fRedBlockError = FLT_MAX;
			CalculateR11(8, 0.0f, 0.0f);
			CalculateG11(8, 0.0f, 0.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			break;

		case 1:
			CalculateR11(8, 2.0f, 1.0f);
			CalculateG11(8, 2.0f, 1.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			if (a_fEffort <= 24.5f)
			{
				m_boolDone = true;
			}
			break;

		case 2:
			CalculateR11(8, 12.0f, 1.0f);
			CalculateG11(8, 12.0f, 1.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			if (a_fEffort <= 49.5f)
			{
				m_boolDone = true;
			}
			break;

		case 3:
			CalculateR11(7, 6.0f, 1.0f);
			CalculateG11(7, 6.0f, 1.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			break;

		case 4:
			CalculateR11(6, 3.0f, 1.0f);
			CalculateG11(6, 3.0f, 1.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			break;

		case 5:
			CalculateR11(5, 1.0f, 0.0f);
			CalculateG11(5, 1.0f, 0.0f);
			m_fError = (m_fGrnBlockError + m_fRedBlockError);
			m_boolDone = true;
			break;

		default:
			assert(0);
			break;
		}

		m_uiEncodingIterations++;
		SetDoneIfPerfect();
	}

	// ----------------------------------------------------------------------------------------------------
	// find the best combination of base color, multiplier and selectors
	//
	// a_uiSelectorsUsed limits the number of selector combinations to try
	// a_fBaseRadius limits the range of base colors to try
	// a_fMultiplierRadius limits the range of multipliers to try
	//
	void Block4x4Encoding_RG11::CalculateG11(unsigned int a_uiSelectorsUsed,
		float a_fBaseRadius, float a_fMultiplierRadius)
	{
		// maps from virtual (monotonic) selector to etc selector
		static const unsigned int auiVirtualSelectorMap[8] = { 3, 2, 1, 0, 4, 5, 6, 7 };

		// find min/max Grn
		float fMinGrn = 1.0f;
		float fMaxGrn = 0.0f;
		for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
		{
			// ignore border pixels
			float fAlpha = m_pafrgbaSource[uiPixel].fA;
			if (isnan(fAlpha))
			{
				continue;
			}

			float fGrn = m_pafrgbaSource[uiPixel].fG;

			if (fGrn < fMinGrn)
			{
				fMinGrn = fGrn;
			}
			if (fGrn > fMaxGrn)
			{
				fMaxGrn = fGrn;
			}
		}
		assert(fMinGrn <= fMaxGrn);

		float fGrnRange = (fMaxGrn - fMinGrn);

		// try each modifier table entry							  
		for (unsigned int uiTableEntry = 0; uiTableEntry < MODIFIER_TABLE_ENTRYS; uiTableEntry++)
		{
			for (unsigned int uiMinVirtualSelector = 0;
			uiMinVirtualSelector <= (8 - a_uiSelectorsUsed);
				uiMinVirtualSelector++)
			{
				unsigned int uiMaxVirtualSelector = uiMinVirtualSelector + a_uiSelectorsUsed - 1;

				unsigned int uiMinSelector = auiVirtualSelectorMap[uiMinVirtualSelector];
				unsigned int uiMaxSelector = auiVirtualSelectorMap[uiMaxVirtualSelector];

				float fTableEntryCenter = -s_aafModifierTable[uiTableEntry][uiMinSelector];

				float fTableEntryRange = s_aafModifierTable[uiTableEntry][uiMaxSelector] -
					s_aafModifierTable[uiTableEntry][uiMinSelector];

				float fCenterRatio = fTableEntryCenter / fTableEntryRange;

				float fCenter = fMinGrn + fCenterRatio*fGrnRange;
				fCenter = roundf(255.0f * fCenter) / 255.0f;

				float fMinBase = fCenter - (a_fBaseRadius / 255.0f);
				if (fMinBase < 0.0f)
				{
					fMinBase = 0.0f;
				}

				float fMaxBase = fCenter + (a_fBaseRadius / 255.0f);
				if (fMaxBase > 1.0f)
				{
					fMaxBase = 1.0f;
				}

				for (float fBase = fMinBase; fBase <= fMaxBase; fBase += (0.999999f / 255.0f))
				{
					float fRangeMultiplier = roundf(fGrnRange / fTableEntryRange);

					float fMinMultiplier = fRangeMultiplier - a_fMultiplierRadius;
					if (fMinMultiplier < 1.0f)
					{
						fMinMultiplier = 0.0f;
					}
					else if (fMinMultiplier > 15.0f)
					{
						fMinMultiplier = 15.0f;
					}

					float fMaxMultiplier = fRangeMultiplier + a_fMultiplierRadius;
					if (fMaxMultiplier < 1.0f)
					{
						fMaxMultiplier = 1.0f;
					}
					else if (fMaxMultiplier > 15.0f)
					{
						fMaxMultiplier = 15.0f;
					}

					for (float fMultiplier = fMinMultiplier; fMultiplier <= fMaxMultiplier; fMultiplier += 1.0f)
					{
						// find best selector for each pixel
						unsigned int auiBestSelectors[PIXELS];
						float afBestGrnError[PIXELS];
						float afBestPixelGrn[PIXELS];

						for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
						{
							float fBestPixelGrnError = FLT_MAX;

							for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
							{
								//DecodePixelRed is not red channel specific
								float fPixelGrn = DecodePixelRed(fBase * 255.0f, fMultiplier, uiTableEntry, uiSelector);
								
								ColorFloatRGBA frgba(m_pafrgbaSource[uiPixel].fR, fPixelGrn, 0.0f, 1.0f);
									
								float fPixelGrnError = CalcPixelError(frgba, 1.0f, m_pafrgbaSource[uiPixel]);

								if (fPixelGrnError < fBestPixelGrnError)
								{
									fBestPixelGrnError = fPixelGrnError;
									auiBestSelectors[uiPixel] = uiSelector;
									afBestGrnError[uiPixel] = fBestPixelGrnError;
									afBestPixelGrn[uiPixel] = fPixelGrn;
								}
							}
						}
						float fBlockError = 0.0f;
						for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
						{
							fBlockError += afBestGrnError[uiPixel];
						}

						if (fBlockError < m_fGrnBlockError)
						{
							m_fGrnBlockError = fBlockError;

							if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
							{
								m_fGrnBase = 255.0f * fBase;
							}
							else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
							{
								m_fGrnBase = (fBase * 255) - 128;
							}
							else
							{
								assert(0);
							}
							m_fGrnMultiplier = fMultiplier;
							m_uiGrnModifierTableIndex = uiTableEntry;
							for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
							{
								m_auiGrnSelectors[uiPixel] = auiBestSelectors[uiPixel];
								m_afrgbaDecodedColors[uiPixel].fG = afBestPixelGrn[uiPixel];
								m_afDecodedAlphas[uiPixel] = 1.0f;
							}
						}
					}
				}

			}
		}
	}
	
	// ----------------------------------------------------------------------------------------------------
	// set the encoding bits based on encoding state
	//
	void Block4x4Encoding_RG11::SetEncodingBits(void)
	{
		unsigned long long int ulliSelectorBitsR = 0;
		unsigned long long int ulliSelectorBitsG = 0;
		for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
		{
			unsigned int uiShift = 45 - (3 * uiPixel);
			ulliSelectorBitsR |= ((unsigned long long int)m_auiRedSelectors[uiPixel]) << uiShift;
			ulliSelectorBitsG |= ((unsigned long long int)m_auiGrnSelectors[uiPixel]) << uiShift;
		}
		if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
		{
			m_pencodingbitsRG11->data.baseR = (unsigned char)roundf(m_fRedBase);
		}
		else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
		{
			m_pencodingbitsRG11->data.baseR = (signed char)roundf(m_fRedBase);
		}
		else
		{
			assert(0);
		}
		m_pencodingbitsRG11->data.tableIndexR = m_uiRedModifierTableIndex;
		m_pencodingbitsRG11->data.multiplierR = (unsigned char)roundf(m_fRedMultiplier);

		m_pencodingbitsRG11->data.selectorsR0 = ulliSelectorBitsR >> 40;
		m_pencodingbitsRG11->data.selectorsR1 = ulliSelectorBitsR >> 32;
		m_pencodingbitsRG11->data.selectorsR2 = ulliSelectorBitsR >> 24;
		m_pencodingbitsRG11->data.selectorsR3 = ulliSelectorBitsR >> 16;
		m_pencodingbitsRG11->data.selectorsR4 = ulliSelectorBitsR >> 8;
		m_pencodingbitsRG11->data.selectorsR5 = ulliSelectorBitsR;

		if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
		{
			m_pencodingbitsRG11->data.baseG = (unsigned char)roundf(m_fGrnBase);
		}
		else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
		{
			m_pencodingbitsRG11->data.baseG = (signed char)roundf(m_fGrnBase);
		}
		else
		{
			assert(0);
		}
		m_pencodingbitsRG11->data.tableIndexG = m_uiGrnModifierTableIndex;
		m_pencodingbitsRG11->data.multiplierG = (unsigned char)roundf(m_fGrnMultiplier);

		m_pencodingbitsRG11->data.selectorsG0 = ulliSelectorBitsG >> 40;
		m_pencodingbitsRG11->data.selectorsG1 = ulliSelectorBitsG >> 32;
		m_pencodingbitsRG11->data.selectorsG2 = ulliSelectorBitsG >> 24;
		m_pencodingbitsRG11->data.selectorsG3 = ulliSelectorBitsG >> 16;
		m_pencodingbitsRG11->data.selectorsG4 = ulliSelectorBitsG >> 8;
		m_pencodingbitsRG11->data.selectorsG5 = ulliSelectorBitsG;

	}

	// ----------------------------------------------------------------------------------------------------
	//
}