• 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: Getting colour data at specified point in an NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting colour data at specified point in an NSImage


  • Subject: Re: Getting colour data at specified point in an NSImage
  • From: Heinrich Giesen <email@hidden>
  • Date: Thu, 15 Mar 2007 16:41:46 +0100

Hello,

On 14.03.2007, at 01:28, Michael Watson wrote:

I'm creating an NSBitmapImageRep and then creating an NSImage. I add
the bitmap rep to the NSImage, lock focus on the bitmap rep, and then
fill it with a colour generated via NSColor. I then ask for a
component value from a point in the bitmap rep, but it always returns
0.0. What am I doing incorrectly?

Example code: ... code deleted

With a ... lockFocus ... draw ... unlockFocus you cannot direktly draw into
an NSBitmapImageRep. But it is possible to fill an NSBitmapImageRep using
+graphicsContextWithBitmapImageRep: (Tiger only). Using your (modified) code
it looks something like this:


    NSSize imageSize = NSMakeSize(800, 600);
    NSBitmapImageRep *bitmapRep =
    [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                  pixelsWide:imageSize.width
                  pixelsHigh:imageSize.height
                bitsPerSample:8
              samplesPerPixel:4
                     hasAlpha:YES
                     isPlanar:NO  // <--- important !
               colorSpaceName:NSCalibratedRGBColorSpace
                  bytesPerRow:0
                 bitsPerPixel:0 ];

// starting new code
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *context =
[NSGraphicsContext graphicsContextWithBitmapImageRep:bitmapRep];
[NSGraphicsContext setCurrentContext:context];
[[NSColor colorWithDeviceRed:0.2 green:0.3 blue:0.7 alpha:0.9] set];
//[[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] set];
NSRectFillUsingOperation(NSMakeRect(0, 0, imageSize.width,
imageSize.height), NSCompositeCopy);
[[NSColor redColor] set]; // adding a rect to see that (and how) it works
[NSBezierPath fillRect:NSMakeRect(50, 50, 200,100)];
[NSGraphicsContext restoreGraphicsState];


NSLog(@"%@", [bitmapRep colorAtX:10 y:10]);
// for a test only:
[[bitmapRep TIFFRepresentation] writeToFile:@"/tmp/tst.tiff" atomically:NO];


(As you see, no NSImage is needed.)

--
Heinrich Giesen
email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: Getting colour data at specified point in an NSImage
      • From: Michael Watson <email@hidden>
  • Prev by Date: Re: Reloading a window from NIB
  • Next by Date: Detecting currently active NSColorWell(s)?
  • Previous by thread: Re: Getting colour data at specified point in an NSImage
  • Next by thread: Re: Getting colour data at specified point in an NSImage
  • Index(es):
    • Date
    • Thread