Re: Drawing 32-bit images into an NSBitmapImageRep
Re: Drawing 32-bit images into an NSBitmapImageRep
- Subject: Re: Drawing 32-bit images into an NSBitmapImageRep
- From: "Bruce Johnson" <email@hidden>
- Date: Sat, 16 Sep 2006 23:17:42 -0700
I haven't had much luck using NSImage or NSBitmapImageRep to directly
deal with anything other than 8-bit image data on an elegant, clean
way.
I've had to go down to Quartz and CoreGraphics to gain this
functionality. In my example I'm using the vImage_Buffer from the
Accelerate frameworks, this buffer contains floating point (128) ARGB
data.
the pseudo-code: (your mileage may vary...)
CGDataProviderRef dataProviderRef =
CGDataProviderCreateWithData (NULL, (void *)vImg->data,
(vImg->rowBytes * vImg->height), NULL);
CGColorSpaceRef = colorSpace =
CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGImageRef cgImage =
CGImageCreate (vImg->width, vImg->height, bpp, (size_t)(bpp *
channels), vImg->rowBytes, colorSpace, kCGBitmapFloatComponents,
dataProviderRef, NULL, TRUE, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProviderRef);
CGColorSpaceRelease(colorSpace);
// so now we have a CGImageRef of our floating point data. I think
that from here you
// can get to CIImage (I'm not sure, but if you need the BitmapImageRep...)
NSBitmapImageRep *imageRep;
NSMutableData* imageData = [NSMutableData data];
CGImageDestinationRef destCG =
CGImageDestinationCreateWithData((CFMutableDataRef)imageData,
kUTTypeTIFF, 1, NULL);
CGImageDestinationAddImage(destCG, cgImage, NULL);
CGImageDestinationFinalize(destCG);
imageRep = [[[NSBitmapImageRep alloc] initWithData: imageData] autorelease];
CGImageRelease(cgImage);
CFRelease(destCG);
Now you have a bitmapimagerep that contains the floating point data.
If your image is large, the bitmapimagerep conversion will take some
time.
On 9/16/06, Julian Blow <email@hidden> wrote:
I'm having problems manipulating 32-bit graphics images with
NSBitmapImageRep and Core Image. My ultimate aim is to rotate the
image using the CIAffineTransform filter, but I'm having problems
just creating the image. The guts of the problem lie here:
NSSize newImageSize; // holds the size in pixels of the image to
be created
NSBitmapImageRep* oldImage; // holds an existing 128-bit image
NSBitmapImageRep* newBitmap=[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL // automatically allocates
memory
pixelsWide:newImageSize.width
pixelsHigh:newImageSize.height
bitsPerSample:32
samplesPerPixel:4 // RGBA is required for 32-bit
precision graphics
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSFloatingPointSamplesBitmapFormat; // alpha
last, premultiplied, floating point
bytesPerRow:newImageSize.width*16
bitsPerPixel:128];
CIContext* newCIContext=[[NSGraphicsContext
graphicsContextWithBitmapImageRep:newBitmap] CIContext];
CIImage* theCIImage=[[[CIImage alloc]
initWithBitmapImageRep:oldImage] autorelease];
[newCIContext drawImage:theCIImage atPoint:CGPointZero
fromRect:CGRectMake(0.0,0.0,newImageSize.width,newImageSize.height)];
When I draw the image, I get the following error message in the Console:
CGBitmapContextCreate: unsupported parameter combination: 32 integer
bits/component; 128 bits/pixel; 3-component colorspace;
kCGImageAlphaPremultipliedLast.
So Quartz seems to think I'm trying to make a 32-bit integer bitmap
rather than a 32-bit float bitmap. But I told NSBitmapImageRep the
format was NSFloatingPointSamplesBitmapFormat. If I replace
everything with the relevant numbers for a normal 8-bit (integer)
bitmap (bps 8, spp 4, bpp 32) it works fine.
Can anyone suggest what I'm doing wrong?
Thanks in advance,
Julian Blow
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
--
----
Bruce Johnson
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden