Skip to content

Instantly share code, notes, and snippets.

@ashfurrow
Created February 28, 2012 22:39
Show Gist options
  • Save ashfurrow/1935770 to your computer and use it in GitHub Desktop.
Save ashfurrow/1935770 to your computer and use it in GitHub Desktop.
CIImage filter to invert image colours
CIImage* ciImage = [[CIImage alloc] initWithData:[drawnImage TIFFRepresentation]];
if ([drawnImage isFlipped])
{
CGRect cgRect = [ciImage extent];
CGAffineTransform transform;
transform = CGAffineTransformMakeTranslation(0.0,cgRect.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
ciImage = [ciImage imageByApplyingTransform:transform];
}
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:ciImage forKey:@"inputImage"];
CIImage* output = [filter valueForKey:@"outputImage"];
NSRect centeredRect = NSMakeRect(0, 0, 0, 0);
centeredRect = NSMakeRect(0, 0, [drawnImage size].width, [drawnImage size].height);
// align left if we have a title
if(self.attributedTitle) {
centeredRect.origin.x = 2;
}
else
centeredRect.origin.x = NSMidX([self bounds]) - ([drawnImage size].width / 2);
centeredRect.origin.y = NSMidY([self bounds]) - ([drawnImage size].height / 2) - 1;
centeredRect = NSIntegralRect(centeredRect);
[output drawInRect:centeredRect fromRect:NSRectFromCGRect([output extent]) operation:NSCompositeSourceOut fraction:0.75f];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment