使用SDL_image发出加载BMP图像的问题

时间:2015-01-14 10:15:43

标签: c bmp image-loading sdl-image

我是SDL_image的新手,我正在尝试在C文件中使用它来加载BMP图像。 为此,我编写了以下代码:

#include "SDL/SDL.h"
#include "SDL_image.h"

SDL_RWops *rwop;
rwop = SDL_RWFromFile("sample.bmp", "rb");

但是,出于某种原因,虽然执行这些行之后的rwop不再是NULL,但IMG_isBMP(rwop)为0。

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

或许更好的例子。这可能会产生更多信息,也许是否支持BMP:

https://www.libsdl.org/projects/SDL_image/docs/SDL_image_32.html

您也可以尝试使用IMG_LoadBMP_RW,例如:

https://www.libsdl.org/projects/SDL_image/docs/SDL_image_16.html#SEC16

#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

int main(int argc, char *argv[])
{
    const char *fn = "sample.bmp";
    SDL_Surface *surf;

    if (argc > 1)
        fn = argv[1];

    if ((surf = SDL_LoadBMP(fn)) == NULL) {
        printf("SDL_loadBMP failed: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    printf("%s is bmp\n", fn);

    SDL_FreeSurface(surf);
    SDL_Quit();

    return 0;
}

旧答案:


经过测试和验证:

#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

int main(int argc, char *argv[])
{
    const char *fn = "sample.bmp";
    int v;
    SDL_RWops *rwop;

    if (argc > 1)
        fn = argv[1];

    if ((rwop = SDL_RWFromFile(fn, "rb")) == NULL) {
        printf("SDL_RWFromFile failed: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    v = IMG_isBMP(rwop);

    printf("%s is bmp = %d\n", fn, v);

    SDL_FreeRW(rwop);
    SDL_Quit();

    return 0;
}

编译:

gcc -Wall -Wextra -pedantic -o sdl sdl.c `sdl-config --libs` -lSDL_image

BMP图像的产量,例如:

$ ./sdltest lena.bmp 
lena.bmp is bmp = 1
相关问题