PCL XL Delta Row Compression Method (eDeltaRowCompression) c++实现

FX_BOOL Encode(FX_BYTE* inPut, FX_INT32 width, FX_INT32 height, FX_BYTE*& outPut, FX_INT32& encodeLength)
{
    AllocProxy<FX_BYTE> pResult((width * 2 + 4) * height);
    AllocProxy<FX_INT16> pLength(height);
    AllocProxy<FX_BYTE> pFirstSeed(width);
    memset(pFirstSeed, 0, width);
    FX_BYTE* pSeed = pFirstSeed;

    for (int i = 0; i < height; i++)
    {
        FX_INT32 rowLength = 0;
        FX_BYTE* pOut = pResult + (width * 2 + 4) * i;
        if (i == 1)
        {
            pOut[rowLength++] = 0;
            pOut[rowLength++] = 0;
        }
        rowLength += 2;
        if (i >= 1)
        {
            pSeed = inPut + (i - 1) * width;
        }
        FX_BYTE* rowInput = inPut + (width * i);
        CompressRow(pSeed, rowInput, width, pOut, rowLength);

        if (i == 1)
        {
            pOut[2] = rowLength - 4;
            pOut[3] = (rowLength - 4) >> 8;
        }
        else
        {
            pOut[0] = rowLength - 2;
            pOut[1] = (rowLength - 2) >> 8;
        }
        if (rowLength >= 65535)
            return FALSE;
        pLength[i] = rowLength;
    }

    for (int i = 0; i < height; i++)
    {
        encodeLength += pLength[i];
    }

    outPut = FX_Alloc(FX_BYTE, encodeLength);
    FX_INT32 copyOffSet = 0;
    for (int i = 0; i < height; i++)
    {
        memcpy(outPut + copyOffSet, pResult + (width * 2 + 4) * i, pLength[i]);
        copyOffSet += pLength[i];
    }
    return TRUE;
}


void SetCommand(FX_BYTE* inPut, FX_BYTE* outPut, FX_INT32& index, FX_INT32 offSet, FX_INT32 replaceNum)
{
    if (offSet <= 30)
    {
        outPut[index++] = offSet | ((replaceNum - 1) << 5);
    }
    else
    {
        outPut[index++] = 0x1f | ((replaceNum - 1) << 5);
        offSet -= 31;
        while (offSet >= 255)
        {
            outPut[index++] = 0xff;
            offSet -= 255;
        }
        outPut[index++] = offSet;
    }

    for (FX_INT32 i = 0; i < replaceNum; i++)
    {
        outPut[index++] = inPut[i];
    }
}

void CompressRow(FX_BYTE* seed, FX_BYTE* inPut, FX_INT32 inLength, FX_BYTE* outPut, FX_INT32& outLength)
{
    FX_INT32 offSet = 0;
    FX_INT32 replaceNum = 0;
    FX_BYTE replaceValue = 0;

    for (FX_INT32 i = 0; i < inLength; i++)
    {
        if (inPut[i] == seed[i])
            offSet++;
        else
        {
            replaceNum = 0;
            while ((i + replaceNum) < inLength
                && replaceNum < 9
                && inPut[i + replaceNum] != seed[i + replaceNum])
            {
                replaceNum++;
            }
            if (replaceNum >= 9)
                replaceNum = 8;
            SetCommand(inPut + i, outPut, outLength, offSet, replaceNum);
            i += replaceNum - 1;
            offSet = 0;
        }
    }
}

 

转载于:https://www.cnblogs.com/DeepFlavor/p/9753883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值