Re: OpenGL printing/image
Re: OpenGL printing/image
- Subject: Re: OpenGL printing/image
- From: Charles Brokaw <email@hidden>
- Date: Wed, 15 Jan 2003 09:03:38 -0800
This works for me, but there may be an easier way:
Hope this helps.
- (NSData*)makeTIFFFromThisView{
unsigned char* planes[1];
unsigned int length;
NSImage* image;
NSBitmapImageRep* bitmap;
NSSize size; // these are float values.
NSMutableData* buffer;
size.width = _myglWidth;
size.height = _myglHeight;
length = (unsigned int)(size.height*size.width*3);
buffer= [NSMutableData dataWithLength :length];
glReadBuffer(GL_BACK);
glPixelStorei(GL_PACK_ALIGNMENT, 4); // force 4 byte alignment
glReadPixels(0, 0, _myglWidth, _myglHeight, GL_RGB,
GL_UNSIGNED_BYTE,
[buffer mutableBytes]);
planes[0] = (unsigned char*)[buffer mutableBytes]; // illegal
conversion from NSMutableData to unsigned int
bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes :planes
pixelsWide
:_myglWidth
pixelsHigh
:_myglHeight
bitsPerSample :8
samplesPerPixel :3
hasAlpha :NO
isPlanar :NO
colorSpaceName
:NSDeviceRGBColorSpace
bytesPerRow
:(_myglWidth*3)
bitsPerPixel :24];
image = [[NSImage alloc] initWithSize :size];
[image setFlipped :YES];
[image lockFocus];
[bitmap drawInRect :NSMakeRect(0, 0, size.width, size.height)];
[image unlockFocus];
return [image TIFFRepresentation];
}
- (void)savePanelDidEnd :(NSSavePanel*) sheet
returnCode :(int)returnCode
contextInfo :(void*)contextInfo{
NSData* viewTIFF;
if(returnCode ==0) return; // User did not click OK
[[self OGLview] setNeedsDisplay :YES]; // make sure view is up to
date
viewTIFF=[[self OGLview] makeTIFFFromThisView];
if([viewTIFF writeToFile :[sheet filename] atomically :NO]==NO){
NSRunAlertPanel(nil, @"Cannot save file '%@' : %s",nil,nil,nil,
[sheet filename], strerror(errno));
}
// [viewTIFF release];
}
On Wednesday, January 15, 2003, at 04:42 AM, Simon Bovet wrote:
Has anyone tried to print an NSOpenGLView, or make an NSImage from it?
It's surely easy, but if someone has a ready-made solution, it could
spare me some time!
So thanks for any help!
Simon
_______________________________________________
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.
_______________________________________________
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.