Macos 自定义核心图像过滤器生成未定义大小的图像-固定
我正在开发一个OSX应用程序,它使用定制的核心图像过滤器来实现一种特殊效果:将一幅图像的亮度设置为另一幅图像的alpha通道。有一些过滤器将一个图像用作另一个图像的遮罩,但它们需要第三个背景图像;我需要输出一个透明的部分,没有背景设置的图像 如前所述,我编写了内核代码并在QuartzComposer中进行了测试;它按预期工作 内核代码是:Macos 自定义核心图像过滤器生成未定义大小的图像-固定,macos,cocoa,core-image,cifilter,Macos,Cocoa,Core Image,Cifilter,我正在开发一个OSX应用程序,它使用定制的核心图像过滤器来实现一种特殊效果:将一幅图像的亮度设置为另一幅图像的alpha通道。有一些过滤器将一个图像用作另一个图像的遮罩,但它们需要第三个背景图像;我需要输出一个透明的部分,没有背景设置的图像 如前所述,我编写了内核代码并在QuartzComposer中进行了测试;它按预期工作 内核代码是: kernel vec4 setMask(sampler src, sampler mask) { vec4 color = sample(src, s
kernel vec4 setMask(sampler src, sampler mask)
{
vec4 color = sample(src, samplerCoord(src));
vec4 alpha = sample(mask, samplerCoord(mask));
color.a = alpha.r;
// (mask image is grayscale; any channel colour will do)
return color;
}
但是,当我尝试使用代码中的过滤器时(将其打包为图像单元或直接从应用程序源代码中),输出图像结果显示具有以下“未定义”(?)范围:
进一步处理(转换为NSImage位图表示、写入文件等)失败。过滤器本身完全加载(不是零),它生成的输出图像也不是零,只是有一个无效的rect
编辑:另外,我将导出的图像单元(插件)复制到/Library/Graphics/image Units
和~/Library/Graphics/image Units
,这样它就会出现在QuartzComposer的补丁库中,但当我将其连接到源图像和广告牌渲染器时,不会绘制任何内容(透明背景)
我错过什么了吗
EDIT:看起来像是我假设了很多关于-[CIFilter apply::
的默认行为
我的过滤器子类代码的-outputImage
实现如下:
- (CIImage*) outputImage
{
CISampler* src = [CISampler samplerWithImage:inputImage];
CISampler* mask = [CISampler samplerWithImage:inputMaskImage];
return [self apply:setMaskKernel, src, mask, nil];
}
所以我试着把它改成:
- (CIImage*) outputImage
{
CISampler* src = [CISampler samplerWithImage:inputImage];
CISampler* mask = [CISampler samplerWithImage:inputMaskImage];
CGRect extent = [inputImage extent];
NSDictionary* options = @{ kCIApplyOptionExtent: @[@(extent.origin.x),
@(extent.origin.y),
@(extent.size.width),
@(extent.size.height)],
kCIApplyOptionDefinition: @[@(extent.origin.x),
@(extent.origin.y),
@(extent.size.width),
@(extent.size.height)]
};
return [self apply:setMaskKernel arguments:@[src, mask] options:options];
}
…现在它成功了 你是怎么画的?您的
CIFilter
代码是什么样子的?调用apply:inoutputImage
时,很可能需要提供一个kCIApplyOptionDefinition
或者,您也可以使用
CIContext
的drawImage:inRect:fromRect
更改图像的绘制方式?您的CIFilter
代码是什么样子的?调用apply:inoutputImage
时,很可能需要提供一个kCIApplyOptionDefinition
或者,您也可以使用
CIContext
的drawImage:inRect:fromRect
更改图像的绘制方式。您可能需要设置过滤器的图像范围,例如?这正是我所做的(请参见编辑),谢谢!也许你需要为你的过滤器设置图像范围,比如?这正是我所做的(参见编辑),谢谢!我并没有绘制结果图像,只是将其保存到磁盘。但你的建议是正确的;我的筛选代码似乎缺少一些东西(我将对我的问题进行编辑)…在那里。再次感谢!我并没有绘制结果图像,只是将其保存到磁盘。但你的建议是正确的;我的筛选代码似乎缺少一些东西(我将对我的问题进行编辑)…在那里。再次感谢!
- (CIImage*) outputImage
{
CISampler* src = [CISampler samplerWithImage:inputImage];
CISampler* mask = [CISampler samplerWithImage:inputMaskImage];
CGRect extent = [inputImage extent];
NSDictionary* options = @{ kCIApplyOptionExtent: @[@(extent.origin.x),
@(extent.origin.y),
@(extent.size.width),
@(extent.size.height)],
kCIApplyOptionDefinition: @[@(extent.origin.x),
@(extent.origin.y),
@(extent.size.width),
@(extent.size.height)]
};
return [self apply:setMaskKernel arguments:@[src, mask] options:options];
}