Thread: TGA RLE compressing ( writing ) algorithm on c++

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    TGA RLE compressing ( writing ) algorithm on c++

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


    // f is an stream

    Code:
        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

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    It's an incomplete section of code to write a TGA image. It's would probably work if you had the rest of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a Delete Algorithm in an ordered linked list
    By coolguy67 in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2013, 09:27 AM
  2. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  3. Compressing
    By C_ntua in forum C Programming
    Replies: 2
    Last Post: 07-17-2008, 06:01 AM
  4. help with compressing images
    By dragunov in forum C Programming
    Replies: 8
    Last Post: 08-31-2007, 05:52 AM
  5. Replies: 1
    Last Post: 11-17-2002, 10:52 AM