[futurebasic] Re: [FB] strange results from CGBitmapContextCreate [was malloc size question]

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2007 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Thu, 20 Dec 2007 15:11:00 +1300
Joe Lertola wrote:

> When I run this with FB all is fine. When I run this with FBtoC the  
> program thinks the bytes that make up the image are in little end  
> order. To see this you can run the demo in FB and also in FBtoC and  
> compare them side by side. Look at the multi byte per component  
> color spaces which are the last 8 options in the popup menu. The  
> FBtoC version has a scrambled noisy look.

To work with kCGBitmapFloatComponents in the way you do, the  
CGBitmap's byte order must be specified as kCGBitmapByteOrder32Host,  
because the endianness of the vars you blit into the bitmap is host- 
dependent:

dim as single value // 32-bit; little-endian on Intel binary made by  
FBtoC, else big-endian
value = 0.123
blit_float_value_into_correctly_specified_CGBitmap

Similarly, for 16-bit integer pixel components, the bitmap order must  
be kCGBitmapByteOrder16Host


It's a little hacky to express these symbols in FBtoC as well as FB,  
mainly because they are C macros. Here's one way, which works for FB,  
FBtoC -> PPC and FBtoC -> Intel. The symbols are real globals (bah!)  
in FB but pseudo-globals in FBtoC:

#if ndef _DEFINEDINCRUNTIME // suppress variable declarations in FBtoC
begin globals
dim as long kCGBitmapByteOrder16Host, kCGBitmapByteOrder32Host
end globals
#endif
#if ndef _FBtoC
kCGBitmapByteOrder16Host = _kCGBitmapByteOrder16Big
kCGBitmapByteOrder32Host = _kCGBitmapByteOrder32Big
#endif


Then replace your uses of _kCGBitmapByteOrder32Big:
> xPixelFormat = _kCGImageAlphaNone or _kCGBitmapFloatComponents or  
> _kCGBitmapByteOrder32Big'AA----

xPixelFormat = _kCGImageAlphaNone or _kCGBitmapFloatComponents or  
kCGBitmapByteOrder32Host

and of _kCGBitmapByteOrder16Big:
> xPixelFormat = _kCGImageAlphaNone or _kCGBitmapByteOrder16Big'AA----

xPixelFormat = _kCGImageAlphaNone or kCGBitmapByteOrder16Host


Two minor points.

[1] To allow the FBtoC-built app to quit properly, change the last  
line of code to:
	until gFBQuit

[2] To speed up float-blitting, declare the address as a pointer and  
use dereferencing notation instead of BlockMove or BlockMoveData.

dim as pointer adr
...
adr.4! = value // faster than: BlockMoveData( @value, adr + 4, 4 )


Robert P.