针对8*8大小的ARGB像素数据块进行无损压缩和解压. 完善以下解压、压缩函数,其中pClrBlk为原始数据块,pTile为压缩后的数据块 int argb2tile(const unsigned char* pClrBlk, unsigned char* pTile, int* pTileSize) int tile2argb(const unsigned char* pTile, int nTileSize, unsigned char* pClrBlk)
时间: 2023-08-06 11:03:48 浏览: 98
压缩函数argb2tile:
```
int argb2tile(const unsigned char* pClrBlk, unsigned char* pTile, int* pTileSize)
{
int blockSize = 64; // 每个块的大小为64字节
int tileIndex = 0;
int count = 1; // 当前颜色块出现次数
int i;
unsigned char clr[4]; // 存储当前颜色块的ARGB值
for(i = 0; i < blockSize; i += 4)
{
if(i == 0) // 第一个颜色块
{
clr[0] = pClrBlk[i];
clr[1] = pClrBlk[i + 1];
clr[2] = pClrBlk[i + 2];
clr[3] = pClrBlk[i + 3];
}
else // 不是第一个颜色块
{
if(pClrBlk[i] == clr[0] && pClrBlk[i + 1] == clr[1] && pClrBlk[i + 2] == clr[2] && pClrBlk[i + 3] == clr[3])
{
count++; // 当前颜色块与前一个颜色块相同,计数器加1
}
else // 当前颜色块与前一个颜色块不同
{
// 将前一个颜色块的出现次数和ARGB值写入压缩后的数据块
pTile[tileIndex++] = count;
pTile[tileIndex++] = clr[0];
pTile[tileIndex++] = clr[1];
pTile[tileIndex++] = clr[2];
pTile[tileIndex++] = clr[3];
count = 1; // 重置计数器
clr[0] = pClrBlk[i];
clr[1] = pClrBlk[i + 1];
clr[2] = pClrBlk[i + 2];
clr[3] = pClrBlk[i + 3];
}
}
}
// 将最后一个颜色块的出现次数和ARGB值写入压缩后的数据块
pTile[tileIndex++] = count;
pTile[tileIndex++] = clr[0];
pTile[tileIndex++] = clr[1];
pTile[tileIndex++] = clr[2];
pTile[tileIndex++] = clr[3];
*pTileSize = tileIndex; // 记录压缩后的数据块大小
return 0;
}
```
解压函数tile2argb:
```
int tile2argb(const unsigned char* pTile, int nTileSize, unsigned char* pClrBlk)
{
int blockSize = 64; // 每个块的大小为64字节
int tileIndex = 0;
int i, j;
unsigned char clr[4]; // 存储当前颜色块的ARGB值
for(i = 0; i < nTileSize; i += 5)
{
int count = pTile[i]; // 当前颜色块的出现次数
clr[0] = pTile[i + 1];
clr[1] = pTile[i + 2];
clr[2] = pTile[i + 3];
clr[3] = pTile[i + 4];
for(j = 0; j < count; j++)
{
// 将当前颜色块的ARGB值复制到解压后的数据块中
pClrBlk[tileIndex++] = clr[0];
pClrBlk[tileIndex++] = clr[1];
pClrBlk[tileIndex++] = clr[2];
pClrBlk[tileIndex++] = clr[3];
}
}
return 0;
}
```
阅读全文