We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 990

Summary: SDL_CalculatePitch() too small
Product: SDL Reporter: libsdl-bugtracker
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: trivial    
Priority: P2    
Version: 1.2.13   
Hardware: x86   
OS: Linux   

Description libsdl-bugtracker 2010-04-21 00:27:20 UTC
SDL_CalculatePitch() switches surface.format->BitsPerPixel.

1) SDL doesn't allow BitsPerPixel smaller than 8 and cases are 1 and 4, which will never be matched

2) as surface.pitch is a multiple of 8 bits,1 byte respectively , 1 and 4 BitsPerPixel both result in 1 BytesPerPixel (see SDL_AllocFormat()) and therefore it doesn't make sense to treat them differently considering the byte aligning

3) to access single pixels via surface.pixels you use at least multiples sizeof(char), so that for a given surface.w insufficient memory is allocated at surface creation: (pitch+7)/8, (pitch+1)/2 (surface.w = 5, 1 BitsPerPixel, 1 BytesPerPixel produces pitch=1 (4 aligned) instead of pitch=5 (8 aligned))
Comment 1 libsdl-bugtracker 2010-04-21 00:30:45 UTC
Already mentioned by Sylvain Hellegouarch[1] in 2003, who got poor answers.

[1]http://lists.libsdl.org/pipermail/sdl-libsdl.org/2003-April/034932.html
Comment 2 Sam Lantinga 2010-04-23 02:04:30 UTC
Do you have a specific example of where this is a problem?

The places where I've seen bitmap data used, the pixels are packed in as tightly as possible and the final scanline is aligned on a byte or 4 byte boundary.

To access single pixels, you unpack the bits out of the scanline in order.

For an example, look at the "Bitmap data" section in the BMP file format:
http://en.wikipedia.org/wiki/BMP_file_format

I think what you're talking about is an 8 bpp image with a limited palette.
Comment 3 libsdl-bugtracker 2010-04-24 04:46:56 UTC
See example above: when using a 5 pixel-broad surface with 1 bit per pixel 4 bytes instead of 8 are allocated. If you try to manipulate the last $SURFACE_HEIGHT pixels you will run into memory violation, because no memory was allocated for them. With color depth other than 1 and 4 there is no problem (both not allowed by SDL, so that nobody really runs into problems, but the implementation is incorrect nevertheless)
Comment 4 Sam Lantinga 2012-01-02 00:09:38 UTC
If this is actually a problem for someone, please reopen this bug.  For now, I'll close it, since I have lots of other things I need to do.