Png 转成ETC

(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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值