SDL 加载显示JPEG图片

网友投稿 898 2022-08-27

SDL 加载显示JPEG图片

SDL 加载显示JPEG图片

在《 SDL点、线、面及图像加载 》一文中自己用SDL显示JPEG失败,现在有了解决方案。 关键函数: SDL_rwops.h:

extern DECLSPEC SDL_RWops * SDLCALL SDL_RWFromFile(const char *file, const char *mode);extern DECLSPEC void

SDL_image.h:

extern

#include "SDL_image.h"#ifdef LOAD_JPG#include /* Define this for fast loading and not as good image quality *//*#define FAST_JPEG*/

代码文件:

#include #include #include #include int main(){ char *file = "/home/edemon/Pictures/sun.jpg"; SDL_Surface *screen; SDL_Surface *picture; if(SDL_Init(SDL_INIT_VIDEO) < 0){ fprintf(stderr,"SDL_INIT_VIDEO error %s\n",SDL_GetError()); exit(1); } //extern DECLSPEC SDL_RWops * SDLCALL SDL_RWFromFile(const char *file, const char *mode); SDL_RWops *rwops = SDL_RWFromFile(file,"rb"); //extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src); if(rwops == NULL){ fprintf(stderr," SDL_RWFromFile, error is %s\n",SDL_GetError()); exit(1); } picture = IMG_LoadJPG_RW(rwops); //picture = IMG_Load(file); if(picture == NULL){ fprintf(stderr,"IMG_Load error %s\n",SDL_GetError()); exit(1); } screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); if(screen == NULL){ fprintf(stderr,"couldn't set 640*480*16 bits color model, error is %s\n",SDL_GetError()); exit(1); } /* int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect); */ if(SDL_BlitSurface(picture,NULL,screen,NULL) < 0){ fprintf(stderr,"SDL_BlitSurface error %s\n",SDL_GetError()); exit(1); } SDL_UpdateRect(screen, 0, 0, 0, 0); atexit(SDL_Quit); //extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops *area); SDL_FreeRW(rwops); SDL_FreeSurface(picture); SDL_Delay(5000); SDL_FreeSurface(screen); return 0;}

在编译IMG_jpg.c时用D选项参数提前告诉编译器宏​​LOAD_JPG​​ —— 不是在代码文件中进行宏定义。

gcc -D LOAD_JPG ~/SDL_image-1.2.2/IMG_jpg.c -o IMG_jpg.ogcc loadJPG.c IMG_jpg.o -o loadJPG -lSDL -lSDL_image

编译出现问题,缺少JPEG库。 ​​​IMG_jpg.c:35:21: fatal error: jpeglib.h: No such file or directory​​

需要安装JPEG库。 资源: ​​​git clone Entering directory '/home/edemon/Downloads/libjpeg' /bin/mkdir -p '/usr/local/lib' /bin/bash ./libtool --mode=install /usr/bin/install -c libjpeg.la '/usr/local/lib'libtool: install: /usr/bin/install -c .libs/libjpeg.so.9.1.0 /usr/local/lib/libjpeg.so.9.1.0libtool: install: (cd /usr/local/lib && { ln -s -f libjpeg.so.9.1.0 libjpeg.so.9 || { rm -f libjpeg.so.9 && ln -s libjpeg.so.9.1.0 libjpeg.so.9; }; })libtool: install: (cd /usr/local/lib && { ln -s -f libjpeg.so.9.1.0 libjpeg.so || { rm -f libjpeg.so && ln -s libjpeg.so.9.1.0 libjpeg.so; }; })libtool: install: /usr/bin/install -c .libs/libjpeg.lai /usr/local/lib/libjpeg.lalibtool: install: /usr/bin/install -c .libs/libjpeg.a /usr/local/lib/libjpeg.alibtool: install: chmod 644 /usr/local/lib/libjpeg.alibtool: install: ranlib /usr/local/lib/libjpeg.alibtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/local/lib----------------------------------------------------------------------Libraries have been installed in: /usr/local/libIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify the full pathname of the library, or use the `-LLIBDIR'flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf'See any operating system documentation about shared libraries formore information, such as the ld(1) and ld.so(8) manual pages.---------------------------------------------------------------------- /bin/mkdir -p '/usr/local/bin' /bin/bash ./libtool --mode=install /usr/bin/install -c cjpeg djpeg jpegtran rdjpgcom wrjpgcom '/usr/local/bin'libtool: install: /usr/bin/install -c .libs/cjpeg /usr/local/bin/cjpeglibtool: install: /usr/bin/install -c .libs/djpeg /usr/local/bin/djpeglibtool: install: /usr/bin/install -c .libs/jpegtran /usr/local/bin/jpegtranlibtool: install: /usr/bin/install -c rdjpgcom /usr/local/bin/rdjpgcomlibtool: install: /usr/bin/install -c wrjpgcom /usr/local/bin/wrjpgcom/bin/bash /home/edemon/Downloads/libjpeg/install-sh -d /usr/local/include/usr/bin/install -c -m 644 jconfig.h /usr/local/include/jconfig.h /bin/mkdir -p '/usr/local/include' /usr/bin/install -c -m 644 jerror.h jmorecfg.h jpeglib.h '/usr/local/include' /bin/mkdir -p '/usr/local/share/man/man1' /usr/bin/install -c -m 644 cjpeg.1 djpeg.1 jpegtran.1 rdjpgcom.1 wrjpgcom.1 '/usr/local/share/man/man1'make[1]: Leaving directory '/home/edemon/Downloads/libjpeg'

查看共享库的信息:

edemon@ubuntu1:/usr/local/lib$ file libjpeg.solibjpeg.so: symbolic link to `libjpeg.so.9.1.0'

然后,我们编译、运行:

gcc -D LOAD_JPG -c ~/SDL_image-1.2.2/IMG_jpg.c -o IMG_jpg.ogcc loadJPG.c IMG_jpg.o -o loadJPG -lSDL -lSDL_image -ljpeg

在运行的时候发现一件怪事,gcc怎么也找不到共享库文件。网上查阅:

ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为​​/etc/ld.so.cache​​​, 此文件保存已排好序的动态链接库名字列表. ​

原来如此,我确定了路径​​/usr/local/lib​​​包含在lib搜索目录中,然后ldconfig一下。 再次运行:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:爬虫应用——寻找乔丹相关10条URL
下一篇:1>&2, 2>&1, &>
相关文章

 发表评论

暂时没有评论,来抢沙发吧~