Sign in to follow this  
Altair2

Obtaining RGB and RGBA values from a pixel

Recommended Posts

Hi and, first of all, sorry about my low english ;) I am trying to use SDL_MapRGB in a while, but some time later the while is running, ever I have "Fatal Signal: Segmentation Fault (SDL Parachute Deployed). I trying to use SDL_lockSurface and SDL_UnlockSurface, but the result is repeated. The example I use: http://www.gamedev.net/reference/programming/features/sdl2/page5.asp In this url, says about a macro called "SDL_MUSTLOCK", but in my HTML help of SDL (version 1.2.9, the stable when my little proyect was born) I not find any world about macros. Thanks for all :)

Share this post


Link to post
Share on other sites
If you post your code, it will be easier for us to help you.

In the SDL wiki, the SDL_LockSurface docs make a reference to the SDL_MUSTLOCK macro, it doesn't get a page to itself. I believe the macro basically expands to testing whether the surface is in system memory or video memory.

Share this post


Link to post
Share on other sites
void SetPixel ( SDL_Surface* pSurface , int x , int y , SDL_Color color )
{
// Lock surface
SDL_LockSurface( pSurface );

//convert color
Uint32 col = SDL_MapRGB ( pSurface->format , color.r , color.g , color.b ) ;

//determine position
char* pPosition = ( char* ) pSurface->pixels ;

//offset by y
pPosition += ( pSurface->pitch * y ) ;

//offset by x
pPosition += ( pSurface->format->BytesPerPixel * x ) ;

//copy pixel data
memcpy ( pPosition , &col , pSurface->format->BytesPerPixel ) ;

// Surface unlocked
SDL_UnlockSurface( pSurface );
}

But the error persist.

Share this post


Link to post
Share on other sites
Quote:
Original post by Altair2
...code...

But the error persist.


(You can use [source] and [/source] to enclose code in a nice box.)

You're not checking for depth. The pixel access is different according to depth. There was a page in the wiki about SDL pixel access, but I can't find it right now so here are the functions straight from my code:

void SDL_PutPixel(SDL_Surface *surface, int x, int y, unsigned long c) {
int bpp = surface->format->BytesPerPixel;

unsigned char *p = (unsigned char*)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = c;
break;

case 2:
*(unsigned short*)p = c;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (c >> 16) & 0xff;
p[1] = (c >> 8) & 0xff;
p[2] = c & 0xff;
} else {
p[0] = c & 0xff;
p[1] = (c >> 8) & 0xff;
p[2] = (c >> 16) & 0xff;
}
break;

case 4:
*(unsigned long*)p = c;
break;

default:
break;
}
}

unsigned SDL_GetPixel(SDL_Surface *surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;

unsigned char *p = (unsigned char*)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
return *(unsigned char*)p;
break;

case 2:
return *(unsigned short*)p;
break;

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;
break;

case 4:
return *(unsigned long*)p;
break;

default:
break;
}

return 0;
}





You must lock the surface before using these functions (and unlock after). The good part is that if you are accessing many pixels it'd be a good idea to lock once and unlock after you're done, instead of taking time locking and unlocking for every pixel.

It looks something like this"

if (SDL_MUSTLOCK(surface))
while (SDL_LockSurface(surface)) SDL_Delay(10);
...
// pixel access
...
if (SDL_MUSTLOCK(surface))
SDL_UnlockSurface(surface);




The while loop and SDL_MUSTLOCK ensure that the surface is correctly ready for pixel access.

Share this post


Link to post
Share on other sites
You asked about what SDL_MUSTLOCK is, so here it is:

#define SDL_MUSTLOCK(surface) \
(surface->offset || \
((surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_RLEACCEL)) != 0))

Basically it evaluates whether the surface is in video (hardware) memory - because if it is in RAM it doesn't need to be locked.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this