title: Render pixel buffer with Cocoa
body: <p>I've searched through various Apple's docs and StackOverflow answers, but nothing really helped, still have a blank app's window. I'm trying to display the content of a pixel buffer in the NSWindow, to do that I've allocated a buffer:</p>
  
  <pre><code>UInt8* row = (UInt8 *) malloc(WINDOW_WIDTH * WINDOW_HEIGHT * bytes_per_pixel);
  
  UInt32 pitch = (WINDOW_WIDTH * bytes_per_pixel);
  
  // For each row
  for (UInt32 y = 0; y &lt; WINDOW_HEIGHT; ++y) {
    Pixel* pixel = (Pixel *) row;
    // For each pixel in a row
    for (UInt32 x = 0; x &lt; WINDOW_WIDTH; ++x) {
      *pixel++ = 0xFF000000;
    }
    row += pitch;
  }
  </code></pre>
  
  <p>This should prepare a buffer with red pixels. Then I'm creating a <code>NSBitmapImageRep</code>:</p>
  
  <pre><code>NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:(u8 *) row
                                                                               pixelsWide:WINDOW_WIDTH
                                                                               pixelsHigh:WINDOW_HEIGHT
                                                                            bitsPerSample:8
                                                                          samplesPerPixel:4
                                                                                 hasAlpha:YES
                                                                                 isPlanar:NO
                                                                           colorSpaceName:NSDeviceRGBColorSpace
                                                                              bytesPerRow:WINDOW_WIDTH * 4
                                                                             bitsPerPixel:32];
  </code></pre>
  
  <p>Which then converted into <code>NSImage</code>:</p>
  
  <pre><code>NSSize imageSize = NSMakeSize(CGImageGetWidth([imageRep CGImage]), CGImageGetHeight([imageRep CGImage]));
  NSImage *image = [[NSImage alloc] initWithSize:imageSize];
  [image addRepresentation:imageRep];
  </code></pre>
  
  <p>Then I'm configuring the view:</p>
  
  <pre><code>NSView *view = [window contentView];
  [view setWantsLayer: YES];
  [[view layer] setContents: image];
  </code></pre>
  
  <p>Sadly this doesn't give me the result I expect. </p>