Grayscale NSImage?
Grayscale NSImage?
- Subject: Grayscale NSImage?
- From: Colin Cornaby <email@hidden>
- Date: Wed, 11 Feb 2004 20:31:42 -0800
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
imageRepWith
Data:[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.