Fix incorrect fast AA calculations

This commit is contained in:
elasota
2021-03-30 05:23:02 -04:00
parent 7951f579e0
commit 7916c72fc4
3 changed files with 17 additions and 8 deletions

View File

@@ -90,15 +90,21 @@ namespace PortabilityLayer
rgbScaleTree[i] = (rgbScaleLinear[i] + rgbScaleLinear[i + 1]) / 2;
uint32_t toneScaleLinear[16];
uint32_t grayScaleLinear[16];
for (unsigned int i = 0; i < 16; i++)
{
unsigned int upscaled = i * 17;
toneScaleLinear[i] = upscaled * upscaled * (kDivisions - 1);
grayScaleLinear[i] = toneScaleLinear[i] * 3;
}
uint32_t toneScaleTree[15];
uint32_t grayScaleTree[15];
for (unsigned int i = 0; i < 15; i++)
{
toneScaleTree[i] = (toneScaleLinear[i] + toneScaleLinear[i + 1]) / 2;
grayScaleTree[i] = (grayScaleLinear[i] + grayScaleLinear[i + 1]) / 2;
}
for (int i = 0; i < 3; i++)
baseChLinear[i] = baseCh[i] * baseCh[i];
@@ -126,15 +132,18 @@ namespace PortabilityLayer
unsigned int toneIndexes[3];
unsigned int rgbIndexes[3];
unsigned int grayScaleIndex;
for (int i = 0; i < 3; i++)
{
toneIndexes[i] = BinTreeQuantize(toneScaleTree, toneScaleLinear, newChLinear[i]);
rgbIndexes[i] = BinTreeQuantize(rgbScaleTree, rgbScaleLinear, newChLinear[i]);
}
grayScaleIndex = BinTreeQuantize(grayScaleTree, grayScaleLinear, newChLinear[0] + newChLinear[1] + newChLinear[2]);
uint64_t toneZeroError[3];
uint64_t toneQuantizedError[3];
uint64_t grayError = 0;
uint64_t rgbError = 0;
for (int i = 0; i < 3; i++)
{
@@ -144,15 +153,18 @@ namespace PortabilityLayer
int32_t toneDelta = static_cast<int32_t>(toneScaleLinear[toneIndexes[i]]) - static_cast<int32_t>(newChLinear[i]);
toneQuantizedError[i] = static_cast<uint64_t>(static_cast<int64_t>(toneDelta) * static_cast<int64_t>(toneDelta));
int32_t rgbDelta = static_cast<int32_t>(rgbScaleLinear[toneIndexes[i]]) - static_cast<int32_t>(newChLinear[i]);
rgbError += static_cast<uint64_t>(static_cast<int64_t>(toneDelta) * static_cast<int64_t>(toneDelta));
int32_t rgbDelta = static_cast<int32_t>(rgbScaleLinear[rgbIndexes[i]]) - static_cast<int32_t>(newChLinear[i]);
rgbError += static_cast<uint64_t>(static_cast<int64_t>(rgbDelta) * static_cast<int64_t>(rgbDelta));
int32_t grayDelta = static_cast<int32_t>(toneScaleLinear[grayScaleIndex]) - static_cast<int32_t>(newChLinear[i]);
grayError += static_cast<uint64_t>(static_cast<int64_t>(grayDelta) * static_cast<int64_t>(grayDelta));
}
uint64_t possibleErrors[5];
possibleErrors[0] = toneQuantizedError[0] + toneZeroError[1] + toneZeroError[2];
possibleErrors[1] = toneZeroError[0] + toneQuantizedError[1] + toneZeroError[2];
possibleErrors[2] = toneZeroError[0] + toneZeroError[1] + toneQuantizedError[2];
possibleErrors[3] = toneQuantizedError[0] + toneQuantizedError[1] + toneQuantizedError[2];
possibleErrors[3] = grayError;
possibleErrors[4] = rgbError;
int bestErrorIndex = 0;
@@ -170,7 +182,7 @@ namespace PortabilityLayer
else if (bestErrorIndex == 2)
bestColor = StandardPalette::GetInstance()->MapColorAnalyticTruncated(0, 0, toneIndexes[2]);
else if (bestErrorIndex == 3)
bestColor = StandardPalette::GetInstance()->MapColorAnalyticTruncated(toneIndexes[0], toneIndexes[1], toneIndexes[2]);
bestColor = StandardPalette::GetInstance()->MapColorAnalyticTruncated(grayScaleIndex, grayScaleIndex, grayScaleIndex);
else //if (bestErrorIndex == 4)
bestColor = StandardPalette::GetInstance()->MapColorAnalyticTruncated(rgbIndexes[0] * 3, rgbIndexes[1] * 3, rgbIndexes[2] * 3);
@@ -179,7 +191,6 @@ namespace PortabilityLayer
}
}
#if 0
void AntiAliasTable::GenerateForPalette(const RGBAColor &baseColorRef, const RGBAColor *colors, size_t numColors, bool cacheable)
{
char cacheFileName[256];
@@ -252,7 +263,6 @@ namespace PortabilityLayer
if (cacheable)
SaveToCache(cacheFileName);
}
#endif
void AntiAliasTable::GenerateForSimpleScale(uint8_t colorChannel, bool cacheable)
{

View File

@@ -12,9 +12,7 @@ namespace PortabilityLayer
// Striped 256x16 because constant background color is more likely than constant sample
uint8_t m_aaTranslate[256][16];
#if 0
void GenerateForPalette(const RGBAColor &baseColor, const RGBAColor *colors, size_t numColors, bool cacheable);
#endif
void GenerateForPaletteFast(const RGBAColor &baseColor);
void GenerateForSimpleScale(uint8_t colorChannel, bool cacheable);

View File

@@ -262,6 +262,7 @@ namespace PortabilityLayer
if (mutex)
mutex->Unlock();
//entry.m_aaTable.GenerateForPalette(color, m_colors, 256, true);
entry.m_aaTable.GenerateForPaletteFast(color);
return entry.m_aaTable;