• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Draw to Offscreen Bitmap in Cocoa???
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Draw to Offscreen Bitmap in Cocoa???
      • From: Clyde McQueen <email@hidden>
References: 
 >Draw to Offscreen Bitmap in Cocoa??? (From: Carlos Weber <email@hidden>)

  • Prev by Date: Re: cocoa-dev digest, Vol 1 #123 - 14 msgs
  • Next by Date: Re: Remember window position in multiple monitors
  • Previous by thread: Draw to Offscreen Bitmap in Cocoa???
  • Next by thread: Re: Draw to Offscreen Bitmap in Cocoa???
  • Index(es):
    • Date
    • Thread