src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5184 d976b67150c5
child 5256 bbdf9250ee8b
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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     /* The flags are no longer used, make the compiler happy */
    45     (void)flags;
    46 
    47     /* Allocate the surface */
    48     surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
    49     if (surface == NULL) {
    50         SDL_OutOfMemory();
    51         return NULL;
    52     }
    53 
    54     surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    55     if (!surface->format) {
    56         SDL_FreeSurface(surface);
    57         return NULL;
    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     /* By default surface with an alpha mask are set up for blending */
   142     if (Amask) {
   143         SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
   144     }
   145 
   146     /* The surface is ready to go */
   147     surface->refcount = 1;
   148 #ifdef CHECK_LEAKS
   149     ++surfaces_allocated;
   150 #endif
   151     return surface;
   152 }
   153 
   154 /*
   155  * Create an RGB surface from an existing memory buffer
   156  */
   157 SDL_Surface *
   158 SDL_CreateRGBSurfaceFrom(void *pixels,
   159                          int width, int height, int depth, int pitch,
   160                          Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
   161                          Uint32 Amask)
   162 {
   163     SDL_Surface *surface;
   164 
   165     surface =
   166         SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
   167     if (surface != NULL) {
   168         surface->flags |= SDL_PREALLOC;
   169         surface->pixels = pixels;
   170         surface->w = width;
   171         surface->h = height;
   172         surface->pitch = pitch;
   173         SDL_SetClipRect(surface, NULL);
   174     }
   175     return surface;
   176 }
   177 
   178 static int
   179 SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
   180 {
   181     SDL_Surface *surface = (SDL_Surface *) userdata;
   182 
   183     SDL_FormatChanged(surface);
   184 
   185     return 0;
   186 }
   187 
   188 int
   189 SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
   190 {
   191     if (!surface || !surface->format) {
   192         SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
   193         return -1;
   194     }
   195 
   196     if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
   197         SDL_SetError
   198             ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
   199         return -1;
   200     }
   201 
   202     if (surface->format->palette == palette) {
   203         return 0;
   204     }
   205 
   206     if (surface->format->palette) {
   207         SDL_DelPaletteWatch(surface->format->palette,
   208                             SDL_SurfacePaletteChanged, surface);
   209     }
   210 
   211     surface->format->palette = palette;
   212 
   213     if (surface->format->palette) {
   214         SDL_AddPaletteWatch(surface->format->palette,
   215                             SDL_SurfacePaletteChanged, surface);
   216     }
   217     return 0;
   218 }
   219 
   220 int
   221 SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
   222 {
   223     int flags;
   224 
   225     if (!surface) {
   226         return -1;
   227     }
   228 
   229     flags = surface->map->info.flags;
   230     if (flag) {
   231         surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
   232     } else {
   233         surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
   234     }
   235     if (surface->map->info.flags != flags) {
   236         SDL_InvalidateMap(surface->map);
   237     }
   238     return 0;
   239 }
   240 
   241 int
   242 SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
   243 {
   244     int flags;
   245 
   246     if (!surface) {
   247         return -1;
   248     }
   249 
   250     if (flag & SDL_RLEACCEL) {
   251         SDL_SetSurfaceRLE(surface, 1);
   252     }
   253 
   254     flags = surface->map->info.flags;
   255     if (flag) {
   256         surface->map->info.flags |= SDL_COPY_COLORKEY;
   257         surface->map->info.colorkey = key;
   258     } else {
   259         surface->map->info.flags &= ~SDL_COPY_COLORKEY;
   260     }
   261     if (surface->map->info.flags != flags) {
   262         SDL_InvalidateMap(surface->map);
   263     }
   264 
   265     /* Compatibility mode */
   266     if (surface->map->info.flags & SDL_COPY_COLORKEY) {
   267         surface->flags |= SDL_SRCCOLORKEY;
   268     } else {
   269         surface->flags &= ~SDL_SRCCOLORKEY;
   270     }
   271 
   272     return 0;
   273 }
   274 
   275 int
   276 SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
   277 {
   278     if (!surface) {
   279         return -1;
   280     }
   281 
   282     if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
   283         return -1;
   284     }
   285 
   286     if (key) {
   287         *key = surface->map->info.colorkey;
   288     }
   289     return 0;
   290 }
   291 
   292 /* This is a fairly slow function to switch from colorkey to alpha */
   293 static void
   294 SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
   295 {
   296     int x, y;
   297 
   298     if (!surface) {
   299         return;
   300     }
   301 
   302     if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
   303         !surface->format->Amask) {
   304         return;
   305     }
   306 
   307     SDL_LockSurface(surface);
   308 
   309     switch (surface->format->BytesPerPixel) {
   310     case 2:
   311         {
   312             Uint16 *row, *spot;
   313             Uint16 ckey = (Uint16) surface->map->info.colorkey;
   314             Uint16 mask = (Uint16) (~surface->format->Amask);
   315 
   316             row = (Uint16 *) surface->pixels;
   317             for (y = surface->h; y--;) {
   318                 spot = row;
   319                 for (x = surface->w; x--;) {
   320                     if (*spot == ckey) {
   321                         *spot &= mask;
   322                     }
   323                     ++spot;
   324                 }
   325                 row += surface->pitch / 2;
   326             }
   327         }
   328         break;
   329     case 3:
   330         /* FIXME */
   331         break;
   332     case 4:
   333         {
   334             Uint32 *row, *spot;
   335             Uint32 ckey = surface->map->info.colorkey;
   336             Uint32 mask = ~surface->format->Amask;
   337 
   338             row = (Uint32 *) surface->pixels;
   339             for (y = surface->h; y--;) {
   340                 spot = row;
   341                 for (x = surface->w; x--;) {
   342                     if (*spot == ckey) {
   343                         *spot &= mask;
   344                     }
   345                     ++spot;
   346                 }
   347                 row += surface->pitch / 4;
   348             }
   349         }
   350         break;
   351     }
   352 
   353     SDL_UnlockSurface(surface);
   354 
   355     SDL_SetColorKey(surface, 0, 0);
   356     SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
   357 }
   358 
   359 int
   360 SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
   361 {
   362     int flags;
   363 
   364     if (!surface) {
   365         return -1;
   366     }
   367 
   368     surface->map->info.r = r;
   369     surface->map->info.g = g;
   370     surface->map->info.b = b;
   371 
   372     flags = surface->map->info.flags;
   373     if (r != 0xFF || g != 0xFF || b != 0xFF) {
   374         surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
   375     } else {
   376         surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
   377     }
   378     if (surface->map->info.flags != flags) {
   379         SDL_InvalidateMap(surface->map);
   380     }
   381     return 0;
   382 }
   383 
   384 
   385 int
   386 SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
   387 {
   388     if (!surface) {
   389         return -1;
   390     }
   391 
   392     if (r) {
   393         *r = surface->map->info.r;
   394     }
   395     if (g) {
   396         *g = surface->map->info.g;
   397     }
   398     if (b) {
   399         *b = surface->map->info.b;
   400     }
   401     return 0;
   402 }
   403 
   404 int
   405 SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
   406 {
   407     int flags;
   408 
   409     if (!surface) {
   410         return -1;
   411     }
   412 
   413     surface->map->info.a = alpha;
   414 
   415     flags = surface->map->info.flags;
   416     if (alpha != 0xFF) {
   417         surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
   418     } else {
   419         surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
   420     }
   421     if (surface->map->info.flags != flags) {
   422         SDL_InvalidateMap(surface->map);
   423     }
   424     return 0;
   425 }
   426 
   427 int
   428 SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
   429 {
   430     if (!surface) {
   431         return -1;
   432     }
   433 
   434     if (alpha) {
   435         *alpha = surface->map->info.a;
   436     }
   437     return 0;
   438 }
   439 
   440 int
   441 SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
   442 {
   443     int flags, status;
   444 
   445     if (!surface) {
   446         return -1;
   447     }
   448 
   449     status = 0;
   450     flags = surface->map->info.flags;
   451     surface->map->info.flags &=
   452         ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
   453     switch (blendMode) {
   454     case SDL_BLENDMODE_NONE:
   455         break;
   456     case SDL_BLENDMODE_BLEND:
   457         surface->map->info.flags |= SDL_COPY_BLEND;
   458         break;
   459     case SDL_BLENDMODE_ADD:
   460         surface->map->info.flags |= SDL_COPY_ADD;
   461         break;
   462     case SDL_BLENDMODE_MOD:
   463         surface->map->info.flags |= SDL_COPY_MOD;
   464         break;
   465     default:
   466         SDL_Unsupported();
   467         status = -1;
   468         break;
   469     }
   470 
   471     if (surface->map->info.flags != flags) {
   472         SDL_InvalidateMap(surface->map);
   473     }
   474 
   475     /* Compatibility mode */
   476     if (surface->map->info.flags & SDL_COPY_BLEND) {
   477         surface->flags |= SDL_SRCALPHA;
   478     } else {
   479         surface->flags &= ~SDL_SRCALPHA;
   480     }
   481 
   482     return status;
   483 }
   484 
   485 int
   486 SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
   487 {
   488     if (!surface) {
   489         return -1;
   490     }
   491 
   492     if (!blendMode) {
   493         return 0;
   494     }
   495 
   496     switch (surface->map->
   497             info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
   498     case SDL_COPY_BLEND:
   499         *blendMode = SDL_BLENDMODE_BLEND;
   500         break;
   501     case SDL_COPY_ADD:
   502         *blendMode = SDL_BLENDMODE_ADD;
   503         break;
   504     case SDL_COPY_MOD:
   505         *blendMode = SDL_BLENDMODE_MOD;
   506         break;
   507     default:
   508         *blendMode = SDL_BLENDMODE_NONE;
   509         break;
   510     }
   511     return 0;
   512 }
   513 
   514 SDL_bool
   515 SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
   516 {
   517     SDL_Rect full_rect;
   518 
   519     /* Don't do anything if there's no surface to act on */
   520     if (!surface) {
   521         return SDL_FALSE;
   522     }
   523 
   524     /* Set up the full surface rectangle */
   525     full_rect.x = 0;
   526     full_rect.y = 0;
   527     full_rect.w = surface->w;
   528     full_rect.h = surface->h;
   529 
   530     /* Set the clipping rectangle */
   531     if (!rect) {
   532         surface->clip_rect = full_rect;
   533         return SDL_TRUE;
   534     }
   535     return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
   536 }
   537 
   538 void
   539 SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
   540 {
   541     if (surface && rect) {
   542         *rect = surface->clip_rect;
   543     }
   544 }
   545 
   546 /* 
   547  * Set up a blit between two surfaces -- split into three parts:
   548  * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
   549  * verification.  The lower part is a pointer to a low level
   550  * accelerated blitting function.
   551  *
   552  * These parts are separated out and each used internally by this 
   553  * library in the optimimum places.  They are exported so that if
   554  * you know exactly what you are doing, you can optimize your code
   555  * by calling the one(s) you need.
   556  */
   557 int
   558 SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   559               SDL_Surface * dst, SDL_Rect * dstrect)
   560 {
   561     /* Check to make sure the blit mapping is valid */
   562     if ((src->map->dst != dst) ||
   563         (src->map->dst->format_version != src->map->format_version)) {
   564         if (SDL_MapSurface(src, dst) < 0) {
   565             return (-1);
   566         }
   567         /* just here for debugging */
   568 /*         printf */
   569 /*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
   570 /*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
   571 /*              dst->map->info.flags, src->map->blit); */
   572     }
   573     return (src->map->blit(src, srcrect, dst, dstrect));
   574 }
   575 
   576 
   577 int
   578 SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
   579               SDL_Surface * dst, SDL_Rect * dstrect)
   580 {
   581     SDL_Rect fulldst;
   582     int srcx, srcy, w, h;
   583 
   584     /* Make sure the surfaces aren't locked */
   585     if (!src || !dst) {
   586         SDL_SetError("SDL_UpperBlit: passed a NULL surface");
   587         return (-1);
   588     }
   589     if (src->locked || dst->locked) {
   590         SDL_SetError("Surfaces must not be locked during blit");
   591         return (-1);
   592     }
   593 
   594     /* If the destination rectangle is NULL, use the entire dest surface */
   595     if (dstrect == NULL) {
   596         fulldst.x = fulldst.y = 0;
   597         dstrect = &fulldst;
   598     }
   599 
   600     /* clip the source rectangle to the source surface */
   601     if (srcrect) {
   602         int maxw, maxh;
   603 
   604         srcx = srcrect->x;
   605         w = srcrect->w;
   606         if (srcx < 0) {
   607             w += srcx;
   608             dstrect->x -= srcx;
   609             srcx = 0;
   610         }
   611         maxw = src->w - srcx;
   612         if (maxw < w)
   613             w = maxw;
   614 
   615         srcy = srcrect->y;
   616         h = srcrect->h;
   617         if (srcy < 0) {
   618             h += srcy;
   619             dstrect->y -= srcy;
   620             srcy = 0;
   621         }
   622         maxh = src->h - srcy;
   623         if (maxh < h)
   624             h = maxh;
   625 
   626     } else {
   627         srcx = srcy = 0;
   628         w = src->w;
   629         h = src->h;
   630     }
   631 
   632     /* clip the destination rectangle against the clip rectangle */
   633     {
   634         SDL_Rect *clip = &dst->clip_rect;
   635         int dx, dy;
   636 
   637         dx = clip->x - dstrect->x;
   638         if (dx > 0) {
   639             w -= dx;
   640             dstrect->x += dx;
   641             srcx += dx;
   642         }
   643         dx = dstrect->x + w - clip->x - clip->w;
   644         if (dx > 0)
   645             w -= dx;
   646 
   647         dy = clip->y - dstrect->y;
   648         if (dy > 0) {
   649             h -= dy;
   650             dstrect->y += dy;
   651             srcy += dy;
   652         }
   653         dy = dstrect->y + h - clip->y - clip->h;
   654         if (dy > 0)
   655             h -= dy;
   656     }
   657 
   658     if (w > 0 && h > 0) {
   659         SDL_Rect sr;
   660         sr.x = srcx;
   661         sr.y = srcy;
   662         sr.w = dstrect->w = w;
   663         sr.h = dstrect->h = h;
   664         return SDL_LowerBlit(src, &sr, dst, dstrect);
   665     }
   666     dstrect->w = dstrect->h = 0;
   667     return 0;
   668 }
   669 
   670 /*
   671  * Lock a surface to directly access the pixels
   672  */
   673 int
   674 SDL_LockSurface(SDL_Surface * surface)
   675 {
   676     if (!surface->locked) {
   677         /* Perform the lock */
   678         if (surface->flags & SDL_RLEACCEL) {
   679             SDL_UnRLESurface(surface, 1);
   680             surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
   681         }
   682     }
   683 
   684     /* Increment the surface lock count, for recursive locks */
   685     ++surface->locked;
   686 
   687     /* Ready to go.. */
   688     return (0);
   689 }
   690 
   691 /*
   692  * Unlock a previously locked surface
   693  */
   694 void
   695 SDL_UnlockSurface(SDL_Surface * surface)
   696 {
   697     /* Only perform an unlock if we are locked */
   698     if (!surface->locked || (--surface->locked > 0)) {
   699         return;
   700     }
   701 
   702     /* Update RLE encoded surface with new data */
   703     if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   704         surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
   705         SDL_RLESurface(surface);
   706     }
   707 }
   708 
   709 /* 
   710  * Convert a surface into the specified pixel format.
   711  */
   712 SDL_Surface *
   713 SDL_ConvertSurface(SDL_Surface * surface, SDL_PixelFormat * format,
   714                    Uint32 flags)
   715 {
   716     SDL_Surface *convert;
   717     Uint32 copy_flags;
   718     SDL_Rect bounds;
   719 
   720     /* Check for empty destination palette! (results in empty image) */
   721     if (format->palette != NULL) {
   722         int i;
   723         for (i = 0; i < format->palette->ncolors; ++i) {
   724             if ((format->palette->colors[i].r != 0xFF) ||
   725                 (format->palette->colors[i].g != 0xFF) ||
   726                 (format->palette->colors[i].b != 0xFF))
   727                 break;
   728         }
   729         if (i == format->palette->ncolors) {
   730             SDL_SetError("Empty destination palette");
   731             return (NULL);
   732         }
   733     }
   734 
   735     /* Create a new surface with the desired format */
   736     convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
   737                                    format->BitsPerPixel, format->Rmask,
   738                                    format->Gmask, format->Bmask,
   739                                    format->Amask);
   740     if (convert == NULL) {
   741         return (NULL);
   742     }
   743 
   744     /* Copy the palette if any */
   745     if (format->palette && convert->format->palette) {
   746         SDL_memcpy(convert->format->palette->colors,
   747                    format->palette->colors,
   748                    format->palette->ncolors * sizeof(SDL_Color));
   749         convert->format->palette->ncolors = format->palette->ncolors;
   750     }
   751 
   752     /* Save the original copy flags */
   753     copy_flags = surface->map->info.flags;
   754     surface->map->info.flags = 0;
   755 
   756     /* Copy over the image data */
   757     bounds.x = 0;
   758     bounds.y = 0;
   759     bounds.w = surface->w;
   760     bounds.h = surface->h;
   761     SDL_LowerBlit(surface, &bounds, convert, &bounds);
   762 
   763     /* Clean up the original surface, and update converted surface */
   764     convert->map->info.r = surface->map->info.r;
   765     convert->map->info.g = surface->map->info.g;
   766     convert->map->info.b = surface->map->info.b;
   767     convert->map->info.a = surface->map->info.a;
   768     convert->map->info.flags =
   769         (copy_flags &
   770          ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
   771            | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
   772            SDL_COPY_RLE_ALPHAKEY));
   773     surface->map->info.flags = copy_flags;
   774     if (copy_flags & SDL_COPY_COLORKEY) {
   775         Uint8 keyR, keyG, keyB, keyA;
   776 
   777         SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
   778                     &keyG, &keyB, &keyA);
   779         SDL_SetColorKey(convert, 1,
   780                         SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
   781         /* This is needed when converting for 3D texture upload */
   782         SDL_ConvertColorkeyToAlpha(convert);
   783     }
   784     SDL_SetClipRect(convert, &surface->clip_rect);
   785 
   786     /* Enable alpha blending by default if the new surface has an
   787      * alpha channel or alpha modulation */
   788     if ((surface->format->Amask && format->Amask) ||
   789         (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
   790         SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
   791     }
   792     if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
   793         SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
   794     }
   795 
   796     /* We're ready to go! */
   797     return (convert);
   798 }
   799 
   800 /*
   801  * Create a surface on the stack for quick blit operations
   802  */
   803 static __inline__ SDL_bool
   804 SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
   805                          void * pixels, int pitch, SDL_Surface * surface, 
   806                          SDL_PixelFormat * format, SDL_BlitMap * blitmap)
   807 {
   808     int bpp;
   809     Uint32 Rmask, Gmask, Bmask, Amask;
   810 
   811     if (!SDL_PixelFormatEnumToMasks(pixel_format,
   812                                     &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   813         return SDL_FALSE;
   814     }
   815     if (bpp <= 8) {
   816         SDL_SetError("Indexed pixel formats not supported");
   817         return SDL_FALSE;
   818     }
   819 
   820     SDL_zerop(surface);
   821     surface->flags = SDL_PREALLOC;
   822     surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
   823     surface->pixels = pixels;
   824     surface->w = width;
   825     surface->h = height;
   826     surface->pitch = pitch;
   827     /* We don't actually need to set up the clip rect for our purposes */
   828     /*SDL_SetClipRect(surface, NULL);*/
   829 
   830     /* Allocate an empty mapping */
   831     SDL_zerop(blitmap);
   832     blitmap->info.r = 0xFF;
   833     blitmap->info.g = 0xFF;
   834     blitmap->info.b = 0xFF;
   835     blitmap->info.a = 0xFF;
   836     surface->map = blitmap;
   837     SDL_FormatChanged(surface);
   838 
   839     /* The surface is ready to go */
   840     surface->refcount = 1;
   841     return SDL_TRUE;
   842 }
   843 
   844 /*
   845  * Copy a block of pixels of one format to another format
   846  */
   847 int SDL_ConvertPixels(int width, int height,
   848                       Uint32 src_format, const void * src, int src_pitch,
   849                       Uint32 dst_format, void * dst, int dst_pitch)
   850 {
   851     SDL_Surface src_surface, dst_surface;
   852     SDL_PixelFormat src_fmt, dst_fmt;
   853     SDL_BlitMap src_blitmap, dst_blitmap;
   854     SDL_Rect rect;
   855 
   856     /* Fast path for same format copy */
   857     if (src_format == dst_format) {
   858         int bpp;
   859 
   860         if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
   861             switch (src_format) {
   862             case SDL_PIXELFORMAT_YV12:
   863             case SDL_PIXELFORMAT_IYUV:
   864             case SDL_PIXELFORMAT_YUY2:
   865             case SDL_PIXELFORMAT_UYVY:
   866             case SDL_PIXELFORMAT_YVYU:
   867                 bpp = 2;
   868             default:
   869                 SDL_SetError("Unknown FOURCC pixel format");
   870                 return -1;
   871             }
   872         } else {
   873             bpp = SDL_BYTESPERPIXEL(src_format);
   874         }
   875         width *= bpp;
   876 
   877         while (height-- > 0) {
   878             SDL_memcpy(dst, src, width);
   879             src = (Uint8*)src + src_pitch;
   880             dst = (Uint8*)dst + dst_pitch;
   881         }
   882         return SDL_TRUE;
   883     }
   884 
   885     if (!SDL_CreateSurfaceOnStack(width, height, src_format, (void*)src,
   886                                   src_pitch,
   887                                   &src_surface, &src_fmt, &src_blitmap)) {
   888         return -1;
   889     }
   890     if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
   891                                   &dst_surface, &dst_fmt, &dst_blitmap)) {
   892         return -1;
   893     }
   894 
   895     /* Set up the rect and go! */
   896     rect.x = 0;
   897     rect.y = 0;
   898     rect.w = width;
   899     rect.h = height;
   900     return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
   901 }
   902 
   903 /*
   904  * Free a surface created by the above function.
   905  */
   906 void
   907 SDL_FreeSurface(SDL_Surface * surface)
   908 {
   909     if (surface == NULL) {
   910         return;
   911     }
   912     if (--surface->refcount > 0) {
   913         return;
   914     }
   915     while (surface->locked > 0) {
   916         SDL_UnlockSurface(surface);
   917     }
   918     if (surface->flags & SDL_RLEACCEL) {
   919         SDL_UnRLESurface(surface, 0);
   920     }
   921     if (surface->format) {
   922         SDL_SetSurfacePalette(surface, NULL);
   923         SDL_FreeFormat(surface->format);
   924         surface->format = NULL;
   925     }
   926     if (surface->map != NULL) {
   927         SDL_FreeBlitMap(surface->map);
   928         surface->map = NULL;
   929     }
   930     if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
   931         SDL_free(surface->pixels);
   932     }
   933     SDL_free(surface);
   934 #ifdef CHECK_LEAKS
   935     --surfaces_allocated;
   936 #endif
   937 }
   938 
   939 /* vi: set ts=4 sw=4 expandtab: */