Apply a Gamma of 1.8 to an NSImage
I'm trying to convert an NSImage to have a a gamma of 1.8 and I'm not having much luck. I've found some code but nothing that's working. I开发者_开发知识库've got a method that returns an NSBitmapImageRep on an NSImage category.
First I tried changing the color space with a CGColorSpaceCreateCalibratedRGB function I found online, but this had no effect:
- (NSBitmapImageRep *)OnePointEightBitmapImageRep{
CGColorSpaceRef colorSpace = CGColorSpaceCreateCalibratedRGB(
(CGFloat[3]){0.9505, 1.0000, 1.0891},//white point
(CGFloat[3]){0.0000, 0.0000, 0.0000},//black point
(CGFloat[3]){1.8010, 1.8010, 1.8010},//gamma rgb
(CGFloat[9]){0.4543, 0.2426, 0.0148, 0.3533, 0.6744, 0.0904, 0.1566, 0.0834, 0.7195} //rgb tristimulus
);
CGImageRef imageRef = CGImageCreateCopyWithColorSpace([self CGImage], colorSpace);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return bitmap;
}
- (CGImageRef)CGImage{
return [self CGImageForProposedRect:nil context:nil hints:nil];
}
Next I tried setting the gamma on the bitmap image properties:
- (NSBitmapImageRep *)OnePointEightBitmapImageRep{
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:[self CGImage]];
NSLog(@"gamma: %@", [bitmap valueForProperty:NSImageGamma]);
[bitmap setProperty:NSImageGamma withValue:[NSNumber numberWithDouble:1.8f]];
NSLog(@"gamma: %@", [bitmap valueForProperty:NSImageGamma]);
return bitmap;
}
This set the property but had no effect on the output image.
Is there a way to do this?
From the docs:
The gamma value is a floating-point number between 0.0 and 1.0, with 0.0 being black and 1.0 being the maximum color.
As you can see, your value of 1.8 is outside this range. Note that setting this property sets the gamma for PNG images only, by writing a property to the PNG file. It doesn't actually change the pixel values in the image.
If you want to do that, you should use Core Image and apply the CIGammaAdjust
filter.
精彩评论