src/video/SDL_bmp.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
     1.1 --- a/src/video/SDL_bmp.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/video/SDL_bmp.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -45,476 +45,482 @@
     1.4  #endif
     1.5  
     1.6  
     1.7 -SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
     1.8 +SDL_Surface *
     1.9 +SDL_LoadBMP_RW (SDL_RWops * src, int freesrc)
    1.10  {
    1.11 -	int was_error;
    1.12 -	long fp_offset;
    1.13 -	int bmpPitch;
    1.14 -	int i, pad;
    1.15 -	SDL_Surface *surface;
    1.16 -	Uint32 Rmask;
    1.17 -	Uint32 Gmask;
    1.18 -	Uint32 Bmask;
    1.19 -	SDL_Palette *palette;
    1.20 -	Uint8 *bits;
    1.21 -	int ExpandBMP;
    1.22 +    int was_error;
    1.23 +    long fp_offset;
    1.24 +    int bmpPitch;
    1.25 +    int i, pad;
    1.26 +    SDL_Surface *surface;
    1.27 +    Uint32 Rmask;
    1.28 +    Uint32 Gmask;
    1.29 +    Uint32 Bmask;
    1.30 +    SDL_Palette *palette;
    1.31 +    Uint8 *bits;
    1.32 +    int ExpandBMP;
    1.33  
    1.34 -	/* The Win32 BMP file header (14 bytes) */
    1.35 -	char   magic[2];
    1.36 -	Uint32 bfSize;
    1.37 -	Uint16 bfReserved1;
    1.38 -	Uint16 bfReserved2;
    1.39 -	Uint32 bfOffBits;
    1.40 +    /* The Win32 BMP file header (14 bytes) */
    1.41 +    char magic[2];
    1.42 +    Uint32 bfSize;
    1.43 +    Uint16 bfReserved1;
    1.44 +    Uint16 bfReserved2;
    1.45 +    Uint32 bfOffBits;
    1.46  
    1.47 -	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
    1.48 -	Uint32 biSize;
    1.49 -	Sint32 biWidth;
    1.50 -	Sint32 biHeight;
    1.51 -	Uint16 biPlanes;
    1.52 -	Uint16 biBitCount;
    1.53 -	Uint32 biCompression;
    1.54 -	Uint32 biSizeImage;
    1.55 -	Sint32 biXPelsPerMeter;
    1.56 -	Sint32 biYPelsPerMeter;
    1.57 -	Uint32 biClrUsed;
    1.58 -	Uint32 biClrImportant;
    1.59 +    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
    1.60 +    Uint32 biSize;
    1.61 +    Sint32 biWidth;
    1.62 +    Sint32 biHeight;
    1.63 +    Uint16 biPlanes;
    1.64 +    Uint16 biBitCount;
    1.65 +    Uint32 biCompression;
    1.66 +    Uint32 biSizeImage;
    1.67 +    Sint32 biXPelsPerMeter;
    1.68 +    Sint32 biYPelsPerMeter;
    1.69 +    Uint32 biClrUsed;
    1.70 +    Uint32 biClrImportant;
    1.71  
    1.72 -	/* Make sure we are passed a valid data source */
    1.73 -	surface = NULL;
    1.74 -	was_error = 0;
    1.75 -	if ( src == NULL ) {
    1.76 -		was_error = 1;
    1.77 -		goto done;
    1.78 -	}
    1.79 +    /* Make sure we are passed a valid data source */
    1.80 +    surface = NULL;
    1.81 +    was_error = 0;
    1.82 +    if (src == NULL) {
    1.83 +        was_error = 1;
    1.84 +        goto done;
    1.85 +    }
    1.86  
    1.87 -	/* Read in the BMP file header */
    1.88 -	fp_offset = SDL_RWtell(src);
    1.89 -	SDL_ClearError();
    1.90 -	if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
    1.91 -		SDL_Error(SDL_EFREAD);
    1.92 -		was_error = 1;
    1.93 -		goto done;
    1.94 -	}
    1.95 -	if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
    1.96 -		SDL_SetError("File is not a Windows BMP file");
    1.97 -		was_error = 1;
    1.98 -		goto done;
    1.99 -	}
   1.100 -	bfSize		= SDL_ReadLE32(src);
   1.101 -	bfReserved1	= SDL_ReadLE16(src);
   1.102 -	bfReserved2	= SDL_ReadLE16(src);
   1.103 -	bfOffBits	= SDL_ReadLE32(src);
   1.104 +    /* Read in the BMP file header */
   1.105 +    fp_offset = SDL_RWtell (src);
   1.106 +    SDL_ClearError ();
   1.107 +    if (SDL_RWread (src, magic, 1, 2) != 2) {
   1.108 +        SDL_Error (SDL_EFREAD);
   1.109 +        was_error = 1;
   1.110 +        goto done;
   1.111 +    }
   1.112 +    if (SDL_strncmp (magic, "BM", 2) != 0) {
   1.113 +        SDL_SetError ("File is not a Windows BMP file");
   1.114 +        was_error = 1;
   1.115 +        goto done;
   1.116 +    }
   1.117 +    bfSize = SDL_ReadLE32 (src);
   1.118 +    bfReserved1 = SDL_ReadLE16 (src);
   1.119 +    bfReserved2 = SDL_ReadLE16 (src);
   1.120 +    bfOffBits = SDL_ReadLE32 (src);
   1.121  
   1.122 -	/* Read the Win32 BITMAPINFOHEADER */
   1.123 -	biSize		= SDL_ReadLE32(src);
   1.124 -	if ( biSize == 12 ) {
   1.125 -		biWidth		= (Uint32)SDL_ReadLE16(src);
   1.126 -		biHeight	= (Uint32)SDL_ReadLE16(src);
   1.127 -		biPlanes	= SDL_ReadLE16(src);
   1.128 -		biBitCount	= SDL_ReadLE16(src);
   1.129 -		biCompression	= BI_RGB;
   1.130 -		biSizeImage	= 0;
   1.131 -		biXPelsPerMeter	= 0;
   1.132 -		biYPelsPerMeter	= 0;
   1.133 -		biClrUsed	= 0;
   1.134 -		biClrImportant	= 0;
   1.135 -	} else {
   1.136 -		biWidth		= SDL_ReadLE32(src);
   1.137 -		biHeight	= SDL_ReadLE32(src);
   1.138 -		biPlanes	= SDL_ReadLE16(src);
   1.139 -		biBitCount	= SDL_ReadLE16(src);
   1.140 -		biCompression	= SDL_ReadLE32(src);
   1.141 -		biSizeImage	= SDL_ReadLE32(src);
   1.142 -		biXPelsPerMeter	= SDL_ReadLE32(src);
   1.143 -		biYPelsPerMeter	= SDL_ReadLE32(src);
   1.144 -		biClrUsed	= SDL_ReadLE32(src);
   1.145 -		biClrImportant	= SDL_ReadLE32(src);
   1.146 -	}
   1.147 +    /* Read the Win32 BITMAPINFOHEADER */
   1.148 +    biSize = SDL_ReadLE32 (src);
   1.149 +    if (biSize == 12) {
   1.150 +        biWidth = (Uint32) SDL_ReadLE16 (src);
   1.151 +        biHeight = (Uint32) SDL_ReadLE16 (src);
   1.152 +        biPlanes = SDL_ReadLE16 (src);
   1.153 +        biBitCount = SDL_ReadLE16 (src);
   1.154 +        biCompression = BI_RGB;
   1.155 +        biSizeImage = 0;
   1.156 +        biXPelsPerMeter = 0;
   1.157 +        biYPelsPerMeter = 0;
   1.158 +        biClrUsed = 0;
   1.159 +        biClrImportant = 0;
   1.160 +    } else {
   1.161 +        biWidth = SDL_ReadLE32 (src);
   1.162 +        biHeight = SDL_ReadLE32 (src);
   1.163 +        biPlanes = SDL_ReadLE16 (src);
   1.164 +        biBitCount = SDL_ReadLE16 (src);
   1.165 +        biCompression = SDL_ReadLE32 (src);
   1.166 +        biSizeImage = SDL_ReadLE32 (src);
   1.167 +        biXPelsPerMeter = SDL_ReadLE32 (src);
   1.168 +        biYPelsPerMeter = SDL_ReadLE32 (src);
   1.169 +        biClrUsed = SDL_ReadLE32 (src);
   1.170 +        biClrImportant = SDL_ReadLE32 (src);
   1.171 +    }
   1.172  
   1.173 -	/* Check for read error */
   1.174 -	if ( SDL_strcmp(SDL_GetError(), "") != 0 ) {
   1.175 -		was_error = 1;
   1.176 -		goto done;
   1.177 -	}
   1.178 +    /* Check for read error */
   1.179 +    if (SDL_strcmp (SDL_GetError (), "") != 0) {
   1.180 +        was_error = 1;
   1.181 +        goto done;
   1.182 +    }
   1.183  
   1.184 -	/* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
   1.185 -	switch (biBitCount) {
   1.186 -		case 1:
   1.187 -		case 4:
   1.188 -			ExpandBMP = biBitCount;
   1.189 -			biBitCount = 8;
   1.190 -			break;
   1.191 -		default:
   1.192 -			ExpandBMP = 0;
   1.193 -			break;
   1.194 -	}
   1.195 +    /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
   1.196 +    switch (biBitCount) {
   1.197 +    case 1:
   1.198 +    case 4:
   1.199 +        ExpandBMP = biBitCount;
   1.200 +        biBitCount = 8;
   1.201 +        break;
   1.202 +    default:
   1.203 +        ExpandBMP = 0;
   1.204 +        break;
   1.205 +    }
   1.206  
   1.207 -	/* We don't support any BMP compression right now */
   1.208 -	Rmask = Gmask = Bmask = 0;
   1.209 -	switch (biCompression) {
   1.210 -		case BI_RGB:
   1.211 -			/* If there are no masks, use the defaults */
   1.212 -			if ( bfOffBits == (14+biSize) ) {
   1.213 -				/* Default values for the BMP format */
   1.214 -				switch (biBitCount) {
   1.215 -					case 15:
   1.216 -					case 16:
   1.217 -						Rmask = 0x7C00;
   1.218 -						Gmask = 0x03E0;
   1.219 -						Bmask = 0x001F;
   1.220 -						break;
   1.221 -					case 24:
   1.222 +    /* We don't support any BMP compression right now */
   1.223 +    Rmask = Gmask = Bmask = 0;
   1.224 +    switch (biCompression) {
   1.225 +    case BI_RGB:
   1.226 +        /* If there are no masks, use the defaults */
   1.227 +        if (bfOffBits == (14 + biSize)) {
   1.228 +            /* Default values for the BMP format */
   1.229 +            switch (biBitCount) {
   1.230 +            case 15:
   1.231 +            case 16:
   1.232 +                Rmask = 0x7C00;
   1.233 +                Gmask = 0x03E0;
   1.234 +                Bmask = 0x001F;
   1.235 +                break;
   1.236 +            case 24:
   1.237  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.238 -					        Rmask = 0x000000FF;
   1.239 -					        Gmask = 0x0000FF00;
   1.240 -					        Bmask = 0x00FF0000;
   1.241 -						break;
   1.242 +                Rmask = 0x000000FF;
   1.243 +                Gmask = 0x0000FF00;
   1.244 +                Bmask = 0x00FF0000;
   1.245 +                break;
   1.246  #endif
   1.247 -					case 32:
   1.248 -						Rmask = 0x00FF0000;
   1.249 -						Gmask = 0x0000FF00;
   1.250 -						Bmask = 0x000000FF;
   1.251 -						break;
   1.252 -					default:
   1.253 -						break;
   1.254 -				}
   1.255 -				break;
   1.256 -			}
   1.257 -			/* Fall through -- read the RGB masks */
   1.258 +            case 32:
   1.259 +                Rmask = 0x00FF0000;
   1.260 +                Gmask = 0x0000FF00;
   1.261 +                Bmask = 0x000000FF;
   1.262 +                break;
   1.263 +            default:
   1.264 +                break;
   1.265 +            }
   1.266 +            break;
   1.267 +        }
   1.268 +        /* Fall through -- read the RGB masks */
   1.269  
   1.270 -		case BI_BITFIELDS:
   1.271 -			switch (biBitCount) {
   1.272 -				case 15:
   1.273 -				case 16:
   1.274 -				case 32:
   1.275 -					Rmask = SDL_ReadLE32(src);
   1.276 -					Gmask = SDL_ReadLE32(src);
   1.277 -					Bmask = SDL_ReadLE32(src);
   1.278 -					break;
   1.279 -				default:
   1.280 -					break;
   1.281 -			}
   1.282 -			break;
   1.283 -		default:
   1.284 -			SDL_SetError("Compressed BMP files not supported");
   1.285 -			was_error = 1;
   1.286 -			goto done;
   1.287 -	}
   1.288 +    case BI_BITFIELDS:
   1.289 +        switch (biBitCount) {
   1.290 +        case 15:
   1.291 +        case 16:
   1.292 +        case 32:
   1.293 +            Rmask = SDL_ReadLE32 (src);
   1.294 +            Gmask = SDL_ReadLE32 (src);
   1.295 +            Bmask = SDL_ReadLE32 (src);
   1.296 +            break;
   1.297 +        default:
   1.298 +            break;
   1.299 +        }
   1.300 +        break;
   1.301 +    default:
   1.302 +        SDL_SetError ("Compressed BMP files not supported");
   1.303 +        was_error = 1;
   1.304 +        goto done;
   1.305 +    }
   1.306  
   1.307 -	/* Create a compatible surface, note that the colors are RGB ordered */
   1.308 -	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
   1.309 -			biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
   1.310 -	if ( surface == NULL ) {
   1.311 -		was_error = 1;
   1.312 -		goto done;
   1.313 -	}
   1.314 +    /* Create a compatible surface, note that the colors are RGB ordered */
   1.315 +    surface = SDL_CreateRGBSurface (SDL_SWSURFACE,
   1.316 +                                    biWidth, biHeight, biBitCount, Rmask,
   1.317 +                                    Gmask, Bmask, 0);
   1.318 +    if (surface == NULL) {
   1.319 +        was_error = 1;
   1.320 +        goto done;
   1.321 +    }
   1.322  
   1.323 -	/* Load the palette, if any */
   1.324 -	palette = (surface->format)->palette;
   1.325 -	if ( palette ) {
   1.326 -		if ( biClrUsed == 0 ) {
   1.327 -			biClrUsed = 1 << biBitCount;
   1.328 -		}
   1.329 -		if ( biSize == 12 ) {
   1.330 -			for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.331 -				SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.332 -				SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.333 -				SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.334 -				palette->colors[i].unused = 0;
   1.335 -			}	
   1.336 -		} else {
   1.337 -			for ( i = 0; i < (int)biClrUsed; ++i ) {
   1.338 -				SDL_RWread(src, &palette->colors[i].b, 1, 1);
   1.339 -				SDL_RWread(src, &palette->colors[i].g, 1, 1);
   1.340 -				SDL_RWread(src, &palette->colors[i].r, 1, 1);
   1.341 -				SDL_RWread(src, &palette->colors[i].unused, 1, 1);
   1.342 -			}	
   1.343 -		}
   1.344 -		palette->ncolors = biClrUsed;
   1.345 -	}
   1.346 +    /* Load the palette, if any */
   1.347 +    palette = (surface->format)->palette;
   1.348 +    if (palette) {
   1.349 +        if (biClrUsed == 0) {
   1.350 +            biClrUsed = 1 << biBitCount;
   1.351 +        }
   1.352 +        if (biSize == 12) {
   1.353 +            for (i = 0; i < (int) biClrUsed; ++i) {
   1.354 +                SDL_RWread (src, &palette->colors[i].b, 1, 1);
   1.355 +                SDL_RWread (src, &palette->colors[i].g, 1, 1);
   1.356 +                SDL_RWread (src, &palette->colors[i].r, 1, 1);
   1.357 +                palette->colors[i].unused = 0;
   1.358 +            }
   1.359 +        } else {
   1.360 +            for (i = 0; i < (int) biClrUsed; ++i) {
   1.361 +                SDL_RWread (src, &palette->colors[i].b, 1, 1);
   1.362 +                SDL_RWread (src, &palette->colors[i].g, 1, 1);
   1.363 +                SDL_RWread (src, &palette->colors[i].r, 1, 1);
   1.364 +                SDL_RWread (src, &palette->colors[i].unused, 1, 1);
   1.365 +            }
   1.366 +        }
   1.367 +        palette->ncolors = biClrUsed;
   1.368 +    }
   1.369  
   1.370 -	/* Read the surface pixels.  Note that the bmp image is upside down */
   1.371 -	if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
   1.372 -		SDL_Error(SDL_EFSEEK);
   1.373 -		was_error = 1;
   1.374 -		goto done;
   1.375 -	}
   1.376 -	bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
   1.377 -	switch (ExpandBMP) {
   1.378 -		case 1:
   1.379 -			bmpPitch = (biWidth + 7) >> 3;
   1.380 -			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.381 -			break;
   1.382 -		case 4:
   1.383 -			bmpPitch = (biWidth + 1) >> 1;
   1.384 -			pad  = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
   1.385 -			break;
   1.386 -		default:
   1.387 -			pad  = ((surface->pitch%4) ?
   1.388 -					(4-(surface->pitch%4)) : 0);
   1.389 -			break;
   1.390 -	}
   1.391 -	while ( bits > (Uint8 *)surface->pixels ) {
   1.392 -		bits -= surface->pitch;
   1.393 -		switch (ExpandBMP) {
   1.394 -			case 1:
   1.395 -			case 4: {
   1.396 -			Uint8 pixel = 0;
   1.397 -			int   shift = (8-ExpandBMP);
   1.398 -			for ( i=0; i<surface->w; ++i ) {
   1.399 -				if ( i%(8/ExpandBMP) == 0 ) {
   1.400 -					if ( !SDL_RWread(src, &pixel, 1, 1) ) {
   1.401 -						SDL_SetError(
   1.402 -					"Error reading from BMP");
   1.403 -						was_error = 1;
   1.404 -						goto done;
   1.405 -					}
   1.406 -				}
   1.407 -				*(bits+i) = (pixel>>shift);
   1.408 -				pixel <<= ExpandBMP;
   1.409 -			} }
   1.410 -			break;
   1.411 +    /* Read the surface pixels.  Note that the bmp image is upside down */
   1.412 +    if (SDL_RWseek (src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
   1.413 +        SDL_Error (SDL_EFSEEK);
   1.414 +        was_error = 1;
   1.415 +        goto done;
   1.416 +    }
   1.417 +    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.418 +    switch (ExpandBMP) {
   1.419 +    case 1:
   1.420 +        bmpPitch = (biWidth + 7) >> 3;
   1.421 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.422 +        break;
   1.423 +    case 4:
   1.424 +        bmpPitch = (biWidth + 1) >> 1;
   1.425 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.426 +        break;
   1.427 +    default:
   1.428 +        pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
   1.429 +        break;
   1.430 +    }
   1.431 +    while (bits > (Uint8 *) surface->pixels) {
   1.432 +        bits -= surface->pitch;
   1.433 +        switch (ExpandBMP) {
   1.434 +        case 1:
   1.435 +        case 4:
   1.436 +            {
   1.437 +                Uint8 pixel = 0;
   1.438 +                int shift = (8 - ExpandBMP);
   1.439 +                for (i = 0; i < surface->w; ++i) {
   1.440 +                    if (i % (8 / ExpandBMP) == 0) {
   1.441 +                        if (!SDL_RWread (src, &pixel, 1, 1)) {
   1.442 +                            SDL_SetError ("Error reading from BMP");
   1.443 +                            was_error = 1;
   1.444 +                            goto done;
   1.445 +                        }
   1.446 +                    }
   1.447 +                    *(bits + i) = (pixel >> shift);
   1.448 +                    pixel <<= ExpandBMP;
   1.449 +                }
   1.450 +            }
   1.451 +            break;
   1.452  
   1.453 -			default:
   1.454 -			if ( SDL_RWread(src, bits, 1, surface->pitch)
   1.455 -							 != surface->pitch ) {
   1.456 -				SDL_Error(SDL_EFREAD);
   1.457 -				was_error = 1;
   1.458 -				goto done;
   1.459 -			}
   1.460 +        default:
   1.461 +            if (SDL_RWread (src, bits, 1, surface->pitch)
   1.462 +                != surface->pitch) {
   1.463 +                SDL_Error (SDL_EFREAD);
   1.464 +                was_error = 1;
   1.465 +                goto done;
   1.466 +            }
   1.467  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   1.468 -			/* Byte-swap the pixels if needed. Note that the 24bpp
   1.469 -			   case has already been taken care of above. */
   1.470 -			switch(biBitCount) {
   1.471 -				case 15:
   1.472 -				case 16: {
   1.473 -				        Uint16 *pix = (Uint16 *)bits;
   1.474 -					for(i = 0; i < surface->w; i++)
   1.475 -					        pix[i] = SDL_Swap16(pix[i]);
   1.476 -					break;
   1.477 -				}
   1.478 +            /* Byte-swap the pixels if needed. Note that the 24bpp
   1.479 +               case has already been taken care of above. */
   1.480 +            switch (biBitCount) {
   1.481 +            case 15:
   1.482 +            case 16:
   1.483 +                {
   1.484 +                    Uint16 *pix = (Uint16 *) bits;
   1.485 +                    for (i = 0; i < surface->w; i++)
   1.486 +                        pix[i] = SDL_Swap16 (pix[i]);
   1.487 +                    break;
   1.488 +                }
   1.489  
   1.490 -				case 32: {
   1.491 -				        Uint32 *pix = (Uint32 *)bits;
   1.492 -					for(i = 0; i < surface->w; i++)
   1.493 -					        pix[i] = SDL_Swap32(pix[i]);
   1.494 -					break;
   1.495 -				}
   1.496 -			}
   1.497 +            case 32:
   1.498 +                {
   1.499 +                    Uint32 *pix = (Uint32 *) bits;
   1.500 +                    for (i = 0; i < surface->w; i++)
   1.501 +                        pix[i] = SDL_Swap32 (pix[i]);
   1.502 +                    break;
   1.503 +                }
   1.504 +            }
   1.505  #endif
   1.506 -			break;
   1.507 -		}
   1.508 -		/* Skip padding bytes, ugh */
   1.509 -		if ( pad ) {
   1.510 -			Uint8 padbyte;
   1.511 -			for ( i=0; i<pad; ++i ) {
   1.512 -				SDL_RWread(src, &padbyte, 1, 1);
   1.513 -			}
   1.514 -		}
   1.515 -	}
   1.516 -done:
   1.517 -	if ( was_error ) {
   1.518 -		if ( src ) {
   1.519 -			SDL_RWseek(src, fp_offset, RW_SEEK_SET);
   1.520 -		}
   1.521 -		if ( surface ) {
   1.522 -			SDL_FreeSurface(surface);
   1.523 -		}
   1.524 -		surface = NULL;
   1.525 -	}
   1.526 -	if ( freesrc && src ) {
   1.527 -		SDL_RWclose(src);
   1.528 -	}
   1.529 -	return(surface);
   1.530 +            break;
   1.531 +        }
   1.532 +        /* Skip padding bytes, ugh */
   1.533 +        if (pad) {
   1.534 +            Uint8 padbyte;
   1.535 +            for (i = 0; i < pad; ++i) {
   1.536 +                SDL_RWread (src, &padbyte, 1, 1);
   1.537 +            }
   1.538 +        }
   1.539 +    }
   1.540 +  done:
   1.541 +    if (was_error) {
   1.542 +        if (src) {
   1.543 +            SDL_RWseek (src, fp_offset, RW_SEEK_SET);
   1.544 +        }
   1.545 +        if (surface) {
   1.546 +            SDL_FreeSurface (surface);
   1.547 +        }
   1.548 +        surface = NULL;
   1.549 +    }
   1.550 +    if (freesrc && src) {
   1.551 +        SDL_RWclose (src);
   1.552 +    }
   1.553 +    return (surface);
   1.554  }
   1.555  
   1.556 -int SDL_SaveBMP_RW (SDL_Surface *saveme, SDL_RWops *dst, int freedst)
   1.557 +int
   1.558 +SDL_SaveBMP_RW (SDL_Surface * saveme, SDL_RWops * dst, int freedst)
   1.559  {
   1.560 -	long fp_offset;
   1.561 -	int i, pad;
   1.562 -	SDL_Surface *surface;
   1.563 -	Uint8 *bits;
   1.564 +    long fp_offset;
   1.565 +    int i, pad;
   1.566 +    SDL_Surface *surface;
   1.567 +    Uint8 *bits;
   1.568  
   1.569 -	/* The Win32 BMP file header (14 bytes) */
   1.570 -	char   magic[2] = { 'B', 'M' };
   1.571 -	Uint32 bfSize;
   1.572 -	Uint16 bfReserved1;
   1.573 -	Uint16 bfReserved2;
   1.574 -	Uint32 bfOffBits;
   1.575 +    /* The Win32 BMP file header (14 bytes) */
   1.576 +    char magic[2] = { 'B', 'M' };
   1.577 +    Uint32 bfSize;
   1.578 +    Uint16 bfReserved1;
   1.579 +    Uint16 bfReserved2;
   1.580 +    Uint32 bfOffBits;
   1.581  
   1.582 -	/* The Win32 BITMAPINFOHEADER struct (40 bytes) */
   1.583 -	Uint32 biSize;
   1.584 -	Sint32 biWidth;
   1.585 -	Sint32 biHeight;
   1.586 -	Uint16 biPlanes;
   1.587 -	Uint16 biBitCount;
   1.588 -	Uint32 biCompression;
   1.589 -	Uint32 biSizeImage;
   1.590 -	Sint32 biXPelsPerMeter;
   1.591 -	Sint32 biYPelsPerMeter;
   1.592 -	Uint32 biClrUsed;
   1.593 -	Uint32 biClrImportant;
   1.594 +    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
   1.595 +    Uint32 biSize;
   1.596 +    Sint32 biWidth;
   1.597 +    Sint32 biHeight;
   1.598 +    Uint16 biPlanes;
   1.599 +    Uint16 biBitCount;
   1.600 +    Uint32 biCompression;
   1.601 +    Uint32 biSizeImage;
   1.602 +    Sint32 biXPelsPerMeter;
   1.603 +    Sint32 biYPelsPerMeter;
   1.604 +    Uint32 biClrUsed;
   1.605 +    Uint32 biClrImportant;
   1.606  
   1.607 -	/* Make sure we have somewhere to save */
   1.608 -	surface = NULL;
   1.609 -	if ( dst ) {
   1.610 -		if ( saveme->format->palette ) {
   1.611 -			if ( saveme->format->BitsPerPixel == 8 ) {
   1.612 -				surface = saveme;
   1.613 -			} else {
   1.614 -				SDL_SetError("%d bpp BMP files not supported",
   1.615 -						saveme->format->BitsPerPixel);
   1.616 -			}
   1.617 -		}
   1.618 -		else if ( (saveme->format->BitsPerPixel == 24) &&
   1.619 +    /* Make sure we have somewhere to save */
   1.620 +    surface = NULL;
   1.621 +    if (dst) {
   1.622 +        if (saveme->format->palette) {
   1.623 +            if (saveme->format->BitsPerPixel == 8) {
   1.624 +                surface = saveme;
   1.625 +            } else {
   1.626 +                SDL_SetError ("%d bpp BMP files not supported",
   1.627 +                              saveme->format->BitsPerPixel);
   1.628 +            }
   1.629 +        } else if ((saveme->format->BitsPerPixel == 24) &&
   1.630  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.631 -				(saveme->format->Rmask == 0x00FF0000) &&
   1.632 -				(saveme->format->Gmask == 0x0000FF00) &&
   1.633 -				(saveme->format->Bmask == 0x000000FF)
   1.634 +                   (saveme->format->Rmask == 0x00FF0000) &&
   1.635 +                   (saveme->format->Gmask == 0x0000FF00) &&
   1.636 +                   (saveme->format->Bmask == 0x000000FF)
   1.637  #else
   1.638 -				(saveme->format->Rmask == 0x000000FF) &&
   1.639 -				(saveme->format->Gmask == 0x0000FF00) &&
   1.640 -				(saveme->format->Bmask == 0x00FF0000)
   1.641 +                   (saveme->format->Rmask == 0x000000FF) &&
   1.642 +                   (saveme->format->Gmask == 0x0000FF00) &&
   1.643 +                   (saveme->format->Bmask == 0x00FF0000)
   1.644  #endif
   1.645 -			  ) {
   1.646 -			surface = saveme;
   1.647 -		} else {
   1.648 -			SDL_Rect bounds;
   1.649 +            ) {
   1.650 +            surface = saveme;
   1.651 +        } else {
   1.652 +            SDL_Rect bounds;
   1.653  
   1.654 -			/* Convert to 24 bits per pixel */
   1.655 -			surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
   1.656 -					saveme->w, saveme->h, 24,
   1.657 +            /* Convert to 24 bits per pixel */
   1.658 +            surface = SDL_CreateRGBSurface (SDL_SWSURFACE,
   1.659 +                                            saveme->w, saveme->h, 24,
   1.660  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.661 -					0x00FF0000, 0x0000FF00, 0x000000FF,
   1.662 +                                            0x00FF0000, 0x0000FF00,
   1.663 +                                            0x000000FF,
   1.664  #else
   1.665 -					0x000000FF, 0x0000FF00, 0x00FF0000,
   1.666 +                                            0x000000FF, 0x0000FF00,
   1.667 +                                            0x00FF0000,
   1.668  #endif
   1.669 -					0);
   1.670 -			if ( surface != NULL ) {
   1.671 -				bounds.x = 0;
   1.672 -				bounds.y = 0;
   1.673 -				bounds.w = saveme->w;
   1.674 -				bounds.h = saveme->h;
   1.675 -				if ( SDL_LowerBlit(saveme, &bounds, surface,
   1.676 -							&bounds) < 0 ) {
   1.677 -					SDL_FreeSurface(surface);
   1.678 -					SDL_SetError(
   1.679 -					"Couldn't convert image to 24 bpp");
   1.680 -					surface = NULL;
   1.681 -				}
   1.682 -			}
   1.683 -		}
   1.684 -	}
   1.685 +                                            0);
   1.686 +            if (surface != NULL) {
   1.687 +                bounds.x = 0;
   1.688 +                bounds.y = 0;
   1.689 +                bounds.w = saveme->w;
   1.690 +                bounds.h = saveme->h;
   1.691 +                if (SDL_LowerBlit (saveme, &bounds, surface, &bounds) < 0) {
   1.692 +                    SDL_FreeSurface (surface);
   1.693 +                    SDL_SetError ("Couldn't convert image to 24 bpp");
   1.694 +                    surface = NULL;
   1.695 +                }
   1.696 +            }
   1.697 +        }
   1.698 +    }
   1.699  
   1.700 -	if ( surface && (SDL_LockSurface(surface) == 0) ) {
   1.701 -		const int bw = surface->w*surface->format->BytesPerPixel;
   1.702 +    if (surface && (SDL_LockSurface (surface) == 0)) {
   1.703 +        const int bw = surface->w * surface->format->BytesPerPixel;
   1.704  
   1.705 -		/* Set the BMP file header values */
   1.706 -		bfSize = 0;		 /* We'll write this when we're done */
   1.707 -		bfReserved1 = 0;
   1.708 -		bfReserved2 = 0;
   1.709 -		bfOffBits = 0;		/* We'll write this when we're done */
   1.710 +        /* Set the BMP file header values */
   1.711 +        bfSize = 0;             /* We'll write this when we're done */
   1.712 +        bfReserved1 = 0;
   1.713 +        bfReserved2 = 0;
   1.714 +        bfOffBits = 0;          /* We'll write this when we're done */
   1.715  
   1.716 -		/* Write the BMP file header values */
   1.717 -		fp_offset = SDL_RWtell(dst);
   1.718 -		SDL_ClearError();
   1.719 -		SDL_RWwrite(dst, magic, 2, 1);
   1.720 -		SDL_WriteLE32(dst, bfSize);
   1.721 -		SDL_WriteLE16(dst, bfReserved1);
   1.722 -		SDL_WriteLE16(dst, bfReserved2);
   1.723 -		SDL_WriteLE32(dst, bfOffBits);
   1.724 +        /* Write the BMP file header values */
   1.725 +        fp_offset = SDL_RWtell (dst);
   1.726 +        SDL_ClearError ();
   1.727 +        SDL_RWwrite (dst, magic, 2, 1);
   1.728 +        SDL_WriteLE32 (dst, bfSize);
   1.729 +        SDL_WriteLE16 (dst, bfReserved1);
   1.730 +        SDL_WriteLE16 (dst, bfReserved2);
   1.731 +        SDL_WriteLE32 (dst, bfOffBits);
   1.732  
   1.733 -		/* Set the BMP info values */
   1.734 -		biSize = 40;
   1.735 -		biWidth = surface->w;
   1.736 -		biHeight = surface->h;
   1.737 -		biPlanes = 1;
   1.738 -		biBitCount = surface->format->BitsPerPixel;
   1.739 -		biCompression = BI_RGB;
   1.740 -		biSizeImage = surface->h*surface->pitch;
   1.741 -		biXPelsPerMeter = 0;
   1.742 -		biYPelsPerMeter = 0;
   1.743 -		if ( surface->format->palette ) {
   1.744 -			biClrUsed = surface->format->palette->ncolors;
   1.745 -		} else {
   1.746 -			biClrUsed = 0;
   1.747 -		}
   1.748 -		biClrImportant = 0;
   1.749 +        /* Set the BMP info values */
   1.750 +        biSize = 40;
   1.751 +        biWidth = surface->w;
   1.752 +        biHeight = surface->h;
   1.753 +        biPlanes = 1;
   1.754 +        biBitCount = surface->format->BitsPerPixel;
   1.755 +        biCompression = BI_RGB;
   1.756 +        biSizeImage = surface->h * surface->pitch;
   1.757 +        biXPelsPerMeter = 0;
   1.758 +        biYPelsPerMeter = 0;
   1.759 +        if (surface->format->palette) {
   1.760 +            biClrUsed = surface->format->palette->ncolors;
   1.761 +        } else {
   1.762 +            biClrUsed = 0;
   1.763 +        }
   1.764 +        biClrImportant = 0;
   1.765  
   1.766 -		/* Write the BMP info values */
   1.767 -		SDL_WriteLE32(dst, biSize);
   1.768 -		SDL_WriteLE32(dst, biWidth);
   1.769 -		SDL_WriteLE32(dst, biHeight);
   1.770 -		SDL_WriteLE16(dst, biPlanes);
   1.771 -		SDL_WriteLE16(dst, biBitCount);
   1.772 -		SDL_WriteLE32(dst, biCompression);
   1.773 -		SDL_WriteLE32(dst, biSizeImage);
   1.774 -		SDL_WriteLE32(dst, biXPelsPerMeter);
   1.775 -		SDL_WriteLE32(dst, biYPelsPerMeter);
   1.776 -		SDL_WriteLE32(dst, biClrUsed);
   1.777 -		SDL_WriteLE32(dst, biClrImportant);
   1.778 +        /* Write the BMP info values */
   1.779 +        SDL_WriteLE32 (dst, biSize);
   1.780 +        SDL_WriteLE32 (dst, biWidth);
   1.781 +        SDL_WriteLE32 (dst, biHeight);
   1.782 +        SDL_WriteLE16 (dst, biPlanes);
   1.783 +        SDL_WriteLE16 (dst, biBitCount);
   1.784 +        SDL_WriteLE32 (dst, biCompression);
   1.785 +        SDL_WriteLE32 (dst, biSizeImage);
   1.786 +        SDL_WriteLE32 (dst, biXPelsPerMeter);
   1.787 +        SDL_WriteLE32 (dst, biYPelsPerMeter);
   1.788 +        SDL_WriteLE32 (dst, biClrUsed);
   1.789 +        SDL_WriteLE32 (dst, biClrImportant);
   1.790  
   1.791 -		/* Write the palette (in BGR color order) */
   1.792 -		if ( surface->format->palette ) {
   1.793 -			SDL_Color *colors;
   1.794 -			int       ncolors;
   1.795 +        /* Write the palette (in BGR color order) */
   1.796 +        if (surface->format->palette) {
   1.797 +            SDL_Color *colors;
   1.798 +            int ncolors;
   1.799  
   1.800 -			colors = surface->format->palette->colors;
   1.801 -			ncolors = surface->format->palette->ncolors;
   1.802 -			for ( i=0; i<ncolors; ++i ) {
   1.803 -				SDL_RWwrite(dst, &colors[i].b, 1, 1);
   1.804 -				SDL_RWwrite(dst, &colors[i].g, 1, 1);
   1.805 -				SDL_RWwrite(dst, &colors[i].r, 1, 1);
   1.806 -				SDL_RWwrite(dst, &colors[i].unused, 1, 1);
   1.807 -			}
   1.808 -		}
   1.809 +            colors = surface->format->palette->colors;
   1.810 +            ncolors = surface->format->palette->ncolors;
   1.811 +            for (i = 0; i < ncolors; ++i) {
   1.812 +                SDL_RWwrite (dst, &colors[i].b, 1, 1);
   1.813 +                SDL_RWwrite (dst, &colors[i].g, 1, 1);
   1.814 +                SDL_RWwrite (dst, &colors[i].r, 1, 1);
   1.815 +                SDL_RWwrite (dst, &colors[i].unused, 1, 1);
   1.816 +            }
   1.817 +        }
   1.818  
   1.819 -		/* Write the bitmap offset */
   1.820 -		bfOffBits = SDL_RWtell(dst)-fp_offset;
   1.821 -		if ( SDL_RWseek(dst, fp_offset+10, RW_SEEK_SET) < 0 ) {
   1.822 -			SDL_Error(SDL_EFSEEK);
   1.823 -		}
   1.824 -		SDL_WriteLE32(dst, bfOffBits);
   1.825 -		if ( SDL_RWseek(dst, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
   1.826 -			SDL_Error(SDL_EFSEEK);
   1.827 -		}
   1.828 +        /* Write the bitmap offset */
   1.829 +        bfOffBits = SDL_RWtell (dst) - fp_offset;
   1.830 +        if (SDL_RWseek (dst, fp_offset + 10, RW_SEEK_SET) < 0) {
   1.831 +            SDL_Error (SDL_EFSEEK);
   1.832 +        }
   1.833 +        SDL_WriteLE32 (dst, bfOffBits);
   1.834 +        if (SDL_RWseek (dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
   1.835 +            SDL_Error (SDL_EFSEEK);
   1.836 +        }
   1.837  
   1.838 -		/* Write the bitmap image upside down */
   1.839 -		bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
   1.840 -		pad  = ((bw%4) ? (4-(bw%4)) : 0);
   1.841 -		while ( bits > (Uint8 *)surface->pixels ) {
   1.842 -			bits -= surface->pitch;
   1.843 -			if ( SDL_RWwrite(dst, bits, 1, bw) != bw) {
   1.844 -				SDL_Error(SDL_EFWRITE);
   1.845 -				break;
   1.846 -			}
   1.847 -			if ( pad ) {
   1.848 -				const Uint8 padbyte = 0;
   1.849 -				for ( i=0; i<pad; ++i ) {
   1.850 -					SDL_RWwrite(dst, &padbyte, 1, 1);
   1.851 -				}
   1.852 -			}
   1.853 -		}
   1.854 +        /* Write the bitmap image upside down */
   1.855 +        bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.856 +        pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
   1.857 +        while (bits > (Uint8 *) surface->pixels) {
   1.858 +            bits -= surface->pitch;
   1.859 +            if (SDL_RWwrite (dst, bits, 1, bw) != bw) {
   1.860 +                SDL_Error (SDL_EFWRITE);
   1.861 +                break;
   1.862 +            }
   1.863 +            if (pad) {
   1.864 +                const Uint8 padbyte = 0;
   1.865 +                for (i = 0; i < pad; ++i) {
   1.866 +                    SDL_RWwrite (dst, &padbyte, 1, 1);
   1.867 +                }
   1.868 +            }
   1.869 +        }
   1.870  
   1.871 -		/* Write the BMP file size */
   1.872 -		bfSize = SDL_RWtell(dst)-fp_offset;
   1.873 -		if ( SDL_RWseek(dst, fp_offset+2, RW_SEEK_SET) < 0 ) {
   1.874 -			SDL_Error(SDL_EFSEEK);
   1.875 -		}
   1.876 -		SDL_WriteLE32(dst, bfSize);
   1.877 -		if ( SDL_RWseek(dst, fp_offset+bfSize, RW_SEEK_SET) < 0 ) {
   1.878 -			SDL_Error(SDL_EFSEEK);
   1.879 -		}
   1.880 +        /* Write the BMP file size */
   1.881 +        bfSize = SDL_RWtell (dst) - fp_offset;
   1.882 +        if (SDL_RWseek (dst, fp_offset + 2, RW_SEEK_SET) < 0) {
   1.883 +            SDL_Error (SDL_EFSEEK);
   1.884 +        }
   1.885 +        SDL_WriteLE32 (dst, bfSize);
   1.886 +        if (SDL_RWseek (dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
   1.887 +            SDL_Error (SDL_EFSEEK);
   1.888 +        }
   1.889  
   1.890 -		/* Close it up.. */
   1.891 -		SDL_UnlockSurface(surface);
   1.892 -		if ( surface != saveme ) {
   1.893 -			SDL_FreeSurface(surface);
   1.894 -		}
   1.895 -	}
   1.896 +        /* Close it up.. */
   1.897 +        SDL_UnlockSurface (surface);
   1.898 +        if (surface != saveme) {
   1.899 +            SDL_FreeSurface (surface);
   1.900 +        }
   1.901 +    }
   1.902  
   1.903 -	if ( freedst && dst ) {
   1.904 -		SDL_RWclose(dst);
   1.905 -	}
   1.906 -	return((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
   1.907 +    if (freedst && dst) {
   1.908 +        SDL_RWclose (dst);
   1.909 +    }
   1.910 +    return ((SDL_strcmp (SDL_GetError (), "") == 0) ? 0 : -1);
   1.911  }
   1.912 +
   1.913 +/* vi: set ts=4 sw=4 expandtab: */