• 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
Re: NSImage representations
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSImage representations


  • Subject: Re: NSImage representations
  • From: Dorian Johnson <email@hidden>
  • Date: Sun, 8 Jul 2007 11:03:22 -0500

You're using integer division, which I don't think is what you want:

	p2[0] = (unsigned char)rint(p1[0] / 25.0 * 25); // update red
	p2[1] = (unsigned char)rint(p1[1] / 25.0 * 25); // update gree
	p2[2] = (unsigned char)rint(p1[2] / 25.0 * 25); // update blue

I don't exactly know what that's supposed to do, since dividing by 25 then multiplying by the same number is just * 1.


Since this is all in-place operations, I'd suggest something like:

NSBitmapImageRep *paintImageRep = [NSBitmapImageRep imageRepWithData: [myImage TIFFRepresentation]];

NSSize imageSize = NSMakeSize([paintImageRep pixelsWide], [paintImageRep pixelsHigh]);


unsigned char *data = [paintImageRep bitmapData], *p1;
int n = ([paintImageRep bitsPerPixel] + 7) / 8, x, y;

for (x=0; x<imageSize.width; x++
{
for (y=0; y<imageSize.height; y++)
{

p1 = data + n * (y * (int)imageSize.width + x);

p1[0] = p1[0]/2; // note this will simply darken the image, but I don't know what you actually want to do
p1[1] = p1[1]/2;
p1[2] = p1[2]/2;
}
}

NSImage *quantizedImage = [[NSImage alloc] initWithSize:imageSize];
[quantizedImage addRepresentation:paintImageRep];

return quantizedImage;



On Jul 8, 2007, at 1:02 AM, Jeremy Rotsztain wrote:

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


  • Follow-Ups:
    • Re: NSImage representations
      • From: Jeremy Rotsztain <email@hidden>
References: 
 >Fwd: NSImage representations (From: Jeremy Rotsztain <email@hidden>)
 >Re: NSImage representations (From: Dorian Johnson <email@hidden>)
 >Re: NSImage representations (From: Dorian Johnson <email@hidden>)
 >Re: NSImage representations (From: Jeremy Rotsztain <email@hidden>)

  • Prev by Date: Releasing WebView After Caching
  • Next by Date: Re: NSNumber increment
  • Previous by thread: Re: NSImage representations
  • Next by thread: Re: NSImage representations
  • Index(es):
    • Date
    • Thread