- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-ILUT/src/ilut_sdlsurface.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2002 by Denton Woods
    00005 // Copyright (C) 2002 Nelson Rush.
    00006 // Last modified: 05/18/2002
    00007 //
    00008 // Filename: src-ILUT/src/ilut_sdlsurface.c
    00009 //
    00010 // Description:  SDL Surface functions for images
    00011 //
    00012 //-----------------------------------------------------------------------------
    00013
    00014
    00015 #include "ilut_internal.h"
    00016 #ifdef ILUT_USE_SDL
    00017 #include <SDL.h>
    00018 //#include "endian.h"
    00019
    00020 #ifdef  _MSC_VER
    00021         #pragma comment(lib, "sdl.lib")
    00022 #endif//_MSC_VER
    00023
    00024 int isBigEndian;
    00025 int rmask, gmask, bmask, amask;
    00026
    00027 void InitSDL()
    00028 {
    00029 //#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    00030 #ifdef __BIG_ENDIAN__
    00031         isBigEndian = 1;
    00032     rmask = 0xFF000000;
    00033     gmask = 0x00FF0000;
    00034     bmask = 0x0000FF00;
    00035     amask = 0x000000FF;
    00036 #else
    00037         isBigEndian = 0;
    00038     rmask = 0x000000FF;
    00039     gmask = 0x0000FF00;
    00040     bmask = 0x00FF0000;
    00041     amask = 0xFF000000;
    00042 #endif
    00043         return;
    00044 }
    00045
    00046 //ILboolean ilConvertPal(ILenum DestFormat);
    00047
    00048 //ILpal *Pal;
    00049
    00050 // Does not account for converting luminance...
    00051 SDL_Surface *ILAPIENTRY ilutConvertToSDLSurface(unsigned int flags)
    00052 {
    00053         SDL_Surface *Bitmap = NULL;
    00054         ILuint          i = 0, Pad, BppPal;
    00055         ILubyte         *Dest, *Data;
    00056         ILimage         *Image;
    00057
    00058         Image = ilutCurImage = ilGetCurImage();
    00059         if (ilutCurImage == NULL) {
    00060                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00061                 return NULL;
    00062         }
    00063
    00064         InitSDL();
    00065
    00066         // Should be IL_BGR(A).
    00067         if (ilutCurImage->Format == IL_RGB || ilutCurImage->Format == IL_RGBA) {
    00068                 if (!isBigEndian) {
    00069                         //iluSwapColours();  // No need to swap colors.  Just use the bitmasks.
    00070                         rmask = 0x00FF0000;
    00071                         gmask = 0x0000FF00;
    00072                         bmask = 0x000000FF;
    00073                 }
    00074         }
    00075         else if (ilutCurImage->Format == IL_BGR || ilutCurImage->Format == IL_BGRA) {
    00076                 if (isBigEndian) {
    00077                         rmask = 0x0000FF00;
    00078                         gmask = 0x00FF0000;
    00079                         bmask = 0xFF000000;
    00080                 }
    00081         }
    00082         else if (Image->Format != IL_COLOR_INDEX) {  // We have to convert the image.
    00083                 #ifdef __BIG_ENDIAN__
    00084                 Image = iConvertImage(Image, IL_RGBA, IL_UNSIGNED_BYTE);
    00085                 #else
    00086                 Image = iConvertImage(Image, IL_BGRA, IL_UNSIGNED_BYTE);
    00087                 #endif
    00088                 if (Image == NULL)
    00089                         return NULL;
    00090         }
    00091
    00092         if (Image->Type != IL_UNSIGNED_BYTE) {
    00093                 // We do not have to worry about Image != iCurImage at this point, because if it was converted,
    00094                 //  it was converted to a type of unsigned byte.
    00095                 Image = iConvertImage(Image, Image->Format, IL_UNSIGNED_BYTE);
    00096                 if (Image == NULL)
    00097                         return NULL;
    00098         }
    00099
    00100         Data = Image->Data;
    00101         if (Image->Origin == IL_ORIGIN_LOWER_LEFT) {
    00102                 Data = iGetFlipped(Image);
    00103                 if (Data == NULL)
    00104                         goto done;
    00105         }
    00106
    00107         Bitmap = SDL_CreateRGBSurface(flags, Image->Width, Image->Height, Image->Bpp * 8,
    00108                                         rmask,gmask,bmask,amask);
    00109         if (Bitmap == NULL)
    00110                 goto done;
    00111
    00112         if (SDL_MUSTLOCK(Bitmap))
    00113                 SDL_LockSurface(Bitmap);
    00114
    00115         Pad = Bitmap->pitch - Image->Bps;
    00116         if (Pad == 0) {
    00117                 memcpy(Bitmap->pixels, Data, Image->SizeOfData);
    00118         }
    00119         else {  // Must pad the lines on some images.
    00120                 Dest = Bitmap->pixels;
    00121                 for (i = 0; i < Image->Height; i++) {
    00122                         memcpy(Dest, Data + i * Image->Bps, Image->Bps);
    00123                         imemclear(Dest + Image->Bps, Pad);
    00124                         Dest += Bitmap->pitch;
    00125                 }
    00126         }
    00127
    00128         if (SDL_MUSTLOCK(Bitmap))
    00129                 SDL_UnlockSurface(Bitmap);
    00130
    00131         if (Image->Format == IL_COLOR_INDEX) {
    00132                 BppPal = ilGetBppPal(Image->Pal.PalType);
    00133                 switch (ilutCurImage->Pal.PalType)
    00134                 {
    00135                         case IL_PAL_RGB24:
    00136                         case IL_PAL_RGB32:
    00137                         case IL_PAL_RGBA32:
    00138                                 for (i = 0; i < ilutCurImage->Pal.PalSize / BppPal; i++) {
    00139                                         (Bitmap->format)->palette->colors[i].r = ilutCurImage->Pal.Palette[i*BppPal+0];
    00140                                         (Bitmap->format)->palette->colors[i].g = ilutCurImage->Pal.Palette[i*BppPal+1];
    00141                                         (Bitmap->format)->palette->colors[i].b = ilutCurImage->Pal.Palette[i*BppPal+2];
    00142                                         (Bitmap->format)->palette->colors[i].unused = 0xFF;
    00143                                 }
    00144                                 break;
    00145                         case IL_PAL_BGR24:
    00146                         case IL_PAL_BGR32:
    00147                         case IL_PAL_BGRA32:
    00148                                 for (i = 0; i < ilutCurImage->Pal.PalSize / BppPal; i++) {
    00149                                         (Bitmap->format)->palette->colors[i].b = ilutCurImage->Pal.Palette[i*BppPal+0];
    00150                                         (Bitmap->format)->palette->colors[i].g = ilutCurImage->Pal.Palette[i*BppPal+1];
    00151                                         (Bitmap->format)->palette->colors[i].r = ilutCurImage->Pal.Palette[i*BppPal+2];
    00152                                         (Bitmap->format)->palette->colors[i].unused = 0xFF;
    00153                                 }
    00154                                 break;
    00155                         default:
    00156                                 ilSetError(IL_INTERNAL_ERROR);  // Do anything else?
    00157                 }
    00158         }
    00159
    00160 done:
    00161         if (Data != Image->Data)
    00162                 ifree(Data);  // This is flipped data.
    00163         if (Image != ilutCurImage)
    00164                 ilCloseImage(Image);  // This is a converted image.
    00165         return Bitmap;  // This is NULL if there was an error.
    00166 }
    00167
    00168
    00169 #ifndef _WIN32_WCE
    00170 SDL_Surface* ILAPIENTRY ilutSDLSurfaceLoadImage(ILstring FileName)
    00171 {
    00172         SDL_Surface *Surface;
    00173
    00174         iBindImageTemp();
    00175         if (!ilLoadImage(FileName)) {
    00176                 return NULL;
    00177         }
    00178
    00179         Surface = ilutConvertToSDLSurface(SDL_SWSURFACE);
    00180
    00181         return Surface;
    00182 }
    00183 #endif//_WIN32_WCE
    00184
    00185
    00186 // Unfinished
    00187 ILboolean ILAPIENTRY ilutSDLSurfaceFromBitmap(SDL_Surface *Bitmap)
    00188 {
    00189         ilutCurImage = ilGetCurImage();
    00190         if (ilutCurImage == NULL) {
    00191                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00192                 return IL_FALSE;
    00193         }
    00194
    00195         if (Bitmap == NULL || Bitmap->w == 0 || Bitmap->h == 0) {
    00196                 ilSetError(ILUT_INVALID_PARAM);
    00197                 return IL_FALSE;
    00198         }
    00199
    00200         if (!ilTexImage(Bitmap->w, Bitmap->h, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
    00201                 return IL_FALSE;
    00202
    00203         return IL_TRUE;
    00204 }
    00205
    00206
    00207 #endif//ILUT_USE_SDL