views:

136

answers:

1

I have an int* of rgb data,width, height, and scanlinestride where i would like to create an NSImage.

i have looked around and found that i need to use NSData?

what is the ideal way of doing this?

+1  A: 

Use this NSBitmapImageRep method:

- (id)initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide:(NSInteger)width pixelsHigh:(NSInteger)height bitsPerSample:(NSInteger)bps samplesPerPixel:(NSInteger)spp hasAlpha:(BOOL)alpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormat bytesPerRow:(NSInteger)rowBytes bitsPerPixel:(NSInteger)pixelBits

Seriously. It’s easy to use though:

NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
   initWithBitmapDataPlanes:(unsigned char **)&bitmapArray
   pixelsWide:width pixelsHigh:height
   bitsPerSample:8
   samplesPerPixel:3  // or 4 with alpha
   hasAlpha:NO
   isPlanar:NO
   colorSpaceName:NSDeviceRGBColorSpace
   bitmapFormat:0
   bytesPerRow:0  // 0 == determine automatically
   bitsPerPixel:0];  // 0 == determine automatically

NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];

[image addRepresentation:bitmap];
Todd Yandell
Joe
Is it really `int*` data though? You need to use `unsigned char*` to get 8 bits per channel per pixel.
Rob Keniger
As far as I know NSBitmapImageRep only accepts chars. If you have ints, you’ll probably need to convert it a char array first. I may be wrong — I’ve only ever worked with image data as chars.
Todd Yandell
ah yes, im going to cast it as un unsigned char*.. i think that should work.
Joe