简体   繁体   English

宽颜色范围的位图上下文

[英]Bitmap context for wide color range

I am trying to create an image mask with kCGColorSpaceDisplayP3 colorspace to support the iPhone 7's wide color range.我正在尝试使用 kCGColorSpaceDisplayP3 色彩空间创建一个图像蒙版,以支持 iPhone 7 的广泛色彩范围。

I am able to create image mask correctly when using sRGB colorspace on iPhone 6 and earlier devices using iOS 10 and earlier iOS.在使用 iOS 10 和更早版本 iOS 的 iPhone 6 和更早设备上使用 sRGB 色彩空间时,我能够正确创建图像蒙版。 But I have no clue where I am going wrong when creating colorspace using kCGColorSpaceDisplayP3:但是我不知道在使用 kCGColorSpaceDisplayP3 创建颜色空间时我哪里出错了:

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);

CGContextRef context = CGBitmapContextCreate(NULL, 320.0, 320.0, 32, 320.0*16, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapFloatComponents);

CGFloat radius = 10.0;
CGFloat components[] = {1.0,1.0,1.0,1.0,   1.0,1.0,1.0,1.0,    1.0,1.0,1.0,1.0,     1.0,1.0,1.0,1.0,    1.0,1.0,1.0,0.5,    1.0,1.0,1.0,0.0};
CGFloat locations[] = {0.0, 0.1, 0.2, 0.8, 0.9, 1.0};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 6); //colorSpaceP3
CGPoint center = CGPointMake(100.0, 100.0);
CGContextDrawRadialGradient(context, gradient, center, 0.1, center, radius, 0);

CGGradientRelease(gradient);

CGImageRef imageHole = CGBitmapContextCreateImage(context);
CGImageRef maskHole = CGImageMaskCreate(CGImageGetWidth(imageHole), CGImageGetHeight(imageHole), CGImageGetBitsPerComponent(imageHole), CGImageGetBitsPerPixel(imageHole), CGImageGetBytesPerRow(imageHole), CGImageGetDataProvider(imageHole), NULL, FALSE);

CGImageRelease(imageHole);

CGImageRef image = [UIImage imageNamed:@"prosbo_hires.jpg"].CGImage;
CGImageRef masked = CGImageCreateWithMask(image, maskHole);

CGImageRelease(maskHole);
UIImage *img = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

The log says:日志说:

: CGImageMaskCreate: invalid mask bits/component: 32. :CGImageMaskCreate:无效的掩码位/组件:32。

I don't have much experience with Core Graphics.我对核心图形没有太多经验。 Can anyone please suggest something here.任何人都可以在这里提出建议。

Thanks.谢谢。

The documentation for the bitsPerComponent parameter of CGImageMaskCreate() says: CGImageMaskCreate()bitsPerComponent参数的CGImageMaskCreate()说:

Image masks must be 1, 2, 4, or 8 bits per component.每个组件的图像掩码必须为 1、2、4 或 8 位。

You're passing CGImageGetBitsPerComponent(imageHole) , which is 32 bits per component.您正在传递CGImageGetBitsPerComponent(imageHole) ,即每个组件 32 位。 As per both the documentation and the log message, that's not valid.根据文档和日志消息,这是无效的。

The implication is that image masks don't support floating point bitmap formats.这意味着图像掩码不支持浮点位图格式。

It should be possible to create the bitmap context and the mask using 8 bits per component.应该可以使用每个组件 8 位创建位图上下文和掩码。 More or less, just leave out kCGBitmapFloatComponents .或多或少,只需省略kCGBitmapFloatComponents I expect that will result in reduced granularity of the opacity of the mask, but won't affect the color range of masked images.我预计这会导致蒙版不透明度的粒度降低,但不会影响蒙版图像的颜色范围。

this fixed my issue:这解决了我的问题:

contextRef = CGBitmapContextCreate(
    m.data,
    m.cols,
    m.rows,
    8,
    m.step[0], 
    CGColorSpaceCreateDeviceRGB(),
    bitmapInfo);

https://developer.apple.com/search/?q=CGColorSpaceCreate https://developer.apple.com/search/?q=CGColorSpaceCreate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM