Re: Grayscale NSImage?
Re: Grayscale NSImage?
- Subject: Re: Grayscale NSImage?
- From: Ryan Bates <email@hidden>
- Date: Wed, 11 Feb 2004 21:14:55 -0800
Have you seen the Monochrome Image example? It seems to handle the
Alpha channel properly. It shouldn't be too hard to adjust the code to
get a true grayscale:
<
http://developer.apple.com/samplecode/Sample_Code/Cocoa/
Monochrome_Image.htm>
Hope that helps.
Ryan
On Feb 11, 2004, at 8:31 PM, Colin Cornaby wrote:
Does anyone have example code for making an NSImage grayscale? The
example code I have doesn't play nice with alphas...
- (NSImage *)filterImage:(NSImage *)srcImage
{
NSBitmapImageRep *srcImageRep = [NSBitmapImageRep
imageRepWithData:[srcImage TIFFRepresentation]];
int w = [srcImageRep pixelsWide];
int h = [srcImageRep pixelsHigh];
int x, y;
NSImage *destImage = [[NSImage alloc] initWithSize:NSMakeSize(w,
h)];
NSBitmapImageRep *destImageRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:w
pixelsHigh:h
bitsPerSample:8
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:NULL
bitsPerPixel:NULL] autorelease];
unsigned char *srcData = [srcImageRep bitmapData];
unsigned char *destData = [destImageRep bitmapData];
unsigned char *p1, *p2;
int n = [srcImageRep bitsPerPixel] / 8;
for ( y = 0; y < h; y++ ) {
for ( x = 0; x < w; x++ ) {
p1 = srcData + 3 * (y * w + x);
p2 = destData + y * w + x;
*p2 = (char)rint((*p1 + *(p1 + 1) + *(p1 + 2)) / 3);
}
}
[destImage addRepresentation:destImageRep];
return destImage;
}
-----------------------
Colin Cornaby - Co-Founder, Carpe Stellarem. Duality and XGame
Projects Manager. http://www.carpestellarem.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.