Endianness

Hi

Sorry, I have no access to a big endian machine for now, only PCs, so that’s
why I ask here.

When SDL is running on a big endian machine, at 24bpp & 32bpp, do the
surfaces return the masks as if each pixel was accessed through a 32 bit
pointer, or does it only help for ordering the RGB triplet?

So, if I do

Uint32 temp,mask;
unsigned char r,g,b;
unsigned char *p;
SDL_PixelFormat *fmt = surface->format;

...

p = surface->pixels + ...

 mask = fmt->Rmask | fmt->Gmask | fmt->Bmask;

 temp  = (((Uint32)(r>>fmt->Rloss))<<fmt->Rshift)&fmt->Rmask;
 temp |= (((Uint32)(g>>fmt->Gloss))<<fmt->Gshift)&fmt->Gmask;
 temp |= (((Uint32)(b>>fmt->Bloss))<<fmt->Bshift)&fmt->Bmask;

p = (p & ~mask) | (temp & mask);

Will the RGB bytes be always in the three lowest bytes (so I need to shift
left if the machine is big endian)? or will they be already in the right
position always?

Thanks
Drirr C.

“Drirr C.” wrote:

When SDL is running on a big endian machine, at 24bpp & 32bpp, do the
surfaces return the masks as if each pixel was accessed through a 32 bit
pointer, or does it only help for ordering the RGB triplet?

Surfaces only store pixel values, which are N-byte integers (N=1…4)
stored in the native byte order. The conversion between these integers
and RGB colours are governed by the masks (or the palette for 8bpp).
Everything else follows from this.

temp  = (((Uint32)(r>>fmt->Rloss))<<fmt->Rshift)&fmt->Rmask;
temp |= (((Uint32)(g>>fmt->Gloss))<<fmt->Gshift)&fmt->Gmask;
temp |= (((Uint32)(b>>fmt->Bloss))<<fmt->Bshift)&fmt->Bmask;

‘temp’ will at this point be a valid pixel value from your r, g, b values.

p = (p & ~mask) | (temp & mask);

this line is completely nonsense. For reading/writing pixel values,
see the getpixel/putpixel examples in the tutorial