c++ - Targa Run-Length encoding -
i trying decompress targa (rgb24_rle) image data.
my algorithm looks this:
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) { (auto = a_in.begin(); != a_in.end();) { //read packet header int header = *it & 0xff; int count = (header & 0x7f) + 1; if ((header & 0x80) != 0) //packet type { //for run length packet, header followed //a single color value, assumed repeated //the number of times specified in header. auto pastart = + kpacketheadersize; auto paend = pastart + a_byteperpixel; //insert packets output buffer (size_t pk = 0; pk < count; ++pk) { a_out.insert(a_out.end(), pastart, paend); } //jump next header std::advance(it, kpacketheadersize + a_byteperpixel); } else { //for raw packet, header s followed //the number of color values specified in header. auto pastart = + kpacketheadersize; auto paend = pastart + count * a_byteperpixel; //insert packets output buffer a_out.insert(a_out.end(), pastart, paend); //jump next header std::advance(it, kpacketheadersize + count * a_byteperpixel); } } }
it called here:
//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
defined this:
size_t imagesize = hd.width * hd.height * byteperpixel
however, after decompressrle() finishes (which takes very long time 2048x2048 textures) decompressed still empty/only contains zeros. maybe missing somehting out.
count
seems unreasonably high sometimes, think abnormal. compressedsize
should less imagesize, otherwise it's not compressed. however, using ifstream::tellg()
gives me wrong results. help?
if @ variables in debugger, see std::vector<crbyte> decompressed(imagesize);
declares vector imagesize
elements in it. in decompressrle
insert @ end of vector, causing grow. why decompressed image full of zeros, , why takes long (because vector resized periodically).
what want reserve space:
std::vector<crbyte> decompressed; decompressed.reserve(imagesize);
your compressed buffer looks larger file content, you're still decompressing past end of file. compressed file size should in header
. use it.
Comments
Post a Comment