I have a large 1D dynamic array in my program that represents a FITS image on disk i.e. it holds all the pixel values of the image. The type of the array is double. At the moment, I am only concerned with monochrome images.
Since Cocoa does not support the FITS format directly, I am reading in the images using the CFITSIO library. This works - I can manipulate the array as I wish and save the result to disk using the library.
However, I now want to display the image. I presume this is something NSImage or NSView can do. But the class references don't seem to list a method which could take a C array and ultimately return an NSImage object. The closest I found was -initWithData:(NSData*). But I'm not 100% sure if this is what I need.
Am I barking up the wrong tree here? Any pointers to a class or method which could handle this would be
EDIT:
Here's the updated code. Note that I'm setting every pixel to 0xFFFF. This only results in a grey image.This is ofcourse just a test. When loading the actual FITS file, I replace 0xFFFF with imageArray[i * width + j]
. This works perfectly in 8 bits (of course, I divide every pixel value by 256 to represent it in 8 bits).
NSBitmapImageRep *greyRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:width
pixelsHigh:height
bitsPerSample:16
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:0
bitsPerPixel:16];
NSInteger rowBytes = [greyRep bytesPerRow];
unsigned short*pix = (unsigned short*)[greyRep bitmapData];
NSLog(@"Row Bytes: %d",rowBytes);
if(temp.bitPix == 16) // 16 bit image
{
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
pix[i * rowBytes + j] = 0xFFFF;
}
}
}
I also tried using Quartz2D directly. That does produce a proper image, even in 16 bits. But bizarrely, the data array takes 0xFF as white and not 0xFFFF. So I still have to divide everything by 0xFF - losing data in the process. Quartz2D code:
short* grey = (short*)malloc(width*height*sizeof(short));
for(int i=0;i<width*height; i++)
{
grey[i] = imageArray[i];
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext = CGBitmapContextCreate(grey, width, height, 16, width*2, colorSpace, kCGImageAlphaNone);
CFRelease(colorSpace);
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
NSImage *greyImage = [[NSImage alloc] initWithCGImage:cgImage size:NSMakeSize(width, height)];
Any suggestions?