bitmapsdlpixelpixelformat

How does SDL pack pixels?


Say I have an SDL_Surface with 5-bit pixels.
May the byte containing the first pixel contain part of the second pixel?
May the byte containing the last pixel on one line contain part of the first pixel on the next?


Solution

  • The byte containing the first pixel cannot contain part of the second pixel. That would be terrible for read and write operations performed on the surface. Each pixel is byte-aligned, and can be 8, 16, 24 or 32-bit long. Even when palettes are used, each pixel has its own byte.

    There is an exception however: if the surface has been run-length encoded, then more than one pixel can be encoded in the same byte (or group of bytes). But that is completely different from one byte containing a pixel plus part of another. You can use SDL_SetSurfaceRLE to give a hint that you want a surface to be encoded.

    There might be another exception somewhere, but you are most certainly not concerned.

    If however you are talking about the pixel's color components, that's a different story. It all depends on the format of your surface. If each component of your pixel can be contained in 5 bits but you are using a 32-bit format, then each channel is probably 8 bits. In which case each color component stays in its own byte. But if you have pixels with 5-bit channels, for example with a RGB555 (16-bit) format, then of course, components have to share bytes: the first byte contains the red component and part of the green component, and the second byte contains the rest of the green component as well as the blue component. Have a look at SDL_PixelFormatEnum for more information.