Fixed bug 1534 - SIGSEGV in SDL_ConvertSurface() for certain formats in SDL2
authorSam Lantinga <slouken@libsdl.org>
Tue, 21 May 2013 22:01:18 -0700
changeset 7208414ad123be9b
parent 7207 7103daaf5414
child 7209 805754c011a1
Fixed bug 1534 - SIGSEGV in SDL_ConvertSurface() for certain formats in SDL2

Don't assume that 8 bit formats are indexed.
Thanks to Gabriel Jacobo for research and potential patches.
src/video/SDL_blit.c
src/video/SDL_blit_N.c
src/video/SDL_stretch.c
     1.1 --- a/src/video/SDL_blit.c	Tue May 21 23:35:45 2013 -0400
     1.2 +++ b/src/video/SDL_blit.c	Tue May 21 22:01:18 2013 -0700
     1.3 @@ -239,9 +239,11 @@
     1.4      /* Choose a standard blit function */
     1.5      if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
     1.6          blit = SDL_BlitCopy;
     1.7 -    } else if (surface->format->BitsPerPixel < 8) {
     1.8 +    } else if (surface->format->BitsPerPixel < 8 &&
     1.9 +               SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
    1.10          blit = SDL_CalculateBlit0(surface);
    1.11 -    } else if (surface->format->BytesPerPixel == 1) {
    1.12 +    } else if (surface->format->BytesPerPixel == 1 &&
    1.13 +               SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
    1.14          blit = SDL_CalculateBlit1(surface);
    1.15      } else if (map->info.flags & SDL_COPY_BLEND) {
    1.16          blit = SDL_CalculateBlitA(surface);
    1.17 @@ -260,8 +262,13 @@
    1.18      if (blit == NULL)
    1.19  #endif
    1.20      {
    1.21 -        if (surface->format->BytesPerPixel > 1
    1.22 -            && dst->format->BytesPerPixel > 1) {
    1.23 +        Uint32 src_format = surface->format->format;
    1.24 +        Uint32 dst_format = dst->format->format;
    1.25 +
    1.26 +        if (!SDL_ISPIXELFORMAT_INDEXED(src_format) &&
    1.27 +            !SDL_ISPIXELFORMAT_FOURCC(src_format) &&
    1.28 +            !SDL_ISPIXELFORMAT_INDEXED(dst_format) &&
    1.29 +            !SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
    1.30              blit = SDL_Blit_Slow;
    1.31          }
    1.32      }
     2.1 --- a/src/video/SDL_blit_N.c	Tue May 21 23:35:45 2013 -0400
     2.2 +++ b/src/video/SDL_blit_N.c	Tue May 21 22:01:18 2013 -0700
     2.3 @@ -275,7 +275,7 @@
     2.4      vector unsigned int v16 = vec_add(v8, v8);
     2.5      vector unsigned short v2 = vec_splat_u16(2);
     2.6      vector unsigned short v3 = vec_splat_u16(3);
     2.7 -    /* 
     2.8 +    /*
     2.9         0x10 - 0x1f is the alpha
    2.10         0x00 - 0x0e evens are the red
    2.11         0x01 - 0x0f odds are zero
    2.12 @@ -422,7 +422,7 @@
    2.13      vector unsigned int v16 = vec_add(v8, v8);
    2.14      vector unsigned short v1 = vec_splat_u16(1);
    2.15      vector unsigned short v3 = vec_splat_u16(3);
    2.16 -    /* 
    2.17 +    /*
    2.18         0x10 - 0x1f is the alpha
    2.19         0x00 - 0x0e evens are the red
    2.20         0x01 - 0x0f odds are zero
    2.21 @@ -876,18 +876,18 @@
    2.22  
    2.23  /* This is now endian dependent */
    2.24  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    2.25 -#define HI	1
    2.26 -#define LO	0
    2.27 +#define HI  1
    2.28 +#define LO  0
    2.29  #else /* SDL_BYTEORDER == SDL_BIG_ENDIAN */
    2.30 -#define HI	0
    2.31 -#define LO	1
    2.32 +#define HI  0
    2.33 +#define LO  1
    2.34  #endif
    2.35  
    2.36  /* Special optimized blit for RGB 8-8-8 --> RGB 3-3-2 */
    2.37  #define RGB888_RGB332(dst, src) { \
    2.38 -	dst = (Uint8)((((src)&0x00E00000)>>16)| \
    2.39 -	              (((src)&0x0000E000)>>11)| \
    2.40 -	              (((src)&0x000000C0)>>6)); \
    2.41 +    dst = (Uint8)((((src)&0x00E00000)>>16)| \
    2.42 +                  (((src)&0x0000E000)>>11)| \
    2.43 +                  (((src)&0x000000C0)>>6)); \
    2.44  }
    2.45  static void
    2.46  Blit_RGB888_index8(SDL_BlitInfo * info)
    2.47 @@ -913,11 +913,11 @@
    2.48      if (map == NULL) {
    2.49          while (height--) {
    2.50  #ifdef USE_DUFFS_LOOP
    2.51 -			/* *INDENT-OFF* */
    2.52 -			DUFFS_LOOP(
    2.53 -				RGB888_RGB332(*dst++, *src);
    2.54 -			, width);
    2.55 -			/* *INDENT-ON* */
    2.56 +            /* *INDENT-OFF* */
    2.57 +            DUFFS_LOOP(
    2.58 +                RGB888_RGB332(*dst++, *src);
    2.59 +            , width);
    2.60 +            /* *INDENT-ON* */
    2.61  #else
    2.62              for (c = width / 4; c; --c) {
    2.63                  /* Pack RGB into 8bit pixel */
    2.64 @@ -949,13 +949,13 @@
    2.65  
    2.66          while (height--) {
    2.67  #ifdef USE_DUFFS_LOOP
    2.68 -			/* *INDENT-OFF* */
    2.69 -			DUFFS_LOOP(
    2.70 -				RGB888_RGB332(Pixel, *src);
    2.71 -				*dst++ = map[Pixel];
    2.72 -				++src;
    2.73 -			, width);
    2.74 -			/* *INDENT-ON* */
    2.75 +            /* *INDENT-OFF* */
    2.76 +            DUFFS_LOOP(
    2.77 +                RGB888_RGB332(Pixel, *src);
    2.78 +                *dst++ = map[Pixel];
    2.79 +                ++src;
    2.80 +            , width);
    2.81 +            /* *INDENT-ON* */
    2.82  #else
    2.83              for (c = width / 4; c; --c) {
    2.84                  /* Pack RGB into 8bit pixel */
    2.85 @@ -995,17 +995,17 @@
    2.86  
    2.87  /* Special optimized blit for RGB 8-8-8 --> RGB 5-5-5 */
    2.88  #define RGB888_RGB555(dst, src) { \
    2.89 -	*(Uint16 *)(dst) = (Uint16)((((*src)&0x00F80000)>>9)| \
    2.90 -	                            (((*src)&0x0000F800)>>6)| \
    2.91 -	                            (((*src)&0x000000F8)>>3)); \
    2.92 +    *(Uint16 *)(dst) = (Uint16)((((*src)&0x00F80000)>>9)| \
    2.93 +                                (((*src)&0x0000F800)>>6)| \
    2.94 +                                (((*src)&0x000000F8)>>3)); \
    2.95  }
    2.96  #define RGB888_RGB555_TWO(dst, src) { \
    2.97 -	*(Uint32 *)(dst) = (((((src[HI])&0x00F80000)>>9)| \
    2.98 -	                     (((src[HI])&0x0000F800)>>6)| \
    2.99 -	                     (((src[HI])&0x000000F8)>>3))<<16)| \
   2.100 -	                     (((src[LO])&0x00F80000)>>9)| \
   2.101 -	                     (((src[LO])&0x0000F800)>>6)| \
   2.102 -	                     (((src[LO])&0x000000F8)>>3); \
   2.103 +    *(Uint32 *)(dst) = (((((src[HI])&0x00F80000)>>9)| \
   2.104 +                         (((src[HI])&0x0000F800)>>6)| \
   2.105 +                         (((src[HI])&0x000000F8)>>3))<<16)| \
   2.106 +                         (((src[LO])&0x00F80000)>>9)| \
   2.107 +                         (((src[LO])&0x0000F800)>>6)| \
   2.108 +                         (((src[LO])&0x000000F8)>>3); \
   2.109  }
   2.110  static void
   2.111  Blit_RGB888_RGB555(SDL_BlitInfo * info)
   2.112 @@ -1028,13 +1028,13 @@
   2.113  
   2.114  #ifdef USE_DUFFS_LOOP
   2.115      while (height--) {
   2.116 -		/* *INDENT-OFF* */
   2.117 -		DUFFS_LOOP(
   2.118 -			RGB888_RGB555(dst, src);
   2.119 -			++src;
   2.120 -			++dst;
   2.121 -		, width);
   2.122 -		/* *INDENT-ON* */
   2.123 +        /* *INDENT-OFF* */
   2.124 +        DUFFS_LOOP(
   2.125 +            RGB888_RGB555(dst, src);
   2.126 +            ++src;
   2.127 +            ++dst;
   2.128 +        , width);
   2.129 +        /* *INDENT-ON* */
   2.130          src += srcskip;
   2.131          dst += dstskip;
   2.132      }
   2.133 @@ -1119,17 +1119,17 @@
   2.134  
   2.135  /* Special optimized blit for RGB 8-8-8 --> RGB 5-6-5 */
   2.136  #define RGB888_RGB565(dst, src) { \
   2.137 -	*(Uint16 *)(dst) = (Uint16)((((*src)&0x00F80000)>>8)| \
   2.138 -	                            (((*src)&0x0000FC00)>>5)| \
   2.139 -	                            (((*src)&0x000000F8)>>3)); \
   2.140 +    *(Uint16 *)(dst) = (Uint16)((((*src)&0x00F80000)>>8)| \
   2.141 +                                (((*src)&0x0000FC00)>>5)| \
   2.142 +                                (((*src)&0x000000F8)>>3)); \
   2.143  }
   2.144  #define RGB888_RGB565_TWO(dst, src) { \
   2.145 -	*(Uint32 *)(dst) = (((((src[HI])&0x00F80000)>>8)| \
   2.146 -	                     (((src[HI])&0x0000FC00)>>5)| \
   2.147 -	                     (((src[HI])&0x000000F8)>>3))<<16)| \
   2.148 -	                     (((src[LO])&0x00F80000)>>8)| \
   2.149 -	                     (((src[LO])&0x0000FC00)>>5)| \
   2.150 -	                     (((src[LO])&0x000000F8)>>3); \
   2.151 +    *(Uint32 *)(dst) = (((((src[HI])&0x00F80000)>>8)| \
   2.152 +                         (((src[HI])&0x0000FC00)>>5)| \
   2.153 +                         (((src[HI])&0x000000F8)>>3))<<16)| \
   2.154 +                         (((src[LO])&0x00F80000)>>8)| \
   2.155 +                         (((src[LO])&0x0000FC00)>>5)| \
   2.156 +                         (((src[LO])&0x000000F8)>>3); \
   2.157  }
   2.158  static void
   2.159  Blit_RGB888_RGB565(SDL_BlitInfo * info)
   2.160 @@ -1152,13 +1152,13 @@
   2.161  
   2.162  #ifdef USE_DUFFS_LOOP
   2.163      while (height--) {
   2.164 -		/* *INDENT-OFF* */
   2.165 -		DUFFS_LOOP(
   2.166 -			RGB888_RGB565(dst, src);
   2.167 -			++src;
   2.168 -			++dst;
   2.169 -		, width);
   2.170 -		/* *INDENT-ON* */
   2.171 +        /* *INDENT-OFF* */
   2.172 +        DUFFS_LOOP(
   2.173 +            RGB888_RGB565(dst, src);
   2.174 +            ++src;
   2.175 +            ++dst;
   2.176 +        , width);
   2.177 +        /* *INDENT-ON* */
   2.178          src += srcskip;
   2.179          dst += dstskip;
   2.180      }
   2.181 @@ -1265,14 +1265,14 @@
   2.182  
   2.183  #ifdef USE_DUFFS_LOOP
   2.184      while (height--) {
   2.185 -		/* *INDENT-OFF* */
   2.186 -		DUFFS_LOOP(
   2.187 -		{
   2.188 -			*dst++ = RGB565_32(dst, src, map);
   2.189 -			src += 2;
   2.190 -		},
   2.191 -		width);
   2.192 -		/* *INDENT-ON* */
   2.193 +        /* *INDENT-OFF* */
   2.194 +        DUFFS_LOOP(
   2.195 +        {
   2.196 +            *dst++ = RGB565_32(dst, src, map);
   2.197 +            src += 2;
   2.198 +        },
   2.199 +        width);
   2.200 +        /* *INDENT-ON* */
   2.201          src += srcskip;
   2.202          dst += dstskip;
   2.203      }
   2.204 @@ -1863,9 +1863,9 @@
   2.205  /* Special optimized blit for RGB 8-8-8 --> RGB 3-3-2 */
   2.206  #ifndef RGB888_RGB332
   2.207  #define RGB888_RGB332(dst, src) { \
   2.208 -	dst = (((src)&0x00E00000)>>16)| \
   2.209 -	      (((src)&0x0000E000)>>11)| \
   2.210 -	      (((src)&0x000000C0)>>6); \
   2.211 +    dst = (((src)&0x00E00000)>>16)| \
   2.212 +          (((src)&0x0000E000)>>11)| \
   2.213 +          (((src)&0x000000C0)>>6); \
   2.214  }
   2.215  #endif
   2.216  static void
   2.217 @@ -1892,13 +1892,13 @@
   2.218  
   2.219  #ifdef USE_DUFFS_LOOP
   2.220      while (height--) {
   2.221 -		/* *INDENT-OFF* */
   2.222 -		DUFFS_LOOP(
   2.223 -			RGB888_RGB332(Pixel, *src);
   2.224 -			*dst++ = map[Pixel];
   2.225 -			++src;
   2.226 -		, width);
   2.227 -		/* *INDENT-ON* */
   2.228 +        /* *INDENT-OFF* */
   2.229 +        DUFFS_LOOP(
   2.230 +            RGB888_RGB332(Pixel, *src);
   2.231 +            *dst++ = map[Pixel];
   2.232 +            ++src;
   2.233 +        , width);
   2.234 +        /* *INDENT-ON* */
   2.235          src += srcskip;
   2.236          dst += dstskip;
   2.237      }
   2.238 @@ -1969,20 +1969,20 @@
   2.239      if (map == NULL) {
   2.240          while (height--) {
   2.241  #ifdef USE_DUFFS_LOOP
   2.242 -			/* *INDENT-OFF* */
   2.243 -			DUFFS_LOOP(
   2.244 -				DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.245 -								sR, sG, sB);
   2.246 -				if ( 1 ) {
   2.247 -				  	/* Pack RGB into 8bit pixel */
   2.248 -				  	*dst = ((sR>>5)<<(3+2))|
   2.249 -					        ((sG>>5)<<(2)) |
   2.250 -					        ((sB>>6)<<(0)) ;
   2.251 -				}
   2.252 -				dst++;
   2.253 -				src += srcbpp;
   2.254 -			, width);
   2.255 -			/* *INDENT-ON* */
   2.256 +            /* *INDENT-OFF* */
   2.257 +            DUFFS_LOOP(
   2.258 +                DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.259 +                                sR, sG, sB);
   2.260 +                if ( 1 ) {
   2.261 +                    /* Pack RGB into 8bit pixel */
   2.262 +                    *dst = ((sR>>5)<<(3+2))|
   2.263 +                            ((sG>>5)<<(2)) |
   2.264 +                            ((sB>>6)<<(0)) ;
   2.265 +                }
   2.266 +                dst++;
   2.267 +                src += srcbpp;
   2.268 +            , width);
   2.269 +            /* *INDENT-ON* */
   2.270  #else
   2.271              for (c = width; c; --c) {
   2.272                  DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
   2.273 @@ -2001,20 +2001,20 @@
   2.274      } else {
   2.275          while (height--) {
   2.276  #ifdef USE_DUFFS_LOOP
   2.277 -			/* *INDENT-OFF* */
   2.278 -			DUFFS_LOOP(
   2.279 -				DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.280 -								sR, sG, sB);
   2.281 -				if ( 1 ) {
   2.282 -				  	/* Pack RGB into 8bit pixel */
   2.283 -				  	*dst = map[((sR>>5)<<(3+2))|
   2.284 -						   ((sG>>5)<<(2))  |
   2.285 -						   ((sB>>6)<<(0))  ];
   2.286 -				}
   2.287 -				dst++;
   2.288 -				src += srcbpp;
   2.289 -			, width);
   2.290 -			/* *INDENT-ON* */
   2.291 +            /* *INDENT-OFF* */
   2.292 +            DUFFS_LOOP(
   2.293 +                DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.294 +                                sR, sG, sB);
   2.295 +                if ( 1 ) {
   2.296 +                    /* Pack RGB into 8bit pixel */
   2.297 +                    *dst = map[((sR>>5)<<(3+2))|
   2.298 +                           ((sG>>5)<<(2))  |
   2.299 +                           ((sB>>6)<<(0))  ];
   2.300 +                }
   2.301 +                dst++;
   2.302 +                src += srcbpp;
   2.303 +            , width);
   2.304 +            /* *INDENT-ON* */
   2.305  #else
   2.306              for (c = width; c; --c) {
   2.307                  DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
   2.308 @@ -2051,15 +2051,15 @@
   2.309          Uint32 mask = (info->a >> dstfmt->Aloss) << dstfmt->Ashift;
   2.310  
   2.311          while (height--) {
   2.312 -			/* *INDENT-OFF* */
   2.313 -			DUFFS_LOOP(
   2.314 -			{
   2.315 -				*dst = *src | mask;
   2.316 -				++dst;
   2.317 -				++src;
   2.318 -			},
   2.319 -			width);
   2.320 -			/* *INDENT-ON* */
   2.321 +            /* *INDENT-OFF* */
   2.322 +            DUFFS_LOOP(
   2.323 +            {
   2.324 +                *dst = *src | mask;
   2.325 +                ++dst;
   2.326 +                ++src;
   2.327 +            },
   2.328 +            width);
   2.329 +            /* *INDENT-ON* */
   2.330              src = (Uint32 *) ((Uint8 *) src + srcskip);
   2.331              dst = (Uint32 *) ((Uint8 *) dst + dstskip);
   2.332          }
   2.333 @@ -2068,15 +2068,15 @@
   2.334          Uint32 mask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
   2.335  
   2.336          while (height--) {
   2.337 -			/* *INDENT-OFF* */
   2.338 -			DUFFS_LOOP(
   2.339 -			{
   2.340 -				*dst = *src & mask;
   2.341 -				++dst;
   2.342 -				++src;
   2.343 -			},
   2.344 -			width);
   2.345 -			/* *INDENT-ON* */
   2.346 +            /* *INDENT-OFF* */
   2.347 +            DUFFS_LOOP(
   2.348 +            {
   2.349 +                *dst = *src & mask;
   2.350 +                ++dst;
   2.351 +                ++src;
   2.352 +            },
   2.353 +            width);
   2.354 +            /* *INDENT-ON* */
   2.355              src = (Uint32 *) ((Uint8 *) src + srcskip);
   2.356              dst = (Uint32 *) ((Uint8 *) dst + dstskip);
   2.357          }
   2.358 @@ -2099,20 +2099,20 @@
   2.359      unsigned alpha = dstfmt->Amask ? info->a : 0;
   2.360  
   2.361      while (height--) {
   2.362 -		/* *INDENT-OFF* */
   2.363 -		DUFFS_LOOP(
   2.364 -		{
   2.365 +        /* *INDENT-OFF* */
   2.366 +        DUFFS_LOOP(
   2.367 +        {
   2.368              Uint32 Pixel;
   2.369 -			unsigned sR;
   2.370 -			unsigned sG;
   2.371 -			unsigned sB;
   2.372 -			DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
   2.373 -			ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, alpha);
   2.374 -			dst += dstbpp;
   2.375 -			src += srcbpp;
   2.376 -		},
   2.377 -		width);
   2.378 -		/* *INDENT-ON* */
   2.379 +            unsigned sR;
   2.380 +            unsigned sG;
   2.381 +            unsigned sB;
   2.382 +            DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
   2.383 +            ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, alpha);
   2.384 +            dst += dstbpp;
   2.385 +            src += srcbpp;
   2.386 +        },
   2.387 +        width);
   2.388 +        /* *INDENT-ON* */
   2.389          src += srcskip;
   2.390          dst += dstskip;
   2.391      }
   2.392 @@ -2170,43 +2170,43 @@
   2.393  
   2.394      if (palmap == NULL) {
   2.395          while (height--) {
   2.396 -			/* *INDENT-OFF* */
   2.397 -			DUFFS_LOOP(
   2.398 -			{
   2.399 -				DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.400 -								sR, sG, sB);
   2.401 -				if ( (Pixel & rgbmask) != ckey ) {
   2.402 -				  	/* Pack RGB into 8bit pixel */
   2.403 -				  	*dst = (Uint8)(((sR>>5)<<(3+2))|
   2.404 -						           ((sG>>5)<<(2)) |
   2.405 -						           ((sB>>6)<<(0)));
   2.406 -				}
   2.407 -				dst++;
   2.408 -				src += srcbpp;
   2.409 -			},
   2.410 -			width);
   2.411 -			/* *INDENT-ON* */
   2.412 +            /* *INDENT-OFF* */
   2.413 +            DUFFS_LOOP(
   2.414 +            {
   2.415 +                DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.416 +                                sR, sG, sB);
   2.417 +                if ( (Pixel & rgbmask) != ckey ) {
   2.418 +                    /* Pack RGB into 8bit pixel */
   2.419 +                    *dst = (Uint8)(((sR>>5)<<(3+2))|
   2.420 +                                   ((sG>>5)<<(2)) |
   2.421 +                                   ((sB>>6)<<(0)));
   2.422 +                }
   2.423 +                dst++;
   2.424 +                src += srcbpp;
   2.425 +            },
   2.426 +            width);
   2.427 +            /* *INDENT-ON* */
   2.428              src += srcskip;
   2.429              dst += dstskip;
   2.430          }
   2.431      } else {
   2.432          while (height--) {
   2.433 -			/* *INDENT-OFF* */
   2.434 -			DUFFS_LOOP(
   2.435 -			{
   2.436 -				DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.437 -								sR, sG, sB);
   2.438 -				if ( (Pixel & rgbmask) != ckey ) {
   2.439 -				  	/* Pack RGB into 8bit pixel */
   2.440 -				  	*dst = (Uint8)palmap[((sR>>5)<<(3+2))|
   2.441 -							             ((sG>>5)<<(2))  |
   2.442 -							             ((sB>>6)<<(0))  ];
   2.443 -				}
   2.444 -				dst++;
   2.445 -				src += srcbpp;
   2.446 -			},
   2.447 -			width);
   2.448 -			/* *INDENT-ON* */
   2.449 +            /* *INDENT-OFF* */
   2.450 +            DUFFS_LOOP(
   2.451 +            {
   2.452 +                DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel,
   2.453 +                                sR, sG, sB);
   2.454 +                if ( (Pixel & rgbmask) != ckey ) {
   2.455 +                    /* Pack RGB into 8bit pixel */
   2.456 +                    *dst = (Uint8)palmap[((sR>>5)<<(3+2))|
   2.457 +                                         ((sG>>5)<<(2))  |
   2.458 +                                         ((sB>>6)<<(0))  ];
   2.459 +                }
   2.460 +                dst++;
   2.461 +                src += srcbpp;
   2.462 +            },
   2.463 +            width);
   2.464 +            /* *INDENT-ON* */
   2.465              src += srcskip;
   2.466              dst += dstskip;
   2.467          }
   2.468 @@ -2231,17 +2231,17 @@
   2.469      ckey &= rgbmask;
   2.470  
   2.471      while (height--) {
   2.472 -		/* *INDENT-OFF* */
   2.473 -		DUFFS_LOOP(
   2.474 -		{
   2.475 -			if ( (*srcp & rgbmask) != ckey ) {
   2.476 -				*dstp = *srcp;
   2.477 -			}
   2.478 -			dstp++;
   2.479 -			srcp++;
   2.480 -		},
   2.481 -		width);
   2.482 -		/* *INDENT-ON* */
   2.483 +        /* *INDENT-OFF* */
   2.484 +        DUFFS_LOOP(
   2.485 +        {
   2.486 +            if ( (*srcp & rgbmask) != ckey ) {
   2.487 +                *dstp = *srcp;
   2.488 +            }
   2.489 +            dstp++;
   2.490 +            srcp++;
   2.491 +        },
   2.492 +        width);
   2.493 +        /* *INDENT-ON* */
   2.494          srcp += srcskip;
   2.495          dstp += dstskip;
   2.496      }
   2.497 @@ -2268,23 +2268,23 @@
   2.498      ckey &= rgbmask;
   2.499  
   2.500      while (height--) {
   2.501 -		/* *INDENT-OFF* */
   2.502 -		DUFFS_LOOP(
   2.503 -		{
   2.504 +        /* *INDENT-OFF* */
   2.505 +        DUFFS_LOOP(
   2.506 +        {
   2.507              Uint32 Pixel;
   2.508 -			unsigned sR;
   2.509 -			unsigned sG;
   2.510 -			unsigned sB;
   2.511 -			RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
   2.512 -			if ( (Pixel & rgbmask) != ckey ) {
   2.513 +            unsigned sR;
   2.514 +            unsigned sG;
   2.515 +            unsigned sB;
   2.516 +            RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
   2.517 +            if ( (Pixel & rgbmask) != ckey ) {
   2.518                  RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
   2.519 -				ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, alpha);
   2.520 -			}
   2.521 -			dst += dstbpp;
   2.522 -			src += srcbpp;
   2.523 -		},
   2.524 -		width);
   2.525 -		/* *INDENT-ON* */
   2.526 +                ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, alpha);
   2.527 +            }
   2.528 +            dst += dstbpp;
   2.529 +            src += srcbpp;
   2.530 +        },
   2.531 +        width);
   2.532 +        /* *INDENT-ON* */
   2.533          src += srcskip;
   2.534          dst += dstskip;
   2.535      }
   2.536 @@ -2315,18 +2315,18 @@
   2.537      ckey &= rgbmask;
   2.538  
   2.539      while (height--) {
   2.540 -		/* *INDENT-OFF* */
   2.541 -		DUFFS_LOOP(
   2.542 -		{
   2.543 -			DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
   2.544 -			if ( (Pixel & rgbmask) != ckey ) {
   2.545 -				  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, sA);
   2.546 -			}
   2.547 -			dst += dstbpp;
   2.548 -			src += srcbpp;
   2.549 -		},
   2.550 -		width);
   2.551 -		/* *INDENT-ON* */
   2.552 +        /* *INDENT-OFF* */
   2.553 +        DUFFS_LOOP(
   2.554 +        {
   2.555 +            DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
   2.556 +            if ( (Pixel & rgbmask) != ckey ) {
   2.557 +                  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, sR, sG, sB, sA);
   2.558 +            }
   2.559 +            dst += dstbpp;
   2.560 +            src += srcbpp;
   2.561 +        },
   2.562 +        width);
   2.563 +        /* *INDENT-ON* */
   2.564          src += srcskip;
   2.565          dst += dstskip;
   2.566      }
   2.567 @@ -2436,7 +2436,6 @@
   2.568      case 0:
   2.569          blitfun = NULL;
   2.570          if (dstfmt->BitsPerPixel == 8) {
   2.571 -            /* We assume 8-bit destinations are palettized */
   2.572              if ((srcfmt->BytesPerPixel == 4) &&
   2.573                  (srcfmt->Rmask == 0x00FF0000) &&
   2.574                  (srcfmt->Gmask == 0x0000FF00) &&
     3.1 --- a/src/video/SDL_stretch.c	Tue May 21 23:35:45 2013 -0400
     3.2 +++ b/src/video/SDL_stretch.c	Tue May 21 22:01:18 2013 -0700
     3.3 @@ -54,12 +54,12 @@
     3.4  #endif
     3.5  
     3.6  #if defined(_M_IX86) || defined(i386)
     3.7 -#define PREFIX16	0x66
     3.8 -#define STORE_BYTE	0xAA
     3.9 -#define STORE_WORD	0xAB
    3.10 -#define LOAD_BYTE	0xAC
    3.11 -#define LOAD_WORD	0xAD
    3.12 -#define RETURN		0xC3
    3.13 +#define PREFIX16    0x66
    3.14 +#define STORE_BYTE  0xAA
    3.15 +#define STORE_WORD  0xAB
    3.16 +#define LOAD_BYTE   0xAC
    3.17 +#define LOAD_WORD   0xAD
    3.18 +#define RETURN      0xC3
    3.19  #else
    3.20  #error Need assembly opcodes for this architecture
    3.21  #endif
    3.22 @@ -148,23 +148,23 @@
    3.23  
    3.24  #endif /* USE_ASM_STRETCH */
    3.25  
    3.26 -#define DEFINE_COPY_ROW(name, type)			\
    3.27 -static void name(type *src, int src_w, type *dst, int dst_w)	\
    3.28 -{							\
    3.29 -	int i;						\
    3.30 -	int pos, inc;					\
    3.31 -	type pixel = 0;					\
    3.32 -							\
    3.33 -	pos = 0x10000;					\
    3.34 -	inc = (src_w << 16) / dst_w;			\
    3.35 -	for ( i=dst_w; i>0; --i ) {			\
    3.36 -		while ( pos >= 0x10000L ) {		\
    3.37 -			pixel = *src++;			\
    3.38 -			pos -= 0x10000L;		\
    3.39 -		}					\
    3.40 -		*dst++ = pixel;				\
    3.41 -		pos += inc;				\
    3.42 -	}						\
    3.43 +#define DEFINE_COPY_ROW(name, type)         \
    3.44 +static void name(type *src, int src_w, type *dst, int dst_w)    \
    3.45 +{                           \
    3.46 +    int i;                      \
    3.47 +    int pos, inc;                   \
    3.48 +    type pixel = 0;                 \
    3.49 +                            \
    3.50 +    pos = 0x10000;                  \
    3.51 +    inc = (src_w << 16) / dst_w;            \
    3.52 +    for ( i=dst_w; i>0; --i ) {         \
    3.53 +        while ( pos >= 0x10000L ) {     \
    3.54 +            pixel = *src++;         \
    3.55 +            pos -= 0x10000L;        \
    3.56 +        }                   \
    3.57 +        *dst++ = pixel;             \
    3.58 +        pos += inc;             \
    3.59 +    }                       \
    3.60  }
    3.61  /* *INDENT-OFF* */
    3.62  DEFINE_COPY_ROW(copy_row1, Uint8)
    3.63 @@ -220,7 +220,7 @@
    3.64  #endif /* USE_ASM_STRETCH */
    3.65      const int bpp = dst->format->BytesPerPixel;
    3.66  
    3.67 -    if (src->format->BitsPerPixel != dst->format->BitsPerPixel) {
    3.68 +    if (src->format->format != dst->format->format) {
    3.69          return SDL_SetError("Only works with same format surfaces");
    3.70      }
    3.71