Search code examples
swiftmacosgpuimage

How to correctly pass GPUImage ExposureAdjustment "exposure" parameter in Swift?


I am struggling with GPUImage on Swift for a few days now, and I think it is time to ask for help or at least a clue.

What I am trying to do is adjust the exposure of an NSImage with GPUImage, but I have failed miserably on this so far.

My code works for filters like SmoothToonFilter, but I just seem unable to set the exposure parameter for the ExposureAdjustment filter... and I don't have a clue on how to fix this.

Here's what works with smoothSmoothToonFilter:

filteredImage = filteredImage?.filterWithOperation(GPUImage.SmoothToonFilter()

.. while this doesn’t:

filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment() )

It does not accept me to do this either:

filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment(5.0) )

Or this:

filteredImage = filteredImage?.filterWithOperation(GPUImage.ExposureAdjustment(exposure: 5.0) )

While ExposureAdjustment.swift in the GPUImage project clearly (to me, anyways) shows there is a way to set this…

public class ExposureAdjustment: BasicOperation {
    public var exposure:Float = 0.0 { didSet { uniformSettings["exposure"] = exposure } }
    
    public init() {
        super.init(fragmentShader:ExposureFragmentShader, numberOfInputs:1)
        
        ({exposure = 0.0})()
    }
}

I am aware this issue is fully the fault of my lack of knowledge, but I really tried solving this on my own up to the point of it driving me absolutely nuts... and I would really appreciate some help so I can move on with my Swift experiments... Thanks in advance!


Solution

  • This line: public var exposure:Float... denotes a property on the ExposureAdjustment class. You can set it by splitting out the initialization of the filter and setting it in a new statement. Like this:

    let exposureFilter = GPUImage.ExposureAdjustment()
    exposureFilter.exposure = 5.0
    filteredImage = filteredImage?.filterWithOperation(exposureFilter)