gpt4 book ai didi

cocoa - CIColorMap 没有对输入图像均匀应用渐变

转载 作者:行者123 更新时间:2023-12-03 16:35:28 25 4
gpt4 key购买 nike

我通过加载附加的蓝到红渐变图像创建了一个 CIColorMap 过滤器。

enter image description here

然后我尝试将其应用于灰度输入图像,这只是从黑到白的线性渐变。

enter image description here

当我在 CIContext 中绘制 CIColorMap 的输出图像时,目的是用深蓝色渲染黑色,用深红色渲染白色,用白色渲染中灰色。然而实际结果如下:

enter image description here

如您所见,中灰色实际上映射为浅蓝色,而不是我预期的白色。

下面是源代码。鉴于文档非常稀缺,我想知道我是否错误地使用了 CIColorMap。

// 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

最佳答案

这似乎与色彩管理问题有关。如果我在创建 CIContext 对象时将 kCIContextWorkingColorSpace 设置为 NSNull,我将得到预期的结果。

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

关于cocoa - CIColorMap 没有对输入图像均匀应用渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com