Re: NSImage representations
Re: NSImage representations
- Subject: Re: NSImage representations
- From: Jeremy Rotsztain <email@hidden>
- Date: Sun, 8 Jul 2007 02:02:39 -0400
On Jul 7, 2007, at 5:18 PM, Dorian Johnson wrote:
setPixel is alright for some things, but it is *far* quicker to use
-[NSBitmapImageRep bitmapData] and modify it directly. Just keep
that in mind if you have a situation where you're working with
entire images or otherwise are manipulating many pixels.
OK, I found an example on the O'Reilly website documenting how to
turn a color image into a greyscale image using bitmapData.
I was planning to do something similar - quantizing the colors of an
image (to create an image with 1000 colors).
This code works on some images, but fails on others. Any thoughts on
what I could be doing incorrectly?
Thanks again,
Jeremy
- (NSImage *) quantizeImage: (NSImage *) myImage {
NSBitmapImageRep *paintImageRep = [ NSBitmapImageRep
imageRepWithData:[myImage TIFFRepresentation]];
NSBitmapImageRep *quantizeRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: nil // Nil pointer tells the kit to
allocate the pixel buffer for us.
pixelsWide: [paintImageRep pixelsWide]
pixelsHigh: [paintImageRep pixelsHigh]
bitsPerSample: 8
samplesPerPixel: 3
hasAlpha: NO
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: 0 // Passing zero means "you figure it out."
bitsPerPixel: 24]; // This must agree with bitsPerSample and
samplesPerPixel.
NSSize imageSize = [ myImage size ];
NSImage *quantizedImage = [[ NSImage alloc ] initWithSize:imageSize];
int x, y;
int imgWidth = imageSize.width;
int imgHeight = imageSize.height;
unsigned char *srcData = [ paintImageRep bitmapData ];
unsigned char *dstData = [ quantizeRep bitmapData ];
unsigned char *p1, *p2;
int n = [paintImageRep bitsPerPixel] / 8;
//int o = [quantizeRep bitsPerPixel] / 8;
for (x=0; x<imgHeight; x++){
for (y=0; y<imgWidth; y++) {
p1 = srcData + n * (y * imgWidth + x);
p2 = dstData + n * (y * imgWidth + x);
p2[0] = (unsigned char)rint(p1[0] / 25 * 25); // update red
p2[1] = (unsigned char)rint(p1[1] / 25 * 25); // update green
p2[2] = (unsigned char)rint(p1[2] / 25 * 25); // update blue
}
}
[quantizedImage addRepresentation:quantizeRep];
return quantizedImage;
}
_______________________________________________
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