Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sdl_image.cpp

00001 //FILE:         sdl_image.cpp
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
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         //let SDL_image load the image
00013         SDL_Surface *surface = IMG_Load( filename.c_str( ) );
00014 
00015         //was the load successful
00016         if( surface ) {
00017                 //conver SDL_Suface to image_t
00018                 image_t *img = load( surface );
00019                 
00020                 //delete SDL_Surface
00021                 SDL_FreeSurface( surface );
00022                 return img;
00023         } else {
00024                 //could not load image
00025                 return NULL;
00026         }
00027 }
00028 #endif
00029 
00031 //most the code below is in public domain
00032 image::image_t*
00033 image::sdl::load( SDL_Surface *surface )
00034 {
00035         //sanity check
00036         assert( surface );
00037         
00038         //create a SDL surface that is easy to read from
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         //was the new surface created
00055         if ( image == NULL ) {
00056                 return NULL;
00057         }
00058 
00059         //save alpha
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         //copy image to new friendly format
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         //restore alpha
00075         if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00076                 SDL_SetAlpha(surface, saved_flags, saved_alpha);
00077         }
00078 
00079         //copy the image to an image_t*
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         //we no longer need image
00094         SDL_FreeSurface( image );
00095 
00096         //return new image
00097         return img;
00098 }
00099 #endif

Generated on Tue Feb 11 18:49:41 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002