Re: Draw to Offscreen Bitmap in Cocoa???
Re: Draw to Offscreen Bitmap in Cocoa???
- Subject: Re: Draw to Offscreen Bitmap in Cocoa???
- From: Clyde McQueen <email@hidden>
- Date: Sun, 10 Jun 2001 12:50:39 -0700
If you need to draw images with rotation, this doesn't work. As near as
I can tell, the only way to rotate an image is to create a custom view
class, then:
myView = [[[MyCustomView alloc] initWithFrame:size] autorelease];
... insert myView into the view hierarchy in some window - hopefully
hidden...
[myView setBoundsRotation:90];
[myView lockFocus];
[myView drawRect:[myView bounds]]; // This method must use [image
drawToPoint: rather than compositeToPoint:
rotatedBitmap = [NSBitmapImageRep initWithFocusedViewRect:[myView
bounds]];
[myView unlockFocus];
... remove myView from view hierarchy...
This seems like a lot of work to rotate an image 90 degrees, and
although I haven't tried it (yet), it seems that the resulting bitmap
will be unduly influenced by the current display depth, etc.
Am I missing something obvious, or should I give up on using Quartz to
manipulate bitmaps?
/Clyde
On Saturday, June 9, 2001, at 02:49 PM, Greg Titus wrote:
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
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev