src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Jan 2011 23:23:57 -0800
changeset 5140 e743b9c3f6d6
parent 5138 da10636e5eca
child 5184 d976b67150c5
permissions -rw-r--r--
Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
     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 &= ~(SDL_COPY_BLEND | SDL_COPY_ADD);
   452     switch (blendMode) {
   453     case SDL_BLENDMODE_NONE:
   454         break;
   455     case SDL_BLENDMODE_BLEND:
   456         surface->map->info.flags |= SDL_COPY_BLEND;
   457         break;
   458     case SDL_BLENDMODE_ADD:
   459         surface->map->info.flags |= SDL_COPY_ADD;
   460         break;
   461     default:
   462         SDL_Unsupported();
   463         status = -1;
   464         break;
   465     }
   466 
   467     if (surface->map->info.flags != flags) {
   468         SDL_InvalidateMap(surface->map);
   469     }
   470 
   471     /* Compatibility mode */
   472     if (surface->map->info.flags & SDL_COPY_BLEND) {
   473         surface->flags |= SDL_SRCALPHA;
   474     } else {
   475         surface->flags &= ~SDL_SRCALPHA;
   476     }
   477 
   478     return status;
   479 }
   480 
   481 int
   482 SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
   483 {
   484     if (!surface) {
   485         return -1;
   486     }
   487 
   488     if (!blendMode) {
   489         return 0;
   490     }
   491 
   492     switch (surface->map->info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
   493     case SDL_COPY_BLEND:
   494         *blendMode = SDL_BLENDMODE_BLEND;
   495         break;
   496     case SDL_COPY_ADD:
   497         *blendMode = SDL_BLENDMODE_ADD;
   498         break;
   499     default:
   500         *blendMode = SDL_BLENDMODE_NONE;
   501         break;
   502     }
   503     return 0;
   504 }
   505 
   506 SDL_bool
   507 SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
   508 {
   509     SDL_Rect full_rect;
   510 
   511     /* Don't do anything if there's no surface to act on */
   512     if (!surface) {
   513         return SDL_FALSE;
   514     }
   515 
   516     /* Set up the full surface rectangle */
   517     full_rect.x = 0;
   518     full_rect.y = 0;
   519     full_rect.w = surface->w;
   520     full_rect.h = surface->h;
   521 
   522     /* Set the clipping rectangle */
   523     if (!rect) {
   524         surface->clip_rect = full_rect;
   525         return SDL_TRUE;
   526     }
   527     return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
   528 }
   529 
   530 void
   531 SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
   532 {
   533     if (surface && rect) {
   534         *rect = surface->clip_rect;
   535     }
   536 }
   537 
   538 /* 
   539  * Set up a blit between two surfaces -- split into three parts:
   540  * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
   541  * verification.  The lower part is a pointer to a low level
   542  * accelerated blitting function.
   543  *
   544  * These parts are separated out and each used internally by this 
   545  * library in the optimimum places.  They are exported so that if
   546  * you know exactly what you are doing, you can optimize your code
   547  * by calling the one(s) you need.
   548  */
   549 int
   550 SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   551               SDL_Surface * dst, SDL_Rect * dstrect)
   552 {
   553     /* Check to make sure the blit mapping is valid */
   554     if ((src->map->dst != dst) ||
   555         (src->map->dst->format_version != src->map->format_version)) {
   556         if (SDL_MapSurface(src, dst) < 0) {
   557             return (-1);
   558         }
   559         /* just here for debugging */
   560 /*         printf */
   561 /*             ("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", */
   562 /*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
   563 /*              dst->map->info.flags, src->map->blit); */
   564     }
   565     return (src->map->blit(src, srcrect, dst, dstrect));
   566 }
   567 
   568 
   569 int
   570 SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
   571               SDL_Surface * dst, SDL_Rect * dstrect)
   572 {
   573     SDL_Rect fulldst;
   574     int srcx, srcy, w, h;
   575 
   576     /* Make sure the surfaces aren't locked */
   577     if (!src || !dst) {
   578         SDL_SetError("SDL_UpperBlit: passed a NULL surface");
   579         return (-1);
   580     }
   581     if (src->locked || dst->locked) {
   582         SDL_SetError("Surfaces must not be locked during blit");
   583         return (-1);
   584     }
   585 
   586     /* If the destination rectangle is NULL, use the entire dest surface */
   587     if (dstrect == NULL) {
   588         fulldst.x = fulldst.y = 0;
   589         dstrect = &fulldst;
   590     }
   591 
   592     /* clip the source rectangle to the source surface */
   593     if (srcrect) {
   594         int maxw, maxh;
   595 
   596         srcx = srcrect->x;
   597         w = srcrect->w;
   598         if (srcx < 0) {
   599             w += srcx;
   600             dstrect->x -= srcx;
   601             srcx = 0;
   602         }
   603         maxw = src->w - srcx;
   604         if (maxw < w)
   605             w = maxw;
   606 
   607         srcy = srcrect->y;
   608         h = srcrect->h;
   609         if (srcy < 0) {
   610             h += srcy;
   611             dstrect->y -= srcy;
   612             srcy = 0;
   613         }
   614         maxh = src->h - srcy;
   615         if (maxh < h)
   616             h = maxh;
   617 
   618     } else {
   619         srcx = srcy = 0;
   620         w = src->w;
   621         h = src->h;
   622     }
   623 
   624     /* clip the destination rectangle against the clip rectangle */
   625     {
   626         SDL_Rect *clip = &dst->clip_rect;
   627         int dx, dy;
   628 
   629         dx = clip->x - dstrect->x;
   630         if (dx > 0) {
   631             w -= dx;
   632             dstrect->x += dx;
   633             srcx += dx;
   634         }
   635         dx = dstrect->x + w - clip->x - clip->w;
   636         if (dx > 0)
   637             w -= dx;
   638 
   639         dy = clip->y - dstrect->y;
   640         if (dy > 0) {
   641             h -= dy;
   642             dstrect->y += dy;
   643             srcy += dy;
   644         }
   645         dy = dstrect->y + h - clip->y - clip->h;
   646         if (dy > 0)
   647             h -= dy;
   648     }
   649 
   650     if (w > 0 && h > 0) {
   651         SDL_Rect sr;
   652         sr.x = srcx;
   653         sr.y = srcy;
   654         sr.w = dstrect->w = w;
   655         sr.h = dstrect->h = h;
   656         return SDL_LowerBlit(src, &sr, dst, dstrect);
   657     }
   658     dstrect->w = dstrect->h = 0;
   659     return 0;
   660 }
   661 
   662 /*
   663  * Lock a surface to directly access the pixels
   664  */
   665 int
   666 SDL_LockSurface(SDL_Surface * surface)
   667 {
   668     if (!surface->locked) {
   669         /* Perform the lock */
   670         if (surface->flags & SDL_RLEACCEL) {
   671             SDL_UnRLESurface(surface, 1);
   672             surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
   673         }
   674     }
   675 
   676     /* Increment the surface lock count, for recursive locks */
   677     ++surface->locked;
   678 
   679     /* Ready to go.. */
   680     return (0);
   681 }
   682 
   683 /*
   684  * Unlock a previously locked surface
   685  */
   686 void
   687 SDL_UnlockSurface(SDL_Surface * surface)
   688 {
   689     /* Only perform an unlock if we are locked */
   690     if (!surface->locked || (--surface->locked > 0)) {
   691         return;
   692     }
   693 
   694     /* Update RLE encoded surface with new data */
   695     if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   696         surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
   697         SDL_RLESurface(surface);
   698     }
   699 }
   700 
   701 /* 
   702  * Convert a surface into the specified pixel format.
   703  */
   704 SDL_Surface *
   705 SDL_ConvertSurface(SDL_Surface * surface, SDL_PixelFormat * format,
   706                    Uint32 flags)
   707 {
   708     SDL_Surface *convert;
   709     Uint32 copy_flags;
   710     SDL_Rect bounds;
   711 
   712     /* Check for empty destination palette! (results in empty image) */
   713     if (format->palette != NULL) {
   714         int i;
   715         for (i = 0; i < format->palette->ncolors; ++i) {
   716             if ((format->palette->colors[i].r != 0xFF) ||
   717                 (format->palette->colors[i].g != 0xFF) ||
   718                 (format->palette->colors[i].b != 0xFF))
   719                 break;
   720         }
   721         if (i == format->palette->ncolors) {
   722             SDL_SetError("Empty destination palette");
   723             return (NULL);
   724         }
   725     }
   726 
   727     /* Create a new surface with the desired format */
   728     convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
   729                                    format->BitsPerPixel, format->Rmask,
   730                                    format->Gmask, format->Bmask,
   731                                    format->Amask);
   732     if (convert == NULL) {
   733         return (NULL);
   734     }
   735 
   736     /* Copy the palette if any */
   737     if (format->palette && convert->format->palette) {
   738         SDL_memcpy(convert->format->palette->colors,
   739                    format->palette->colors,
   740                    format->palette->ncolors * sizeof(SDL_Color));
   741         convert->format->palette->ncolors = format->palette->ncolors;
   742     }
   743 
   744     /* Save the original copy flags */
   745     copy_flags = surface->map->info.flags;
   746     surface->map->info.flags = 0;
   747 
   748     /* Copy over the image data */
   749     bounds.x = 0;
   750     bounds.y = 0;
   751     bounds.w = surface->w;
   752     bounds.h = surface->h;
   753     SDL_LowerBlit(surface, &bounds, convert, &bounds);
   754 
   755     /* Clean up the original surface, and update converted surface */
   756     convert->map->info.r = surface->map->info.r;
   757     convert->map->info.g = surface->map->info.g;
   758     convert->map->info.b = surface->map->info.b;
   759     convert->map->info.a = surface->map->info.a;
   760     convert->map->info.flags =
   761         (copy_flags &
   762          ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
   763            | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
   764            SDL_COPY_RLE_ALPHAKEY));
   765     surface->map->info.flags = copy_flags;
   766     if (copy_flags & SDL_COPY_COLORKEY) {
   767         Uint8 keyR, keyG, keyB, keyA;
   768 
   769         SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
   770                     &keyG, &keyB, &keyA);
   771         SDL_SetColorKey(convert, 1,
   772                         SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
   773         /* This is needed when converting for 3D texture upload */
   774         SDL_ConvertColorkeyToAlpha(convert);
   775     }
   776     SDL_SetClipRect(convert, &surface->clip_rect);
   777 
   778     /* Enable alpha blending by default if the new surface has an
   779      * alpha channel or alpha modulation */
   780     if ((surface->format->Amask && format->Amask) ||
   781         (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
   782         SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
   783     }
   784     if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
   785         SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
   786     }
   787 
   788     /* We're ready to go! */
   789     return (convert);
   790 }
   791 
   792 /*
   793  * Create a surface on the stack for quick blit operations
   794  */
   795 static __inline__ SDL_bool
   796 SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
   797                          void * pixels, int pitch, SDL_Surface * surface, 
   798                          SDL_PixelFormat * format, SDL_BlitMap * blitmap)
   799 {
   800     int bpp;
   801     Uint32 Rmask, Gmask, Bmask, Amask;
   802 
   803     if (!SDL_PixelFormatEnumToMasks(pixel_format,
   804                                     &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   805         return SDL_FALSE;
   806     }
   807     if (bpp <= 8) {
   808         SDL_SetError("Indexed pixel formats not supported");
   809         return SDL_FALSE;
   810     }
   811 
   812     SDL_zerop(surface);
   813     surface->flags = SDL_PREALLOC;
   814     surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
   815     surface->pixels = pixels;
   816     surface->w = width;
   817     surface->h = height;
   818     surface->pitch = pitch;
   819     /* We don't actually need to set up the clip rect for our purposes */
   820     /*SDL_SetClipRect(surface, NULL);*/
   821 
   822     /* Allocate an empty mapping */
   823     SDL_zerop(blitmap);
   824     blitmap->info.r = 0xFF;
   825     blitmap->info.g = 0xFF;
   826     blitmap->info.b = 0xFF;
   827     blitmap->info.a = 0xFF;
   828     surface->map = blitmap;
   829     SDL_FormatChanged(surface);
   830 
   831     /* The surface is ready to go */
   832     surface->refcount = 1;
   833     return SDL_TRUE;
   834 }
   835 
   836 /*
   837  * Copy a block of pixels of one format to another format
   838  */
   839 int SDL_ConvertPixels(int width, int height,
   840                       Uint32 src_format, const void * src, int src_pitch,
   841                       Uint32 dst_format, void * dst, int dst_pitch)
   842 {
   843     SDL_Surface src_surface, dst_surface;
   844     SDL_PixelFormat src_fmt, dst_fmt;
   845     SDL_BlitMap src_blitmap, dst_blitmap;
   846     SDL_Rect rect;
   847 
   848     /* Fast path for same format copy */
   849     if (src_format == dst_format) {
   850         int bpp;
   851 
   852         if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
   853             switch (src_format) {
   854             case SDL_PIXELFORMAT_YV12:
   855             case SDL_PIXELFORMAT_IYUV:
   856             case SDL_PIXELFORMAT_YUY2:
   857             case SDL_PIXELFORMAT_UYVY:
   858             case SDL_PIXELFORMAT_YVYU:
   859                 bpp = 2;
   860             default:
   861                 SDL_SetError("Unknown FOURCC pixel format");
   862                 return -1;
   863             }
   864         } else {
   865             bpp = SDL_BYTESPERPIXEL(src_format);
   866         }
   867         width *= bpp;
   868 
   869         while (height-- > 0) {
   870             SDL_memcpy(dst, src, width);
   871             src = (Uint8*)src + src_pitch;
   872             dst = (Uint8*)dst + dst_pitch;
   873         }
   874         return SDL_TRUE;
   875     }
   876 
   877     if (!SDL_CreateSurfaceOnStack(width, height, src_format, (void*)src,
   878                                   src_pitch,
   879                                   &src_surface, &src_fmt, &src_blitmap)) {
   880         return -1;
   881     }
   882     if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
   883                                   &dst_surface, &dst_fmt, &dst_blitmap)) {
   884         return -1;
   885     }
   886 
   887     /* Set up the rect and go! */
   888     rect.x = 0;
   889     rect.y = 0;
   890     rect.w = width;
   891     rect.h = height;
   892     return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
   893 }
   894 
   895 /*
   896  * Free a surface created by the above function.
   897  */
   898 void
   899 SDL_FreeSurface(SDL_Surface * surface)
   900 {
   901     if (surface == NULL) {
   902         return;
   903     }
   904     if (--surface->refcount > 0) {
   905         return;
   906     }
   907     while (surface->locked > 0) {
   908         SDL_UnlockSurface(surface);
   909     }
   910     if (surface->flags & SDL_RLEACCEL) {
   911         SDL_UnRLESurface(surface, 0);
   912     }
   913     if (surface->format) {
   914         SDL_SetSurfacePalette(surface, NULL);
   915         SDL_FreeFormat(surface->format);
   916         surface->format = NULL;
   917     }
   918     if (surface->map != NULL) {
   919         SDL_FreeBlitMap(surface->map);
   920         surface->map = NULL;
   921     }
   922     if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
   923         SDL_free(surface->pixels);
   924     }
   925     SDL_free(surface);
   926 #ifdef CHECK_LEAKS
   927     --surfaces_allocated;
   928 #endif
   929 }
   930 
   931 /* vi: set ts=4 sw=4 expandtab: */