[SDL] Get pixel value ...more in details

David Olofson david.olofson at reologica.se
Mon Aug 27 07:19:01 PDT 2001


On Sunday 26 August 2001 17:40, John R. Hall wrote:
> On Sunday, August 26, 2001, at 02:35 AM, sragno at libero.it wrote:
> >> 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)
> >
> >  I understand a little more...but:
> >
> >  - shifting one bite by 16 or even 8 bits doesn't always gives
> >    a zero value ?
>
> Not necessarily. Compilers sometimes take the liberty of extending the
> field to accommodate the new value. I don't know what the C standard
> says on this, but it doesn't seem to be guaranteed, as I've had it fail
> on some platforms.

Same here.

It seems that old compilers (that don't know that modern CPUs prefer 
working with 32 bit registers for speed) frequently work by the rule 
"cast as late as possible, so we can use the samllest possible word size 
for each operation". On such compilers, all shifting and or:ing will be 
performed on Uint8, and then the result will be extended (zero padded) to 
Uint32.


> The SDL code seems to make this assumption a lot.

Then again, that code isn't meant for 8 or 16 bit CPUs, is it...? (Not an 
excuse to write unsafe code, though!)


> >  - we are returning p[2]<< a | p[1] << b | p[0] that is not*
> >    a Uint32 (also *p or *(Uint16 *)p are not) , so how and where
> >    happens this conversion ??
>
> Again, it's a bad assumption that seems to work on most platforms.

Doesn't work on Borland C++/DOS real mode! :-) (Thank God I'm supposed to 
ditch it for good, in favor if gcc/g++ within less than half a year.)


> It
> will probably fail on platforms where sizeof(int) != 4 or (possibly)
> the compiler isn't gcc. I had big problems with this when I tried to
> port SDL to PalmOS (finally did get it to compile without warnings
> after adding a bunch of Uint32 casts to this kind of code).

Oops. (16 bit RISC?)


//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