金属质感比原始图像暗 [英] Metal Texture is darker than original image

查看:112
本文介绍了金属质感比原始图像暗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing some code for a little program in macOS to play with image processing with Metal Performance Shaders. For some reason, the code below produces an image that looks significantly darker than the original.

该代码仅获取纹理,对其进行一点高斯模糊,然后将图像输出到MTKView.不过,我无法弄清为什么生成的图像太暗.

The code simply takes a texture, performs a little guassian blur on it, and then outputs the image to the MTKView. I cannot figure out why the resulting image is so dark, though.

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.textureBGRA8UNorm,但是MPS的所有示例并不真正在乎这种像素格式是什么.

What I did try to do on my own was to see if maybe for some reason the pixel format of the view being different mattered, as my input texture is a RGBA8UNorm_sRGB while imageView.currentDrawable.texture is a BGRA8UNorm, however all of the examples of MPS don't really care what this pixel format is.

输入图像: 奇怪的更暗的输出图像:

Input Image: Weird Darker Output Image:

推荐答案

这种变暗通常表示伽马校正设置错误.就像您在评论中所说的那样,可以通过选项对其进行修复

This kind of darkening usually means that gamma correction is set up wrongly. Like you said in a comment, it can be fixed by option

[MTKTextureLoader.Option.SRGB : false]

这篇关于金属质感比原始图像暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆