CIColorMap doesn't apply gradient uniformly to input image

[亡魂溺海] 提交于 2019-12-11 02:23:43

问题


I created a CIColorMap filter by loading the attached blue-to-red gradient image.

Then I tried to apply this to a gray scale input image, which is simply a linear gradient from black to white.

When I draw the out image of CIColorMap in a CIContext, the intention is to render black color in dark blue, white color in dark red, and mid gray in white. However the actual result looks like:

As you can see, the mid gray is actually mapped to light blue, instead of white as I have expected.

Below is the source code. I wonder if I have been using CIColorMap incorrectly given the very scarce documentation.

// Interface file
#import <Cocoa/Cocoa.h>
@interface ColorMapView : NSView
@end

// Implementation file
#import <QuartzCore/QuartzCore.h>
#import "ColorMapView.h"

static size_t imageWitdh = 512;
static size_t imageHeight = 125;

@implementation ColorMapView {
    CIImage *m_backgroundImage;
    CIImage *m_colorMapGradient;
    CIFilter *m_colorMapFilter;
    CIContext *m_ciContext;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self initBackgroundImage];
        [self initColorMapGradient];
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    if (!m_ciContext)  {
        m_ciContext = [CIContext contextWithCGContext:[NSGraphicsContext currentContext].graphicsPort options:nil];
    }

    CGFloat x = (self.bounds.size.width - imageWitdh) / 2;
    CGFloat y = (self.bounds.size.height - imageHeight) / 2;

    // Drawing code here.
    [m_colorMapFilter setValue:m_backgroundImage forKey:kCIInputImageKey];
    [m_ciContext drawImage:[m_colorMapFilter valueForKey:kCIOutputImageKey] inRect:CGRectMake(x, y, imageWitdh, imageHeight) fromRect:m_backgroundImage.extent];
}

- (void)initBackgroundImage {
     CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
    CGContextRef context = CGBitmapContextCreate(NULL, imageWitdh, imageHeight, 8, imageWitdh, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CGFloat locations[2] = {0.0, 1.0};
    CGFloat components[4] = {0.0, 1.0, 1.0, 1.0};
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(imageWitdh, 0), NO);
    CGImageRef gradientImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    m_backgroundImage = [CIImage imageWithCGImage:gradientImage];
    CGImageRelease(gradientImage);
}

- (void)initColorMapGradient {
    NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"blue_red_scale_gradient" ofType:@"png"];
    CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename(imagePath.UTF8String);
    CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);
    m_colorMapGradient = [CIImage imageWithCGImage:image];
    CGImageRelease(image);
    m_colorMapFilter = [CIFilter filterWithName:@"CIColorMap"];
    [m_colorMapFilter setValue:m_colorMapGradient forKey:@"inputGradientImage"];
}

@end

回答1:


It seems this is related to color management issue. If I set kCIContextWorkingColorSpace to NSNull when creating CIContext object, I will get expected result.

if (!m_ciContext)  {
    NSDictionary *options = @{kCIContextWorkingColorSpace : [NSNull null]};
    m_ciContext = [CIContext contextWithCGContext:[NSGraphicsContext currentContext].graphicsPort options:options];
}


来源:https://stackoverflow.com/questions/29996325/cicolormap-doesnt-apply-gradient-uniformly-to-input-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!