渲染到 CVPixelBuffer 的 CIImage在 CIContext 帮助下改变颜色

问题描述

我从 IOSurface 创建了一个 CIImage。然后我将它渲染到 CVPixelBuffer。如果我跳过 CIImage 步骤,颜色很好,但是我打算使用 CIImage/CIContext 函数裁剪图像。然而问题是 CIImage/CIContext 步骤引入了颜色变化。查看示例图片,左边是预期的颜色,右边是我最终得到的颜色。

enter image description here

我很确定(虽然不确定)这与色彩空间有关,以及 iPhone 使用 .. 宽的事实? .. 色彩空间?但是,我在 CIContext 和渲染步骤中尝试了无数配置。例如:

CIContext *cicontext = [CIContext contextWithOptions:@{
    kCIContextWorkingColorSpace: (id)CGColorSpaceCreateWithName(kCGColorSpaceSRGB)
}];

[cicontext render:ciimage 
  toCVPixelBuffer:pixelBuffer 
           bounds:CGRectMake(0,1125,2436) 
       colorSpace:CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB)];

但我从来没有得到任何不同的结果。我的完整代码是:

CIImage *ciimage = [CIImage imageWithIOSurface:surface];
CIContext *cicontext = [CIContext context];
CVPixelBufferRef pixelBuffer = NULL;
NSDictionary *pixelAttributes = @{(id)kCVPixelBufferCGImageCompatibilityKey: (id)kCFBooleanTrue,(id)kCVPixelBufferBytesPerRowAlignmentKey: @(16),(id)kCVPixelBufferCGBitmapContextCompatibilityKey: (id)kCFBooleanTrue};
CVPixelBufferCreate(kCFAllocatorDefault,2436,kCVPixelFormatType_32BGRA,(__bridge CFDictionaryRef _Nullable)(pixelAttributes),&pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer,0);
[cicontext render:ciimage 
  toCVPixelBuffer:pixelBuffer 
           bounds:CGRectMake(0,2436) 
       colorSpace:CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB)];
[writerAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentTime];
CVPixelBufferUnlockBaseAddress(pixelBuffer,0);    
CVPixelBufferRelease(pixelBuffer);
CFRelease(surface);

以下是来自 CIImage、CIContext、CVPixelBuffer 的 NSLog,以防万一。我有一些配置,我将 CIContext CGColorSpace 设置为与 CIImage 相同,但输出没有任何变化。 CIImage:

<CIImage: 0x28191cbf0 extent [0 0 1125 2436]>
<CGColorSpace 0x283f5c240> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)

CI上下文

<CIContext: 0x28191dff0 (metal 24) MTLDevice=0x104738000>
    priority: default
    workingSpace: <CGColorSpace 0x283f71500> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB Linear)
    workingFormat: RGBAh
    downsampleQuality: Low

像素缓冲区

<CVPixelBuffer 0x282a7b2a0 width=1125 height=2436 bytesPerRow=4544 pixelFormat=BGRA iosurface=0x0 attributes={
    BytesPerRowAlignment = 16;
    CGBitmapContextCompatibility = 1;
    CGImageCompatibility = 1;
    PixelFormatDescription =     {
        BitsPerBlock = 32;
        BitsPerComponent = 8;
        BlackBlock = {length = 4,bytes = 0x000000ff};
        CGBitmapContextCompatibility = 1;
        CGBitmapInfo = 8196;
        CGImageCompatibility = 1;
        ComponentRange = FullRange;
        ContainsAlpha = 1;
        ContainsGrayscale = 0;
        ContainsRGB = 1;
        ContainsYCbCr = 0;
        FillExtendedPixelsCallback = {length = 24,bytes = 0x00000000000000008cfcbea8010000000000000000000000};
        IOSurfaceCoreAnimationCompatibility = 1;
        IOSurfaceOpenGLESFBOCompatibility = 1;
        IOSurfaceOpenGLESTextureCompatibility = 1;
        OpenGLESCompatibility = 1;
        PixelFormat = 1111970369;
    };
} propagatedAttachments={
} nonPropagatedAttac

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...