Silence-GitHub / BBMetalImage

A high performance Swift library for GPU-accelerated image/video processing based on Metal.
MIT License
985 stars 126 forks source link

Live filtering with slider #78

Open agruchala opened 3 years ago

agruchala commented 3 years ago

Is there an option to change filter values in fly, with slider or other control?

Silence-GitHub commented 3 years ago

For camera and video, just change the filter value.

contrastFilter.contrast = slider.value

For static image, change the filter value and transmit texture.

contrastFilter.contrast = slider.value
imageSource.transmitTexture()
niraj547 commented 3 years ago

Hi It's not working, what I was doing wrong??

@objc func sliderValueDidChange(_ sender:UISlider!){
        let roundedStepValue = Float(round(sender.value / step) * step)
        sender.value = roundedStepValue

        var contrastFilter = BBMetalContrastFilter(contrast: roundedStepValue)
        contrastFilter.contrast = roundedStepValue

        if let filter = self.filter {
           camera.add(consumer: filter).add(consumer: filterView)
            filter.add(consumer: videoWriter)
            if let source = imageSource {
                camera.willTransmitTexture = { [weak self] _, _ in
                    guard self != nil else { return }
                    source.transmitTexture()
                }
                source.add(consumer: filter)
            }

        } else {
            camera.add(consumer: filterView)
            camera.willTransmitTexture = nil
            imageSource = nil
            camera.add(consumer: videoWriter)

        }

    }
Silence-GitHub commented 3 years ago

@niraj547 Hi, the problem is the contrastFilter (local variable) is not in the filter chain.

Set up filter chain before changing the slider value. When changing the slider value, do not change the filter chain. Please try the code below.

// Call this function before changing the slider value
func setupFilterChain() {
    if let filter = self.filter {
        camera.add(consumer: filter).add(consumer: filterView)
        ...
    } else {
        camera.add(consumer: filterView)
        ...
    }
}

@objc func sliderValueDidChange(_ sender:UISlider!) {
    self.filter.contrast = ...
}
niraj547 commented 3 years ago

Hi It doesn't work and also I think I have multiple filter buttons and each of them has a slider, doing this freeze the whole screen that might be the reason?

Also 1 quick question can we chain multiple filters in a single video recording or do I have to join all the video after each filter has been applied?? For example: filter1 applied 32sec + filter2 applied 32sec = final Video something like this

Sorry for asking a dumb question, I'm really new in ios Thanks