Re: Unfathomable CIImage - Really get crazy about it
Re: Unfathomable CIImage - Really get crazy about it
- Subject: Re: Unfathomable CIImage - Really get crazy about it
- From: Erik Buck <email@hidden>
- Date: Sat, 3 Mar 2007 12:31:31 -0500
See http://developer.apple.com/samplecode/CIVideoDemoGL/listing7.html
for sample code to read back from a GL context (like the one
associated with a CIContext or CIImageBuffer) and save to a file.
or http://developer.apple.com/samplecode/QuartzComposerOffline/
listing1.html for creating an NSBitmapImageRep containing the
contents of a GL context (like the one associated with a CIContext or
CIImageBuffer).
or this code adapted/edited in Mail.app from Apple's samples:
- (NSBitmapImageRep*) bitmapImageRepPixelBuffer:(NSOpenGLPixelBuffer
*)pixelBuffer glContext:(NSOpenGLContext *)openGLContext
{
//IMPORTANT: We use the macros provided by <OpenGL/CGLMacro.h>
which provide better performances and allows us not to bother with
making sure the current context is valid
CGLContextObj cgl_ctx = [openGLContext CGLContextObj];
int width = [pixelBuffer pixelsWide],
height = [pixelBuffer pixelsHigh],
bitmapRowBytes = 4 * width;
NSBitmapImageRep* bitmapImageRep = nil;
GLint save;
int i;
//Read pixels back from the OpenGL pixel buffer in ARGB 32 bits
format - For extra safety, we save / restore the OpenGL states we change
glGetIntegerv(GL_PACK_ROW_LENGTH, &save);
glPixelStorei(GL_PACK_ROW_LENGTH, _scratchBufferRowBytes / 4);
glReadPixels(0, 0, width, height, GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV, _scratchBufferPtr);
glPixelStorei(GL_PACK_ROW_LENGTH, save);
if(glGetError())
{
return nil;
}
//User NSBitmapImageRep to allocate a memory buffer of ARGB 32
bits pixels - We use the "NSCalibratedRGBColorSpace" so that no color
profile is embedded in the bitmap
bitmapImageRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL pixelsWide:width pixelsHigh:height
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat bytesPerRow:bitmapRowBytes
bitsPerPixel:32] autorelease];
if(bitmapImageRep == nil)
{
return nil;
}
//Copy the pixels line by line from the scratch buffer to the
bitmap and flip vertically - OpenGL downloaded images are upside-down
for(i = 0; i < height; ++i)
{
bcopy(_scratchBufferPtr + i * _scratchBufferRowBytes, (char*)
[bitmapImageRep bitmapData] + (height - i - 1) * bitmapRowBytes,
bitmapRowBytes);
}
return bitmapImageRep;
}
_______________________________________________
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