From 1b4de45d05f2391391674f5f93214e5a8c8f3039 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 16 Oct 2019 08:45:54 -0700 Subject: [PATCH] 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. --- src/video/SDL_surface.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c index 814e536e2..a575aa067 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; }