使用 SDL_image 加载 BMP 图像问题
Issue loading BMP image using 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。
知道哪里出了问题吗?
也许更好的例子。这(可能)会产生更多信息,如果支持 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
我是 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。
知道哪里出了问题吗?
也许更好的例子。这(可能)会产生更多信息,如果支持 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