Fixed bug 3352 - Adding alpha mask support to SDL_SaveBMP_RW
authorSam Lantinga <slouken@libsdl.org>
Sat, 01 Oct 2016 11:29:13 -0700
changeset 10404a990b0da18cc
parent 10403 a0c76d344583
child 10405 b8ca95a8eea9
Fixed bug 3352 - Adding alpha mask support to SDL_SaveBMP_RW

Simon Hug

The current SDL_SaveBMP_RW function that saves surfaces to a BMP uses an old bitmap header which doesn't officially support alpha channels. Applications just ignore the byte where the alpha is stored. This can easily be extended by using a newer header version and setting the alpha mask.

The attached patch has these changes:

- Extending the description of the function in the SDL_surface.h header with the supported formats.
- Refining when surfaces get stored to a 32-bit BMP. (Must have bit depth of 8 or higher and must have an alpha mask or colorkey.)
- Fixing a small bug that saves 24-bit BGR surfaces with a colorkey in a 24-bit BMP.
- Adding code that switches to the bitmap header version 4 if the surface has an alpha mask or colorkey. (I chose version 4 because Microsoft didn't lose its documentation behind a file cabinet like they did with version 3.)
- Adding a hint that can disable the use of the version 4 header. This is for people that need the legacy header or like the old behavior better. (I'm not sure about the hint name, though. May need changing if there are any rules to that.)
include/SDL_hints.h
include/SDL_surface.h
src/video/SDL_bmp.c
     1.1 --- a/include/SDL_hints.h	Sat Oct 01 11:22:39 2016 -0700
     1.2 +++ b/include/SDL_hints.h	Sat Oct 01 11:29:13 2016 -0700
     1.3 @@ -670,6 +670,25 @@
     1.4  #define SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4	"SDL_WINDOWS_NO_CLOSE_ON_ALT_F4"
     1.5  
     1.6  /**
     1.7 + *  \brief Prevent SDL from using version 4 of the bitmap header when saving BMPs.
     1.8 + *
     1.9 + * The bitmap header version 4 is required for proper alpha channel support and
    1.10 + * SDL will use it when required. Should this not be desired, this hint can
    1.11 + * force the use of the 40 byte header version which is supported everywhere.
    1.12 + *
    1.13 + * The variable can be set to the following values:
    1.14 + *   "0"       - Surfaces with a colorkey or an alpha channel are saved to a
    1.15 + *               32-bit BMP file with an alpha mask. SDL will use the bitmap
    1.16 + *               header version 4 and set the alpha mask accordingly.
    1.17 + *   "1"       - Surfaces with a colorkey or an alpha channel are saved to a
    1.18 + *               32-bit BMP file without an alpha mask. The alpha channel data
    1.19 + *               will be in the file, but applications are going to ignore it.
    1.20 + *
    1.21 + * The default value is "0".
    1.22 + */
    1.23 +#define SDL_HINT_BMP_SAVE_LEGACY_FORMAT "SDL_BMP_SAVE_LEGACY_FORMAT"
    1.24 +
    1.25 +/**
    1.26   *  \brief  An enumeration of hint priorities
    1.27   */
    1.28  typedef enum
     2.1 --- a/include/SDL_surface.h	Sat Oct 01 11:22:39 2016 -0700
     2.2 +++ b/include/SDL_surface.h	Sat Oct 01 11:29:13 2016 -0700
     2.3 @@ -184,6 +184,12 @@
     2.4  /**
     2.5   *  Save a surface to a seekable SDL data stream (memory or file).
     2.6   *
     2.7 + *  Surfaces with a 24-bit, 32-bit and paletted 8-bit format get saved in the
     2.8 + *  BMP directly. Other RGB formats with 8-bit or higher get converted to a
     2.9 + *  24-bit surface or, if they have an alpha mask or a colorkey, to a 32-bit
    2.10 + *  surface before they are saved. YUV and paletted 1-bit and 4-bit formats are
    2.11 + *  not supported.
    2.12 + *
    2.13   *  If \c freedst is non-zero, the stream will be closed after being written.
    2.14   *
    2.15   *  \return 0 if successful or -1 if there was an error.
     3.1 --- a/src/video/SDL_bmp.c	Sat Oct 01 11:22:39 2016 -0700
     3.2 +++ b/src/video/SDL_bmp.c	Sat Oct 01 11:29:13 2016 -0700
     3.3 @@ -32,6 +32,7 @@
     3.4     This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
     3.5  */
     3.6  
     3.7 +#include "SDL_hints.h"
     3.8  #include "SDL_video.h"
     3.9  #include "SDL_assert.h"
    3.10  #include "SDL_endian.h"
    3.11 @@ -47,6 +48,11 @@
    3.12  #define BI_BITFIELDS    3
    3.13  #endif
    3.14  
    3.15 +/* Logical color space values for BMP files */
    3.16 +#ifndef LCS_WINDOWS_COLOR_SPACE
    3.17 +/* 0x57696E20 == "Win " */
    3.18 +#define LCS_WINDOWS_COLOR_SPACE    0x57696E20
    3.19 +#endif
    3.20  
    3.21  static void CorrectAlphaChannel(SDL_Surface *surface)
    3.22  {
    3.23 @@ -457,6 +463,8 @@
    3.24      int i, pad;
    3.25      SDL_Surface *surface;
    3.26      Uint8 *bits;
    3.27 +    SDL_bool save32bit = SDL_FALSE;
    3.28 +    SDL_bool saveLegacyBMP = SDL_FALSE;
    3.29  
    3.30      /* The Win32 BMP file header (14 bytes) */
    3.31      char magic[2] = { 'B', 'M' };
    3.32 @@ -478,14 +486,24 @@
    3.33      Uint32 biClrUsed;
    3.34      Uint32 biClrImportant;
    3.35  
    3.36 +    /* The additional header members from the Win32 BITMAPV4HEADER struct (108 bytes in total) */
    3.37 +    Uint32 bV4RedMask = 0;
    3.38 +    Uint32 bV4GreenMask = 0;
    3.39 +    Uint32 bV4BlueMask = 0;
    3.40 +    Uint32 bV4AlphaMask = 0;
    3.41 +    Uint32 bV4CSType = 0;
    3.42 +    Sint32 bV4Endpoints[3 * 3] = {0};
    3.43 +    Uint32 bV4GammaRed = 0;
    3.44 +    Uint32 bV4GammaGreen = 0;
    3.45 +    Uint32 bV4GammaBlue = 0;
    3.46 +
    3.47      /* Make sure we have somewhere to save */
    3.48      surface = NULL;
    3.49      if (dst) {
    3.50 -        SDL_bool save32bit = SDL_FALSE;
    3.51  #ifdef SAVE_32BIT_BMP
    3.52          /* We can save alpha information in a 32-bit BMP */
    3.53 -        if (saveme->map->info.flags & SDL_COPY_COLORKEY ||
    3.54 -            saveme->format->Amask) {
    3.55 +        if (saveme->format->BitsPerPixel >= 8 && (saveme->format->Amask ||
    3.56 +            saveme->map->info.flags & SDL_COPY_COLORKEY)) {
    3.57              save32bit = SDL_TRUE;
    3.58          }
    3.59  #endif /* SAVE_32BIT_BMP */
    3.60 @@ -497,7 +515,7 @@
    3.61                  SDL_SetError("%d bpp BMP files not supported",
    3.62                               saveme->format->BitsPerPixel);
    3.63              }
    3.64 -        } else if ((saveme->format->BitsPerPixel == 24) &&
    3.65 +        } else if ((saveme->format->BitsPerPixel == 24) && !save32bit &&
    3.66  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    3.67                     (saveme->format->Rmask == 0x00FF0000) &&
    3.68                     (saveme->format->Gmask == 0x0000FF00) &&
    3.69 @@ -537,6 +555,13 @@
    3.70          return -1;
    3.71      }
    3.72  
    3.73 +    if (save32bit) {
    3.74 +        const char *hint = SDL_GetHint(SDL_HINT_BMP_SAVE_LEGACY_FORMAT);
    3.75 +        if (hint != NULL && (hint[0] == '1' && hint[1] == 0)) {
    3.76 +            saveLegacyBMP = SDL_TRUE;
    3.77 +        }
    3.78 +    }
    3.79 +
    3.80      if (surface && (SDL_LockSurface(surface) == 0)) {
    3.81          const int bw = surface->w * surface->format->BytesPerPixel;
    3.82  
    3.83 @@ -572,6 +597,21 @@
    3.84          }
    3.85          biClrImportant = 0;
    3.86  
    3.87 +        /* Set the BMP info values for the version 4 header */
    3.88 +        if (save32bit && !saveLegacyBMP) {
    3.89 +            biSize = 108;
    3.90 +            biCompression = BI_BITFIELDS;
    3.91 +            /* The BMP format is always little endian, these masks stay the same */
    3.92 +            bV4RedMask   = 0x00ff0000;
    3.93 +            bV4GreenMask = 0x0000ff00;
    3.94 +            bV4BlueMask  = 0x000000ff;
    3.95 +            bV4AlphaMask = 0xff000000;
    3.96 +            bV4CSType = LCS_WINDOWS_COLOR_SPACE;
    3.97 +            bV4GammaRed = 0;
    3.98 +            bV4GammaGreen = 0;
    3.99 +            bV4GammaBlue = 0;
   3.100 +        }
   3.101 +
   3.102          /* Write the BMP info values */
   3.103          SDL_WriteLE32(dst, biSize);
   3.104          SDL_WriteLE32(dst, biWidth);
   3.105 @@ -585,6 +625,21 @@
   3.106          SDL_WriteLE32(dst, biClrUsed);
   3.107          SDL_WriteLE32(dst, biClrImportant);
   3.108  
   3.109 +        /* Write the BMP info values for the version 4 header */
   3.110 +        if (save32bit && !saveLegacyBMP) {
   3.111 +            SDL_WriteLE32(dst, bV4RedMask);
   3.112 +            SDL_WriteLE32(dst, bV4GreenMask);
   3.113 +            SDL_WriteLE32(dst, bV4BlueMask);
   3.114 +            SDL_WriteLE32(dst, bV4AlphaMask);
   3.115 +            SDL_WriteLE32(dst, bV4CSType);
   3.116 +            for (i = 0; i < 3 * 3; i++) {
   3.117 +                SDL_WriteLE32(dst, bV4Endpoints[i]);
   3.118 +            }
   3.119 +            SDL_WriteLE32(dst, bV4GammaRed);
   3.120 +            SDL_WriteLE32(dst, bV4GammaGreen);
   3.121 +            SDL_WriteLE32(dst, bV4GammaBlue);
   3.122 +        }
   3.123 +
   3.124          /* Write the palette (in BGR color order) */
   3.125          if (surface->format->palette) {
   3.126              SDL_Color *colors;