title: NSImage from a 1D pixel array?
body: <p>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 <strong>double</strong>. At the moment, I am only concerned with monochrome images.</p>
  
  <p>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.</p>
  
  <p>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 <strong>-initWithData:(NSData*)</strong>. But I'm not 100% sure if this is what I need.</p>
  
  <p>Am I barking up the wrong tree here? Any pointers to a class or method which could handle this would be </p>
  
  <p>EDIT:</p>
  
  <p>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 <code>imageArray[i * width + j]</code>. This works perfectly in 8 bits (of course, I divide every pixel value by 256 to represent it in 8 bits).</p>
  
  <pre><code> 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&lt;height;i++)
      {
          for(j=0;j&lt;width;j++)
          {
              pix[i * rowBytes + j] = 0xFFFF;
          }
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    short* grey = (short*)malloc(width*height*sizeof(short));
      for(int i=0;i&lt;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)];
  </code></pre>
  
  <p>Any suggestions?</p>