Pixel access in SDL_Surface

How do you access pixels directly? The SDL_Surface struct has a “pixels” member, but it’s defined as a void*, which really doesn’t provide much useful data, and I can’t seem to find any tutorials on how it works. I’m trying to write an import routine for a graphics format that SDL_Image doesn’t support. It’s 8-bit and palletized, and I’ve got the palette stuff all figured out, but I can’t figure out how to set the pixels themselves. Any help?

Sure :slight_smile:

http://www.libsdl.org/cgi/docwiki.cgi/Pixel_20AccessOn Fri, Apr 4, 2008 at 1:08 PM, Mason Wheeler wrote:

How do you access pixels directly? The SDL_Surface struct has a “pixels” member, but it’s defined as a void*, which really doesn’t provide much useful data, and I can’t seem to find any tutorials on how it works. I’m trying to write an import routine for a graphics format that SDL_Image doesn’t support. It’s 8-bit and palletized, and I’ve got the palette stuff all figured out, but I can’t figure out how to set the pixels themselves. Any help?

Mason Wheeler wrote:

How do you access pixels directly? The SDL_Surface struct has a
"pixels" member, but it’s defined as a void*, which really doesn’t
provide much useful data, and I can’t seem to find any tutorials on
how it works. I’m trying to write an import routine for a graphics
format that SDL_Image doesn’t support. It’s 8-bit and palletized,
and I’ve got the palette stuff all figured out, but I can’t figure
out how to set the pixels themselves. Any help?

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSurface

The members of SDL_Surface w, h, pitch, and format give all you need for
addressing pixels. But if you’re using 8 bit, a pixel (x,y) is just
pixels[x+y*pitch]. Don’t forget to lock/unlock the surface for direct
access. It’s a void pointer because the element size depends on the
color format used.

–Jacob

All right. Does it make a difference whether the image uses a palette or not? If not, how do you tell the difference between references in the original to two palette entries that are identical, with the slight distinction that one of them happens to be the color key?>http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSurface

The members of SDL_Surface w, h, pitch, and format give all you need for
addressing pixels. But if you’re using 8 bit, a pixel (x,y) is just
pixels[x+y*pitch]. Don’t forget to lock/unlock the surface for direct
access. It’s a void pointer because the element size depends on the
color format used.

–Jacob