0
我目前正在尝试解压缩targa(RGB24_RLE)图像数据。Targa游程长度编码
我的算法是这样的:
static constexpr size_t kPacketHeaderSize = sizeof(char);
//http://paulbourke.net/dataformats/tga/
inline void DecompressRLE(unsigned int a_BytePerPixel, std::vector<CrByte>& a_In, std::vector<CrByte>& a_Out)
{
for (auto it = a_In.begin(); it != a_In.end();)
{
//Read packet header
int header = *it & 0xFF;
int count = (header & 0x7F) + 1;
if ((header & 0x80) != 0) //packet type
{
//For the run length packet, the header is followed by
//a single color value, which is assumed to be repeated
//the number of times specified in the header.
auto paStart = it + kPacketHeaderSize;
auto paEnd = paStart + a_BytePerPixel;
//Insert packets into output buffer
for (size_t pk = 0; pk < count; ++pk)
{
a_Out.insert(a_Out.end(), paStart, paEnd);
}
//Jump to next header
std::advance(it, kPacketHeaderSize + a_BytePerPixel);
}
else
{
//For the raw packet, the header s followed by
//the number of color values specified in the header.
auto paStart = it + kPacketHeaderSize;
auto paEnd = paStart + count * a_BytePerPixel;
//Insert packets into output buffer
a_Out.insert(a_Out.end(), paStart, paEnd);
//Jump to next header
std::advance(it, kPacketHeaderSize + count * a_BytePerPixel);
}
}
}
在此呼吁:
//Read compressed data
std::vector<CrByte> compressed(imageSize);
ifs.seekg(sizeof(Header), std::ifstream::beg);
ifs.read(reinterpret_cast<char*>(compressed.data()), imageSize);
//Decompress
std::vector<CrByte> decompressed(imageSize);
DecompressRLE(bytePerPixel, compressed, decompressed);
imageSize
的定义是这样的:
size_t imageSize = hd.width * hd.height * bytePerPixel
然而,DecompressRLE()完成后(这需要很长的时间与2048x2048纹理)解压仍然是空的/只包含零。也许我错过了某些东西。
count
有时似乎不合理地高,我认为这是不正常的。 compressedSize
应该小于imageSize,否则它不会被压缩。但是,使用ifstream::tellg()
会给我错误的结果。 有什么帮助吗?
谢谢! “压缩的文件大小应该在标题中。”你的头是什么意思? targa头文件或运行长度数据包头文件? – mutex36
@ mutex36 targa头文件。 – 1201ProgramAlarm
根据[本规范](http://paulbourke.net/dataformats/tga/),标题中没有指定压缩大小?只是宽度和高度,它等于最终的未压缩图像的宽度和高度。 – mutex36