Re: Convert GWorldPtr into a NSImage
Re: Convert GWorldPtr into a NSImage
- Subject: Re: Convert GWorldPtr into a NSImage
- From: Eric Gorr <email@hidden>
- Date: Wed, 7 Jan 2009 09:56:36 -0500
On Jan 6, 2009, at 6:42 PM, Graham Cox wrote:
When I tried to setup a NSBitmapImageRep, I tried
NSDeviceRGBColorSpace for the colorSpaceName which I believe
expects the pixelFormat for the GWorldPtr to be either RGBA or ARGB.
It should be straightforward to wrap an NSBitmapImageRep around some
of the GWorld formats. Not all of them are supported but ARGB 32-bit
is.
So, it is likely a problem that the pixelFormat (according to the
debugger) for the GWorldPtr is BGRA...?
You don't say what you're trying to do
I am trying to convert a GWorldPtr to a NSImage.
or what you've tried,
Here is some code I found (http://www.cocoadev.com/index.pl?NSImageFromAMovieFrame
). The code comes close to working, but when I draw the NSImage on my
window, what is drawn is looks like what I would expect except that
all of the colors are wrong - based on past experience such pictures
appear when the individual color components are mixed up. I noticed
that the pixelFormat of my GWorldPtr (well, actually the PixMapHandle
of the GWorldPtr) was BGRA and since that looked odd, I figured it was
a good possibility that it was the reason why it wasn't working.
The fields of the PixMapHandle from the GWorldPtr are:
rowBytes: -32496
bounds: (0, 0) (64, 64)
pmVersion: 1
packType: 0
packSize: 0
hRes: 4718592
vRes: 4718592
pixelType: 16
pixelSize: 32
cmpCount: 4
cmpSize: 8
pixelFormat: BGRA
I could, I suppose, process all of the individual components manually,
but was hoping for a simpler solution.
-----
PixMapHandle pixMapHandle = GetGWorldPixMap( aGWorld );
if ( LockPixels( pixMapHandle ) )
{
Rect portRect;
GetPortBounds( aGWorld, &portRect );
int pixels_wide = (portRect.right - portRect.left);
int pixels_high = (portRect.bottom - portRect.top);
int bps = 8;
int spp = 4;
BOOL has_alpha = YES;
NSBitmapImageRep *bitmap_rep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:pixels_wide
pixelsHigh:pixels_high
bitsPerSample:bps
samplesPerPixel:spp
hasAlpha:has_alpha
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:NULL
bitsPerPixel:NULL] autorelease];
CGColorSpaceRef dst_colorspaceref = CGColorSpaceCreateDeviceRGB();
CGImageAlphaInfo dst_alphainfo = has_alpha ?
kCGImageAlphaPremultipliedLast : kCGImageAlphaNone;
CGContextRef dst_contextref = CGBitmapContextCreate( [bitmap_rep
bitmapData],
pixels_wide,
pixels_high,
bps,
[bitmap_rep
bytesPerRow],
dst_colorspaceref,
dst_alphainfo );
void *pixBaseAddr = GetPixBaseAddr(pixMapHandle);
long pixmapRowBytes = GetPixRowBytes(pixMapHandle);
CGDataProviderRef dataproviderref =
CGDataProviderCreateWithData( NULL, pixBaseAddr, pixmapRowBytes *
pixels_high, NULL );
int src_bps = 8;
int src_spp = 4;
BOOL src_has_alpha = YES;
CGColorSpaceRef src_colorspaceref = CGColorSpaceCreateDeviceRGB();
CGImageAlphaInfo src_alphainfo = src_has_alpha ?
kCGImageAlphaPremultipliedFirst : kCGImageAlphaNone;
CGImageRef src_imageref = CGImageCreate( pixels_wide,
pixels_high,
src_bps,
src_bps * src_spp,
pixmapRowBytes,
src_colorspaceref,
src_alphainfo,
dataproviderref,
NULL,
NO, // shouldInterpolate
kCGRenderingIntentDefault );
CGRect rect = CGRectMake( 0, 0, pixels_wide, pixels_high );
CGContextDrawImage( dst_contextref, rect, src_imageref );
CGImageRelease( src_imageref );
CGColorSpaceRelease( src_colorspaceref );
CGDataProviderRelease( dataproviderref );
CGContextRelease( dst_contextref );
CGColorSpaceRelease( dst_colorspaceref );
UnlockPixels( pixMapHandle );
NSImage *image = [[[NSImage alloc]
initWithSize:NSMakeSize(pixels_wide, pixels_high)] autorelease];
[image addRepresentation:bitmap_rep];
return image;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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