[MacRuby-devel] working with pixels in bitmaps?

Emil Tin emil at tin.dk
Wed Jul 28 06:19:00 PDT 2010


hi,
i'm trying to draw pixels in a bitmap. first i'm allocating a NSBitmapImageRep:

	def awakeFromNib
		@nsBitmapImageRepObj = NSBitmapImageRep.alloc.initWithBitmapDataPlanes nil,
                     pixelsWide:@nsRectFrameRect.size.width,
                     pixelsHigh:@nsRectFrameRect.size.height,
                     bitsPerSample:8,
                     samplesPerPixel:3,
                     hasAlpha:false,
                     isPlanar:false,
                     colorSpaceName:"NSCalibratedRGBColorSpace",
                     bytesPerRow:0,
                     bitsPerPixel:0
	end


i can in fact write pixels if i use the setPixel method:

	def drawBitmap
		xs = 255.0/@nsRectFrameRect.size.width
		ys = 255.0/@nsRectFrameRect.size.height
		0.upto @nsRectFrameRect.size.height do |y|
			0.upto @nsRectFrameRect.size.width do |x|
				color = [x*xs,y*ys,0]
				@nsBitmapImageRepObj.setPixel color, atX:x, y:y   
			end
		end
		setNeedsDisplay true
	end


however, i can't figure out how to write directly to the bitmap. bitmapData returns a pointer to the allocated memery, but how do i write to it?

		@nsBitmapImageRepObj.bitmapData[0] = 255		# => can't convert Fixnum into String (TypeError)
		@nsBitmapImageRepObj.bitmapData[1] = '\255'		# => index 1 out of string (IndexError)

documentation states that bitmapData returns an unsigned char*, but when ruby seems to think it's a string with length zero.
do i need to use the Pointer class?


searching for 'pixel' here on the list revelealed this post by julien jassaud back in dec 2009:

> I also had a problem with function NSBitmapImageRep. Again, a pointer problem. Having an NSBitmapImageRep, the function bitmapData returns an (unsigned char *) but in MacRuby, it returns an empty string. The workaround was to create the class in an Objective C bundle.

any news on this issue?


thanks in advance,
emil tin
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1920 bytes
Desc: not available
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100728/c2351214/attachment.bin>


More information about the MacRuby-devel mailing list