SDL_image question

Hello all,

I’m trying to load a height map from a TGA.
The TGA is a greyscale image where black is min height
and white is max height.

I’ve loaded it all to an SDL_Surface and am now trying to
access the individual pixels…Goes something like this:

SDL_Surface *image;
// load TGA to image

int width = image->w;
int height = image->h;

int x,y;
for (x=0; x<width; x++)
for (y=0; y<height; y++)
{
theHeight[x][y] = someValueBasedOnPixelColour ;
}

Can someone tell me how to get the pixel colour for (x,y) from the
SDL_surface data?

cheers,
k.

Kostas Kostiadis wrote:

Hello all,

I’m trying to load a height map from a TGA.
The TGA is a greyscale image where black is min height
and white is max height.

I’ve loaded it all to an SDL_Surface and am now trying to
access the individual pixels…Goes something like this:

SDL_Surface *image;
// load TGA to image

int width = image->w;
int height = image->h;

int x,y;
for (x=0; x<width; x++)
for (y=0; y<height; y++)
{
theHeight[x][y] = someValueBasedOnPixelColour ;
}

Can someone tell me how to get the pixel colour for (x,y) from the
SDL_surface data?

cheers,
k.

See http://sdldoc.csn.ul.ie/guidevideo.php#AEN112 for an example.
I will transfer it into the Doc Wiki shortly.
The getpixel() function there can be used on a surface of unknown depth.
You should lock the surface before the loop and unlock it at the end - see SDL_LockSurface() docs.
That function returns a format-dependent value, use SDL_GetRGB() to get color components.

If you know that the pixel depth is going to be 8 bits per pixel, you can use the following instead of that function:
((Uint8*) image->pixels)[y * image->pitch + x]

Chris E.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20041221/c0ce300d/attachment.pgp