金属质感比原始图像暗

时间:2017-12-11 01:57:54

标签: ios swift macos image-processing metal

我正在为macOS中的一个小程序编写一些代码,以便使用Metal Performance Shaders进行图像处理。出于某种原因,下面的代码会生成一个看起来比原始图像暗很多的图像。

代码只需要纹理,在其上执行一点guassian模糊,然后将图像输出到MTKView。但我无法弄清楚为什么生成的图像太暗了。

import Cocoa
import Metal
import MetalKit
import CoreGraphics
import MetalPerformanceShaders


class ViewController: NSViewController, MTKViewDelegate {
@IBOutlet weak var imageView: MTKView!
override func viewDidLoad() {
    super.viewDidLoad()

    //Setup the Metal Pipeline
    let device = MTLCreateSystemDefaultDevice()!
    imageView.device = device
    imageView.framebufferOnly = false
    imageView.isPaused = true
    let commandQueue = device.makeCommandQueue()!
    let commandBuffer = commandQueue.makeCommandBuffer()!
    let gaussian = MPSImageGaussianBlur(device: device, sigma: 2)
    let data = imageData(name:"sample", type:"jpg")

    let inputTexture = try! MTKTextureLoader(device: device).newTexture(data: data, options: nil)

    gaussian.encode(commandBuffer: commandBuffer, sourceTexture: inputTexture, destinationTexture: (imageView.currentDrawable?.texture)!)
    commandBuffer.present(imageView.currentDrawable!)
    commandBuffer.commit()
}

func imageData(name: String, type: String) -> Data {
    let urlpath = Bundle.main.path(forResource: name, ofType: type)!
    let url = NSURL.fileURL(withPath: urlpath)
    var data : Data? = nil
    do{ try data = Data(contentsOf: url)}
    catch{print("Couldn't set data.")}
    return data!
}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {

}
func draw(in view: MTKView) {
}

}

我自己尝试做的是看看是否由于某种原因视图的像素格式不同,因为我的输入纹理是RGBA8UNorm_sRGBimageView.currentDrawable.texture是{ {1}}但是,MPS的所有示例都并不关心这种像素格式。

输入图片:enter image description here 奇怪的较暗输出图像:enter image description here

1 个答案:

答案 0 :(得分:4)

这种变暗通常意味着错误地设置了伽马校正。就像你在评论中说的那样,它可以通过选项

来修复
[MTKTextureLoader.Option.SRGB : false]
相关问题