• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Grayscale NSImage?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
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.
  • Follow-Ups:
    • Re: Grayscale NSImage?
      • From: Ryan Bates <email@hidden>
  • Prev by Date: Custom control class question
  • Next by Date: Re: Type/Creator codes for RTFD Documents
  • Previous by thread: Re: Custom control class question
  • Next by thread: Re: Grayscale NSImage?
  • Index(es):
    • Date
    • Thread