[SDL] Get pixel value

David Olofson david.olofson at reologica.se
Mon Aug 27 06:17:01 PDT 2001


On Sunday 26 August 2001 01:29, sragno at libero.it wrote:
> 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 + y*sur->pitch + x*bpp
>
>    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 -'




More information about the SDL mailing list