Re: Advice on offscreen drawing.
Re: Advice on offscreen drawing.
- Subject: Re: Advice on offscreen drawing.
- From: Dennis De Mars <email@hidden>
- Date: Thu, 14 Mar 2002 05:34:32 -0800
On Thursday, March 14, 2002, at 03:37 AM, Oscar Morales Vivs wrote:
On Thursday, March 14, 2002, at 02:47 AM, Erik M. Buck wrote:
Draw directly into a suitably configured bitmap image rep
See NSImageRep and -lockFocus
You can also just allocate a buffer for your image. Draw into the
buffer
using standard pointer math to set pixel data. Then initialize an
image rep
from your data.
Hmmm... the raw pixel matrix idea attracts me, as it's not particularly
complex and is directly multiplatform code, just needing to create a
function for each platform to pass the pixel matrix to whatever the
platform uses. However, looking the documentation hasn't been too
helpful. I assume I should create a NSImage containing a
NSBitmapImageRep with an NSData object created from my pixel matrix,
and set the pixel width and height to be those of the view I'm drawing.
Unfortunately, as I've already said, apple's documentation is quite
scarce when it comes to initializing an NSImage with raw data, and
although I have a few ideas of how it could be done, I'm not really
sure.
I thought the documentation was pretty clear on this. It is true that
the init routines for NSBitmapImageRep that take NSData could go into a
little more detail about the accepted formats, but those routines expect
structured data (in TIFF format, for instance) so they are not want you
want, you need to use initWithBitmapDataPlanes:[yadayadayada:]
Take a look at my example code at
http://www.cocoadev.com/index.pl?MakingFractalsUsingBitmapImageRep
which draws into a bitmap to create an image of the Mandelbrot set.
So, if someone could post a snippet of code completing the following
declaration (which I assume isn't that hard) I'd be eternally grateful.
- (NSImage*)imageFromPixmap:(UInt32*)pixmapData width:(int)pixmapWidth
height:(int)pixmapHeight
{
}
Yes, it isn't that hard. Something like:
- (NSImage*)imageFromPixmap:(UInt32*)pixmapData width:(int)pixmapWidth
height:(int)pixmapHeight
{
NSBitmapImageRep* bitmap;
unsigned char* pixels;
NSImage* theImage;
bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL // Let the class allocate
it
pixelsWide: pixmapWidth
pixelsHigh: pixmapHeight
bitsPerSample: 8 // Each component is 8
bits (one byte)
samplesPerPixel: 4 // Number of components (R, G,
B, alpha)
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: 0 // 0 means: Let the class figure it out
bitsPerPixel: 0 // 0 means: Let the class
figure it out
];
pixels = [bitmap bitmapData];
memcpy(pixels, pixMapData, pixmapHeight* pixmapWidth*sizeof(UInt32));
theImage = [[NSImage alloc] initWithSize: imageSize];
[theImage addRepresentation: bitmap];
return theImage;
}
(I didn't compile this so there might be some mistakes.)
The only subtlety here is if you want the image rep to use your pixel
matrix directly, rather than copying it in, you can do that by
specifiying a non null argument for initWithBitmapDataPlanes (but you
can't specify your data pointer directly, this argument expexts an array
of pointers to your data planes; in your case the first array element
would be your data pointer and the others would be nil). Then the image
rep would access your data buffer directly rather than creating its own.
See the documentation for NSBitmapImageRep for details.
Also, in the code above you might need to autorelease the created
instance of NSBitmapImageRep since the NSImage probably retains it when
you add the representation. I'll let you work out the details.
- Dennis D.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.