如何使用CPU而不是GPU来处理CIFilter?

发布于 2024-09-26 15:40:01 字数 99 浏览 1 评论 0原文

有谁知道如何告诉核心图像使用CPU而不是GPU通过CIFilter处理CIImage?我需要处理一些非常大的图像,但使用 GPU 得到了奇怪的结果。我不在乎CPU要花多长时间就可以了。

Does anyone know how to tell core image to process a CIImage through a CIFilter using the CPU instead of the GPU? I need to process some very large images and I get strange results using the GPU. I don't care how long it takes to CPU will be fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰火雁神 2024-10-03 15:40:01

kCIContextUseSoftwareRenderer 是这里的关键:

+ (CIContext*)coreContextFor:(NSGraphicsContext *)context forceSoftware:(BOOL)forceSoftware
{
    //CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSDictionary *contextOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (id)colorSpace, kCIContextWorkingColorSpace,
                                    (id)colorSpace, kCIContextOutputColorSpace,
                                    [NSNumber numberWithBool:forceSoftware], kCIContextUseSoftwareRenderer,
                                    nil];
    CIContext* result = [CIContext contextWithCGContext:(CGContext *)[context graphicsPort] options:contextOptions];
    CGColorSpaceRelease(colorSpace);
    return result;
}

在软件中渲染更多(CPU)解决了一些问题,但是......现代机器上的性能损失如此之大,我不能说这是解决方案。我在应用程序中使用 CoreImage,并且总是使用 GPU 在屏幕上进行渲染,而强制 CPU 仅用于保存。我注意到 CPU 渲染在我的测试硬件上更加准确,并且保存过滤后的图像是一个漫长的过程,我可以在这里牺牲速度。

kCIContextUseSoftwareRenderer is key here:

+ (CIContext*)coreContextFor:(NSGraphicsContext *)context forceSoftware:(BOOL)forceSoftware
{
    //CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSDictionary *contextOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (id)colorSpace, kCIContextWorkingColorSpace,
                                    (id)colorSpace, kCIContextOutputColorSpace,
                                    [NSNumber numberWithBool:forceSoftware], kCIContextUseSoftwareRenderer,
                                    nil];
    CIContext* result = [CIContext contextWithCGContext:(CGContext *)[context graphicsPort] options:contextOptions];
    CGColorSpaceRelease(colorSpace);
    return result;
}

Rendering in software more (CPU) solves some issues, but... Performance penalty is so strong on modern machines that I can't say it is solution. I use CoreImage in my apps and I always render with GPU on-screen while forced CPU is used for saving only. I have noticed that CPU rendering is a bit more accurate on my test hardwares, and saving filtered image is a long process, I can sacrifice the speed here.

握住你手 2024-10-03 15:40:01

这是一个 Swift 版本:

func coreContextFor(context : CGContext,forceSoftware force : Bool) -> CIContext {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let options : [String:AnyObject] = [
        kCIContextWorkingColorSpace: colorSpace!,
        kCIContextOutputColorSpace : colorSpace!,
        kCIContextUseSoftwareRenderer : NSNumber(bool: force)
    ]

    return CIContext(CGContext: context, options: options)
}

Here is a Swift version :

func coreContextFor(context : CGContext,forceSoftware force : Bool) -> CIContext {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let options : [String:AnyObject] = [
        kCIContextWorkingColorSpace: colorSpace!,
        kCIContextOutputColorSpace : colorSpace!,
        kCIContextUseSoftwareRenderer : NSNumber(bool: force)
    ]

    return CIContext(CGContext: context, options: options)
}
吃素的狼 2024-10-03 15:40:01

不是答案,但您始终可以将 CIkernel 重写为对像素 RGB 值进行操作的函数,然后在图像的 unsigned char [] 数据上循环应用该函数并将结果转换为 CIImage 。

Not an answer, but you always can re-write CIkernel as function which operates on pixel RGB value, and after that apply that function in a loop on unsigned char [] data of image and convert result to CIImage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文