• 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
Problem with converting an NSImage to grayscale
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem with converting an NSImage to grayscale


  • Subject: Problem with converting an NSImage to grayscale
  • From: "Arik Devens" <email@hidden>
  • Date: Mon, 29 Aug 2005 23:28:38 -0700 (PDT)

Hi,

I'm trying to write some code to covert arbitrary RGB and RGBA NSImage's
to grayscale. I've written a routine, which started out as the negative
image sample from ImageDifference but I'm running into some odd problems.

The code works on some images and not others, but I haven't been able to
figure out why some work and some don't. For the ones that don't I get a
totally skewed grayscale image, and even some of the ones that mostly work
don't completely work. They get almost to the bottom of the image and then
are just black. Any image works if it is 640 by 480.

Here is the code I'm using, I'm sure it's something simple that I'm just
missing but I can't seem to figure out what's going on:

- (NSBitmapImageRep *)grayscaleImageRep
{
    RGBPixel *pixels = (RGBPixel *)[self bitmapData];

    int row;
    int column;
    int widthInPixels;
    int heightInPixels;

    widthInPixels = [self pixelsWide];
    heightInPixels = [self pixelsHigh];

    NSBitmapImageRep *destImageRep = [[NSBitmapImageRep alloc]
                    initWithBitmapDataPlanes:nil
                                  pixelsWide:widthInPixels
                                  pixelsHigh:heightInPixels
                               bitsPerSample:8
                             samplesPerPixel:1
                                    hasAlpha:NO
                                    isPlanar:NO
                              colorSpaceName:NSCalibratedWhiteColorSpace
                                 bytesPerRow:0
                                bitsPerPixel:8];

    GreyPixel *greyPixels = (GreyPixel *)[destImageRep bitmapData];

    for (row = 0; row < heightInPixels; row++)
    {
        for (column = 0; column < widthInPixels; column++)
        {
            RGBPixel *thisPixel = (RGBPixel *) &(pixels[((widthInPixels *
row) + column)]);
            GreyPixel *thisGreyPixel = (GreyPixel *)
&(greyPixels[((widthInPixels * row) + column)]);

            *thisGreyPixel = (thisPixel->redByte + thisPixel->greenByte +
thisPixel->blueByte) / 3;
        }
    }

    return destImageRep;
}

Thanks,
Arik

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Problem with converting an NSImage to grayscale
      • From: "John C. Randolph" <email@hidden>
  • Prev by Date: Re: Pausing and terminating a thread
  • Next by Date: Re: Pausing and terminating a thread
  • Previous by thread: Re: Pausing and terminating a thread
  • Next by thread: Re: Problem with converting an NSImage to grayscale
  • Index(es):
    • Date
    • Thread