Question

I have a class that looks like the following:

class myTexture
{
    public: 
        myTexture();
        ~myTexture();
        unsigned char * data;
        void loadFile(string file)
        {
            FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
            FIBITMAP *dib(0);
            BYTE* bits;

            fif = FreeImage_GetFileType(filename.c_str(), 0);
            if(fif == FIF_UNKNOWN) 
                fif = FreeImage_GetFIFFromFilename(filename.c_str());

            if(FreeImage_FIFSupportsReading(fif))
                dib = FreeImage_Load(fif, filename.c_str(), 0);

            bits = FreeImage_GetBits(dib);
            data = bits;
            FreeImage_Unload(dib);
        }
};

When I do FreeImage_Unload(dib), I lose the data information, how can I copy the info on 'bits' to 'data' so whenever I unload the 'dib' I do not lose the information?

Any suggestions?

Was it helpful?

Solution

You have to copy image data from FreeImage's DIB to a separate location:

int Width  = FreeImage_GetWidth( dib );
int Height = FreeImage_GetHeight( dib );
int BPP    = FreeImage_GetBPP( dib ) / 8;
bits = FreeImage_GetBits( dib );
data = malloc( Width * Height * BPP );
memcpy( data, bits, Width * Height * BPP );

Now you can free the dib as you do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top