Fixed bug 4785 - SDL_CreateRGBSurface creates 1-bit surfaces with zero pitch

Sylvain

Seems to be a regression in this commit: https://hg.libsdl.org/SDL/rev/7fdbffd47c0e
SDL_CalculatePitch() was using format->BytesPerPixel, now it uses SDL_BYTESPERPIXEL().

The underlying issue is that "surface->format->BytesPerPixel" is *not* always the same as SDL_BYTESPERPIXEL(format);
BytesPerPixel defined as format->BytesPerPixel = (bpp + 7) / 8;
vs
#define SDL_BYTESPERPIXEL(format)  ... (format & 0xff)

Because of SDL_pixels.h format definitions, one is giving a BytesPP 1, the other 0.
This commit is contained in:
Sam Lantinga 2019-10-16 08:45:54 -07:00
parent ed7483f82c
commit 1b4de45d05
1 changed files with 6 additions and 13 deletions

View File

@ -42,19 +42,12 @@ SDL_CalculatePitch(Uint32 format, int width)
{
int pitch;
/* Surface should be 4-byte aligned for speed */
pitch = width * SDL_BYTESPERPIXEL(format);
switch (SDL_BITSPERPIXEL(format)) {
case 1:
pitch = (pitch + 7) / 8;
break;
case 4:
pitch = (pitch + 1) / 2;
break;
default:
break;
}
pitch = (pitch + 3) & ~3; /* 4-byte aligning */
if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_BITSPERPIXEL(format) >= 8) {
pitch = (width * SDL_BYTESPERPIXEL(format));
} else {
pitch = ((width * SDL_BITSPERPIXEL(format)) + 7) / 8;
}
pitch = (pitch + 3) & ~3; /* 4-byte aligning for speed */
return pitch;
}