Re: Create an NSImage using pixel (RGB) data
Re: Create an NSImage using pixel (RGB) data
- Subject: Re: Create an NSImage using pixel (RGB) data
- From: Simon Raisin <email@hidden>
- Date: Tue, 25 May 2010 12:03:01 -0400
This is what I came up with - I hope it helps someone.
#import <Foundation/Foundation.h>
typedef struct
{
unsigned char red;
unsigned char green;
unsigned char blue;
} HL_RGB;
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSSize imageSize = { 100, 100 };
NSBitmapImageRep* bitmapImageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:(unsigned
char **)NULL
pixelsWide:(int)imageSize.width
pixelsHigh:(int)imageSize.height
bitsPerSample:(int)8
samplesPerPixel:(int)3
hasAlpha:(BOOL)NO
isPlanar:(BOOL)NO
colorSpaceName:(NSString *)
NSDeviceRGBColorSpace
bytesPerRow:(int)(imageSize.width *
sizeof(HL_RGB))
bitsPerPixel:(int)0];
HL_RGB* pOutputPixel = (HL_RGB*)[bitmapImageRep bitmapData];
// Draw a red diagonal line
for (NSUInteger row = 0; row < imageSize.width; row++)
{
for (NSUInteger col = 0; col < imageSize.height; col++)
{
if (row == col)
{
pOutputPixel[row * (int)imageSize.width + col].red = 0xFF;
}
}
}
// Write the data out to disk - no compression
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber
numberWithFloat:1.0]
forKey:
NSImageCompressionFactor];
NSData* outputImageData = [bitmapImageRep representationUsingType:
NSJPEGFileType properties:imageProps];
[outputImageData writeToFile:@"output.jpg" atomically:NO];
[bitmapImageRep release];
[pool drain];
return 0;
}
On Tue, May 25, 2010 at 8:24 AM, Simon Raisin <email@hidden> wrote:
> Hi,
>
> I would like to create an new NSImage (of a specified size) by modifying
> its pixel data directly. I'm assuming that I have to create/provide a
> representation then call [rep bitmapData], but I've been unable to come up
> with a working solution thus far.
>
> Does anyone know of an example I might look at?
>
> Thanks in advance,
> Cat
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden