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
|
/*
* 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.
*/
/*
EtcBlock4x4.cpp
Implements the state associated with each 4x4 block of pixels in an image
Source images that are not a multiple of 4x4 are extended to fill the Block4x4 using pixels with an
alpha of NAN
*/
#include "EtcConfig.h"
#include "EtcBlock4x4.h"
#include "EtcBlock4x4EncodingBits.h"
#include "EtcColor.h"
#include "EtcImage.h"
#include "EtcColorFloatRGBA.h"
#include "EtcBlock4x4Encoding_RGB8.h"
#include "EtcBlock4x4Encoding_RGBA8.h"
#include "EtcBlock4x4Encoding_RGB8A1.h"
#include "EtcBlock4x4Encoding_R11.h"
#include "EtcBlock4x4Encoding_RG11.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
namespace Etc
{
// ETC pixels are scanned vertically.
// this mapping is for when someone wants to scan the ETC pixels horizontally
const unsigned int Block4x4::s_auiPixelOrderHScan[PIXELS] = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
// ----------------------------------------------------------------------------------------------------
//
Block4x4::Block4x4(void)
{
m_pimageSource = nullptr;
m_uiSourceH = 0;
m_uiSourceV = 0;
m_sourcealphamix = SourceAlphaMix::UNKNOWN;
m_boolBorderPixels = false;
m_boolPunchThroughPixels = false;
m_pencoding = nullptr;
m_errormetric = ErrorMetric::NUMERIC;
}
Block4x4::~Block4x4()
{
m_pimageSource = nullptr;
if (m_pencoding)
{
delete m_pencoding;
m_pencoding = nullptr;
}
}
// ----------------------------------------------------------------------------------------------------
// initialization prior to encoding from a source image
// [a_uiSourceH,a_uiSourceV] is the location of the block in a_pimageSource
// a_paucEncodingBits is the place to store the final encoding
// a_errormetric is used for finding the best encoding
//
void Block4x4::InitFromSource(Image *a_pimageSource,
unsigned int a_uiSourceH, unsigned int a_uiSourceV,
unsigned char *a_paucEncodingBits,
ErrorMetric a_errormetric)
{
Block4x4();
m_pimageSource = a_pimageSource;
m_uiSourceH = a_uiSourceH;
m_uiSourceV = a_uiSourceV;
m_errormetric = a_errormetric;
SetSourcePixels();
// set block encoder function
switch (m_pimageSource->GetFormat())
{
case Image::Format::ETC1:
m_pencoding = new Block4x4Encoding_ETC1;
break;
case Image::Format::RGB8:
case Image::Format::SRGB8:
m_pencoding = new Block4x4Encoding_RGB8;
break;
case Image::Format::RGBA8:
case Image::Format::SRGBA8:
if (a_errormetric == RGBX)
{
m_pencoding = new Block4x4Encoding_RGBA8;
}
else
{
switch (m_sourcealphamix)
{
case SourceAlphaMix::OPAQUE:
m_pencoding = new Block4x4Encoding_RGBA8_Opaque;
break;
case SourceAlphaMix::TRANSPARENT:
m_pencoding = new Block4x4Encoding_RGBA8_Transparent;
break;
case SourceAlphaMix::TRANSLUCENT:
m_pencoding = new Block4x4Encoding_RGBA8;
break;
default:
assert(0);
break;
}
break;
}
break;
case Image::Format::RGB8A1:
case Image::Format::SRGB8A1:
switch (m_sourcealphamix)
{
case SourceAlphaMix::OPAQUE:
m_pencoding = new Block4x4Encoding_RGB8A1_Opaque;
break;
case SourceAlphaMix::TRANSPARENT:
m_pencoding = new Block4x4Encoding_RGB8A1_Transparent;
break;
case SourceAlphaMix::TRANSLUCENT:
if (m_boolPunchThroughPixels)
{
m_pencoding = new Block4x4Encoding_RGB8A1;
}
else
{
m_pencoding = new Block4x4Encoding_RGB8A1_Opaque;
}
break;
default:
assert(0);
break;
}
break;
case Image::Format::R11:
case Image::Format::SIGNED_R11:
m_pencoding = new Block4x4Encoding_R11;
break;
case Image::Format::RG11:
case Image::Format::SIGNED_RG11:
m_pencoding = new Block4x4Encoding_RG11;
break;
default:
assert(0);
break;
}
m_pencoding->InitFromSource(this, m_afrgbaSource,
a_paucEncodingBits, a_errormetric);
}
// ----------------------------------------------------------------------------------------------------
// initialization of encoding state from a prior encoding using encoding bits
// [a_uiSourceH,a_uiSourceV] is the location of the block in a_pimageSource
// a_paucEncodingBits is the place to read the prior encoding
// a_imageformat is used to determine how to interpret a_paucEncodingBits
// a_errormetric was used for the prior encoding
//
void Block4x4::InitFromEtcEncodingBits(Image::Format a_imageformat,
unsigned int a_uiSourceH, unsigned int a_uiSourceV,
unsigned char *a_paucEncodingBits,
Image *a_pimageSource,
ErrorMetric a_errormetric)
{
Block4x4();
m_pimageSource = a_pimageSource;
m_uiSourceH = a_uiSourceH;
m_uiSourceV = a_uiSourceV;
m_errormetric = a_errormetric;
SetSourcePixels();
// set block encoder function
switch (a_imageformat)
{
case Image::Format::ETC1:
m_pencoding = new Block4x4Encoding_ETC1;
break;
case Image::Format::RGB8:
case Image::Format::SRGB8:
m_pencoding = new Block4x4Encoding_RGB8;
break;
case Image::Format::RGBA8:
case Image::Format::SRGBA8:
m_pencoding = new Block4x4Encoding_RGBA8;
break;
case Image::Format::RGB8A1:
case Image::Format::SRGB8A1:
m_pencoding = new Block4x4Encoding_RGB8A1;
break;
case Image::Format::R11:
case Image::Format::SIGNED_R11:
m_pencoding = new Block4x4Encoding_R11;
break;
case Image::Format::RG11:
case Image::Format::SIGNED_RG11:
m_pencoding = new Block4x4Encoding_RG11;
break;
default:
assert(0);
break;
}
m_pencoding->InitFromEncodingBits(this, a_paucEncodingBits, m_afrgbaSource,
m_pimageSource->GetErrorMetric());
}
// ----------------------------------------------------------------------------------------------------
// set source pixels from m_pimageSource
// set m_alphamix
//
void Block4x4::SetSourcePixels(void)
{
Image::Format imageformat = m_pimageSource->GetFormat();
// alpha census
unsigned int uiTransparentSourcePixels = 0;
unsigned int uiOpaqueSourcePixels = 0;
// copy source to consecutive memory locations
// convert from image horizontal scan to block vertical scan
unsigned int uiPixel = 0;
for (unsigned int uiBlockPixelH = 0; uiBlockPixelH < Block4x4::COLUMNS; uiBlockPixelH++)
{
unsigned int uiSourcePixelH = m_uiSourceH + uiBlockPixelH;
for (unsigned int uiBlockPixelV = 0; uiBlockPixelV < Block4x4::ROWS; uiBlockPixelV++)
{
unsigned int uiSourcePixelV = m_uiSourceV + uiBlockPixelV;
ColorFloatRGBA *pfrgbaSource = m_pimageSource->GetSourcePixel(uiSourcePixelH, uiSourcePixelV);
// if pixel extends beyond source image because of block padding
if (pfrgbaSource == nullptr)
{
m_afrgbaSource[uiPixel] = ColorFloatRGBA(0.0f, 0.0f, 0.0f, NAN); // denotes border pixel
m_boolBorderPixels = true;
uiTransparentSourcePixels++;
}
else
{
//get teh current pixel data, and store some of the attributes
//before capping values to fit the encoder type
m_afrgbaSource[uiPixel] = (*pfrgbaSource).ClampRGBA();
if (m_afrgbaSource[uiPixel].fA == 1.0f || m_errormetric == RGBX)
{
m_pimageSource->m_iNumOpaquePixels++;
}
else if (m_afrgbaSource[uiPixel].fA == 0.0f)
{
m_pimageSource->m_iNumTransparentPixels++;
}
else if(m_afrgbaSource[uiPixel].fA > 0.0f && m_afrgbaSource[uiPixel].fA < 1.0f)
{
m_pimageSource->m_iNumTranslucentPixels++;
}
else
{
m_pimageSource->m_numOutOfRangeValues.fA++;
}
if (m_afrgbaSource[uiPixel].fR != 0.0f)
{
m_pimageSource->m_numColorValues.fR++;
//make sure we are getting a float between 0-1
if (m_afrgbaSource[uiPixel].fR - 1.0f > 0.0f)
{
m_pimageSource->m_numOutOfRangeValues.fR++;
}
}
if (m_afrgbaSource[uiPixel].fG != 0.0f)
{
m_pimageSource->m_numColorValues.fG++;
if (m_afrgbaSource[uiPixel].fG - 1.0f > 0.0f)
{
m_pimageSource->m_numOutOfRangeValues.fG++;
}
}
if (m_afrgbaSource[uiPixel].fB != 0.0f)
{
m_pimageSource->m_numColorValues.fB++;
if (m_afrgbaSource[uiPixel].fB - 1.0f > 0.0f)
{
m_pimageSource->m_numOutOfRangeValues.fB++;
}
}
// for formats with no alpha, set source alpha to 1
if (imageformat == Image::Format::ETC1 ||
imageformat == Image::Format::RGB8 ||
imageformat == Image::Format::SRGB8)
{
m_afrgbaSource[uiPixel].fA = 1.0f;
}
if (imageformat == Image::Format::R11 ||
imageformat == Image::Format::SIGNED_R11)
{
m_afrgbaSource[uiPixel].fA = 1.0f;
m_afrgbaSource[uiPixel].fG = 0.0f;
m_afrgbaSource[uiPixel].fB = 0.0f;
}
if (imageformat == Image::Format::RG11 ||
imageformat == Image::Format::SIGNED_RG11)
{
m_afrgbaSource[uiPixel].fA = 1.0f;
m_afrgbaSource[uiPixel].fB = 0.0f;
}
// for RGB8A1, set source alpha to 0.0 or 1.0
// set punch through flag
if (imageformat == Image::Format::RGB8A1 ||
imageformat == Image::Format::SRGB8A1)
{
if (m_afrgbaSource[uiPixel].fA >= 0.5f)
{
m_afrgbaSource[uiPixel].fA = 1.0f;
}
else
{
m_afrgbaSource[uiPixel].fA = 0.0f;
m_boolPunchThroughPixels = true;
}
}
if (m_afrgbaSource[uiPixel].fA == 1.0f || m_errormetric == RGBX)
{
uiOpaqueSourcePixels++;
}
else if (m_afrgbaSource[uiPixel].fA == 0.0f)
{
uiTransparentSourcePixels++;
}
}
uiPixel += 1;
}
}
if (uiOpaqueSourcePixels == PIXELS)
{
m_sourcealphamix = SourceAlphaMix::OPAQUE;
}
else if (uiTransparentSourcePixels == PIXELS)
{
m_sourcealphamix = SourceAlphaMix::TRANSPARENT;
}
else
{
m_sourcealphamix = SourceAlphaMix::TRANSLUCENT;
}
}
// ----------------------------------------------------------------------------------------------------
// return a name for the encoding mode
//
const char * Block4x4::GetEncodingModeName(void)
{
switch (m_pencoding->GetMode())
{
case Block4x4Encoding::MODE_ETC1:
return "ETC1";
case Block4x4Encoding::MODE_T:
return "T";
case Block4x4Encoding::MODE_H:
return "H";
case Block4x4Encoding::MODE_PLANAR:
return "PLANAR";
default:
return "???";
}
}
// ----------------------------------------------------------------------------------------------------
//
}
|