Code Snippet: Raw PixelBuffer Creation/Output

smokris's picture
QCImagePixelBuffer *pb =
    [[context imageManager]
        createPixelBufferWithFormat:[QCPixelFormat pixelFormatARGB8]
        pixelsWide:2560
        pixelsHigh:1600
        options:nil
    ];
 
[pb beginUpdatePixels:TRUE colorSpace:CGColorSpaceCreateDeviceRGB()];
// this next bit is of course wildly inefficient
int x,y,c;
for(y=0;y<1600;++y)
   for(x=0;x<2560;++x)
      for(c=0;c<4;++c)
         *((char *)([pb baseAddress]+[pb bytesPerRow]*y+x*4+c)) = x*4+y*2+c;
[pb endUpdatePixels];
 
QCImage *image=[[QCImage alloc] initWithQCImageBuffer:pb options:nil];
[outputImage setImageValue:image];

"context" is what is passed to the execute method. (Used to be QCGLContext in Tiger.)

cwright's picture
Not Safe

Doing math with void's is a bad idea nowadays. sizeof(void) can be 4 or 8. The release version of Xcode happily notifies the user of this indiscretion.

The easiest fix is to tell the compiler how to handle the sizes:

((char *)((char)[pb baseAddress]+[pb bytesPerRow]y+x4+c)) = x4+y2+c;