Loading TGA Image

Apprentice
Posts: 13
Joined: 2006.09
Post: #1
Hello!
I'm here with a question about targa pictures for you experts Wink!
I'm building a 3D engine using openGL basics routines (just wasting my time LOL) but I've got a problem with my ctarga class: in fact, loading the targa's header, the width and the height fields (with a 640x480 image) get negative values. I've tried to put them into unsigned short, but the values jump from 20000 to 50000 Cry

This is my code!
Code:
// from etarga.h
[...]
typedef struct eeTGAheader {
    char headersize, colortype, imagetype;
    short colorindex, colorlength;
    char colorbits;
    unsigned short xorigin, yorigin, width, height;
    char bpp, description;
} eeTGAheader;
[...]

// from etarga.cpp
[...]
int ctarga::initialize (const char *filename) {
    FILE *inputfile = fopen(filename, "rb");
    unsigned char backup;
    unsigned int total = 0, index;
    int bppmode = 0;
    eeTGAresult returncode = RDONE;
    if (inputfile) {
        if (fread(&header, 1, sizeof(eeTGAheader), inputfile) >= sizeof(eeTGAheader)) {
            if ((header.imagetype == 2) || (header.imagetype == 3)) {
                bppmode = (header.bpp/8);
                total = (header.height * header.width * bppmode);
                if (datablock) { free(datablock); datablock = NULL; }
                if (datablock = (unsigned char *) malloc (total)) {
                    if (header.headersize > 0) fseek(inputfile, SEEK_CUR, header.headersize);
                    fread(datablock, total, 1, inputfile);
                    if (bppmode >= 3) {
                        for (index = 0; index < total; index += bppmode) {
                            backup = datablock[index];
                            datablock[index] = datablock[index+2];
                            datablock[index+2] = backup;
                        }
                    }
                } else returncode = RNOSPACE;
            } else returncode = RRLE;
        } else returncode = RBROKEN;
        fclose(inputfile);
    } else returncode = RNOFILE;
        return (returncode==RDONE)?0:1;
}
[...]
Thanks in advice!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
I'd leave the targa header alone -- leave it as the spec ( or reference code ) defines it. My guess is this is an endian issue. Instead of fread-ing the header, I'd just manually read each field and perform an endian conversion on each.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
Alternatively, load PNG or JPEG instead: http://onesadcookie.com/svn/SimpleImage and http://onesadcookie.com/svn/SimpleImageCDemo . Not at all sure why you'd want to load TGA.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2006.09
Post: #4
Thank you so much! Grin
Quote this message in a reply
Apprentice
Posts: 19
Joined: 2007.03
Post: #5
If you would like, I have a function for loading all types of TGA images (not just 24 bit RGB). It's about 700 lines of code with comments and reads compressed and uncompressed color-mapped, grayscale, and true color images from memory. It could be adapted to read from a file, just read the whole file in and send it to the function.

Let me know if I should post it
Quote this message in a reply
Sage
Posts: 1,234
Joined: 2002.10
Post: #6
Quote this message in a reply
Post Reply