• 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
Pixel Editing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Pixel Editing


  • Subject: Pixel Editing
  • From: Chris Outwin <email@hidden>
  • Date: Wed, 26 Apr 2006 17:07:37 -0500

After scaling an image and using mouseDown to obtain a pixel location, I need to obtain the corresponding pixel location in the original image. I'd like to correct colors pixel-by-pixel.

My custom NSView is in a NSScrollView. Here's an abridged version of my code. The updated pixels are either too high or too low unless the mouse is in the middle of the image. Is there an example of this somewhere? Am I using the right approach? Thank you.

- (void)mouseDown:(NSEvent *)theEvent {
NSPoint pointInImage;

NSPoint viewPixelLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
[self lockFocus];
inputPixelColor = NSReadPixel(viewPixelLoc);
[self unlockFocus];

// Convert back to the pixel location in the original image.
float scale = [self imageScale];
NSRect visViewRect = [self visibleRect];
float visViewOriginX = visViewRect.origin.x / scale;
float visViewOriginY = visViewRect.origin.y / scale;
pointInImage.x = (viewPixelLoc.x / scale) + visViewOriginX;
pointInImage.y = (viewPixelLoc.y / scale) + visViewOriginY;

// If the control key is down during a mouse click, paint the pixel under the cursor.
if (flags & NSControlKeyMask) {
NSColor* color = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
[self setDestinationPixelColor:color];
}
// If the command key is down during a mouse click, paint the pixel under the cursor.
if (flags & NSCommandKeyMask) {
// Invert the transformation in scaleFrameBy to obtain the point in the original image to paint.
[[self image] setColor:[self destinationPixelColor] atX:pointInImage.x y:pointInImage.y];
[self setNeedsDisplay:YES];
}
}


- (void)scaleFrameBy:(float)scale {
	[self setImageScale:scale];
	NSSize imageSize = [[self image] size];
	NSAffineTransform* transform = [NSAffineTransform transform];
	[transform scaleBy:scale];
	[[self superview] setNeedsDisplayInRect:[self frame]];
    [self setFrameSize:[transform transformSize:imageSize]];
	[self setNeedsDisplay:YES];
}


_______________________________________________ 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
  • Prev by Date: Re: temporarily disabling undo
  • Next by Date: Re: NSTreeController and NSBrowser multiple selection
  • Previous by thread: Re: temporarily disabling undo
  • Next by thread: NSTextTable
  • Index(es):
    • Date
    • Thread