Skip to main content
gamedev.net
🔒 Locked

pixel color

Started by tjrr3d Dec 21, 2005 at 12:57 PM 5 replies 900+ views
Original Post
tjrr3d
tjrr3d
I have an SDL_Surface* with an overal majority color. I'm having trouble extracting the rgb from the pixel data. if I do: SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100, 24, 0,0,0,0); SDL_FillRect(tmp,0, SDL_MapRGBA(CORE->SDLGLwin->format, 255,0,0,0)); int pix = *(int*)tmp->pixels; pix is 16711680 and 0, 255, 255 is -16711681 I believe this is 1 or 2's complement. My problem is I'm not surev how int numbers are stored (in the binary sense). How can I extract the rgb given a binary number.
tjrr3d
tjrr3d
Just wanted to say the number is 2's compliment.

So given a number N say -16711681;
 2^32 - 1    =    11111111111111111111111111111111-16711681    =    00000000111111110000000000000001               ------------------------------------                  11111111000000001111111111111110+1                 11111111000000001111111111111111                   /        |         \                        11111111   00000000   11111111  11111111               alpha      Red        Green     Blue  

So how can I represent any number in binary?
Steven Hansen
Steven Hansen
Instead of casting to an int where you need to mess with sign, try casting to an unsigned int instead. That should lay things out in the way you would expect.

ARGB - 8 bits each.
tjrr3d
tjrr3d
unsigned int pix = *(unsigned int*)tmp->pixels;

does the same as

int pix = *(int*)tmp->pixels;

i don't know why
Steven Hansen
Steven Hansen
With an unsigned int, the first 8 bits represent alpha, the next 8 bits represent red, then green, then blue (unless you have RGBA, then red would be high 8 bits).

Should work - give it a shot.

UCHAR alpha = (UCHAR) ((pix >> 24) | 0xff);
UCHAR red = (UCHAR) ((pix >> 16) | 0xff);
UCHAR green = (UCHAR) ((pix >> 8) | 0xff);
UCHAR blue = (UCHAR) (pix | 0xff);
MatrixCubed
MatrixCubed
Some code I use...

Uint32 GetPixel(SDL_Surface *lpSurface, Sint32 X, Sint32 Y){   Uint8  *bits;   Uint32 Bpp;   if (X<0) return -1;   if (X>=lpSurface->w) return -1;   Bpp = lpSurface->format->BytesPerPixel;   bits = ((Uint8 *)lpSurface->pixels)+Y*Surface->pitch+X*Bpp;   // Get the pixel   switch(Bpp) {      case 1:         return *((Uint8 *)lpSurface->pixels + Y * lpSurface->pitch + X);         break;      case 2:         return *((Uint16 *)lpSurface->pixels + Y * lpSurface->pitch/2 + X);         break;      case 3: { // Format/endian independent         Uint8 r, g, b;         r = *((bits)+lpSurface->format->Rshift/8);         g = *((bits)+lpSurface->format->Gshift/8);         b = *((bits)+lpSurface->format->Bshift/8);         return SDL_MapRGB(lpSurface->format, r, g, b);         }         break;      case 4:         return *((Uint32 *)lpSurface->pixels + Y * lpSurface->pitch/4 + X);         break;   }    return -1;}
tjrr3d
tjrr3d
thanks guys your the best

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.