| 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.cpp |
| 19 |
|
| 20 |
Block4x4Encoding is the abstract base class for the different encoders. Each encoder targets a |
| 21 |
particular file format (e.g. ETC1, RGB8, RGBA8, R11) |
| 22 |
|
| 23 |
*/ |
| 24 |
|
| 25 |
#include "EtcConfig.h" |
| 26 |
#include "EtcBlock4x4Encoding.h" |
| 27 |
|
| 28 |
#include "EtcBlock4x4EncodingBits.h" |
| 29 |
#include "EtcBlock4x4.h" |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <string.h> |
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
namespace Etc |
| 36 |
{ |
| 37 |
// ---------------------------------------------------------------------------------------------------- |
| 38 |
// |
| 39 |
const float Block4x4Encoding::LUMA_WEIGHT = 3.0f; |
| 40 |
const float Block4x4Encoding::CHROMA_BLUE_WEIGHT = 0.5f; |
| 41 |
|
| 42 |
// ---------------------------------------------------------------------------------------------------- |
| 43 |
// |
| 44 |
Block4x4Encoding::Block4x4Encoding(void) |
| 45 |
{ |
| 46 |
|
| 47 |
m_pblockParent = nullptr; |
| 48 |
|
| 49 |
m_pafrgbaSource = nullptr; |
| 50 |
|
| 51 |
m_boolBorderPixels = false; |
| 52 |
|
| 53 |
m_fError = -1.0f; |
| 54 |
|
| 55 |
m_mode = MODE_UNKNOWN; |
| 56 |
|
| 57 |
m_uiEncodingIterations = 0; |
| 58 |
m_boolDone = false; |
| 59 |
|
| 60 |
for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++) |
| 61 |
{ |
| 62 |
m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(-1.0f, -1.0f, -1.0f, -1.0f); |
| 63 |
m_afDecodedAlphas[uiPixel] = -1.0f; |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
// ---------------------------------------------------------------------------------------------------- |
| 69 |
// initialize the generic encoding for a 4x4 block |
| 70 |
// a_pblockParent points to the block associated with this encoding |
| 71 |
// a_errormetric is used to choose the best encoding |
| 72 |
// init the decoded pixels to -1 to mark them as undefined |
| 73 |
// init the error to -1 to mark it as undefined |
| 74 |
// |
| 75 |
void Block4x4Encoding::Init(Block4x4 *a_pblockParent, |
| 76 |
ColorFloatRGBA *a_pafrgbaSource, |
| 77 |
ErrorMetric a_errormetric) |
| 78 |
{ |
| 79 |
|
| 80 |
m_pblockParent = a_pblockParent; |
| 81 |
|
| 82 |
m_pafrgbaSource = a_pafrgbaSource; |
| 83 |
|
| 84 |
m_boolBorderPixels = m_pblockParent->HasBorderPixels(); |
| 85 |
|
| 86 |
m_fError = -1.0f; |
| 87 |
|
| 88 |
m_uiEncodingIterations = 0; |
| 89 |
|
| 90 |
m_errormetric = a_errormetric; |
| 91 |
|
| 92 |
for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++) |
| 93 |
{ |
| 94 |
m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(-1.0f, -1.0f, -1.0f, -1.0f); |
| 95 |
m_afDecodedAlphas[uiPixel] = -1.0f; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
// ---------------------------------------------------------------------------------------------------- |
| 101 |
// calculate the error for the block by summing the pixel errors |
| 102 |
// |
| 103 |
void Block4x4Encoding::CalcBlockError(void) |
| 104 |
{ |
| 105 |
m_fError = 0.0f; |
| 106 |
|
| 107 |
for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++) |
| 108 |
{ |
| 109 |
m_fError += CalcPixelError(m_afrgbaDecodedColors[uiPixel], m_afDecodedAlphas[uiPixel], |
| 110 |
m_pafrgbaSource[uiPixel]); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
// ---------------------------------------------------------------------------------------------------- |
| 116 |
// calculate the error between the source pixel and the decoded pixel |
| 117 |
// the error amount is base on the error metric |
| 118 |
// |
| 119 |
float Block4x4Encoding::CalcPixelError(ColorFloatRGBA a_frgbaDecodedColor, float a_fDecodedAlpha, |
| 120 |
ColorFloatRGBA a_frgbaSourcePixel) |
| 121 |
{ |
| 122 |
|
| 123 |
// if a border pixel |
| 124 |
if (isnan(a_frgbaSourcePixel.fA)) |
| 125 |
{ |
| 126 |
return 0.0f; |
| 127 |
} |
| 128 |
|
| 129 |
if (m_errormetric == ErrorMetric::RGBA) |
| 130 |
{ |
| 131 |
assert(a_fDecodedAlpha >= 0.0f); |
| 132 |
|
| 133 |
float fDRed = (a_fDecodedAlpha * a_frgbaDecodedColor.fR) - |
| 134 |
(a_frgbaSourcePixel.fA * a_frgbaSourcePixel.fR); |
| 135 |
float fDGreen = (a_fDecodedAlpha * a_frgbaDecodedColor.fG) - |
| 136 |
(a_frgbaSourcePixel.fA * a_frgbaSourcePixel.fG); |
| 137 |
float fDBlue = (a_fDecodedAlpha * a_frgbaDecodedColor.fB) - |
| 138 |
(a_frgbaSourcePixel.fA * a_frgbaSourcePixel.fB); |
| 139 |
|
| 140 |
float fDAlpha = a_fDecodedAlpha - a_frgbaSourcePixel.fA; |
| 141 |
|
| 142 |
return fDRed*fDRed + fDGreen*fDGreen + fDBlue*fDBlue + fDAlpha*fDAlpha; |
| 143 |
} |
| 144 |
else if (m_errormetric == ErrorMetric::RGBX) |
| 145 |
{ |
| 146 |
assert(a_fDecodedAlpha >= 0.0f); |
| 147 |
|
| 148 |
float fDRed = a_frgbaDecodedColor.fR - a_frgbaSourcePixel.fR; |
| 149 |
float fDGreen = a_frgbaDecodedColor.fG - a_frgbaSourcePixel.fG; |
| 150 |
float fDBlue = a_frgbaDecodedColor.fB - a_frgbaSourcePixel.fB; |
| 151 |
float fDAlpha = a_fDecodedAlpha - a_frgbaSourcePixel.fA; |
| 152 |
|
| 153 |
return fDRed*fDRed + fDGreen*fDGreen + fDBlue*fDBlue + fDAlpha*fDAlpha; |
| 154 |
} |
| 155 |
else if (m_errormetric == ErrorMetric::REC709) |
| 156 |
{ |
| 157 |
assert(a_fDecodedAlpha >= 0.0f); |
| 158 |
|
| 159 |
float fLuma1 = a_frgbaSourcePixel.fR*0.2126f + a_frgbaSourcePixel.fG*0.7152f + a_frgbaSourcePixel.fB*0.0722f; |
| 160 |
float fChromaR1 = 0.5f * ((a_frgbaSourcePixel.fR - fLuma1) * (1.0f / (1.0f - 0.2126f))); |
| 161 |
float fChromaB1 = 0.5f * ((a_frgbaSourcePixel.fB - fLuma1) * (1.0f / (1.0f - 0.0722f))); |
| 162 |
|
| 163 |
float fLuma2 = a_frgbaDecodedColor.fR*0.2126f + |
| 164 |
a_frgbaDecodedColor.fG*0.7152f + |
| 165 |
a_frgbaDecodedColor.fB*0.0722f; |
| 166 |
float fChromaR2 = 0.5f * ((a_frgbaDecodedColor.fR - fLuma2) * (1.0f / (1.0f - 0.2126f))); |
| 167 |
float fChromaB2 = 0.5f * ((a_frgbaDecodedColor.fB - fLuma2) * (1.0f / (1.0f - 0.0722f))); |
| 168 |
|
| 169 |
float fDeltaL = a_frgbaSourcePixel.fA * fLuma1 - a_fDecodedAlpha * fLuma2; |
| 170 |
float fDeltaCr = a_frgbaSourcePixel.fA * fChromaR1 - a_fDecodedAlpha * fChromaR2; |
| 171 |
float fDeltaCb = a_frgbaSourcePixel.fA * fChromaB1 - a_fDecodedAlpha * fChromaB2; |
| 172 |
|
| 173 |
float fDAlpha = a_fDecodedAlpha - a_frgbaSourcePixel.fA; |
| 174 |
|
| 175 |
// Favor Luma accuracy over Chroma, and Red over Blue |
| 176 |
return LUMA_WEIGHT*fDeltaL*fDeltaL + |
| 177 |
fDeltaCr*fDeltaCr + |
| 178 |
CHROMA_BLUE_WEIGHT*fDeltaCb*fDeltaCb + |
| 179 |
fDAlpha*fDAlpha; |
| 180 |
#if 0 |
| 181 |
float fDRed = a_frgbaDecodedPixel.fR - a_frgbaSourcePixel.fR; |
| 182 |
float fDGreen = a_frgbaDecodedPixel.fG - a_frgbaSourcePixel.fG; |
| 183 |
float fDBlue = a_frgbaDecodedPixel.fB - a_frgbaSourcePixel.fB; |
| 184 |
return 2.0f * 3.0f * fDeltaL * fDeltaL + fDRed*fDRed + fDGreen*fDGreen + fDBlue*fDBlue; |
| 185 |
#endif |
| 186 |
} |
| 187 |
else if (m_errormetric == ErrorMetric::NORMALXYZ) |
| 188 |
{ |
| 189 |
float fDecodedX = 2.0f * a_frgbaDecodedColor.fR - 1.0f; |
| 190 |
float fDecodedY = 2.0f * a_frgbaDecodedColor.fG - 1.0f; |
| 191 |
float fDecodedZ = 2.0f * a_frgbaDecodedColor.fB - 1.0f; |
| 192 |
|
| 193 |
float fDecodedLength = sqrtf(fDecodedX*fDecodedX + fDecodedY*fDecodedY + fDecodedZ*fDecodedZ); |
| 194 |
|
| 195 |
if (fDecodedLength < 0.5f) |
| 196 |
{ |
| 197 |
return 1.0f; |
| 198 |
} |
| 199 |
else if (fDecodedLength == 0.0f) |
| 200 |
{ |
| 201 |
fDecodedX = 1.0f; |
| 202 |
fDecodedY = 0.0f; |
| 203 |
fDecodedZ = 0.0f; |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
fDecodedX /= fDecodedLength; |
| 208 |
fDecodedY /= fDecodedLength; |
| 209 |
fDecodedZ /= fDecodedLength; |
| 210 |
} |
| 211 |
|
| 212 |
float fSourceX = 2.0f * a_frgbaSourcePixel.fR - 1.0f; |
| 213 |
float fSourceY = 2.0f * a_frgbaSourcePixel.fG - 1.0f; |
| 214 |
float fSourceZ = 2.0f * a_frgbaSourcePixel.fB - 1.0f; |
| 215 |
|
| 216 |
float fSourceLength = sqrtf(fSourceX*fSourceX + fSourceY*fSourceY + fSourceZ*fSourceZ); |
| 217 |
|
| 218 |
if (fSourceLength == 0.0f) |
| 219 |
{ |
| 220 |
fSourceX = 1.0f; |
| 221 |
fSourceY = 0.0f; |
| 222 |
fSourceZ = 0.0f; |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
fSourceX /= fSourceLength; |
| 227 |
fSourceY /= fSourceLength; |
| 228 |
fSourceZ /= fSourceLength; |
| 229 |
} |
| 230 |
|
| 231 |
float fDotProduct = fSourceX*fDecodedX + fSourceY*fDecodedY + fSourceZ*fDecodedZ; |
| 232 |
float fNormalizedDotProduct = 1.0f - 0.5f * (fDotProduct + 1.0f); |
| 233 |
float fDotProductError = fNormalizedDotProduct * fNormalizedDotProduct; |
| 234 |
|
| 235 |
float fLength2 = fDecodedX*fDecodedX + fDecodedY*fDecodedY + fDecodedZ*fDecodedZ; |
| 236 |
float fLength2Error = fabsf(1.0f - fLength2); |
| 237 |
|
| 238 |
float fDeltaW = a_frgbaDecodedColor.fA - a_frgbaSourcePixel.fA; |
| 239 |
float fErrorW = fDeltaW * fDeltaW; |
| 240 |
|
| 241 |
return fDotProductError + fLength2Error + fErrorW; |
| 242 |
} |
| 243 |
else // ErrorMetric::NUMERIC |
| 244 |
{ |
| 245 |
assert(a_fDecodedAlpha >= 0.0f); |
| 246 |
|
| 247 |
float fDX = a_frgbaDecodedColor.fR - a_frgbaSourcePixel.fR; |
| 248 |
float fDY = a_frgbaDecodedColor.fG - a_frgbaSourcePixel.fG; |
| 249 |
float fDZ = a_frgbaDecodedColor.fB - a_frgbaSourcePixel.fB; |
| 250 |
float fDW = a_frgbaDecodedColor.fA - a_frgbaSourcePixel.fA; |
| 251 |
|
| 252 |
return fDX*fDX + fDY*fDY + fDZ*fDZ + fDW*fDW; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
// ---------------------------------------------------------------------------------------------------- |
| 258 |
// |
| 259 |
|
| 260 |
} // namespace Etc |
| 261 |
|