LoadBMP: use code from SDL_image which allows loading compressed BMP files
authorSylvain Becker <sylvain.becker@gmail.com>
Sat, 31 Aug 2019 22:52:15 +0200
changeset 13046695321a18b46
parent 13045 8254c364ec4a
child 13047 08db6a6f6c23
LoadBMP: use code from SDL_image which allows loading compressed BMP files
src/video/SDL_bmp.c
     1.1 --- a/src/video/SDL_bmp.c	Fri Aug 30 15:32:15 2019 -0700
     1.2 +++ b/src/video/SDL_bmp.c	Sat Aug 31 22:52:15 2019 +0200
     1.3 @@ -54,6 +54,92 @@
     1.4  #define LCS_WINDOWS_COLOR_SPACE    0x57696E20
     1.5  #endif
     1.6  
     1.7 +static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
     1.8 +{
     1.9 +    /*
    1.10 +    | Sets the surface pixels from src.  A bmp image is upside down.
    1.11 +    */
    1.12 +    int pitch = surface->pitch;
    1.13 +    int height = surface->h;
    1.14 +    Uint8 *start = (Uint8 *)surface->pixels;
    1.15 +    Uint8 *end = start + (height*pitch);
    1.16 +    Uint8 *bits = end-pitch, *spot;
    1.17 +    int ofs = 0;
    1.18 +    Uint8 ch;
    1.19 +    Uint8 needsPad;
    1.20 +
    1.21 +#define COPY_PIXEL(x)   spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)
    1.22 +
    1.23 +    for (;;) {
    1.24 +        if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.25 +        /*
    1.26 +        | encoded mode starts with a run length, and then a byte
    1.27 +        | with two colour indexes to alternate between for the run
    1.28 +        */
    1.29 +        if ( ch ) {
    1.30 +            Uint8 pixel;
    1.31 +            if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.32 +            if ( isRle8 ) {                 /* 256-color bitmap, compressed */
    1.33 +                do {
    1.34 +                    COPY_PIXEL(pixel);
    1.35 +                } while (--ch);
    1.36 +            } else {                         /* 16-color bitmap, compressed */
    1.37 +                Uint8 pixel0 = pixel >> 4;
    1.38 +                Uint8 pixel1 = pixel & 0x0F;
    1.39 +                for (;;) {
    1.40 +                    COPY_PIXEL(pixel0); /* even count, high nibble */
    1.41 +                    if (!--ch) break;
    1.42 +                    COPY_PIXEL(pixel1); /* odd count, low nibble */
    1.43 +                    if (!--ch) break;
    1.44 +                }
    1.45 +            }
    1.46 +        } else {
    1.47 +            /*
    1.48 +            | A leading zero is an escape; it may signal the end of the bitmap,
    1.49 +            | a cursor move, or some absolute data.
    1.50 +            | zero tag may be absolute mode or an escape
    1.51 +            */
    1.52 +            if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.53 +            switch (ch) {
    1.54 +            case 0:                         /* end of line */
    1.55 +                ofs = 0;
    1.56 +                bits -= pitch;               /* go to previous */
    1.57 +                break;
    1.58 +            case 1:                         /* end of bitmap */
    1.59 +                return 0;                    /* success! */
    1.60 +            case 2:                         /* delta */
    1.61 +                if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.62 +                ofs += ch;
    1.63 +                if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.64 +                bits -= (ch * pitch);
    1.65 +                break;
    1.66 +            default:                        /* no compression */
    1.67 +                if (isRle8) {
    1.68 +                    needsPad = ( ch & 1 );
    1.69 +                    do {
    1.70 +                        Uint8 pixel;
    1.71 +                        if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.72 +                        COPY_PIXEL(pixel);
    1.73 +                    } while (--ch);
    1.74 +                } else {
    1.75 +                    needsPad = ( ((ch+1)>>1) & 1 ); /* (ch+1)>>1: bytes size */
    1.76 +                    for (;;) {
    1.77 +                        Uint8 pixel;
    1.78 +                        if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.79 +                        COPY_PIXEL(pixel >> 4);
    1.80 +                        if (!--ch) break;
    1.81 +                        COPY_PIXEL(pixel & 0x0F);
    1.82 +                        if (!--ch) break;
    1.83 +                    }
    1.84 +                }
    1.85 +                /* pad at even boundary */
    1.86 +                if ( needsPad && !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.87 +                break;
    1.88 +            }
    1.89 +        }
    1.90 +    }
    1.91 +}
    1.92 +
    1.93  static void CorrectAlphaChannel(SDL_Surface *surface)
    1.94  {
    1.95      /* Check to see if there is any alpha channel data */
    1.96 @@ -87,7 +173,7 @@
    1.97  SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
    1.98  {
    1.99      SDL_bool was_error;
   1.100 -    Sint64 fp_offset = 0;
   1.101 +    Sint64 fp_offset;
   1.102      int bmpPitch;
   1.103      int i, pad;
   1.104      SDL_Surface *surface;
   1.105 @@ -105,32 +191,29 @@
   1.106      SDL_bool correctAlpha = SDL_FALSE;
   1.107  
   1.108      /* The Win32 BMP file header (14 bytes) */
   1.109 -    char magic[2];
   1.110 -    /* Uint32 bfSize = 0; */
   1.111 -    /* Uint16 bfReserved1 = 0; */
   1.112 -    /* Uint16 bfReserved2 = 0; */
   1.113 -    Uint32 bfOffBits = 0;
   1.114 +    char   magic[2];
   1.115 +    Uint32 bfSize;
   1.116 +    Uint16 bfReserved1;
   1.117 +    Uint16 bfReserved2;
   1.118 +    Uint32 bfOffBits;
   1.119  
   1.120      /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
   1.121 -    Uint32 biSize = 0;
   1.122 -    Sint32 biWidth = 0;
   1.123 +    Uint32 biSize;
   1.124 +    Sint32 biWidth;
   1.125      Sint32 biHeight = 0;
   1.126 -    /* Uint16 biPlanes = 0; */
   1.127 -    Uint16 biBitCount = 0;
   1.128 -    Uint32 biCompression = 0;
   1.129 -    /* Uint32 biSizeImage = 0; */
   1.130 -    /* Sint32 biXPelsPerMeter = 0; */
   1.131 -    /* Sint32 biYPelsPerMeter = 0; */
   1.132 -    Uint32 biClrUsed = 0;
   1.133 -    /* Uint32 biClrImportant = 0; */
   1.134 -
   1.135 -    (void) haveRGBMasks;
   1.136 -    (void) haveAlphaMask;
   1.137 +    Uint16 biPlanes;
   1.138 +    Uint16 biBitCount;
   1.139 +    Uint32 biCompression;
   1.140 +    Uint32 biSizeImage;
   1.141 +    Sint32 biXPelsPerMeter;
   1.142 +    Sint32 biYPelsPerMeter;
   1.143 +    Uint32 biClrUsed;
   1.144 +    Uint32 biClrImportant;
   1.145  
   1.146      /* Make sure we are passed a valid data source */
   1.147      surface = NULL;
   1.148      was_error = SDL_FALSE;
   1.149 -    if (src == NULL) {
   1.150 +    if ( src == NULL ) {
   1.151          was_error = SDL_TRUE;
   1.152          goto done;
   1.153      }
   1.154 @@ -138,52 +221,49 @@
   1.155      /* Read in the BMP file header */
   1.156      fp_offset = SDL_RWtell(src);
   1.157      SDL_ClearError();
   1.158 -    if (SDL_RWread(src, magic, 1, 2) != 2) {
   1.159 +    if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
   1.160          SDL_Error(SDL_EFREAD);
   1.161          was_error = SDL_TRUE;
   1.162          goto done;
   1.163      }
   1.164 -    if (SDL_strncmp(magic, "BM", 2) != 0) {
   1.165 +    if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
   1.166          SDL_SetError("File is not a Windows BMP file");
   1.167          was_error = SDL_TRUE;
   1.168          goto done;
   1.169      }
   1.170 -    /* bfSize = */ SDL_ReadLE32(src);
   1.171 -    /* bfReserved1 = */ SDL_ReadLE16(src);
   1.172 -    /* bfReserved2 = */ SDL_ReadLE16(src);
   1.173 -    bfOffBits = SDL_ReadLE32(src);
   1.174 +    bfSize      = SDL_ReadLE32(src);
   1.175 +    bfReserved1 = SDL_ReadLE16(src);
   1.176 +    bfReserved2 = SDL_ReadLE16(src);
   1.177 +    bfOffBits   = SDL_ReadLE32(src);
   1.178  
   1.179      /* Read the Win32 BITMAPINFOHEADER */
   1.180 -    biSize = SDL_ReadLE32(src);
   1.181 -    if (biSize == 12) {   /* really old BITMAPCOREHEADER */
   1.182 -        biWidth = (Uint32) SDL_ReadLE16(src);
   1.183 -        biHeight = (Uint32) SDL_ReadLE16(src);
   1.184 -        /* biPlanes = */ SDL_ReadLE16(src);
   1.185 -        biBitCount = SDL_ReadLE16(src);
   1.186 -        biCompression = BI_RGB;
   1.187 +    biSize      = SDL_ReadLE32(src);
   1.188 +    if ( biSize == 12 ) {   /* really old BITMAPCOREHEADER */
   1.189 +        biWidth     = (Uint32)SDL_ReadLE16(src);
   1.190 +        biHeight    = (Uint32)SDL_ReadLE16(src);
   1.191 +        biPlanes    = SDL_ReadLE16(src);
   1.192 +        biBitCount  = SDL_ReadLE16(src);
   1.193 +        biCompression   = BI_RGB;
   1.194 +        biSizeImage = 0;
   1.195 +        biXPelsPerMeter = 0;
   1.196 +        biYPelsPerMeter = 0;
   1.197 +        biClrUsed   = 0;
   1.198 +        biClrImportant  = 0;
   1.199      } else if (biSize >= 40) {  /* some version of BITMAPINFOHEADER */
   1.200          Uint32 headerSize;
   1.201 -        biWidth = SDL_ReadLE32(src);
   1.202 -        biHeight = SDL_ReadLE32(src);
   1.203 -        /* biPlanes = */ SDL_ReadLE16(src);
   1.204 -        biBitCount = SDL_ReadLE16(src);
   1.205 -        biCompression = SDL_ReadLE32(src);
   1.206 -        /* biSizeImage = */ SDL_ReadLE32(src);
   1.207 -        /* biXPelsPerMeter = */ SDL_ReadLE32(src);
   1.208 -        /* biYPelsPerMeter = */ SDL_ReadLE32(src);
   1.209 -        biClrUsed = SDL_ReadLE32(src);
   1.210 -        /* biClrImportant = */ SDL_ReadLE32(src);
   1.211 +        biWidth     = SDL_ReadLE32(src);
   1.212 +        biHeight    = SDL_ReadLE32(src);
   1.213 +        biPlanes    = SDL_ReadLE16(src);
   1.214 +        biBitCount  = SDL_ReadLE16(src);
   1.215 +        biCompression   = SDL_ReadLE32(src);
   1.216 +        biSizeImage = SDL_ReadLE32(src);
   1.217 +        biXPelsPerMeter = SDL_ReadLE32(src);
   1.218 +        biYPelsPerMeter = SDL_ReadLE32(src);
   1.219 +        biClrUsed   = SDL_ReadLE32(src);
   1.220 +        biClrImportant  = SDL_ReadLE32(src);
   1.221  
   1.222          /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
   1.223 -        if (biSize == 64) {
   1.224 -            /* ignore these extra fields. */
   1.225 -            if (biCompression == BI_BITFIELDS) {
   1.226 -                /* this value is actually huffman compression in this variant. */
   1.227 -                SDL_SetError("Compressed BMP files not supported");
   1.228 -                was_error = SDL_TRUE;
   1.229 -                goto done;
   1.230 -            }
   1.231 -        } else {
   1.232 +        if (biSize != 64) {
   1.233              /* This is complicated. If compression is BI_BITFIELDS, then
   1.234                 we have 3 DWORDS that specify the RGB masks. This is either
   1.235                 stored here in an BITMAPV2INFOHEADER (which only differs in
   1.236 @@ -246,117 +326,102 @@
   1.237  
   1.238      /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
   1.239      switch (biBitCount) {
   1.240 -    case 1:
   1.241 -    case 4:
   1.242 -        ExpandBMP = biBitCount;
   1.243 -        biBitCount = 8;
   1.244 -        break;
   1.245 -    case 2:
   1.246 -    case 3:
   1.247 -    case 5:
   1.248 -    case 6:
   1.249 -    case 7:
   1.250 -		SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
   1.251 -        was_error = SDL_TRUE;
   1.252 -        goto done;
   1.253 -    default:
   1.254 -        ExpandBMP = 0;
   1.255 -        break;
   1.256 +        case 1:
   1.257 +        case 4:
   1.258 +            ExpandBMP = biBitCount;
   1.259 +            biBitCount = 8;
   1.260 +            break;
   1.261 +        case 2:
   1.262 +        case 3:
   1.263 +        case 5:
   1.264 +        case 6:
   1.265 +        case 7:
   1.266 +            SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
   1.267 +            was_error = SDL_TRUE;
   1.268 +            goto done;
   1.269 +        default:
   1.270 +            ExpandBMP = 0;
   1.271 +            break;
   1.272      }
   1.273  
   1.274 -    /* We don't support any BMP compression right now */
   1.275 +    /* RLE4 and RLE8 BMP compression is supported */
   1.276      switch (biCompression) {
   1.277 -    case BI_RGB:
   1.278 -        /* If there are no masks, use the defaults */
   1.279 -        SDL_assert(!haveRGBMasks);
   1.280 -        SDL_assert(!haveAlphaMask);
   1.281 -        /* Default values for the BMP format */
   1.282 -        switch (biBitCount) {
   1.283 -        case 15:
   1.284 -        case 16:
   1.285 -            Rmask = 0x7C00;
   1.286 -            Gmask = 0x03E0;
   1.287 -            Bmask = 0x001F;
   1.288 +        case BI_RGB:
   1.289 +            /* If there are no masks, use the defaults */
   1.290 +            SDL_assert(!haveRGBMasks);
   1.291 +            SDL_assert(!haveAlphaMask);
   1.292 +            /* Default values for the BMP format */
   1.293 +            switch (biBitCount) {
   1.294 +            case 15:
   1.295 +            case 16:
   1.296 +                Rmask = 0x7C00;
   1.297 +                Gmask = 0x03E0;
   1.298 +                Bmask = 0x001F;
   1.299 +                break;
   1.300 +            case 24:
   1.301 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.302 +                Rmask = 0x000000FF;
   1.303 +                Gmask = 0x0000FF00;
   1.304 +                Bmask = 0x00FF0000;
   1.305 +#else
   1.306 +                Rmask = 0x00FF0000;
   1.307 +                Gmask = 0x0000FF00;
   1.308 +                Bmask = 0x000000FF;
   1.309 +#endif
   1.310 +                break;
   1.311 +            case 32:
   1.312 +                /* We don't know if this has alpha channel or not */
   1.313 +                correctAlpha = SDL_TRUE;
   1.314 +                Amask = 0xFF000000;
   1.315 +                Rmask = 0x00FF0000;
   1.316 +                Gmask = 0x0000FF00;
   1.317 +                Bmask = 0x000000FF;
   1.318 +                break;
   1.319 +            default:
   1.320 +                break;
   1.321 +            }
   1.322              break;
   1.323 -        case 24:
   1.324 -#if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.325 -            Rmask = 0x000000FF;
   1.326 -            Gmask = 0x0000FF00;
   1.327 -            Bmask = 0x00FF0000;
   1.328 -#else
   1.329 -            Rmask = 0x00FF0000;
   1.330 -            Gmask = 0x0000FF00;
   1.331 -            Bmask = 0x000000FF;
   1.332 -#endif
   1.333 -            break;
   1.334 -        case 32:
   1.335 -            /* We don't know if this has alpha channel or not */
   1.336 -            correctAlpha = SDL_TRUE;
   1.337 -            Amask = 0xFF000000;
   1.338 -            Rmask = 0x00FF0000;
   1.339 -            Gmask = 0x0000FF00;
   1.340 -            Bmask = 0x000000FF;
   1.341 -            break;
   1.342 +
   1.343 +        case BI_BITFIELDS:
   1.344 +            break;  /* we handled this in the info header. */
   1.345 +
   1.346          default:
   1.347              break;
   1.348 -        }
   1.349 -        break;
   1.350 -
   1.351 -    case BI_BITFIELDS:
   1.352 -        break;  /* we handled this in the info header. */
   1.353 -
   1.354 -    default:
   1.355 -        SDL_SetError("Compressed BMP files not supported");
   1.356 -        was_error = SDL_TRUE;
   1.357 -        goto done;
   1.358      }
   1.359  
   1.360      /* Create a compatible surface, note that the colors are RGB ordered */
   1.361 -    surface =
   1.362 -        SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
   1.363 -                             Bmask, Amask);
   1.364 -    if (surface == NULL) {
   1.365 +    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
   1.366 +            biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, Amask);
   1.367 +    if ( surface == NULL ) {
   1.368          was_error = SDL_TRUE;
   1.369          goto done;
   1.370      }
   1.371  
   1.372      /* Load the palette, if any */
   1.373      palette = (surface->format)->palette;
   1.374 -    if (palette) {
   1.375 -        SDL_assert(biBitCount <= 8);
   1.376 -        if (biClrUsed == 0) {
   1.377 -            biClrUsed = 1 << biBitCount;
   1.378 -		} else if (biClrUsed > (Uint32)(1 << biBitCount)) {
   1.379 -			SDL_SetError("BMP file has an invalid number of colors");
   1.380 -			was_error = SDL_TRUE;
   1.381 -			goto done;
   1.382 +    if ( palette ) {
   1.383 +        if ( SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0 ) {
   1.384 +            SDL_Error(SDL_EFSEEK);
   1.385 +            was_error = SDL_TRUE;
   1.386 +            goto done;
   1.387          }
   1.388 -        if ((int) biClrUsed > palette->ncolors) {
   1.389 -            SDL_Color *colors;
   1.390 -            int ncolors = biClrUsed;
   1.391 -            colors =
   1.392 -                (SDL_Color *) SDL_realloc(palette->colors,
   1.393 -                                          ncolors *
   1.394 -                                          sizeof(*palette->colors));
   1.395 -            if (!colors) {
   1.396 -                SDL_OutOfMemory();
   1.397 -                was_error = SDL_TRUE;
   1.398 -                goto done;
   1.399 -            }
   1.400 -            palette->ncolors = ncolors;
   1.401 -            palette->colors = colors;
   1.402 -        } else if ((int) biClrUsed < palette->ncolors) {
   1.403 -            palette->ncolors = biClrUsed;
   1.404 -        }
   1.405 -        if (biSize == 12) {
   1.406 -            for (i = 0; i < (int) biClrUsed; ++i) {
   1.407 +
   1.408 +        /*
   1.409 +        | guich: always use 1<<bpp b/c some bitmaps can bring wrong information
   1.410 +        | for colorsUsed
   1.411 +        */
   1.412 +        /* if ( biClrUsed == 0 ) {  */
   1.413 +        biClrUsed = 1 << biBitCount;
   1.414 +        /* } */
   1.415 +        if ( biSize == 12 ) {
   1.416 +            for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.417                  SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.418                  SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.419                  SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.420                  palette->colors[i].a = SDL_ALPHA_OPAQUE;
   1.421              }
   1.422          } else {
   1.423 -            for (i = 0; i < (int) biClrUsed; ++i) {
   1.424 +            for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.425                  SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.426                  SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.427                  SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.428 @@ -369,104 +434,111 @@
   1.429                  palette->colors[i].a = SDL_ALPHA_OPAQUE;
   1.430              }
   1.431          }
   1.432 +        palette->ncolors = biClrUsed;
   1.433      }
   1.434  
   1.435      /* Read the surface pixels.  Note that the bmp image is upside down */
   1.436 -    if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
   1.437 +    if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
   1.438          SDL_Error(SDL_EFSEEK);
   1.439          was_error = SDL_TRUE;
   1.440          goto done;
   1.441      }
   1.442 +    if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
   1.443 +        was_error = (SDL_bool)readRlePixels(surface, src, biCompression == BI_RLE8);
   1.444 +        if (was_error) SDL_SetError("Error reading from BMP");
   1.445 +        goto done;
   1.446 +    }
   1.447      top = (Uint8 *)surface->pixels;
   1.448      end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
   1.449      switch (ExpandBMP) {
   1.450 -    case 1:
   1.451 -        bmpPitch = (biWidth + 7) >> 3;
   1.452 -        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.453 -        break;
   1.454 -    case 4:
   1.455 -        bmpPitch = (biWidth + 1) >> 1;
   1.456 -        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.457 -        break;
   1.458 -    default:
   1.459 -        pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
   1.460 -        break;
   1.461 +        case 1:
   1.462 +            bmpPitch = (biWidth + 7) >> 3;
   1.463 +            pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.464 +            break;
   1.465 +        case 4:
   1.466 +            bmpPitch = (biWidth + 1) >> 1;
   1.467 +            pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.468 +            break;
   1.469 +        default:
   1.470 +            pad  = ((surface->pitch%4) ?
   1.471 +                    (4-(surface->pitch%4)) : 0);
   1.472 +            break;
   1.473      }
   1.474 -    if (topDown) {
   1.475 +    if ( topDown ) {
   1.476          bits = top;
   1.477      } else {
   1.478          bits = end - surface->pitch;
   1.479      }
   1.480 -    while (bits >= top && bits < end) {
   1.481 +    while ( bits >= top && bits < end ) {
   1.482          switch (ExpandBMP) {
   1.483 -        case 1:
   1.484 -        case 4:{
   1.485 +            case 1:
   1.486 +            case 4: {
   1.487                  Uint8 pixel = 0;
   1.488 -                int shift = (8 - ExpandBMP);
   1.489 -                for (i = 0; i < surface->w; ++i) {
   1.490 -                    if (i % (8 / ExpandBMP) == 0) {
   1.491 -                        if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.492 +                int   shift = (8-ExpandBMP);
   1.493 +                for ( i=0; i<surface->w; ++i ) {
   1.494 +                    if ( i%(8/ExpandBMP) == 0 ) {
   1.495 +                        if ( !SDL_RWread(src, &pixel, 1, 1) ) {
   1.496                              SDL_SetError("Error reading from BMP");
   1.497                              was_error = SDL_TRUE;
   1.498                              goto done;
   1.499                          }
   1.500                      }
   1.501                      bits[i] = (pixel >> shift);
   1.502 -					if (bits[i] >= biClrUsed) {
   1.503 -						SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.504 -						was_error = SDL_TRUE;
   1.505 -						goto done;
   1.506 -					}
   1.507 +                    if (bits[i] >= biClrUsed) {
   1.508 +                        SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.509 +                        was_error = SDL_TRUE;
   1.510 +                        goto done;
   1.511 +                    }
   1.512                      pixel <<= ExpandBMP;
   1.513                  }
   1.514              }
   1.515              break;
   1.516  
   1.517 -        default:
   1.518 -            if (SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch) {
   1.519 -                SDL_Error(SDL_EFREAD);
   1.520 -                was_error = SDL_TRUE;
   1.521 -                goto done;
   1.522 -            }
   1.523 -			if (biBitCount == 8 && palette && biClrUsed < (Uint32)(1 << biBitCount)) {
   1.524 -				for (i = 0; i < surface->w; ++i) {
   1.525 -					if (bits[i] >= biClrUsed) {
   1.526 -						SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.527 -						was_error = SDL_TRUE;
   1.528 -						goto done;
   1.529 -					}
   1.530 -				}
   1.531 -			}
   1.532 +            default:
   1.533 +                if ( SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch ) {
   1.534 +                    SDL_Error(SDL_EFREAD);
   1.535 +                    was_error = SDL_TRUE;
   1.536 +                    goto done;
   1.537 +                }
   1.538 +                if (biBitCount == 8 && palette && biClrUsed < (1 << biBitCount)) {
   1.539 +                    for (i = 0; i < surface->w; ++i) {
   1.540 +                        if (bits[i] >= biClrUsed) {
   1.541 +                            SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.542 +                            was_error = SDL_TRUE;
   1.543 +                            goto done;
   1.544 +                        }
   1.545 +                    }
   1.546 +                }
   1.547  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.548 -            /* Byte-swap the pixels if needed. Note that the 24bpp
   1.549 -               case has already been taken care of above. */
   1.550 -            switch (biBitCount) {
   1.551 -            case 15:
   1.552 -            case 16:{
   1.553 -                    Uint16 *pix = (Uint16 *) bits;
   1.554 -                    for (i = 0; i < surface->w; i++)
   1.555 -                        pix[i] = SDL_Swap16(pix[i]);
   1.556 -                    break;
   1.557 +                /* Byte-swap the pixels if needed. Note that the 24bpp
   1.558 +                   case has already been taken care of above. */
   1.559 +                switch(biBitCount) {
   1.560 +                    case 15:
   1.561 +                    case 16: {
   1.562 +                        Uint16 *pix = (Uint16 *)bits;
   1.563 +                        for(i = 0; i < surface->w; i++)
   1.564 +                            pix[i] = SDL_Swap16(pix[i]);
   1.565 +                        break;
   1.566 +                    }
   1.567 +
   1.568 +                    case 32: {
   1.569 +                        Uint32 *pix = (Uint32 *)bits;
   1.570 +                        for(i = 0; i < surface->w; i++)
   1.571 +                            pix[i] = SDL_Swap32(pix[i]);
   1.572 +                        break;
   1.573 +                    }
   1.574                  }
   1.575 -
   1.576 -            case 32:{
   1.577 -                    Uint32 *pix = (Uint32 *) bits;
   1.578 -                    for (i = 0; i < surface->w; i++)
   1.579 -                        pix[i] = SDL_Swap32(pix[i]);
   1.580 -                    break;
   1.581 -                }
   1.582 -            }
   1.583  #endif
   1.584 -            break;
   1.585 +                break;
   1.586          }
   1.587          /* Skip padding bytes, ugh */
   1.588 -        if (pad) {
   1.589 +        if ( pad ) {
   1.590              Uint8 padbyte;
   1.591 -            for (i = 0; i < pad; ++i) {
   1.592 +            for ( i=0; i<pad; ++i ) {
   1.593                  SDL_RWread(src, &padbyte, 1, 1);
   1.594              }
   1.595          }
   1.596 -        if (topDown) {
   1.597 +        if ( topDown ) {
   1.598              bits += surface->pitch;
   1.599          } else {
   1.600              bits -= surface->pitch;
   1.601 @@ -475,18 +547,20 @@
   1.602      if (correctAlpha) {
   1.603          CorrectAlphaChannel(surface);
   1.604      }
   1.605 -  done:
   1.606 -    if (was_error) {
   1.607 -        if (src) {
   1.608 +done:
   1.609 +    if ( was_error ) {
   1.610 +        if ( src ) {
   1.611              SDL_RWseek(src, fp_offset, RW_SEEK_SET);
   1.612          }
   1.613 -        SDL_FreeSurface(surface);
   1.614 +        if ( surface ) {
   1.615 +            SDL_FreeSurface(surface);
   1.616 +        }
   1.617          surface = NULL;
   1.618      }
   1.619 -    if (freesrc && src) {
   1.620 +    if ( freesrc && src ) {
   1.621          SDL_RWclose(src);
   1.622      }
   1.623 -    return (surface);
   1.624 +    return(surface);
   1.625  }
   1.626  
   1.627  int