00001
00002
00003
00004 #include "sdl_image.h"
00005
00006 #ifdef HAVE_SDL
00007 #ifdef HAVE_SDL_IMAGE
00008
00009 image::image_t*
00010 image::sdl::load( const std::string& filename )
00011 {
00012
00013 SDL_Surface *surface = IMG_Load( filename.c_str( ) );
00014
00015
00016 if( surface ) {
00017
00018 image_t *img = load( surface );
00019
00020
00021 SDL_FreeSurface( surface );
00022 return img;
00023 } else {
00024
00025 return NULL;
00026 }
00027 }
00028 #endif
00029
00031
00032 image::image_t*
00033 image::sdl::load( SDL_Surface *surface )
00034 {
00035
00036 assert( surface );
00037
00038
00039 SDL_Surface *image = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w,
00040 surface->h, 32,
00041 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00042 0x000000FF,
00043 0x0000FF00,
00044 0x00FF0000,
00045 0xFF000000
00046 #else
00047 0xFF000000,
00048 0x00FF0000,
00049 0x0000FF00,
00050 0x000000FF
00051 #endif
00052 );
00053
00054
00055 if ( image == NULL ) {
00056 return NULL;
00057 }
00058
00059
00060 Uint32 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
00061 Uint8 saved_alpha = surface->format->alpha;
00062 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00063 SDL_SetAlpha( surface, 0, 0 );
00064 }
00065
00066
00067 SDL_Rect area;
00068 area.x = 0;
00069 area.y = 0;
00070 area.w = surface->w;
00071 area.h = surface->h;
00072 SDL_BlitSurface( surface, &area, image, &area );
00073
00074
00075 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00076 SDL_SetAlpha(surface, saved_flags, saved_alpha);
00077 }
00078
00079
00080 image_t *img = new image_t( image->w, image->h, image_t::RGBA, image_t::BYTE );
00081 img->seek( 0, 0 );
00082 unsigned int pixels = image->w * image->h;
00083 char *channel = (char*)(image->pixels);
00084 char r, g, b, a;
00085 for( unsigned int i = 0; i < pixels; ++i ) {
00086 r = *(channel++);
00087 g = *(channel++);
00088 b = *(channel++);
00089 a = *(channel++);
00090 img->write( r, g, b, a);
00091 }
00092
00093
00094 SDL_FreeSurface( image );
00095
00096
00097 return img;
00098 }
00099 #endif