How group of renderscript filters use for Adjustment with seekbar

144 Views Asked by At

I Want to like this with renderscript filters a example gif is given below

My problem In my code my code all renderscript filters running well but problem is when i am changing filter then resest the bitmap then update the filter value with bitmap .

class Filters(private val context: Context)   {
        private val TAG = BrightnessFilter::class.java.simpleName
    
    
        private var rs: RenderScript = RenderScript.create(context)
    
         private var brightnessScript: ScriptC_brightness = ScriptC_brightness(rs)
    
        private var contrastScript: ScriptC_contrast = ScriptC_contrast(rs)
    
        private var saturationFilter: ScriptC_saturation = ScriptC_saturation(rs)
    
        private var convolution: ScriptIntrinsicConvolve3x3 =
            ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs))
    
        private var vignetteFilter: ScriptC_vignette = ScriptC_vignette(rs)

  fun setBrightnessX(values: Float, inputImage: Bitmap): Bitmap {
        val outBitmap = Bitmap.createBitmap(inputImage)
        val tempIn = Allocation.createFromBitmap(rs, inputImage)
        val tempOut = Allocation.createFromBitmap(rs, outBitmap)

        brightnessScript.invoke_setBright(values)
        brightnessScript.forEach_brightness(tempIn, tempOut)

        tempOut.copyTo(outBitmap)
        return outBitmap

    }

    fun setContrastX(contrast: Float, inputImage: Bitmap): Bitmap {
        val outBitmap = Bitmap.createBitmap(inputImage)
        val tempIn = Allocation.createFromBitmap(rs, inputImage)
        val tempOut = Allocation.createFromBitmap(rs, outBitmap)

        contrastScript._contrast = contrast
         contrastScript.forEach_contrastness(tempIn, tempOut)

        tempOut.copyTo(outBitmap)
        tempIn.destroy()
        tempOut.destroy()
        return outBitmap

    }

    fun setSaturationX(saturation: Float, inputImage: Bitmap): Bitmap {
        val outBitmap = Bitmap.createBitmap(inputImage)
        val tempIn = Allocation.createFromBitmap(rs, inputImage)
        val tempOut = Allocation.createFromBitmap(rs, outBitmap)

        saturationFilter._saturationValue = saturation
        saturationFilter.forEach_saturation(tempIn, tempOut)

        tempOut.copyTo(outBitmap)
        tempIn.destroy()
        tempOut.destroy()
        return outBitmap

    }


    fun sharpenX(sharpen: Float, bitmap: Bitmap): Bitmap {

        val matrix = floatArrayOf(
            0f, -sharpen, 0f,
            -sharpen, 1 + 4 * sharpen, -sharpen,
            0f, -sharpen, 0f
        )

        return applySharpenConvolveX(bitmap, matrix)
    }


    private fun applySharpenConvolveX(inputImage: Bitmap, coefficients: FloatArray): Bitmap {
        val outBitmap = Bitmap.createBitmap(inputImage)
        val tempIn = Allocation.createFromBitmap(rs, inputImage)
        val tempOut = Allocation.createFromBitmap(rs, outBitmap)

        convolution.setInput(tempIn)
        convolution.setCoefficients(coefficients)
        convolution.forEach(tempOut)

        tempOut.copyTo(outBitmap)
        return outBitmap

    }


    fun vignetteX(values: Float, inputImage: Bitmap): Bitmap {

        vignetteFilter.setScale(values)
        return vignetteFilter.x(inputImage)

    }

}

For Slider Value change currentFilter is called and update the output Bitmap with ImageView

private fun currentFilter(values: Float): Bitmap {

        return when (_recentTab.value!!) {

            FilterName.BRIGHTNESS -> _filter.value!!.setBrightnessX(values, mInputImage)
            FilterName.CONTRAST -> _filter.value!!.setContrastX(values, mInputImage)
            FilterName.SATURATION -> _filter.value!!.setSaturationX(values, mInputImage)
            FilterName.SHARPEN -> _filter.value!!.sharpenX(values, mInputImage)
            FilterName.VIGNETTE -> _filter.value!!.vignetteX(values, mInputImage)

        }
    }
0

There are 0 best solutions below