- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个之前从 png 文件创建的 UIImage:
let strokeUIImage = UIImage(data: pngData)
我想将 strokeImage(具有不透明度)转换为 MTLTexture
以便在 MTKView
中显示,但进行转换似乎执行了不需要的预乘,这会使所有内容变暗半透明边缘。
我的混合设置如下:
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
我试过两种转换方法:
let stampTexture = try! MTKTextureLoader(device: self.device!).newTexture(cgImage: strokeUIImage.cgImage!, options: nil)
以及更精细的 dataProvider 驱动方法:
let image = strokeUIImage.cgImage!
let imageWidth = image.width
let imageHeight = image.height
let bytesPerPixel:Int! = 4
let rowBytes = imageWidth * bytesPerPixel
let texDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm_srgb,
width: imageWidth,
height: imageHeight,
mipmapped: false)
guard let stampTexture = device!.makeTexture(descriptor: texDescriptor) else { return }
let srcData: CFData! = image.dataProvider?.data
let pixelData = CFDataGetBytePtr(srcData)
let region = MTLRegionMake2D(0, 0, imageWidth, imageHeight)
stampTexture.replace(region: region, mipmapLevel: 0, withBytes: pixelData!, bytesPerRow: Int(rowBytes))
两者都会产生相同的不需要的预乘结果。
我尝试了后者,因为有一些帖子表明旧的 swift3 方法 CGDataProviderCopyData()
从未预乘的图像中提取原始像素数据。可悲的是,等价物:
let srcData: CFData! = image.dataProvider?.data
似乎并不能解决问题。我错过了什么吗?
如有任何指点,我们将不胜感激。
最佳答案
经过多次试验,我找到了解决 CoreGraphics 图像中固有的预乘问题的解决方案。感谢 Warren 关于使用 Accelerate 函数(特别是 vImageUnpremultiplyData_ARGB8888
)的提示,我想,为什么不使用 vImage_CGImageFormat
构建 CGImage,这将允许我使用 bitmapInfo 设置指定如何解释 alpha...结果并不完美,如下图附件所示:
不知何故,在翻译中,alpha 值被稍微提高了,(可能是 rgb,但不是很明显)。顺便说一下,我应该指出 png 像素格式是 sRGB,我使用的 MTKView 设置为 MTLPixelFormat.rgba16Float
(应用要求)
下面是我实现的完整 metalDrawStrokeUIImage 例程。特别值得注意的是这一行:
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue)
这实际上是在不调用 vImageUnpremultiplyData_ARGB8888
的情况下取消关联 alpha(我认为)。查看生成的图像肯定看起来像未预乘的图像...
最后,为了在 MTKView 端取回预乘纹理,我让片段着色器处理预乘:
fragment float4 premult_fragment(VertexOut interpolated [[stage_in]],
texture2d<float> texture [[texture(0)]],
sampler sampler2D [[sampler(0)]]) {
float4 sampled = texture.sample(sampler2D, interpolated.texCoord);
// this fragment shader premultiplies incoming rgb with texture's alpha
return float4(sampled.r * sampled.a,
sampled.g * sampled.a,
sampled.b * sampled.a,
sampled.a );
} // end of premult_fragment
结果非常接近输入源,但图像可能比传入的 png 不透明 5%。同样,png 像素格式是 sRGB,我用来显示的 MTKView 设置为 MTLPixelFormat.rgba16Float
。所以,我确定某处有些东西变得糊涂了。如果有人有任何指示,我将不胜感激。
下面是相关代码的其余部分:
func metalDrawStrokeUIImage (strokeUIImage: UIImage, strokeBbox: CGRect) {
self.metalSetupRenderPipeline(compStyle: compMode.strokeCopy) // needed so stampTexture is not modified by fragmentFunction
let bytesPerPixel = 4
let bitsPerComponent = 8
let width = Int(strokeUIImage.size.width)
let height = Int(strokeUIImage.size.height)
let rowBytes = width * bytesPerPixel
//
let texDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm_srgb,
width: width,
height: height,
mipmapped: false)
guard let stampTexture = device!.makeTexture(descriptor: texDescriptor) else { return }
//let cgImage: CGImage = strokeUIImage.cgImage!
//let sourceColorSpace = cgImage.colorSpace else {
guard
let cgImage = strokeUIImage.cgImage,
let sourceColorSpace = cgImage.colorSpace else {
print("Unable to initialize cgImage or colorSpace.")
return
}
var format = vImage_CGImageFormat(
bitsPerComponent: UInt32(cgImage.bitsPerComponent),
bitsPerPixel: UInt32(cgImage.bitsPerPixel),
colorSpace: Unmanaged.passRetained(sourceColorSpace),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue),
version: 0, decode: nil,
renderingIntent: CGColorRenderingIntent.defaultIntent)
var sourceBuffer = vImage_Buffer()
defer {
free(sourceBuffer.data)
}
var error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, cgImage, numericCast(kvImageNoFlags))
guard error == kvImageNoError else {
print ("[MetalBrushStrokeView]: can't vImageBuffer_InitWithCGImage")
return
}
//vImagePremultiplyData_RGBA8888(&sourceBuffer, &sourceBuffer, numericCast(kvImageNoFlags))
// create a CGImage from vImage_Buffer
var destCGImage = vImageCreateCGImageFromBuffer(&sourceBuffer, &format, nil, nil, numericCast(kvImageNoFlags), &error)?.takeRetainedValue()
guard error == kvImageNoError else {
print ("[MetalBrushStrokeView]: can't vImageCreateCGImageFromBuffer")
return
}
let dstData: CFData = (destCGImage!.dataProvider!.data)!
let pixelData = CFDataGetBytePtr(dstData)
destCGImage = nil
let region = MTLRegionMake2D(0, 0, Int(width), Int(height))
stampTexture.replace(region: region, mipmapLevel: 0, withBytes: pixelData!, bytesPerRow: Int(rowBytes))
let stampColor = UIColor.white
let stampCorners = self.stampSetVerticesFromBbox(bbox: strokeBbox)
self.stampAppendToVertexBuffer(stampLayer: stampLayerMode.stampLayerFG, stampCorners: stampCorners, stampColor: stampColor)
self.metalRenderStampSingle(stampTexture: stampTexture)
self.initializeStampArray() // clears out the stamp array so we always draw 1 stamp at a time
} // end of func metalDrawStrokeUIImage (strokeUIImage: UIImage, strokeBbox: CGRect)
关于swift - 无需预乘即可将 CGImage 转换为 MTLTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56164586/
我正在尝试将 cgImage 从旧函数转换为新函数,但我不确定最后一个参数的参数应该是什么。基于旧函数的 param alphaType: 参数为 .alphaIsOne,新参数 param isOp
我尝试通过填写自己的数据来创建 UIImage。到目前为止一切正常。但是,如果我尝试多次调用此函数,它似乎会填满内存,直到应用程序崩溃。使用 VM Tracker,我发现当应用程序崩溃时,脏内存会增长
这是 Apple 的代码(来自技术问答 QA1702),用于从视频缓冲区获取 UIImage。不幸的是,返回的图像旋转了 90 度。如何编辑它以使返回的图像方向正确? - (UIImage *) im
使用标准图像格式(jpeg、gif、png 等)从文件加载 CGImage 或 NSImage 非常简单。 但是,我现在需要从使用 libfreetype 生成的内存中的字节数组创建 CGImage。
我正在使用 CIBloom 模糊 CGImage 中的一些文本,但发生的情况是 CIBloom 过滤器似乎使我的图像变得非常小 . 当滤波器内核变大时效果更差。我有点预料到这一点,但我需要一种方法来关
我有一个内存泄漏,我只是不知道如何解决。 这是泄露的代码: [newImg release]; CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDev
在 iOS 4.0 及更高版本中,有没有一种方法可以在不将整个图像加载到内存的情况下对 CGImage 进行分割?我试图做的是*以编程方式*分割图像,以便在使用大图像的 CATiledLayer 应用
在我的应用中,我想生成一个视频的多个缩略图,最好是质量好的缩略图。我的旧方法是执行一个循环 15 次,并在不同的时间复制一个 CGImage。如下图 func generateThumbnails(_
一种方法是: CGImageRef scaledImage(CGImageref, destination rect) { context = CGBitmapContextCreate(re
我有一个 UIView 子类,它呈现应用了 mask 的图像。它在所有设备(仅限 iPad)上都能完美运行,除了那些具有宽色域显示屏(最新的 iPad Pro)的设备,在这些设备上, mask 呈现完
我正在为 mac 开发一个类似放大镜的应用程序。我的目标是能够在放大时精确定位单个像素。我在 mouseMoved(with event: NSEvent) 中使用此代码: let captureSi
我希望将一个 CGImage 覆盖在另一个之上。 作为示例,第一个 CGImage 为 1024x768,并且想要在给定位置覆盖第二个 100x100 CGImage。 我已经了解了如何使用 NSIm
所以我一直在开发一个小的“绘图”应用程序,用户可以在网格 Canvas 上绘制图像(基本上创建像素艺术)。我想创建一个导出此 Canvas 的函数(将其视为 UIColor 数组,然后将其转换为 Ra
有没有一种方法可以在 iOS 中将原始图像数据存储和加载到磁盘上,以节省解压时间和内存并减少应用程序的脏内存占用? 保罗·哈达德已hinting to something like this我知道这项
我有一个手绘 View (用户可以用手指画线)。我只使用几种颜色,所以我有一个我写的压缩算法(想通过本地网络将它发送到另一台 iPad)但我似乎无法准确地从图形上下文中获取数据,即使使用这个简单的测试
我正在尝试编写一段代码来判断 CGPoint 是否位于图像的黑色区域内(成功的答案是 here )。在我的实现中,如果使用此代码触摸点为黑色,我可以成功返回 NSLog: -(void)touches
我正在通过网络连接接收一系列图像,并希望尽快显示它们,最高可达 30 FPS。我派生了一个 UIView 对象(因此我可以覆盖 drawRect)并公开了一个名为 cameraImage 的 UIIm
我有以下代码: UIImage *img = [UIImage imageNamed:@"BRBlueCircleMask"]; CGImageRef activeCirleMaskImage = i
对于物理缩放任何 UIImage 的方法,我需要创建位图上下文,它具有与图像完全相同的设置,除了目标宽度和高度。 到目前为止,这是我的代码。缺少什么:如何为 CGBitmapContextCreate
我有一个图像,其 RGB 分量是全白的,具有不同的 alpha -- 例如,RGBA 格式的 0xFFFFFF09。但是,当我使用 UIImage 或 CGImage API 加载此图像,然后在 CG
我是一名优秀的程序员,十分优秀!