SDL Getpixel?

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 fi=
rst=20
top left pixel in a surface:

fmt =3D 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 t=
he=20
docs)

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

What I tried was

pixel =3D ((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 m=
atter=20
what value I put in number_of_pixel_i_want I keep getting back stuff like=
=20
234,23,48 234,23,58 etc. - as if I am trying to index “between” pixels an=
d=20
getting back the “tail” of one pixel with a byte or more of the “head” of=
=20
another. The surface is 16 bpp “deep” in color depth, is a software surfa=
ce,=20
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

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 fi=
rst=20
top left pixel in a surface:

fmt =3D 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 t=
he=20
docs)

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

What I tried was

pixel =3D ((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 m=
atter=20
what value I put in number_of_pixel_i_want I keep getting back stuff like=
=20
234,23,48 234,23,58 etc. - as if I am trying to index “between” pixels an=
d=20
getting back the “tail” of one pixel with a byte or more of the “head” of=
=20
another. The surface is 16 bpp “deep” in color depth, is a software surfa=
ce,=20
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?

yup, that’s in the docs :
http://sdldoc.csn.ul.ie/guidevideo.php#AEN112

Stephane

Hi guys!

Just thought I’d share the solution I came up with by myself regarding a
getpixel for SDL.

The reason I needed this was to build a mask to test occlusion between two
SDL_Surfaces:

bitmask *create_bitmask(SDL_Surface source_img)
/

Takes source_img, converts it to a white (RGB 255,255,255) mask on a black
background, and returns a bitmask
type pointer to this mask.
*/
{
Uint32 pixel_b,pixel_g,pixel_r;

bitmask *created_bitmask = NULL;

int x = 0,y = 0;

created_bitmask = bitmask_create(source_img->w,source_img->h);

for (y = 0; y < source_img->h; y++)
{
for (x = 0; x < source_img->w; x++)
{
bitmask_clearbit(created_bitmask,x,y);
}
}

SDL_LockSurface(source_img);

for (y = 0; y < source_img->h; y++)
{
for (x = 0; x < source_img->w; x++)
{
pixel_b = ((unsigned char)source_img->pixels + source_img->w * y *
source_img->format->BytesPerPixel + x * source_img->format->BytesPerPixel);
pixel_g = ((unsigned char)source_img->pixels + source_img->w * y *
source_img->format->BytesPerPixel + x * source_img->format->BytesPerPixel +
1);
pixel_r = ((unsigned char)source_img->pixels + source_img->w * y *
source_img->format->BytesPerPixel + x * source_img->format->BytesPerPixel +
2);

  if ((pixel_r != 0) && (pixel_g != 0) && (pixel_b != 0))
    {
      bitmask_setbit(created_bitmask,x,y);
    }
}
}

SDL_UnlockSurface(source_img);

return created_bitmask;
}

At least this works like I want it to. On my system. Can endianess on a non
Intel system affect this code?

How do I fix THAT?

Thanks!–
Stefan Viljoen
Polar Design Solutions
Software Support Technician