2012-05-17 90 views
2

我有一個類,如下所示:如何使用FreeImage_Unload(),並且不會丟失圖像數據

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

當我做FreeImage_Unload(DIB),我失去的數據信息,如何我可以將'位'信息複製到'數據',所以無論何時卸載'dib'我都不會丟失信息?

有什麼建議嗎?

回答

1

你必須從FreeImage的的DIB圖像數據複製到一個單獨的位置:

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

現在你可以釋放dib爲你做。

+1

請注意,如果在音調中有填充,則爲malloc和memcpy中的長度計算使用GetPitch()x GetHeight()會更加正確 – StarShine

相關問題