C ++上的TGA RLE压缩(写入)算法

时间:2014-03-17 00:38:36

标签: c++ algorithm run-length-encoding tga

我找到了这段代码,我想知道为什么它不起作用。

// f是一个流

void writeTGA(ostream& f, bool rle) const{


f<<ubyte(0);//ID length
f<<ubyte(0);//Color map Type

if(rle)
    f<<ubyte(10);//Image Type Code
else
    f<<ubyte(2);//Image Type Code

f<<ubyte(0);//Color map stuff
f<<ubyte(0);
f<<ubyte(0);
f<<ubyte(0);
f<<ubyte(0);
f<<ubyte(0);//X Origin of Image
f<<ubyte(0);//X Origin of Image
f<<ubyte(0);//Y Origin of Image
f<<ubyte(0);//Y Origin of Image
f << ubyte(width%256);
f << ubyte(width>>8);
f << ubyte(height%256);
f << ubyte(height>>8);
f<<ubyte(24);//Image Pixels Size
f<<ubyte(32);//Image Descriptor Byte
if(rle)
{
    cout<<"rleHeadHead"<<endl;
    ubyte rleHead = 0;
    ubyte diff[128];
    int i = 0;
    while(i<width*height)
        {
            rleHead = 1;
            /* RLE */
            if(i+1 < width*height)
                while(pixels[i] == pixels[i+1]) {
                    if(i+1 >= width*height || rleHead >= 128)
                        break;

                    rleHead++;
                    i++;
                }

            if(rleHead > 1)
            {
                f<< (rleHead+127);
                f<<pixels[i].b;
                f<<pixels[i].g;
                f<<pixels[i].r;
            }

            rleHead = 0;
            /* RAW */
            if(i+1 < width*height)
            {
                while(pixels[i+rleHead] != pixels[i+rleHead+1])
                {
                    if( (i+rleHead+1) >= width*height || rleHead >= 128)
                        break;

                    rleHead++;
                }
            } else
                rleHead++;

            if(rleHead > 0)
            {
                f << (rleHead-1);

                for(int j = 0; j < rleHead; j++)
                {
                    diff[j] = pixels[i+j].b;
                    diff[j] = pixels[i+j].g;
                    diff[j] = pixels[i+j].r;
                }
                f.write((const char*) diff, rle*3);
                i += rleHead;
            }}}


else{
        for(int i = 0 ; i < width*height ; i++){
            f<< pixels[i].b;
            f<< pixels[i].g;
            f<< pixels[i].r;}
}

}

我试图实现它,似乎不太好......

否则,有人知道它是否存在库或只是一个简单的文件,我可以找到这个算法?

提前谢谢你

0 个答案:

没有答案