2017-04-01 4 views
0

タイトルに記載されているとおり、FreeImageを使用してOpenGLゲームでテクスチャを読み込みますが、どのようにサブ画像を取得しますか?FreeImageでサブ画像を取得する方法

私の現在のコードは次のようになります。

FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; 
    FIBITMAP* dib = nullptr; 
    fif = FreeImage_GetFileType(filename, 0); 
    if (fif == FIF_UNKNOWN) 
     fif = FreeImage_GetFIFFromFilename(filename); 
    if (fif == FIF_UNKNOWN) 
     return nullptr; 

    if (FreeImage_FIFSupportsReading(fif)) 
     dib = FreeImage_Load(fif, filename); 
    if (!dib) 
     return nullptr; 

    BYTE* pixels = FreeImage_GetBits(dib); 
    *width = FreeImage_GetWidth(dib); 
    *height = FreeImage_GetHeight(dib); 
    *bits = FreeImage_GetBPP(dib); 

    int size = *width * *height * (*bits/8); 
    BYTE* result = new BYTE[size]; 
    memcpy(result, pixels, size); 
    FreeImage_Unload(dib); 
    return result; 

は、私はサブイメージ(左上隅のピクセルの例えば32×32エリア)を取得するために変更するには何が必要でしょうか?

答えて

1

FreeImage_Copy()を使用すると、指定した領域のサブイメージを取得できます。 FreeImage_Copy()left, top, right, bottomで、x, y, width, heightではありません。

FIBITMAP *image = ...; 

int x = 0; 
int y = 0; 
int width = 32; 
int height = 32; 

FIBITMAP *subimage = FreeImage_Copy(image, x, y, x + width, y + height); 

それは与えられた画像のリテラルコピーであるようFreeImage_Unload(subimage)に覚えておいてください。

必要な場合は、その後、実行してPNGに保存することができます:

if (FreeImage_Save(FIF_PNG, subimage, "subimage.png")) 
    printf("Subimage successfully saved!\n"); 
else 
    printf("Failed saving subimage!\n"); 
関連する問題