[MacRuby-devel] MacRuby 0.5 vs. CG Bitmap Context

Laurent Sansonetti lsansonetti at apple.com
Sat Mar 6 18:46:55 PST 2010


Hi Scott,

On Mar 5, 2010, at 8:34 AM, Scott Thompson wrote:

> I'm using MacRuby to work with CoreGraphics and having some trouble with pointers.
> 
> My first attempt was to create the data for a bitmap context using NSMutableData:
> 
> bitmap_data = NSMutableData.alloc.initWithLength(image_data_size)
> bitmap_context = CGBitmapContextCreate(
>                           bitmap_data.mutableBytes(),
>                           image_width, image_height,
>                           8, image_width,
>                           gray_color_space, KCGImageAlphaNone)
> 
> If I do this, I get an error that "expected instance of Pointer of type 'v', got 'C' (TypeError)"
> 
> I gather from this that CGBitmapContextCreate was expecting a (void *) and got a (UInt8 *).  In C I would just type the difference away, but Ruby doesn't like ti.
> 
> I tried something like:
> 
> bitmap_data_ptr = Pointer.new_of_type('v')
> bitmap_data_ptr.assign(bitmap_data.mutableBytes())
> 
> This didn't work very well either.  I get an error that the system can't do a "sizeof" of an unsized type (presumably void *)
> 
> I also tried doing similar using CFDataRef (not much difference really).  It looked like:
> 
> bitmap_data = CFDataCreateMutable(nil, image_data_size)
> CFDataSetLength(bitmap_data, image_data_size)
> bitmap_data_ptr = CFDataGetMutableBytePtr(bitmap_data)
> bitmap_context = CGBitmapContextCreate(
>                           CFDataGetMutableBytePtr(bitmap_data),
>                           image_width, image_height,
>                           8, image_width,
>                           gray_color_space, KCGImageAlphaNone)
> 
> In this case, I get an error that "expected instance of Pointer, got '""' (NSMutableString) (TypeError)
> 
> Evidently MacRuby sees the return value of CFDataGetMutableBytePtr as being a string, subsequently typecasts it as such, and goes on.
> 
> Is there anything I can do to typecast the pointers to a type that MacRuby will accept?

Unfortunately, the Pointer class currently will not allow you to cast types :( If you file a bug in Trac we will add this functionality in the next release. Patches are also welcome :)

In the meantime I would recommend to write this in Objective-C and call it from MacRuby.

Sorry,
Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100306/6b4ccab7/attachment-0001.html>


More information about the MacRuby-devel mailing list