• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
filling NSImage with binary data from NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

filling NSImage with binary data from NSData


  • Subject: filling NSImage with binary data from NSData
  • From: "marc joanisse" <email@hidden>
  • Date: Sun, 7 May 2006 15:37:26 -0400

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];
  [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.

Thanks for whatever help you can give me on this,

-Marc-

--
Marc Joanisse
"... and such small quantities."
_______________________________________________
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


  • Follow-Ups:
    • Re: filling NSImage with binary data from NSData
      • From: "Greg Herlihy" <email@hidden>
    • Re: filling NSImage with binary data from NSData
      • From: Uli Kusterer <email@hidden>
    • Re: filling NSImage with binary data from NSData
      • From: Ricky Sharp <email@hidden>
  • Prev by Date: Re: object type testing
  • Next by Date: Re: filling NSImage with binary data from NSData
  • Previous by thread: DXT1/3/5 image format - where to get a NSImageRep
  • Next by thread: Re: filling NSImage with binary data from NSData
  • Index(es):
    • Date
    • Thread