Directly accessing pixels in a surface

Hi all!

I want to in effect do a getpixel from a SDL software surface.

I got the following code from the SDL docs to directly access the very first
top left pixel in a surface:

fmt = source_img->format;
SDL_LockSurface(source_img);

pixel = ((Uint32)source_img->pixels);

SDL_UnlockSurface(source_img);

(I got the shifting and stuff to get actual RGB values down - also from the
docs)

My question - how do I access all the rest of the pixels? I. e. by how much
(or how?) do I increment ->pixels to get to the rest of the surface pixel
data?

What I tried was

pixel = ((Uint32)source_img->pixels) + number_of_pixel_i_want *
fmt->BytesPerPixel

But this does NOT work - I have a “red” surface (RGBA 234,23,23) and no matter
what value I put in number_of_pixel_i_want I keep getting back stuff like
234,23,48 234,23,58 etc. - as if I am trying to index “between” pixels and
getting back the “tail” of one pixel with a byte or more of the “head” of
another. The surface is 16 bpp “deep” in color depth, is a software surface,
and BytesPerPixel has a value of 3.

How do I directly read any pixel I want from a SDL_Surface? Anybody got a
getpixel() for a SDL_Surface?

Thanks!–
Stefan Viljoen
Polar Design Solutions
Software Support Technician

http://sdldoc.csn.ul.ie/guidevideo.php#AEN112On Thursday 11 March 2004 6:56 pm, Stefan Viljoen wrote:

Hi all!

I want to in effect do a getpixel from a SDL software surface.

I got the following code from the SDL docs to directly access the very
first top left pixel in a surface:

fmt = source_img->format;
SDL_LockSurface(source_img);

pixel = ((Uint32)source_img->pixels);

SDL_UnlockSurface(source_img);

(I got the shifting and stuff to get actual RGB values down - also from the
docs)

My question - how do I access all the rest of the pixels? I. e. by how much
(or how?) do I increment ->pixels to get to the rest of the surface pixel
data?

What I tried was

pixel = ((Uint32)source_img->pixels) + number_of_pixel_i_want *
fmt->BytesPerPixel

But this does NOT work - I have a “red” surface (RGBA 234,23,23) and no
matter what value I put in number_of_pixel_i_want I keep getting back stuff
like 234,23,48 234,23,58 etc. - as if I am trying to index “between” pixels
and getting back the “tail” of one pixel with a byte or more of the "head"
of another. The surface is 16 bpp “deep” in color depth, is a software
surface, and BytesPerPixel has a value of 3.

How do I directly read any pixel I want from a SDL_Surface? Anybody got a
getpixel() for a SDL_Surface?

Thanks!

To get a pointer to the pixel at screen location ‘x’ and ‘y’:

(SDL_PixelFormat *) vfmt = screen->format;
(Uint8) bytes_per_pixel = vfmt->BytesPerPixel;
(void *) pixel_ptr = screen->pixels + y * screen->pitch + x * bytes_per_pixel;

pixel_ptr is of course not void, it should be a pointer to an element of size
’bytes_per_pixel’.

JPOn Thursday 11 March 2004 10:56 am, Stefan Viljoen wrote:

My question - how do I access all the rest of the pixels? I. e. by how much
(or how?) do I increment ->pixels to get to the rest of the surface pixel
data?