首页 > 解决方案 > 渲染到 CVPixelBuffer 的 CIImage(在 CIContext 的帮助下)改变颜色

问题描述

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

在此处输入图像描述

我很确定(尽管不确定)它与色彩空间有关,而且 iPhone 使用 ..wide? .. 色彩空间?但是,我在 CIContext 和渲染步骤中尝试了无数种配置。例如:

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

[cicontext render:ciimage 
  toCVPixelBuffer:pixelBuffer 
           bounds:CGRectMake(0, 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, 
                    1125, 
                    2436, 
                    kCVPixelFormatType_32BGRA, 
                    (__bridge CFDictionaryRef _Nullable)(pixelAttributes),
                    &pixelBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
[cicontext render:ciimage 
  toCVPixelBuffer:pixelBuffer 
           bounds:CGRectMake(0, 0, 1125, 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

标签: iosobjective-cciimageiosurface

解决方案


推荐阅读