src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 13 Aug 2007 06:24:56 +0000
changeset 2239 31835fd24b2b
parent 2235 55a8bebf9ba6
child 2249 5a58b57b6724
permissions -rw-r--r--
Added SSE version of SDL_FillRect() for 32-bit ARGB surfaces
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_video.h"
    25 #include "SDL_compat.h"
    26 #include "SDL_sysvideo.h"
    27 #include "SDL_blit.h"
    28 #include "SDL_RLEaccel_c.h"
    29 #include "SDL_pixels_c.h"
    30 #include "SDL_leaks.h"
    31 
    32 
    33 /* Public routines */
    34 /*
    35  * Create an empty RGB surface of the appropriate depth
    36  */
    37 SDL_Surface *
    38 SDL_CreateRGBSurface(Uint32 flags,
    39                      int width, int height, int depth,
    40                      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    41 {
    42     SDL_Surface *surface;
    43 
    44     /* Allocate the surface */
    45     surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    46     if (surface == NULL) {
    47         SDL_OutOfMemory();
    48         return NULL;
    49     }
    50 
    51     surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    52     if (!surface->format) {
    53         SDL_FreeSurface(surface);
    54         return NULL;
    55     }
    56     if (Amask) {
    57         surface->flags |= SDL_SRCALPHA;
    58     }
    59     surface->w = width;
    60     surface->h = height;
    61     surface->pitch = SDL_CalculatePitch(surface);
    62     SDL_SetClipRect(surface, NULL);
    63 
    64     if (surface->format->BitsPerPixel <= 8) {
    65         SDL_Palette *palette =
    66             SDL_AllocPalette((1 << surface->format->BitsPerPixel));
    67         if (!palette) {
    68             SDL_FreeSurface(surface);
    69             return NULL;
    70         }
    71         if (Rmask || Bmask || Gmask) {
    72             const SDL_PixelFormat *format = surface->format;
    73 
    74             /* create palette according to masks */
    75             int i;
    76             int Rm = 0, Gm = 0, Bm = 0;
    77             int Rw = 0, Gw = 0, Bw = 0;
    78 
    79             if (Rmask) {
    80                 Rw = 8 - format->Rloss;
    81                 for (i = format->Rloss; i > 0; i -= Rw)
    82                     Rm |= 1 << i;
    83             }
    84             if (Gmask) {
    85                 Gw = 8 - format->Gloss;
    86                 for (i = format->Gloss; i > 0; i -= Gw)
    87                     Gm |= 1 << i;
    88             }
    89             if (Bmask) {
    90                 Bw = 8 - format->Bloss;
    91                 for (i = format->Bloss; i > 0; i -= Bw)
    92                     Bm |= 1 << i;
    93             }
    94             for (i = 0; i < palette->ncolors; ++i) {
    95                 int r, g, b;
    96                 r = (i & Rmask) >> format->Rshift;
    97                 r = (r << format->Rloss) | ((r * Rm) >> Rw);
    98                 palette->colors[i].r = r;
    99 
   100                 g = (i & Gmask) >> format->Gshift;
   101                 g = (g << format->Gloss) | ((g * Gm) >> Gw);
   102                 palette->colors[i].g = g;
   103 
   104                 b = (i & Bmask) >> format->Bshift;
   105                 b = (b << format->Bloss) | ((b * Bm) >> Bw);
   106                 palette->colors[i].b = b;
   107             }
   108         } else if (palette->ncolors == 2) {
   109             /* Create a black and white bitmap palette */
   110             palette->colors[0].r = 0xFF;
   111             palette->colors[0].g = 0xFF;
   112             palette->colors[0].b = 0xFF;
   113             palette->colors[1].r = 0x00;
   114             palette->colors[1].g = 0x00;
   115             palette->colors[1].b = 0x00;
   116         }
   117         SDL_SetSurfacePalette(surface, palette);
   118         SDL_FreePalette(palette);
   119     }
   120 
   121     /* Get the pixels */
   122     if (surface->w && surface->h) {
   123         surface->pixels = SDL_malloc(surface->h * surface->pitch);
   124         if (!surface->pixels) {
   125             SDL_FreeSurface(surface);
   126             SDL_OutOfMemory();
   127             return NULL;
   128         }
   129         /* This is important for bitmaps */
   130         SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
   131     }
   132 
   133     /* Allocate an empty mapping */
   134     surface->map = SDL_AllocBlitMap();
   135     if (!surface->map) {
   136         SDL_FreeSurface(surface);
   137         return NULL;
   138     }
   139     SDL_FormatChanged(surface);
   140 
   141     /* The surface is ready to go */
   142     surface->refcount = 1;
   143 #ifdef CHECK_LEAKS
   144     ++surfaces_allocated;
   145 #endif
   146     return surface;
   147 }
   148 
   149 /*
   150  * Create an RGB surface from an existing memory buffer
   151  */
   152 SDL_Surface *
   153 SDL_CreateRGBSurfaceFrom(void *pixels,
   154                          int width, int height, int depth, int pitch,
   155                          Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
   156                          Uint32 Amask)
   157 {
   158     SDL_Surface *surface;
   159 
   160     surface =
   161         SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
   162     if (surface != NULL) {
   163         surface->flags |= SDL_PREALLOC;
   164         surface->pixels = pixels;
   165         surface->w = width;
   166         surface->h = height;
   167         surface->pitch = pitch;
   168         SDL_SetClipRect(surface, NULL);
   169     }
   170     return surface;
   171 }
   172 
   173 static int
   174 SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
   175 {
   176     SDL_Surface *surface = (SDL_Surface *) userdata;
   177 
   178     SDL_FormatChanged(surface);
   179 
   180     return 0;
   181 }
   182 
   183 int
   184 SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
   185 {
   186     if (!surface || !surface->format) {
   187         SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
   188         return -1;
   189     }
   190 
   191     if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
   192         SDL_SetError
   193             ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
   194         return -1;
   195     }
   196 
   197     if (surface->format->palette == palette) {
   198         return 0;
   199     }
   200 
   201     if (surface->format->palette) {
   202         SDL_DelPaletteWatch(surface->format->palette,
   203                             SDL_SurfacePaletteChanged, surface);
   204     }
   205 
   206     surface->format->palette = palette;
   207 
   208     if (surface->format->palette) {
   209         SDL_AddPaletteWatch(surface->format->palette,
   210                             SDL_SurfacePaletteChanged, surface);
   211     }
   212     return 0;
   213 }
   214 
   215 /*
   216  * Set the color key in a blittable surface
   217  */
   218 int
   219 SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
   220 {
   221     /* Sanity check the flag as it gets passed in */
   222     if (flag & SDL_SRCCOLORKEY) {
   223         if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
   224             flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   225         } else {
   226             flag = SDL_SRCCOLORKEY;
   227         }
   228     } else {
   229         flag = 0;
   230     }
   231 
   232     /* Optimize away operations that don't change anything */
   233     if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
   234         (key == surface->format->colorkey)) {
   235         return (0);
   236     }
   237 
   238     /* UnRLE surfaces before we change the colorkey */
   239     if (surface->flags & SDL_RLEACCEL) {
   240         SDL_UnRLESurface(surface, 1);
   241     }
   242 
   243     if (flag) {
   244         surface->flags |= SDL_SRCCOLORKEY;
   245         surface->format->colorkey = key;
   246         if (flag & SDL_RLEACCELOK) {
   247             surface->flags |= SDL_RLEACCELOK;
   248         } else {
   249             surface->flags &= ~SDL_RLEACCELOK;
   250         }
   251     } else {
   252         surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   253         surface->format->colorkey = 0;
   254     }
   255     SDL_InvalidateMap(surface->map);
   256     return (0);
   257 }
   258 
   259 /* This function sets the alpha channel of a surface */
   260 int
   261 SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
   262 {
   263     Uint32 oldflags = surface->flags;
   264     Uint32 oldalpha = surface->format->alpha;
   265 
   266     /* Sanity check the flag as it gets passed in */
   267     if (flag & SDL_SRCALPHA) {
   268         if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
   269             flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
   270         } else {
   271             flag = SDL_SRCALPHA;
   272         }
   273     } else {
   274         flag = 0;
   275     }
   276 
   277     /* Optimize away operations that don't change anything */
   278     if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
   279         (!flag || value == oldalpha)) {
   280         return (0);
   281     }
   282 
   283     if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
   284         SDL_UnRLESurface(surface, 1);
   285 
   286     if (flag) {
   287         surface->flags |= SDL_SRCALPHA;
   288         surface->format->alpha = value;
   289         if (flag & SDL_RLEACCELOK) {
   290             surface->flags |= SDL_RLEACCELOK;
   291         } else {
   292             surface->flags &= ~SDL_RLEACCELOK;
   293         }
   294     } else {
   295         surface->flags &= ~SDL_SRCALPHA;
   296         surface->format->alpha = SDL_ALPHA_OPAQUE;
   297     }
   298     /*
   299      * The representation for software surfaces is independent of
   300      * per-surface alpha, so no need to invalidate the blit mapping
   301      * if just the alpha value was changed. (If either is 255, we still
   302      * need to invalidate.)
   303      */
   304     if (oldflags != surface->flags
   305         || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
   306         SDL_InvalidateMap(surface->map);
   307     }
   308     return (0);
   309 }
   310 
   311 int
   312 SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
   313 {
   314     int row, col;
   315     int offset;
   316     Uint8 *buf;
   317 
   318     if ((surface->format->Amask != 0xFF000000) &&
   319         (surface->format->Amask != 0x000000FF)) {
   320         SDL_SetError("Unsupported surface alpha mask format");
   321         return -1;
   322     }
   323 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   324     if (surface->format->Amask == 0xFF000000) {
   325         offset = 3;
   326     } else {
   327         offset = 0;
   328     }
   329 #else
   330     if (surface->format->Amask == 0xFF000000) {
   331         offset = 0;
   332     } else {
   333         offset = 3;
   334     }
   335 #endif /* Byte ordering */
   336 
   337     /* Quickly set the alpha channel of an RGBA or ARGB surface */
   338     if (SDL_MUSTLOCK(surface)) {
   339         if (SDL_LockSurface(surface) < 0) {
   340             return -1;
   341         }
   342     }
   343     row = surface->h;
   344     while (row--) {
   345         col = surface->w;
   346         buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
   347         while (col--) {
   348             *buf = value;
   349             buf += 4;
   350         }
   351     }
   352     if (SDL_MUSTLOCK(surface)) {
   353         SDL_UnlockSurface(surface);
   354     }
   355     return 0;
   356 }
   357 
   358 /*
   359  * Set the clipping rectangle for a blittable surface
   360  */
   361 SDL_bool
   362 SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
   363 {
   364     SDL_Rect full_rect;
   365 
   366     /* Don't do anything if there's no surface to act on */
   367     if (!surface) {
   368         return SDL_FALSE;
   369     }
   370 
   371     /* Set up the full surface rectangle */
   372     full_rect.x = 0;
   373     full_rect.y = 0;
   374     full_rect.w = surface->w;
   375     full_rect.h = surface->h;
   376 
   377     /* Set the clipping rectangle */
   378     if (!rect) {
   379         surface->clip_rect = full_rect;
   380         return 1;
   381     }
   382     return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
   383 }
   384 
   385 void
   386 SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
   387 {
   388     if (surface && rect) {
   389         *rect = surface->clip_rect;
   390     }
   391 }
   392 
   393 /* 
   394  * Set up a blit between two surfaces -- split into three parts:
   395  * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
   396  * verification.  The lower part is a pointer to a low level
   397  * accelerated blitting function.
   398  *
   399  * These parts are separated out and each used internally by this 
   400  * library in the optimimum places.  They are exported so that if
   401  * you know exactly what you are doing, you can optimize your code
   402  * by calling the one(s) you need.
   403  */
   404 int
   405 SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   406               SDL_Surface * dst, SDL_Rect * dstrect)
   407 {
   408     /* Check to make sure the blit mapping is valid */
   409     if ((src->map->dst != dst) ||
   410         (src->map->dst->format_version != src->map->format_version)) {
   411         if (SDL_MapSurface(src, dst) < 0) {
   412             return (-1);
   413         }
   414     }
   415     return (src->map->sw_blit(src, srcrect, dst, dstrect));
   416 }
   417 
   418 
   419 int
   420 SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
   421               SDL_Surface * dst, SDL_Rect * dstrect)
   422 {
   423     SDL_Rect fulldst;
   424     int srcx, srcy, w, h;
   425 
   426     /* Make sure the surfaces aren't locked */
   427     if (!src || !dst) {
   428         SDL_SetError("SDL_UpperBlit: passed a NULL surface");
   429         return (-1);
   430     }
   431     if (src->locked || dst->locked) {
   432         SDL_SetError("Surfaces must not be locked during blit");
   433         return (-1);
   434     }
   435 
   436     /* If the destination rectangle is NULL, use the entire dest surface */
   437     if (dstrect == NULL) {
   438         fulldst.x = fulldst.y = 0;
   439         dstrect = &fulldst;
   440     }
   441 
   442     /* clip the source rectangle to the source surface */
   443     if (srcrect) {
   444         int maxw, maxh;
   445 
   446         srcx = srcrect->x;
   447         w = srcrect->w;
   448         if (srcx < 0) {
   449             w += srcx;
   450             dstrect->x -= srcx;
   451             srcx = 0;
   452         }
   453         maxw = src->w - srcx;
   454         if (maxw < w)
   455             w = maxw;
   456 
   457         srcy = srcrect->y;
   458         h = srcrect->h;
   459         if (srcy < 0) {
   460             h += srcy;
   461             dstrect->y -= srcy;
   462             srcy = 0;
   463         }
   464         maxh = src->h - srcy;
   465         if (maxh < h)
   466             h = maxh;
   467 
   468     } else {
   469         srcx = srcy = 0;
   470         w = src->w;
   471         h = src->h;
   472     }
   473 
   474     /* clip the destination rectangle against the clip rectangle */
   475     {
   476         SDL_Rect *clip = &dst->clip_rect;
   477         int dx, dy;
   478 
   479         dx = clip->x - dstrect->x;
   480         if (dx > 0) {
   481             w -= dx;
   482             dstrect->x += dx;
   483             srcx += dx;
   484         }
   485         dx = dstrect->x + w - clip->x - clip->w;
   486         if (dx > 0)
   487             w -= dx;
   488 
   489         dy = clip->y - dstrect->y;
   490         if (dy > 0) {
   491             h -= dy;
   492             dstrect->y += dy;
   493             srcy += dy;
   494         }
   495         dy = dstrect->y + h - clip->y - clip->h;
   496         if (dy > 0)
   497             h -= dy;
   498     }
   499 
   500     if (w > 0 && h > 0) {
   501         SDL_Rect sr;
   502         sr.x = srcx;
   503         sr.y = srcy;
   504         sr.w = dstrect->w = w;
   505         sr.h = dstrect->h = h;
   506         return SDL_LowerBlit(src, &sr, dst, dstrect);
   507     }
   508     dstrect->w = dstrect->h = 0;
   509     return 0;
   510 }
   511 
   512 static int
   513 SDL_FillRect1(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   514 {
   515     /* FIXME: We have to worry about packing order.. *sigh* */
   516     SDL_SetError("1-bpp rect fill not yet implemented");
   517     return -1;
   518 }
   519 
   520 static int
   521 SDL_FillRect4(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   522 {
   523     /* FIXME: We have to worry about packing order.. *sigh* */
   524     SDL_SetError("4-bpp rect fill not yet implemented");
   525     return -1;
   526 }
   527 
   528 /* 
   529  * This function performs a fast fill of the given rectangle with 'color'
   530  */
   531 int
   532 SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   533 {
   534     int x, y;
   535     Uint8 *row;
   536 
   537     /* This function doesn't work on surfaces < 8 bpp */
   538     if (dst->format->BitsPerPixel < 8) {
   539         switch (dst->format->BitsPerPixel) {
   540         case 1:
   541             return SDL_FillRect1(dst, dstrect, color);
   542             break;
   543         case 4:
   544             return SDL_FillRect4(dst, dstrect, color);
   545             break;
   546         default:
   547             SDL_SetError("Fill rect on unsupported surface format");
   548             return (-1);
   549             break;
   550         }
   551     }
   552 
   553     /* If 'dstrect' == NULL, then fill the whole surface */
   554     if (dstrect) {
   555         /* Perform clipping */
   556         if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
   557             return (0);
   558         }
   559     } else {
   560         dstrect = &dst->clip_rect;
   561     }
   562 
   563     /* Perform software fill */
   564     if (SDL_LockSurface(dst) != 0) {
   565         return (-1);
   566     }
   567     row = (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
   568         dstrect->x * dst->format->BytesPerPixel;
   569     if (dst->format->palette || (color == 0)) {
   570         x = dstrect->w * dst->format->BytesPerPixel;
   571 #ifndef __MACOSX__              /* memset() is optimized on Mac OS X */
   572         if (!color && !((uintptr_t) row & 3) && !(x & 3)
   573             && !(dst->pitch & 3)) {
   574             int n = x >> 2;
   575             for (y = dstrect->h; y; --y) {
   576                 SDL_memset4(row, 0, n);
   577                 row += dst->pitch;
   578             }
   579         } else
   580 #endif /* !__MACOSX__ */
   581         {
   582             for (y = dstrect->h; y; y--) {
   583                 SDL_memset(row, color, x);
   584                 row += dst->pitch;
   585             }
   586         }
   587     } else {
   588         switch (dst->format->BytesPerPixel) {
   589         case 2:
   590             {
   591                 Uint16 c = (Uint16) color;
   592                 Uint32 cc = (Uint32) c << 16 | c;
   593                 for (y = dstrect->h; y; --y) {
   594                     Uint16 *pixels = (Uint16 *) row;
   595                     int n = dstrect->w;
   596                     if ((uintptr_t) pixels & 3) {
   597                         *pixels++ = c;
   598                         n--;
   599                     }
   600                     if (n >> 1)
   601                         SDL_memset4(pixels, cc, n >> 1);
   602                     if (n & 1)
   603                         pixels[n - 1] = c;
   604                     row += dst->pitch;
   605                 }
   606             }
   607             break;
   608 
   609         case 3:
   610 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   611             color <<= 8;
   612 #endif
   613             for (y = dstrect->h; y; --y) {
   614                 Uint8 *pixels = row;
   615                 for (x = dstrect->w; x; --x) {
   616                     SDL_memcpy(pixels, &color, 3);
   617                     pixels += 3;
   618                 }
   619                 row += dst->pitch;
   620             }
   621             break;
   622 
   623         case 4:
   624 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && SDL_ASSEMBLY_ROUTINES
   625             if (SDL_HasSSE() && !((uintptr_t) row & 15) && !(dstrect->w & 3)) {
   626                 Uint32 cccc[4] __attribute__ ((aligned(16))) = {
   627                 color, color, color, color};
   628                 int i, n = dstrect->w / 4;
   629                 __asm__ __volatile__("	movdqa (%0), %%xmm0\n"::
   630                                      "r"(cccc):"memory");
   631                 for (y = dstrect->h; y; --y) {
   632                     Uint8 *pixels = row;
   633                     for (i = n / 2; i--;) {
   634                         /* *INDENT-OFF* */
   635                         __asm__ __volatile__("	prefetchnta 256(%0)\n"
   636                                              "	movdqa %%xmm0, (%0)\n"
   637                                              "	movdqa %%xmm0, 16(%0)\n"::"r"(pixels):"memory");
   638                         /* *INDENT-ON* */
   639                         pixels += 32;
   640                     }
   641                     if (n & 1) {
   642                         __asm__ __volatile__("	movdqa %%xmm0, (%0)\n"::
   643                                              "r"(pixels):"memory");
   644                     }
   645                     row += dst->pitch;
   646                 }
   647                 __asm__ __volatile__("	emms\n"::);
   648                 break;
   649             }
   650 #endif
   651             for (y = dstrect->h; y; --y) {
   652                 SDL_memset4(row, color, dstrect->w);
   653                 row += dst->pitch;
   654             }
   655             break;
   656         }
   657     }
   658     SDL_UnlockSurface(dst);
   659 
   660     /* We're done! */
   661     return (0);
   662 }
   663 
   664 /*
   665  * Lock a surface to directly access the pixels
   666  */
   667 int
   668 SDL_LockSurface(SDL_Surface * surface)
   669 {
   670     if (!surface->locked) {
   671         /* Perform the lock */
   672         if (surface->flags & SDL_RLEACCEL) {
   673             SDL_UnRLESurface(surface, 1);
   674             surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
   675         }
   676     }
   677 
   678     /* Increment the surface lock count, for recursive locks */
   679     ++surface->locked;
   680 
   681     /* Ready to go.. */
   682     return (0);
   683 }
   684 
   685 /*
   686  * Unlock a previously locked surface
   687  */
   688 void
   689 SDL_UnlockSurface(SDL_Surface * surface)
   690 {
   691     /* Only perform an unlock if we are locked */
   692     if (!surface->locked || (--surface->locked > 0)) {
   693         return;
   694     }
   695 
   696     /* Update RLE encoded surface with new data */
   697     if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   698         surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
   699         SDL_RLESurface(surface);
   700     }
   701 }
   702 
   703 /* 
   704  * Convert a surface into the specified pixel format.
   705  */
   706 SDL_Surface *
   707 SDL_ConvertSurface(SDL_Surface * surface,
   708                    SDL_PixelFormat * format, Uint32 flags)
   709 {
   710     SDL_Surface *convert;
   711     Uint32 colorkey = 0;
   712     Uint8 alpha = 0;
   713     Uint32 surface_flags;
   714     SDL_Rect bounds;
   715 
   716     /* Check for empty destination palette! (results in empty image) */
   717     if (format->palette != NULL) {
   718         int i;
   719         for (i = 0; i < format->palette->ncolors; ++i) {
   720             if ((format->palette->colors[i].r != 0xFF) ||
   721                 (format->palette->colors[i].g != 0xFF) ||
   722                 (format->palette->colors[i].b != 0xFF))
   723                 break;
   724         }
   725         if (i == format->palette->ncolors) {
   726             SDL_SetError("Empty destination palette");
   727             return (NULL);
   728         }
   729     }
   730 
   731     /* Create a new surface with the desired format */
   732     convert = SDL_CreateRGBSurface(flags,
   733                                    surface->w, surface->h,
   734                                    format->BitsPerPixel, format->Rmask,
   735                                    format->Gmask, format->Bmask,
   736                                    format->Amask);
   737     if (convert == NULL) {
   738         return (NULL);
   739     }
   740 
   741     /* Copy the palette if any */
   742     if (format->palette && convert->format->palette) {
   743         SDL_memcpy(convert->format->palette->colors,
   744                    format->palette->colors,
   745                    format->palette->ncolors * sizeof(SDL_Color));
   746         convert->format->palette->ncolors = format->palette->ncolors;
   747     }
   748 
   749     /* Save the original surface color key and alpha */
   750     surface_flags = surface->flags;
   751     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   752         /* Convert colourkeyed surfaces to RGBA if requested */
   753         if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
   754             surface_flags &= ~SDL_SRCCOLORKEY;
   755         } else {
   756             colorkey = surface->format->colorkey;
   757             SDL_SetColorKey(surface, 0, 0);
   758         }
   759     }
   760     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   761         /* Copy over the alpha channel to RGBA if requested */
   762         if (format->Amask) {
   763             surface->flags &= ~SDL_SRCALPHA;
   764         } else {
   765             alpha = surface->format->alpha;
   766             SDL_SetAlpha(surface, 0, 0);
   767         }
   768     }
   769 
   770     /* Copy over the image data */
   771     bounds.x = 0;
   772     bounds.y = 0;
   773     bounds.w = surface->w;
   774     bounds.h = surface->h;
   775     SDL_LowerBlit(surface, &bounds, convert, &bounds);
   776 
   777     /* Clean up the original surface, and update converted surface */
   778     if (convert != NULL) {
   779         SDL_SetClipRect(convert, &surface->clip_rect);
   780     }
   781     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   782         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   783         if (convert != NULL) {
   784             Uint8 keyR, keyG, keyB;
   785 
   786             SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
   787             SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
   788                             SDL_MapRGB(convert->format, keyR, keyG, keyB));
   789         }
   790         SDL_SetColorKey(surface, cflags, colorkey);
   791     }
   792     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   793         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   794         if (convert != NULL) {
   795             SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
   796         }
   797         if (format->Amask) {
   798             surface->flags |= SDL_SRCALPHA;
   799         } else {
   800             SDL_SetAlpha(surface, aflags, alpha);
   801         }
   802     }
   803 
   804     /* We're ready to go! */
   805     return (convert);
   806 }
   807 
   808 /*
   809  * Free a surface created by the above function.
   810  */
   811 void
   812 SDL_FreeSurface(SDL_Surface * surface)
   813 {
   814     if (surface == NULL) {
   815         return;
   816     }
   817     if (--surface->refcount > 0) {
   818         return;
   819     }
   820     while (surface->locked > 0) {
   821         SDL_UnlockSurface(surface);
   822     }
   823     if (surface->flags & SDL_RLEACCEL) {
   824         SDL_UnRLESurface(surface, 0);
   825     }
   826     if (surface->format) {
   827         SDL_SetSurfacePalette(surface, NULL);
   828         SDL_FreeFormat(surface->format);
   829         surface->format = NULL;
   830     }
   831     if (surface->map != NULL) {
   832         SDL_FreeBlitMap(surface->map);
   833         surface->map = NULL;
   834     }
   835     if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
   836         SDL_free(surface->pixels);
   837     }
   838     SDL_free(surface);
   839 #ifdef CHECK_LEAKS
   840     --surfaces_allocated;
   841 #endif
   842 }
   843 
   844 /* vi: set ts=4 sw=4 expandtab: */