Re: Draw to Offscreen Bitmap in Cocoa???
Re: Draw to Offscreen Bitmap in Cocoa???
- Subject: Re: Draw to Offscreen Bitmap in Cocoa???
- From: Greg Titus <email@hidden>
- Date: Sat, 9 Jun 2001 14:49:30 -0700
On Saturday, June 9, 2001, at 10:28 AM, Carlos Weber wrote:
As a relative newcomer to Cocoa from the world of classic Mac
programming, I have hit a mental roadblock trying to figure out the
"Cocoa way" to accomplish this task:
I am trying to draw a simple image (some rectangles and text) which I
will ultimately use to create an OpenGL texture. In the old world this
would involve the following steps:
1. Create an offscreen GWorld
2. Draw your stuff into it.
3. Get the bits you just drew, by getting the PixMap of the offscreen
GWorld and getting its base address (and some other ugly stuff).
4. Swizzle the bits so that they are in the RGBA format OpenGL wants.
5. Stick them in a buffer and tell OpenGL to make a texture therefrom.
I can't seem to get a handle on how to do the Cocoa equivalent of steps
1 thru 3... I need to draw (but I don't want to REALLY draw on the
screen), then obtain the "bits" I just drew in some definable format
that I can massage and send to OpenGL.
1. Create an NSImage (of whatever size is appropriate for you...)
image = [[NSImage alloc] initWithSize:NSMakeSize(100.0, 100.0)];
2. Draw your stuff into it by "locking focus" on your image, drawing,
then unlocking focus...
[image lockFocus];
[[NSColor blueColor] set];
NSFillRect(NSMakeRect(0, 0, 50, 50));
... put any drawing stuff here, including compositing other images,
NSBezierPath, the draw methods on NSString and NSAttributedString, et
cetera.
[image unlockFocus];
3. Get the bits you just drew...
3a. if you want tiff data you can just call:
data = [image TIFFRepresentation];
3b. if you want raw image data you can get the NSBitmapImageRep (which
should be the first (only?) representation of the image you created):
bitmapImageRep = [[image representations] objectAtIndex:0];
And then you can get the bitmap data and all sorts of other
information:
data = [bitmapImageRep bitmapData];
a = [bitmapImageRep bitsPerPixel];
b = [bitmapImageRep bytesPerRow];
c = [bitmapImageRep samplesPerPixel];
Et cetera. Check the documentation for NSImage and NSBitmapImageRep.
Hope this helps,
--Greg