Re: filling NSImage with binary data from NSData
Re: filling NSImage with binary data from NSData
- Subject: Re: filling NSImage with binary data from NSData
- From: Ricky Sharp <email@hidden>
- Date: Sun, 7 May 2006 14:47:03 -0500
On May 7, 2006, at 2:37 PM, marc joanisse wrote:
Hi,
I'm working on a cocoa app that visualizes part of a 3D dataset (MRI
images) as a discrete 2D "slice". I am using NSImage to show the
image. The user chooses a slice of the array (a 256 x 256 grid) to
show in an NSImageView.
The hard part here is the data file format, which is a binary file
consisting of an 8-bit header followed by a 256*256*256 array of
bytes. Each byte specifies a grayscale value.
I have hucked together some code based on other postings and replies
on this list, but I've hit a dead end. Here's where I am at:
I load this data file into an NSData object (declared with global
scope) as follows
vmrData = [NSData dataWithContentsOfFile:fileName];
From what I can tell, this seems to do the right thing.
Then I trigger the following procedure that actually loads the desired
portion of the data into the NSImage using an NSBitmapImageRep:
- (void)drawXImage:(int)xSlice
{
NSBitmapImageRep* imageRep;
// a pointer to the portion of vmrData containing the desired slice
unsigned char *plane;
// pointer to the part of the dataset we want to visualize
// note vmrData has global scope
plane = (unsigned char*)[vmrData bytes]+8+(256 * 256 * xSlice);
// init the NSBitmapImageRep
imageRep=[[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:&plane
pixelsWide:256
pixelsHigh:256
bitsPerSample:8
samplesPerPixel:1
hasAlpha:NO
isPlanar:YES
colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:0
bitsPerPixel:0] autorelease];
// create an NSImage and show it in the window
//n.b. imageView is my delegate for the NSImageView I want to
// show the image in.
NSImage *producedNSImage;
producedNSImage = [NSImage alloc];
Here's at least one problem. You should be calling an appropriate
init method here. For example:
NSImage* producedNSImage = [[[NSImage alloc] initWithSize:NSMakeSize
(256, 256)] autorelease];
[producedNSImage addRepresentation:imageRep];
[imageView setImage:producedNSImage];
}
The procedure produces a segfault when I run it, but not till after it
returns. I suspect the problem ahs to do with how I am setting the
"plane" pointer or how I'm telling the NSBitmapImageRep about it.
As a sidenote: I am also unclear whether I should be releasing
producedNSImage and imageRep at the end of the procedure, or whether
the view needs this to be retained.
Both should be autoreleased; see code snippet above with the
initWithSize:
Finally, I've not yet worked with planar images, so there could be
additional bugs in what you have.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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