MCPcopy Create free account
hub / github.com/axmolengine/axmol / initWithETCData

Method initWithETCData

core/platform/Image.cpp:1975–2035  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1973}
1974
1975bool Image::initWithETCData(uint8_t* data, ssize_t dataLen, bool ownData)
1976{
1977 const etc1_byte* header = static_cast<const etc1_byte*>(data);
1978 uint32_t pixelOffset;
1979 // check the data
1980 if (etc1_pkm_is_valid(header))
1981 {
1982 _width = etc1_pkm_get_width(header);
1983 _height = etc1_pkm_get_height(header);
1984
1985 if (0 == _width || 0 == _height)
1986 return false;
1987 pixelOffset = ETC_PKM_HEADER_SIZE;
1988 }
1989 else
1990 { // we can safe trait as KTX v1 header
1991 auto header = (KTXv1Header*)data;
1992 _width = header->pixelWidth;
1993 _height = header->pixelHeight;
1994
1995 if (0 == _width || 0 == _height)
1996 return false;
1997 pixelOffset = KTX_V1_HEADER_SIZE + header->bytesOfKeyValueData + 4;
1998 }
1999
2000 // GL_ETC1_RGB8_OES is not available in any desktop GL extension but the compression
2001 // format is forwards compatible so just use the ETC2 format.
2002 backend::PixelFormat compressedFormat;
2003 if (Configuration::getInstance()->supportsETC1())
2004 compressedFormat = backend::PixelFormat::ETC1;
2005 else if (Configuration::getInstance()->supportsETC2())
2006 compressedFormat = backend::PixelFormat::ETC2_RGB;
2007 else
2008 compressedFormat = backend::PixelFormat::NONE;
2009
2010 if (compressedFormat != backend::PixelFormat::NONE)
2011 {
2012 _pixelFormat = compressedFormat;
2013 forwardPixels(data, dataLen, pixelOffset, ownData);
2014 return true;
2015 }
2016 else
2017 {
2018 AXLOGW("Hardware ETC1 decoder not present. Using software decoder");
2019
2020 _dataLen = _width * _height * 4;
2021 _data = static_cast<uint8_t*>(malloc(_dataLen));
2022 if (etc2_decode_image(ETC2_RGB_NO_MIPMAPS, static_cast<const uint8_t*>(data) + pixelOffset,
2023 static_cast<etc2_byte*>(_data), _width, _height) == 0)
2024 { // if it is not gles or device do not support ETC1, decode texture by software
2025 // directly decode ETC1_RGB to RGBA8888
2026 _pixelFormat = backend::PixelFormat::RGBA8;
2027 return true;
2028 }
2029
2030 // software decode fail, release pixels data
2031 AX_SAFE_FREE(_data);
2032 _dataLen = 0;

Callers

nothing calls this directly

Calls 7

etc1_pkm_is_validFunction · 0.85
etc1_pkm_get_widthFunction · 0.85
etc1_pkm_get_heightFunction · 0.85
getInstanceFunction · 0.85
etc2_decode_imageFunction · 0.85
supportsETC1Method · 0.80
supportsETC2Method · 0.80

Tested by

no test coverage detected