Fix regression: when a palette + colorkey is converted, it needs a blend mode.
authorSylvain Becker
Sun, 23 Feb 2020 22:38:08 +0100
changeset 13547078a70793bd9
parent 13546 d1c02a50171d
child 13548 96382c849dec
Fix regression: when a palette + colorkey is converted, it needs a blend mode.

- Regression of test_1.c of bug 3827, after fix from bug 4798.
- Blending is also needed when the palette contains alpha value, but not necessarily colorkey.
- Clean up SDL_ConvertColorkeyToAlpha which doesn't seem to need 'ignore_alpha' parameter any-more.

(see bug 3827)
src/video/SDL_surface.c
     1.1 --- a/src/video/SDL_surface.c	Sun Feb 23 14:49:03 2020 -0500
     1.2 +++ b/src/video/SDL_surface.c	Sun Feb 23 22:38:08 2020 +0100
     1.3 @@ -964,6 +964,7 @@
     1.4      int ret;
     1.5      int palette_ck_transform = 0;
     1.6      int palette_ck_value = 0;
     1.7 +    int palette_has_alpha = SDL_FALSE;
     1.8  
     1.9      if (!surface) {
    1.10          SDL_InvalidParamError("surface");
    1.11 @@ -1029,6 +1030,7 @@
    1.12      if (copy_flags & SDL_COPY_COLORKEY) {
    1.13          if (surface->format->palette && !format->palette) {
    1.14              palette_ck_transform = 1;
    1.15 +            palette_has_alpha = SDL_TRUE;
    1.16              palette_ck_value = surface->format->palette->colors[surface->map->info.colorkey].a;
    1.17              surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
    1.18          }
    1.19 @@ -1066,7 +1068,6 @@
    1.20  
    1.21      if (copy_flags & SDL_COPY_COLORKEY) {
    1.22          SDL_bool set_colorkey_by_color = SDL_FALSE;
    1.23 -        SDL_bool ignore_alpha          = SDL_TRUE;  /* Ignore, or not, alpha in colorkey comparison */
    1.24  
    1.25          if (surface->format->palette) {
    1.26              if (format->palette &&
    1.27 @@ -1077,9 +1078,6 @@
    1.28                  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
    1.29              } else if (!format->palette) {
    1.30                  /* Was done by 'palette_ck_transform' */
    1.31 -            }else if (format->Amask) {
    1.32 -                set_colorkey_by_color = SDL_TRUE;
    1.33 -                ignore_alpha = SDL_FALSE;
    1.34              } else {
    1.35                  set_colorkey_by_color = SDL_TRUE;
    1.36              }
    1.37 @@ -1120,14 +1118,27 @@
    1.38              SDL_SetColorKey(convert, 1, converted_colorkey);
    1.39  
    1.40              /* This is needed when converting for 3D texture upload */
    1.41 -            SDL_ConvertColorkeyToAlpha(convert, ignore_alpha);
    1.42 +            SDL_ConvertColorkeyToAlpha(convert, SDL_TRUE);
    1.43          }
    1.44      }
    1.45      SDL_SetClipRect(convert, &surface->clip_rect);
    1.46  
    1.47 +    /* Source surface has a palette with alpha. Will need blend mode */
    1.48 +    if (palette_has_alpha == SDL_FALSE && surface->format->palette) {
    1.49 +        int i;
    1.50 +        for (i = 0; i < surface->format->palette->ncolors; i++) {
    1.51 +            Uint8 alpha_value = surface->format->palette->colors[i].a;
    1.52 +            if (alpha_value != 0 && alpha_value != SDL_ALPHA_OPAQUE) {
    1.53 +                palette_has_alpha = SDL_TRUE;
    1.54 +                break;
    1.55 +            }
    1.56 +        }
    1.57 +    }
    1.58 +
    1.59      /* Enable alpha blending by default if the new surface has an
    1.60       * alpha channel or alpha modulation */
    1.61      if ((surface->format->Amask && format->Amask) ||
    1.62 +        (palette_has_alpha && format->Amask) ||
    1.63          (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
    1.64          SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
    1.65      }