Copying raw RGBA image data to NSBitmapImageRep coming out funny
Copying raw RGBA image data to NSBitmapImageRep coming out funny
- Subject: Copying raw RGBA image data to NSBitmapImageRep coming out funny
- From: "E. Wing" <email@hidden>
- Date: Tue, 9 Jan 2007 21:43:28 -0800
I'm trying to copy raw image data into an NSImage so I may use it
within Cocoa. To start with, I want to pass it to the Printer (or PDF
writer) or pass it to the Pasteboard. But when I do this, my image
data looks strange, maybe over-saturated or quantized. I've posted
screenshots here.
http://www.geocities.com/ewing2121/FountainActual.png
http://www.geocities.com/ewing2121/FountainPasteboard.png
http://www.geocities.com/ewing2121/FountainPDF.png
The first picture is the actual program. The second picture is from
the Pasteboard, and the last picture is what I see in Preview (after
calling print).
My data comes from OpenGL and I essentially use a glReadPixels with
GL_RGBA and GL_UNSIGNED_BYTE. To make sure it is not a glReadPixels
problem, I wrote a file to disk and the image came out okay. So I know
my problem has something to do with the way I am getting the data into
a NSImage.
Can somebody tell me what I'm doing wrong? (Also, I noticed that my
pasteboard image is inverted even though I set isFlipped:YES. Is the
only way to fix this to manually invert my raw OpenGL data?) Below is
an excerpt of my code.
Thanks,
Eric
// This block of code is called when not rendering to the screen (e.g. printing)
if(![[NSGraphicsContext currentContext] isDrawingToScreen])
{
...
// wrapper around glReadPixels
osg_image->readPixels(0, 0, viewport_width, viewport_height,
GL_RGBA, GL_UNSIGNED_BYTE);
// I probably misunderstood one or some of the values here
NSBitmapImageRep* image_rep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:viewport_width
pixelsHigh:viewport_height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:viewport_width*4
bitsPerPixel:32] autorelease];
// Copy the data into the NSBitmapImageRep so my osg_image can be cleaned up
memcpy([image_rep bitmapData], osg_image->data(),
osg_image->getTotalSizeInBytes());
NSImage* ns_image = [[[NSImage alloc]
initWithSize:NSMakeSize(viewport_width,viewport_height)] autorelease];
[ns_image addRepresentation:image_rep];
[ns_image setFlipped:YES];
// Copy to pasteboard
NSPasteboard* pb = [NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:nil];
[pb setData:[ns_image TIFFRepresentation] forType:NSTIFFPboardType];
// Render to Printer
[ns_image drawAtPoint:NSMakePoint(0.0, 0.0)
fromRect: NSMakeRect(0.0, 0.0, viewport_width, viewport_height)
operation: NSCompositeCopy
fraction: 1.0];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden