Fix: set to opaque when a palette surface is converted to an alpha format.
authorSylvain Becker
Sun, 23 Feb 2020 23:07:15 +0100
changeset 1354896382c849dec
parent 13547 078a70793bd9
child 13549 dfe181848aba
Fix: set to opaque when a palette surface is converted to an alpha format.
src/video/SDL_surface.c
     1.1 --- a/src/video/SDL_surface.c	Sun Feb 23 22:38:08 2020 +0100
     1.2 +++ b/src/video/SDL_surface.c	Sun Feb 23 23:07:15 2020 +0100
     1.3 @@ -402,6 +402,64 @@
     1.4      SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
     1.5  }
     1.6  
     1.7 +
     1.8 +static void
     1.9 +SDL_ConvertSetOpaque(SDL_Surface * surface)
    1.10 +{
    1.11 +    int x, y;
    1.12 +
    1.13 +    if (!surface) {
    1.14 +        return;
    1.15 +    }
    1.16 +
    1.17 +    if (!surface->format->Amask) {
    1.18 +        return;
    1.19 +    }
    1.20 +
    1.21 +    SDL_LockSurface(surface);
    1.22 +
    1.23 +    switch (surface->format->BytesPerPixel) {
    1.24 +    case 2:
    1.25 +        {
    1.26 +            Uint16 *row, *spot;
    1.27 +            Uint16 mask = (Uint16) (surface->format->Amask);
    1.28 +
    1.29 +            row = (Uint16 *) surface->pixels;
    1.30 +            for (y = surface->h; y--;) {
    1.31 +                spot = row;
    1.32 +                for (x = surface->w; x--;) {
    1.33 +                    *spot |= mask;
    1.34 +                    ++spot;
    1.35 +                }
    1.36 +                row += surface->pitch / 2;
    1.37 +            }
    1.38 +        }
    1.39 +        break;
    1.40 +    case 3:
    1.41 +        /* FIXME */
    1.42 +        break;
    1.43 +    case 4:
    1.44 +        {
    1.45 +            Uint32 *row, *spot;
    1.46 +            Uint32 mask = surface->format->Amask;
    1.47 +
    1.48 +            row = (Uint32 *) surface->pixels;
    1.49 +            for (y = surface->h; y--;) {
    1.50 +                spot = row;
    1.51 +                for (x = surface->w; x--;) {
    1.52 +                    *spot |= mask;
    1.53 +                    ++spot;
    1.54 +                }
    1.55 +                row += surface->pitch / 4;
    1.56 +            }
    1.57 +        }
    1.58 +        break;
    1.59 +    }
    1.60 +
    1.61 +    SDL_UnlockSurface(surface);
    1.62 +}
    1.63 +
    1.64 +
    1.65  int
    1.66  SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
    1.67  {
    1.68 @@ -962,9 +1020,9 @@
    1.69      SDL_Color copy_color;
    1.70      SDL_Rect bounds;
    1.71      int ret;
    1.72 -    int palette_ck_transform = 0;
    1.73 +    SDL_bool palette_ck_transform = SDL_FALSE;
    1.74      int palette_ck_value = 0;
    1.75 -    int palette_has_alpha = SDL_FALSE;
    1.76 +    SDL_bool palette_has_alpha = SDL_FALSE;
    1.77  
    1.78      if (!surface) {
    1.79          SDL_InvalidParamError("surface");
    1.80 @@ -1029,7 +1087,7 @@
    1.81      /* Transform colorkey to alpha. for cases where source palette has duplicate values, and colorkey is one of them */
    1.82      if (copy_flags & SDL_COPY_COLORKEY) {
    1.83          if (surface->format->palette && !format->palette) {
    1.84 -            palette_ck_transform = 1;
    1.85 +            palette_ck_transform = SDL_TRUE;
    1.86              palette_has_alpha = SDL_TRUE;
    1.87              palette_ck_value = surface->format->palette->colors[surface->map->info.colorkey].a;
    1.88              surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
    1.89 @@ -1135,6 +1193,26 @@
    1.90          }
    1.91      }
    1.92  
    1.93 +    /* Source surface has a palette with no alpha nor colorkey.
    1.94 +     * Destination format has alpha.
    1.95 +     * We may need to set the alpha channel to opaque. */
    1.96 +    if (surface->format->palette && format->Amask && palette_has_alpha == SDL_FALSE) {
    1.97 +        SDL_bool need_opaque = SDL_FALSE;
    1.98 +        int i;
    1.99 +        for (i = 0; i < surface->format->palette->ncolors; i++) {
   1.100 +            Uint8 alpha_value = surface->format->palette->colors[i].a;
   1.101 +            if (alpha_value == 0) {
   1.102 +                need_opaque = SDL_TRUE;
   1.103 +                break;
   1.104 +            }
   1.105 +        }
   1.106 +
   1.107 +        if (need_opaque) {
   1.108 +            SDL_ConvertSetOpaque(convert);
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +
   1.113      /* Enable alpha blending by default if the new surface has an
   1.114       * alpha channel or alpha modulation */
   1.115      if ((surface->format->Amask && format->Amask) ||