src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 18:27:44 +0000
changeset 2234 cf8c3b0117b3
parent 2222 926294b2bb4e
child 2235 55a8bebf9ba6
permissions -rw-r--r--
Gained 5 FPS in testsprite because Mac OS X memset is highly optimized
     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             for (y = dstrect->h; y; --y) {
   591                 Uint16 *pixels = (Uint16 *) row;
   592                 Uint16 c = (Uint16) color;
   593                 Uint32 cc = (Uint32) c << 16 | c;
   594                 int n = dstrect->w;
   595                 if ((uintptr_t) pixels & 3) {
   596                     *pixels++ = c;
   597                     n--;
   598                 }
   599                 if (n >> 1)
   600                     SDL_memset4(pixels, cc, n >> 1);
   601                 if (n & 1)
   602                     pixels[n - 1] = c;
   603                 row += dst->pitch;
   604             }
   605             break;
   606 
   607         case 3:
   608 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   609             color <<= 8;
   610 #endif
   611             for (y = dstrect->h; y; --y) {
   612                 Uint8 *pixels = row;
   613                 for (x = dstrect->w; x; --x) {
   614                     SDL_memcpy(pixels, &color, 3);
   615                     pixels += 3;
   616                 }
   617                 row += dst->pitch;
   618             }
   619             break;
   620 
   621         case 4:
   622             for (y = dstrect->h; y; --y) {
   623                 SDL_memset4(row, color, dstrect->w);
   624                 row += dst->pitch;
   625             }
   626             break;
   627         }
   628     }
   629     SDL_UnlockSurface(dst);
   630 
   631     /* We're done! */
   632     return (0);
   633 }
   634 
   635 /*
   636  * Lock a surface to directly access the pixels
   637  */
   638 int
   639 SDL_LockSurface(SDL_Surface * surface)
   640 {
   641     if (!surface->locked) {
   642         /* Perform the lock */
   643         if (surface->flags & SDL_RLEACCEL) {
   644             SDL_UnRLESurface(surface, 1);
   645             surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
   646         }
   647     }
   648 
   649     /* Increment the surface lock count, for recursive locks */
   650     ++surface->locked;
   651 
   652     /* Ready to go.. */
   653     return (0);
   654 }
   655 
   656 /*
   657  * Unlock a previously locked surface
   658  */
   659 void
   660 SDL_UnlockSurface(SDL_Surface * surface)
   661 {
   662     /* Only perform an unlock if we are locked */
   663     if (!surface->locked || (--surface->locked > 0)) {
   664         return;
   665     }
   666 
   667     /* Update RLE encoded surface with new data */
   668     if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   669         surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
   670         SDL_RLESurface(surface);
   671     }
   672 }
   673 
   674 /* 
   675  * Convert a surface into the specified pixel format.
   676  */
   677 SDL_Surface *
   678 SDL_ConvertSurface(SDL_Surface * surface,
   679                    SDL_PixelFormat * format, Uint32 flags)
   680 {
   681     SDL_Surface *convert;
   682     Uint32 colorkey = 0;
   683     Uint8 alpha = 0;
   684     Uint32 surface_flags;
   685     SDL_Rect bounds;
   686 
   687     /* Check for empty destination palette! (results in empty image) */
   688     if (format->palette != NULL) {
   689         int i;
   690         for (i = 0; i < format->palette->ncolors; ++i) {
   691             if ((format->palette->colors[i].r != 0xFF) ||
   692                 (format->palette->colors[i].g != 0xFF) ||
   693                 (format->palette->colors[i].b != 0xFF))
   694                 break;
   695         }
   696         if (i == format->palette->ncolors) {
   697             SDL_SetError("Empty destination palette");
   698             return (NULL);
   699         }
   700     }
   701 
   702     /* Create a new surface with the desired format */
   703     convert = SDL_CreateRGBSurface(flags,
   704                                    surface->w, surface->h,
   705                                    format->BitsPerPixel, format->Rmask,
   706                                    format->Gmask, format->Bmask,
   707                                    format->Amask);
   708     if (convert == NULL) {
   709         return (NULL);
   710     }
   711 
   712     /* Copy the palette if any */
   713     if (format->palette && convert->format->palette) {
   714         SDL_memcpy(convert->format->palette->colors,
   715                    format->palette->colors,
   716                    format->palette->ncolors * sizeof(SDL_Color));
   717         convert->format->palette->ncolors = format->palette->ncolors;
   718     }
   719 
   720     /* Save the original surface color key and alpha */
   721     surface_flags = surface->flags;
   722     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   723         /* Convert colourkeyed surfaces to RGBA if requested */
   724         if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
   725             surface_flags &= ~SDL_SRCCOLORKEY;
   726         } else {
   727             colorkey = surface->format->colorkey;
   728             SDL_SetColorKey(surface, 0, 0);
   729         }
   730     }
   731     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   732         /* Copy over the alpha channel to RGBA if requested */
   733         if (format->Amask) {
   734             surface->flags &= ~SDL_SRCALPHA;
   735         } else {
   736             alpha = surface->format->alpha;
   737             SDL_SetAlpha(surface, 0, 0);
   738         }
   739     }
   740 
   741     /* Copy over the image data */
   742     bounds.x = 0;
   743     bounds.y = 0;
   744     bounds.w = surface->w;
   745     bounds.h = surface->h;
   746     SDL_LowerBlit(surface, &bounds, convert, &bounds);
   747 
   748     /* Clean up the original surface, and update converted surface */
   749     if (convert != NULL) {
   750         SDL_SetClipRect(convert, &surface->clip_rect);
   751     }
   752     if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   753         Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   754         if (convert != NULL) {
   755             Uint8 keyR, keyG, keyB;
   756 
   757             SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
   758             SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
   759                             SDL_MapRGB(convert->format, keyR, keyG, keyB));
   760         }
   761         SDL_SetColorKey(surface, cflags, colorkey);
   762     }
   763     if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   764         Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   765         if (convert != NULL) {
   766             SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
   767         }
   768         if (format->Amask) {
   769             surface->flags |= SDL_SRCALPHA;
   770         } else {
   771             SDL_SetAlpha(surface, aflags, alpha);
   772         }
   773     }
   774 
   775     /* We're ready to go! */
   776     return (convert);
   777 }
   778 
   779 /*
   780  * Free a surface created by the above function.
   781  */
   782 void
   783 SDL_FreeSurface(SDL_Surface * surface)
   784 {
   785     if (surface == NULL) {
   786         return;
   787     }
   788     if (--surface->refcount > 0) {
   789         return;
   790     }
   791     while (surface->locked > 0) {
   792         SDL_UnlockSurface(surface);
   793     }
   794     if (surface->flags & SDL_RLEACCEL) {
   795         SDL_UnRLESurface(surface, 0);
   796     }
   797     if (surface->format) {
   798         SDL_SetSurfacePalette(surface, NULL);
   799         SDL_FreeFormat(surface->format);
   800         surface->format = NULL;
   801     }
   802     if (surface->map != NULL) {
   803         SDL_FreeBlitMap(surface->map);
   804         surface->map = NULL;
   805     }
   806     if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
   807         SDL_free(surface->pixels);
   808     }
   809     SDL_free(surface);
   810 #ifdef CHECK_LEAKS
   811     --surfaces_allocated;
   812 #endif
   813 }
   814 
   815 /* vi: set ts=4 sw=4 expandtab: */