2

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?

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
JohnnyAce
  • 3,569
  • 9
  • 37
  • 59

1 Answers1

1

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.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • 1
    Note that if there is padding in the pitch, it is more correct to do GetPitch() x GetHeight() for the length calculation in malloc and memcpy – StarShine Feb 19 '15 at 16:11