我的程序中有一个大型一维动态数组,它表示磁盘上的 FITS 图像,即它保存图像的所有像素值。数组的类型是double。目前,我只关心单色图像。
由于 Cocoa 不直接支持 FITS 格式,因此我使用 CFITSIO 库读取图像。这是可行的 - 我可以根据需要操作数组并使用库将结果保存到磁盘。
但是,我现在想显示图像。我认为这是 NSImage 或 NSView 可以做的事情。但类引用似乎没有列出可以采用 C 数组并最终返回 NSImage 对象的方法。我发现的最接近的是-initWithData:(NSData*)。但我不能 100% 确定这是否是我所需要的。
我是不是找错树了?任何指向可以处理这个问题的类或方法的指针都是
EDIT:
这是更新后的代码。请注意,我将每个像素设置为 0xFFFF。这只会产生灰色图像。这当然只是一个测试。加载实际的 FITS 文件时,我将 0xFFFF 替换为imageArray[i * width + j]
。这在 8 位中完美运行(当然,我将每个像素值除以 256 以用 8 位表示)。
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;
}
}
}
我也尝试过直接使用Quartz2D。即使是 16 位,这也确实会产生正确的图像。但奇怪的是,数据数组将 0xFF 作为白色,而不是 0xFFFF。所以我仍然必须将所有内容除以 0xFF - 在此过程中丢失数据。 Quartz2D代码:
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)];
有什么建议么?