Hi all,
many programs use a function like Uint32 getpixel(Surface *sur,
int x,int y) to obtain the value of a pixel.
Bytes-per-pixel are retrieved setting:
int bpp=sur->format->BytesPerPixel
Address of the pixel is computed using:
Uint8 p=(Uint8 )sur->pixels + ysur->pitch + xbpp
Color depths are treated switching bpp, in particular:
case 1: return *p; // the default Uint8 pointer
case 2: return *(Uint16 *)p; // type casting p
case 4: return *(Uint32 *)p; // casting again
But what is the meaning of the following? (Supposing little endian)
case 3: return p[0] | p[1] << 8 | p[2] << 16;
Can anyone explain (at bit level) what happens?
@The_Spider
But what is the meaning of the following? (Supposing little endian)
case 3: return p[0] | p[1] << 8 | p[2] << 16;
It’s pretty simple really. It’s just the first byte at ‘p’, ORed with
the 2nd byte shifted left 8 bits, ORed with the 3rd byte shifted left 16
bits. This combines the 3 bytes into a 24-bit space. Technically it
could be addition instead of OR, since all the bytes are being ORed with
zero.
Example:
p[0] = 10101010
p[1] = 11110000
p[2] = 00001111
This becomes
000011111111000010101010
which is
00001111 11110000 10101010
(ordered with p[2] on the left through to p[0] on the right).
This is returned as a Uint32, so there will be leading zeros… the
result for this example therefore would be:
00000000000011111111000010101010 (with 8 leading zeros added)–
Ben Sizer
Hi all,
many programs use a function like Uint32 getpixel(Surface *sur,
int x,int y) to obtain the value of a pixel.
Yes, but it’s still a bad idea on modern machines, unless you’re sure
you’re on a software surface. Doesn’t matter much if you only do it a few
times per frame, though…
Bytes-per-pixel are retrieved setting:
int bpp=sur->format->BytesPerPixel
Address of the pixel is computed using:
Uint8 p=(Uint8 )sur->pixels + ysur->pitch + xbpp
Color depths are treated switching bpp, in particular:
case 1: return *p; // the default Uint8 pointer
case 2: return *(Uint16 *)p; // type casting p
case 4: return *(Uint32 *)p; // casting again
But what is the meaning of the following? (Supposing little endian)
case 3: return p[0] | p[1] << 8 | p[2] << 16;
p[0] //Put the first byte in the 8 low bits
| p[1] << 8 //Put the second byte in the next higher 8 bits
| p[2] << 16; //Add the third byte in the highest 8 of 24 bits
//David Olofson — Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |
--------------------------------------> david at linuxdj.com -'On Sunday 26 August 2001 01:29, sragno at libero.it wrote: