2017-04-07 34 views
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()会给我错误的结果。 有什么帮助吗?

回答

1

如果仔细查看调试器中的变量,您会看到std::vector<CrByte> decompressed(imageSize);声明了一个带有imageSize元素的向量。然后在DecompressRLE中插入该矢量的末尾,导致它增长。这就是为什么你的解压缩图像充满了零,并且为什么它需要这么长时间(因为矢量会周期性地调整大小)。

你想要做什么是预留的空间:

std::vector<CrByte> decompressed; 
decompressed.reserve(imageSize); 

你的压缩缓冲看起来是不是文件内容较大,所以你还是过去解压文件的末尾。压缩文件的大小应该在Header。用它。

+0

谢谢! “压缩的文件大小应该在标题中。”你的头是什么意思? targa头文件或运行长度数据包头文件? – mutex36

+0

@ mutex36 targa头文件。 – 1201ProgramAlarm

+0

根据[本规范](http://paulbourke.net/dataformats/tga/),标题中没有指定压缩大小?只是宽度和高度,它等于最终的未压缩图像的宽度和高度。 – mutex36