(1)在cocos2dx 3.17.2的基础上,Image类中实现saveImageToETC,核心代码:
bool Image::saveImageToETC(const std::string& filePath)
{
FILE * outfile = nullptr;
int bytePerPixel = 3;
unsigned int stride = _width * bytePerPixel;
outfile = fopen(FileUtils::getInstance()->getSuitableFOpen(filePath).c_str(), "wb");
if (outfile == nullptr)
{
return false;
}
int dataLen = etc1_get_encoded_data_size(_width, _height);
int etcAllDataSize = dataLen + ETC_PKM_HEADER_SIZE;
unsigned char* dataTexture = new unsigned char[etcAllDataSize];
memset(dataTexture, 0, etcAllDataSize);
etc1_pkm_format_header(dataTexture, _width, _height);
int iLen = etc1_encode_image(_data, _width, _height, bytePerPixel, stride, dataTexture+ ETC_PKM_HEADER_SIZE);
if (iLen == -1)
{
fclose(outfile);
delete[]dataTexture;
return false;
}
struct CCZHeader tempCczHead;
tempCczHead.sig[0] = 'C';
tempCczHead.sig[1] = 'C';
tempCczHead.sig[2] = 'Z';
tempCczHead.sig[3] = '!';
tempCczHead.version = 0x0100;
tempCczHead.compression_type = CCZ_COMPRESSION_ZLIB;
int compressSize = compressBound(uLong(etcAllDataSize));
unsigned char* pZipData = new unsigned char[compressSize];
memset(pZipData, 0, compressSize);
uLongf iDesLen = compressSize;
compress((Bytef *)pZipData, &iDesLen, (const Bytef *)dataTexture, (uLong)etcAllDataSize);
int tempLen = compressSize;
tempLen = ((tempLen & 0xff000000) >> 24 | (tempLen & 0x00ff0000) >> 8 | (tempLen & 0x0000ff00) << 8 | (tempLen & 0x000000ff) << 24);
//int tempLen02 = CC_SWAP_INT32_BIG_TO_HOST(tempLen);
tempCczHead.len = tempLen;
fwrite(&tempCczHead, sizeof(tempCczHead), 1, outfile);
fwrite(pZipData, sizeof(char), iDesLen, outfile);
fclose(outfile);
delete[]pZipData;
delete []dataTexture;
return true;
}