Simple question: getpixel in 24-bit

SDL_Surface *surface;
Uint32 firstpixel;

when my surface is 8-bit I can:

firstpixel=((Uint8 *)surface->pixels)[0]

similiar with 16-bit (Uint16) and 32-bit (Uint32)

how can I read first pixel when surface is 24-bit ? must I read 3 bytes, then
add and shift?–
decopter - free SDL/OpenGL simulator under heavy development
download it from http://decopter.sourceforge.net

how can I read first pixel when surface is 24-bit ? must I read 3 bytes,
then add and shift?

/* get 24-bit pixel /
#if SDL_BYTEORDER - SDL_LIL_ENDIAN
/
big endian /
#define GETPX3 ((Uint32)src[2] | (Uint32)src[1] << 8 | (Uint32)src[0] << 16)
#else
/
little endian */
#define GETPX3 ((Uint32)src[0] | (Uint32)src[1] << 8 | (Uint32)src[2] << 16)
#endif

(the GETPX3 macro gets the pixel, src is the location in the pixel surface =)–
Trick


Linux User #229006 * http://counter.li.org

“There is no magic” - Nakor, magic user

You can try this, but the multiplication will slow things down.
firstpixel=((Uint32)(surface->pixels+offset*3));

I think this would also work, but it would not be very portable(would work at
least with gcc):

#pragma pack (1)
typedef struct _UINT24
unsigned char data[3]
} Uint24;
#pragma pack(0)

firstpixel=((Uint24)surface->pixels)[0];

Any other ideas???On Friday 08 February 2002 03:27 pm, you wrote:

SDL_Surface *surface;
Uint32 firstpixel;

when my surface is 8-bit I can:

firstpixel=((Uint8 *)surface->pixels)[0]

similiar with 16-bit (Uint16) and 32-bit (Uint32)

how can I read first pixel when surface is 24-bit ? must I read 3 bytes,
then add and shift?


A triangle was an improvement to the square wheel.
It eliminated one bump
BC

Raphael Assenat <raphael.assenat at 8D.com> wrote:

You can try this, but the multiplication will slow things down.
firstpixel=((Uint32)(surface->pixels+offset*3));

definitely not — unaligned access is in general not possible.
(also, you cannot do arithmetic on a void*)

I think this would also work, but it would not be very portable(would work at
least with gcc):

which is another bad idea. Look, either access 24-bit pixels byte by byte,
or if you want to be really fancy, use a 32 (or 64, 128 etc) bit word
and shift/mask if needed (reading 3N pixels at a time)