0

I found this code and I wonder why it does't works ..

// f is an stream

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;}
}

}

I tried to implement it and it seems not good ..

Otherwise, someone know if it exist a library or just a simple file where I can find this algorithm ?

Thanks you in advance

Pépito
  • 57
  • 4
  • 11
  • Are you trying to compress (write) or decompress (read)? – Brandon Mar 17 '14 at 00:55
  • I'm trying to write ( compress ) . f is an ostream – Pépito Mar 17 '14 at 01:14
  • "It doesn't work" isn't a particularly helpful description. Have you looked it over to see if there's something obvious? Do you understand how the TGA RLE compression is supposed to work, and compared the code with that algorithm? Or are you just looking for some TGA RLE compression code and figured that posting broken code here would prompt somebody to point you at working code? – Jim Mischel Mar 17 '14 at 13:04

0 Answers0