サイト構成 | 連絡先,業績など | 体験演習 | データの扱い | コンピュータ設定と利用 | 教材(公開) | サポートページ |
SDL_image とは, BMP, JPEG, PNG, PNM などの画像ファイルを扱う機能を持ったライブラリです. この Web ページでは,SDL_image を用いて画像ファイルを読み込むプログラムの例を示す.
※ Windows については別のページで説明している.
Linux で SDL, SDL_image のインストールが済んでいること.
インストール前に「yum check-update; yum update」を実行しておくとトラブルが減るだろう. これは更新可能な全パッケージを更新するという操作.
yum check-update yum update yum install SDL*
画像ファイルはヘッダが付いていたり,しばしば圧縮されている.画像ファイルの種類もいろいろある. SDL_image を使うと,こうしたことを気にしなくて済む.
次の仮定を置く
/* SDL を用いた画像読み込みファイルプログラム */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include "SDL.h" #include "SDL_image.h" int main(int argc, char *argv[]) { SDL_Surface *image; int i, j; int r, g, b; /* Check command line usage */ if ( ! argv[1] ) { fprintf(stderr, "Usage: %s <image_file>\n", argv[0]); return(1); } /* Open the image file */ image = IMG_Load(argv[1]); if ( image == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError()); } for ( i = 0; i < image->h; i++ ) { for ( j = 0; j < image->w; j++ ) { // 1画素の R, G, B 成分は1バイトであること.画像データは隙間無くならんでいること.R, G, B の順に並んでいることを仮定している. unsigned char* p = (unsigned char*)image->pixels; int pixel_at = (i * (image->w) + j ) * image->format->BytesPerPixel; r = p[pixel_at]; g = p[pixel_at + 1]; b = p[pixel_at + 2]; fprintf(stderr, "%d, %d = (%d, %d, %d)\n", i, j, r, g, b); } } fprintf( stderr, "image->format->BytesPerPixel = %d", image->format->BytesPerPixel ); fprintf( stderr, "image->w = %d", image->w ); fprintf( stderr, "image->h = %d", image->h ); /* We're done! */ SDL_Quit(); return(0); }
view.cpp のところは実際のファイル名に読み替えてください.
g++ -I/usr/include/SDL -I/usr/include/png12 -o a.out view.cpp -lSDL_image -lSDL -lpthread -lm
テストデータ: 24.bmp
実行結果の例