SDL_bmp.c: restore most of the original formatting.
authorOzkan Sezer <sezeroz@gmail.com>
Mon, 02 Sep 2019 12:35:00 +0300
changeset 130500ff6de20d4f9
parent 13049 6ee12b88beed
child 13052 6203d73874ab
SDL_bmp.c: restore most of the original formatting.
src/video/SDL_bmp.c
     1.1 --- a/src/video/SDL_bmp.c	Mon Sep 02 00:11:58 2019 -0400
     1.2 +++ b/src/video/SDL_bmp.c	Mon Sep 02 12:35:00 2019 +0300
     1.3 @@ -71,15 +71,15 @@
     1.4  #define COPY_PIXEL(x)   spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)
     1.5  
     1.6      for (;;) {
     1.7 -        if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
     1.8 +        if (!SDL_RWread(src, &ch, 1, 1)) return 1;
     1.9          /*
    1.10          | encoded mode starts with a run length, and then a byte
    1.11          | with two colour indexes to alternate between for the run
    1.12          */
    1.13 -        if ( ch ) {
    1.14 +        if (ch) {
    1.15              Uint8 pixel;
    1.16 -            if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.17 -            if ( isRle8 ) {                 /* 256-color bitmap, compressed */
    1.18 +            if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
    1.19 +            if (isRle8) {                   /* 256-color bitmap, compressed */
    1.20                  do {
    1.21                      COPY_PIXEL(pixel);
    1.22                  } while (--ch);
    1.23 @@ -99,7 +99,7 @@
    1.24              | a cursor move, or some absolute data.
    1.25              | zero tag may be absolute mode or an escape
    1.26              */
    1.27 -            if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.28 +            if (!SDL_RWread(src, &ch, 1, 1)) return 1;
    1.29              switch (ch) {
    1.30              case 0:                         /* end of line */
    1.31                  ofs = 0;
    1.32 @@ -108,24 +108,24 @@
    1.33              case 1:                         /* end of bitmap */
    1.34                  return 0;                    /* success! */
    1.35              case 2:                         /* delta */
    1.36 -                if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.37 +                if (!SDL_RWread(src, &ch, 1, 1)) return 1;
    1.38                  ofs += ch;
    1.39 -                if ( !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.40 +                if (!SDL_RWread(src, &ch, 1, 1)) return 1;
    1.41                  bits -= (ch * pitch);
    1.42                  break;
    1.43              default:                        /* no compression */
    1.44                  if (isRle8) {
    1.45 -                    needsPad = ( ch & 1 );
    1.46 +                    needsPad = (ch & 1);
    1.47                      do {
    1.48                          Uint8 pixel;
    1.49 -                        if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.50 +                        if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
    1.51                          COPY_PIXEL(pixel);
    1.52                      } while (--ch);
    1.53                  } else {
    1.54 -                    needsPad = ( ((ch+1)>>1) & 1 ); /* (ch+1)>>1: bytes size */
    1.55 +                    needsPad = (((ch+1)>>1) & 1); /* (ch+1)>>1: bytes size */
    1.56                      for (;;) {
    1.57                          Uint8 pixel;
    1.58 -                        if ( !SDL_RWread(src, &pixel, 1, 1) ) return 1;
    1.59 +                        if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
    1.60                          COPY_PIXEL(pixel >> 4);
    1.61                          if (!--ch) break;
    1.62                          COPY_PIXEL(pixel & 0x0F);
    1.63 @@ -133,7 +133,7 @@
    1.64                      }
    1.65                  }
    1.66                  /* pad at even boundary */
    1.67 -                if ( needsPad && !SDL_RWread(src, &ch, 1, 1) ) return 1;
    1.68 +                if (needsPad && !SDL_RWread(src, &ch, 1, 1)) return 1;
    1.69                  break;
    1.70              }
    1.71          }
    1.72 @@ -191,7 +191,7 @@
    1.73      SDL_bool correctAlpha = SDL_FALSE;
    1.74  
    1.75      /* The Win32 BMP file header (14 bytes) */
    1.76 -    char   magic[2];
    1.77 +    char magic[2];
    1.78      Uint32 bfSize;
    1.79      Uint16 bfReserved1;
    1.80      Uint16 bfReserved2;
    1.81 @@ -213,7 +213,7 @@
    1.82      /* Make sure we are passed a valid data source */
    1.83      surface = NULL;
    1.84      was_error = SDL_FALSE;
    1.85 -    if ( src == NULL ) {
    1.86 +    if (src == NULL) {
    1.87          was_error = SDL_TRUE;
    1.88          goto done;
    1.89      }
    1.90 @@ -221,12 +221,12 @@
    1.91      /* Read in the BMP file header */
    1.92      fp_offset = SDL_RWtell(src);
    1.93      SDL_ClearError();
    1.94 -    if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
    1.95 +    if (SDL_RWread(src, magic, 1, 2) != 2) {
    1.96          SDL_Error(SDL_EFREAD);
    1.97          was_error = SDL_TRUE;
    1.98          goto done;
    1.99      }
   1.100 -    if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
   1.101 +    if (SDL_strncmp(magic, "BM", 2) != 0) {
   1.102          SDL_SetError("File is not a Windows BMP file");
   1.103          was_error = SDL_TRUE;
   1.104          goto done;
   1.105 @@ -237,30 +237,30 @@
   1.106      bfOffBits   = SDL_ReadLE32(src);
   1.107  
   1.108      /* Read the Win32 BITMAPINFOHEADER */
   1.109 -    biSize      = SDL_ReadLE32(src);
   1.110 -    if ( biSize == 12 ) {   /* really old BITMAPCOREHEADER */
   1.111 -        biWidth     = (Uint32)SDL_ReadLE16(src);
   1.112 -        biHeight    = (Uint32)SDL_ReadLE16(src);
   1.113 -        biPlanes    = SDL_ReadLE16(src);
   1.114 -        biBitCount  = SDL_ReadLE16(src);
   1.115 -        biCompression   = BI_RGB;
   1.116 +    biSize = SDL_ReadLE32(src);
   1.117 +    if (biSize == 12) {   /* really old BITMAPCOREHEADER */
   1.118 +        biWidth = (Uint32) SDL_ReadLE16(src);
   1.119 +        biHeight = (Uint32) SDL_ReadLE16(src);
   1.120 +        biPlanes = SDL_ReadLE16(src);
   1.121 +        biBitCount = SDL_ReadLE16(src);
   1.122 +        biCompression = BI_RGB;
   1.123          biSizeImage = 0;
   1.124          biXPelsPerMeter = 0;
   1.125          biYPelsPerMeter = 0;
   1.126 -        biClrUsed   = 0;
   1.127 -        biClrImportant  = 0;
   1.128 +        biClrUsed = 0;
   1.129 +        biClrImportant = 0;
   1.130      } else if (biSize >= 40) {  /* some version of BITMAPINFOHEADER */
   1.131          Uint32 headerSize;
   1.132 -        biWidth     = SDL_ReadLE32(src);
   1.133 -        biHeight    = SDL_ReadLE32(src);
   1.134 -        biPlanes    = SDL_ReadLE16(src);
   1.135 -        biBitCount  = SDL_ReadLE16(src);
   1.136 -        biCompression   = SDL_ReadLE32(src);
   1.137 +        biWidth = SDL_ReadLE32(src);
   1.138 +        biHeight = SDL_ReadLE32(src);
   1.139 +        biPlanes = SDL_ReadLE16(src);
   1.140 +        biBitCount = SDL_ReadLE16(src);
   1.141 +        biCompression = SDL_ReadLE32(src);
   1.142          biSizeImage = SDL_ReadLE32(src);
   1.143          biXPelsPerMeter = SDL_ReadLE32(src);
   1.144          biYPelsPerMeter = SDL_ReadLE32(src);
   1.145 -        biClrUsed   = SDL_ReadLE32(src);
   1.146 -        biClrImportant  = SDL_ReadLE32(src);
   1.147 +        biClrUsed = SDL_ReadLE32(src);
   1.148 +        biClrImportant = SDL_ReadLE32(src);
   1.149  
   1.150          /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
   1.151          if (biSize != 64) {
   1.152 @@ -326,81 +326,82 @@
   1.153  
   1.154      /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
   1.155      switch (biBitCount) {
   1.156 -        case 1:
   1.157 -        case 4:
   1.158 -            ExpandBMP = biBitCount;
   1.159 -            biBitCount = 8;
   1.160 -            break;
   1.161 -        case 2:
   1.162 -        case 3:
   1.163 -        case 5:
   1.164 -        case 6:
   1.165 -        case 7:
   1.166 -            SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
   1.167 -            was_error = SDL_TRUE;
   1.168 -            goto done;
   1.169 -        default:
   1.170 -            ExpandBMP = 0;
   1.171 -            break;
   1.172 +    case 1:
   1.173 +    case 4:
   1.174 +        ExpandBMP = biBitCount;
   1.175 +        biBitCount = 8;
   1.176 +        break;
   1.177 +    case 2:
   1.178 +    case 3:
   1.179 +    case 5:
   1.180 +    case 6:
   1.181 +    case 7:
   1.182 +        SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
   1.183 +        was_error = SDL_TRUE;
   1.184 +        goto done;
   1.185 +    default:
   1.186 +        ExpandBMP = 0;
   1.187 +        break;
   1.188      }
   1.189  
   1.190      /* RLE4 and RLE8 BMP compression is supported */
   1.191      switch (biCompression) {
   1.192 -        case BI_RGB:
   1.193 -            /* If there are no masks, use the defaults */
   1.194 -            SDL_assert(!haveRGBMasks);
   1.195 -            SDL_assert(!haveAlphaMask);
   1.196 -            /* Default values for the BMP format */
   1.197 -            switch (biBitCount) {
   1.198 -            case 15:
   1.199 -            case 16:
   1.200 -                Rmask = 0x7C00;
   1.201 -                Gmask = 0x03E0;
   1.202 -                Bmask = 0x001F;
   1.203 -                break;
   1.204 -            case 24:
   1.205 +    case BI_RGB:
   1.206 +        /* If there are no masks, use the defaults */
   1.207 +        SDL_assert(!haveRGBMasks);
   1.208 +        SDL_assert(!haveAlphaMask);
   1.209 +        /* Default values for the BMP format */
   1.210 +        switch (biBitCount) {
   1.211 +        case 15:
   1.212 +        case 16:
   1.213 +            Rmask = 0x7C00;
   1.214 +            Gmask = 0x03E0;
   1.215 +            Bmask = 0x001F;
   1.216 +            break;
   1.217 +        case 24:
   1.218  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.219 -                Rmask = 0x000000FF;
   1.220 -                Gmask = 0x0000FF00;
   1.221 -                Bmask = 0x00FF0000;
   1.222 +            Rmask = 0x000000FF;
   1.223 +            Gmask = 0x0000FF00;
   1.224 +            Bmask = 0x00FF0000;
   1.225  #else
   1.226 -                Rmask = 0x00FF0000;
   1.227 -                Gmask = 0x0000FF00;
   1.228 -                Bmask = 0x000000FF;
   1.229 +            Rmask = 0x00FF0000;
   1.230 +            Gmask = 0x0000FF00;
   1.231 +            Bmask = 0x000000FF;
   1.232  #endif
   1.233 -                break;
   1.234 -            case 32:
   1.235 -                /* We don't know if this has alpha channel or not */
   1.236 -                correctAlpha = SDL_TRUE;
   1.237 -                Amask = 0xFF000000;
   1.238 -                Rmask = 0x00FF0000;
   1.239 -                Gmask = 0x0000FF00;
   1.240 -                Bmask = 0x000000FF;
   1.241 -                break;
   1.242 -            default:
   1.243 -                break;
   1.244 -            }
   1.245              break;
   1.246 -
   1.247 -        case BI_BITFIELDS:
   1.248 -            break;  /* we handled this in the info header. */
   1.249 -
   1.250 +        case 32:
   1.251 +            /* We don't know if this has alpha channel or not */
   1.252 +            correctAlpha = SDL_TRUE;
   1.253 +            Amask = 0xFF000000;
   1.254 +            Rmask = 0x00FF0000;
   1.255 +            Gmask = 0x0000FF00;
   1.256 +            Bmask = 0x000000FF;
   1.257 +            break;
   1.258          default:
   1.259              break;
   1.260 +        }
   1.261 +        break;
   1.262 +
   1.263 +    case BI_BITFIELDS:
   1.264 +        break;  /* we handled this in the info header. */
   1.265 +
   1.266 +    default:
   1.267 +        break;
   1.268      }
   1.269  
   1.270      /* Create a compatible surface, note that the colors are RGB ordered */
   1.271 -    surface = SDL_CreateRGBSurface(0,
   1.272 -            biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, Amask);
   1.273 -    if ( surface == NULL ) {
   1.274 +    surface =
   1.275 +        SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
   1.276 +                             Bmask, Amask);
   1.277 +    if (surface == NULL) {
   1.278          was_error = SDL_TRUE;
   1.279          goto done;
   1.280      }
   1.281  
   1.282      /* Load the palette, if any */
   1.283      palette = (surface->format)->palette;
   1.284 -    if ( palette ) {
   1.285 -        if ( SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0 ) {
   1.286 +    if (palette) {
   1.287 +        if (SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0) {
   1.288              SDL_Error(SDL_EFSEEK);
   1.289              was_error = SDL_TRUE;
   1.290              goto done;
   1.291 @@ -410,18 +411,18 @@
   1.292          | guich: always use 1<<bpp b/c some bitmaps can bring wrong information
   1.293          | for colorsUsed
   1.294          */
   1.295 -        /* if ( biClrUsed == 0 ) {  */
   1.296 +        /* if (biClrUsed == 0) {  */
   1.297          biClrUsed = 1 << biBitCount;
   1.298          /* } */
   1.299 -        if ( biSize == 12 ) {
   1.300 -            for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.301 +        if (biSize == 12) {
   1.302 +            for (i = 0; i < (int) biClrUsed; ++i) {
   1.303                  SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.304                  SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.305                  SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.306                  palette->colors[i].a = SDL_ALPHA_OPAQUE;
   1.307              }
   1.308          } else {
   1.309 -            for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.310 +            for (i = 0; i < (int) biClrUsed; ++i) {
   1.311                  SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.312                  SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.313                  SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.314 @@ -438,7 +439,7 @@
   1.315      }
   1.316  
   1.317      /* Read the surface pixels.  Note that the bmp image is upside down */
   1.318 -    if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
   1.319 +    if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
   1.320          SDL_Error(SDL_EFSEEK);
   1.321          was_error = SDL_TRUE;
   1.322          goto done;
   1.323 @@ -451,33 +452,32 @@
   1.324      top = (Uint8 *)surface->pixels;
   1.325      end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
   1.326      switch (ExpandBMP) {
   1.327 -        case 1:
   1.328 -            bmpPitch = (biWidth + 7) >> 3;
   1.329 -            pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.330 -            break;
   1.331 -        case 4:
   1.332 -            bmpPitch = (biWidth + 1) >> 1;
   1.333 -            pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.334 -            break;
   1.335 -        default:
   1.336 -            pad  = ((surface->pitch%4) ?
   1.337 -                    (4-(surface->pitch%4)) : 0);
   1.338 -            break;
   1.339 +    case 1:
   1.340 +        bmpPitch = (biWidth + 7) >> 3;
   1.341 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.342 +        break;
   1.343 +    case 4:
   1.344 +        bmpPitch = (biWidth + 1) >> 1;
   1.345 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.346 +        break;
   1.347 +    default:
   1.348 +        pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
   1.349 +        break;
   1.350      }
   1.351 -    if ( topDown ) {
   1.352 +    if (topDown) {
   1.353          bits = top;
   1.354      } else {
   1.355          bits = end - surface->pitch;
   1.356      }
   1.357 -    while ( bits >= top && bits < end ) {
   1.358 +    while (bits >= top && bits < end) {
   1.359          switch (ExpandBMP) {
   1.360 -            case 1:
   1.361 -            case 4: {
   1.362 +        case 1:
   1.363 +        case 4:{
   1.364                  Uint8 pixel = 0;
   1.365 -                int   shift = (8-ExpandBMP);
   1.366 -                for ( i=0; i<surface->w; ++i ) {
   1.367 -                    if ( i%(8/ExpandBMP) == 0 ) {
   1.368 -                        if ( !SDL_RWread(src, &pixel, 1, 1) ) {
   1.369 +                int shift = (8 - ExpandBMP);
   1.370 +                for (i = 0; i < surface->w; ++i) {
   1.371 +                    if (i % (8 / ExpandBMP) == 0) {
   1.372 +                        if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.373                              SDL_SetError("Error reading from BMP");
   1.374                              was_error = SDL_TRUE;
   1.375                              goto done;
   1.376 @@ -494,51 +494,51 @@
   1.377              }
   1.378              break;
   1.379  
   1.380 -            default:
   1.381 -                if ( SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch ) {
   1.382 -                    SDL_Error(SDL_EFREAD);
   1.383 -                    was_error = SDL_TRUE;
   1.384 -                    goto done;
   1.385 -                }
   1.386 -                if (biBitCount == 8 && palette && biClrUsed < (1 << biBitCount)) {
   1.387 -                    for (i = 0; i < surface->w; ++i) {
   1.388 -                        if (bits[i] >= biClrUsed) {
   1.389 -                            SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.390 -                            was_error = SDL_TRUE;
   1.391 -                            goto done;
   1.392 -                        }
   1.393 +        default:
   1.394 +            if (SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch) {
   1.395 +                SDL_Error(SDL_EFREAD);
   1.396 +                was_error = SDL_TRUE;
   1.397 +                goto done;
   1.398 +            }
   1.399 +            if (biBitCount == 8 && palette && biClrUsed < (1u << biBitCount)) {
   1.400 +                for (i = 0; i < surface->w; ++i) {
   1.401 +                    if (bits[i] >= biClrUsed) {
   1.402 +                        SDL_SetError("A BMP image contains a pixel with a color out of the palette");
   1.403 +                        was_error = SDL_TRUE;
   1.404 +                        goto done;
   1.405                      }
   1.406                  }
   1.407 +            }
   1.408  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.409 -                /* Byte-swap the pixels if needed. Note that the 24bpp
   1.410 -                   case has already been taken care of above. */
   1.411 -                switch(biBitCount) {
   1.412 -                    case 15:
   1.413 -                    case 16: {
   1.414 -                        Uint16 *pix = (Uint16 *)bits;
   1.415 -                        for(i = 0; i < surface->w; i++)
   1.416 -                            pix[i] = SDL_Swap16(pix[i]);
   1.417 -                        break;
   1.418 -                    }
   1.419 +            /* Byte-swap the pixels if needed. Note that the 24bpp
   1.420 +               case has already been taken care of above. */
   1.421 +            switch (biBitCount) {
   1.422 +            case 15:
   1.423 +            case 16:{
   1.424 +                    Uint16 *pix = (Uint16 *) bits;
   1.425 +                    for (i = 0; i < surface->w; i++)
   1.426 +                        pix[i] = SDL_Swap16(pix[i]);
   1.427 +                    break;
   1.428 +                }
   1.429  
   1.430 -                    case 32: {
   1.431 -                        Uint32 *pix = (Uint32 *)bits;
   1.432 -                        for(i = 0; i < surface->w; i++)
   1.433 -                            pix[i] = SDL_Swap32(pix[i]);
   1.434 -                        break;
   1.435 -                    }
   1.436 +            case 32:{
   1.437 +                    Uint32 *pix = (Uint32 *) bits;
   1.438 +                    for (i = 0; i < surface->w; i++)
   1.439 +                        pix[i] = SDL_Swap32(pix[i]);
   1.440 +                    break;
   1.441                  }
   1.442 +            }
   1.443  #endif
   1.444 -                break;
   1.445 +            break;
   1.446          }
   1.447          /* Skip padding bytes, ugh */
   1.448 -        if ( pad ) {
   1.449 +        if (pad) {
   1.450              Uint8 padbyte;
   1.451 -            for ( i=0; i<pad; ++i ) {
   1.452 +            for (i = 0; i < pad; ++i) {
   1.453                  SDL_RWread(src, &padbyte, 1, 1);
   1.454              }
   1.455          }
   1.456 -        if ( topDown ) {
   1.457 +        if (topDown) {
   1.458              bits += surface->pitch;
   1.459          } else {
   1.460              bits -= surface->pitch;
   1.461 @@ -547,20 +547,20 @@
   1.462      if (correctAlpha) {
   1.463          CorrectAlphaChannel(surface);
   1.464      }
   1.465 -done:
   1.466 -    if ( was_error ) {
   1.467 -        if ( src ) {
   1.468 +  done:
   1.469 +    if (was_error) {
   1.470 +        if (src) {
   1.471              SDL_RWseek(src, fp_offset, RW_SEEK_SET);
   1.472          }
   1.473 -        if ( surface ) {
   1.474 +        if (surface) {
   1.475              SDL_FreeSurface(surface);
   1.476          }
   1.477          surface = NULL;
   1.478      }
   1.479 -    if ( freesrc && src ) {
   1.480 +    if (freesrc && src) {
   1.481          SDL_RWclose(src);
   1.482      }
   1.483 -    return(surface);
   1.484 +    return (surface);
   1.485  }
   1.486  
   1.487  int