Food for thought.
Code:
Uint32 get_pixel(SDL_Surface *surface, int x, int y) {
Uint8 *p;
if(!surface->pixels) return 0;
p = (Uint8 *)surface->pixels
+ y * surface->pitch
+ x * surface->format->BytesPerPixel;
if(x < 0 || y < 0 || x >= surface->w || y >= surface->h) return 0;
switch(surface->format->BytesPerPixel) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
return p[0] << 16 | p[1] << 8 | p[2];
}
else return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
Taken almost verbatim from the SDL documentation.