Commit 1b4de45d05f2391391674f5f93214e5a8c8f3039

Sam Lantinga 2019-10-16T08:45:54

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.

diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index 814e536..a575aa0 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -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;
 }