Date: Mon, 13 Dec 2004 21:28:18 -0500
authorSam Lantinga <slouken@libsdl.org>
Tue, 14 Dec 2004 06:20:49 +0000
changeset 1012f14e3059e138
parent 1011 4095d9ca23f2
child 1013 a649064a3215
Date: Mon, 13 Dec 2004 21:28:18 -0500
From: Jonathan Atkins
Subject: [SDL] SDL_SaveBMP width bugfix

this fixes the pitch versus width difference that can happen
(especially for 8bit and 24bit (with the exact RGBAmasks) surfaces)
when you use SDL_SaveBMP. The problem was the pitch was used
instead of the width, which in some cases is much wider than the
screen area you really want to save...making for ugly crud on the
saved image borders.
This code has been tested with & without pitch overhangs...and
with the right masks for 24 bit surfaces.
I tested 8,15,16,24,32-0RGB,32-RGBA(with no SDL_SRCALPHA flag).
src/video/SDL_bmp.c
     1.1 --- a/src/video/SDL_bmp.c	Mon Dec 13 08:49:17 2004 +0000
     1.2 +++ b/src/video/SDL_bmp.c	Tue Dec 14 06:20:49 2004 +0000
     1.3 @@ -413,6 +413,8 @@
     1.4  	}
     1.5  
     1.6  	if ( surface && (SDL_LockSurface(surface) == 0) ) {
     1.7 +		const int bw = surface->w*surface->format->BytesPerPixel;
     1.8 +
     1.9  		/* Set the BMP file header values */
    1.10  		bfSize = 0;		 /* We'll write this when we're done */
    1.11  		bfReserved1 = 0;
    1.12 @@ -485,11 +487,10 @@
    1.13  
    1.14  		/* Write the bitmap image upside down */
    1.15  		bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
    1.16 -		pad  = ((surface->pitch%4) ? (4-(surface->pitch%4)) : 0);
    1.17 +		pad  = ((bw%4) ? (4-(bw%4)) : 0);
    1.18  		while ( bits > (Uint8 *)surface->pixels ) {
    1.19  			bits -= surface->pitch;
    1.20 -			if ( SDL_RWwrite(dst, bits, 1, surface->pitch)
    1.21 -							!= surface->pitch) {
    1.22 +			if ( SDL_RWwrite(dst, bits, 1, bw) != bw) {
    1.23  				SDL_Error(SDL_EFWRITE);
    1.24  				break;
    1.25  			}