android kotlin - Bitmap color balance

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.widget.*
import java.io.IOException


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val imageView = findViewById<ImageView>(R.id.imageView)
        val imageView2 = findViewById<ImageView>(R.id.imageView2)
        val textView = findViewById<TextView>(R.id.textView)
        val seekBarRed = findViewById<SeekBar>(R.id.seekBarRed)
        val seekBarGreen = findViewById<SeekBar>(R.id.seekBarGreen)
        val seekBarBlue = findViewById<SeekBar>(R.id.seekBarBlue)
        val tvIntensity = findViewById<TextView>(R.id.tvIntensity)


        // get the bitmap from assets folder
        val bitmap = assetsToBitmap("flower103.jpg")


        bitmap?.apply {
            // show original bitmap in first image view
            imageView.setImageBitmap(this)

            // show initial bitmap on second image view
            imageView2.setImageBitmap(colorBalance())

            // bitmap color balance on red seek bar change
            seekBarRed.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = progress - 100
                    val intensityGreen = seekBarGreen.progress - 100
                    val intensityBlue = seekBarBlue.progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


            // bitmap color balance on green seek bar change
            seekBarGreen.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = seekBarRed.progress - 100
                    val intensityGreen = progress - 100
                    val intensityBlue = seekBarBlue.progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


            // bitmap color balance on blue seek bar change
            seekBarBlue.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = seekBarRed.progress - 100
                    val intensityGreen = seekBarGreen.progress - 100
                    val intensityBlue = progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })
        }
    }
}


// extension function to get bitmap from assets
fun Context.assetsToBitmap(fileName:String):Bitmap?{
    return try {
        val stream = assets.open(fileName)
        BitmapFactory.decodeStream(stream)
    } catch (e: IOException) {
        e.printStackTrace()
        null
    }
}


// extension function for bitmap color balance
fun Bitmap.colorBalance(
    intensityRed:Int = 0,
    intensityGreen:Int = 0,
    intensityBlue:Int = 0
):Bitmap?{
    val bitmap = copy(config,true)
    var alpha:Int
    var red:Int
    var green:Int
    var blue:Int
    var pixel:Int

    // scan through all pixels
    for (x in 0 until width){
        for (y in 0 until height){
            pixel = getPixel(x,y)
            alpha = Color.alpha(pixel)
            red = Color.red(pixel)
            green = Color.green(pixel)
            blue = Color.blue(pixel)

            red += intensityRed
            if (red > 255) red = 255 else if (red < 0) red = 0

            green += intensityGreen
            if (green > 255) green = 255 else if (green < 0) green = 0

            blue += intensityBlue
            if (blue > 255) blue = 255 else if (blue < 0) blue = 0

            val color = Color.argb(
                alpha,
                red,
                green,
                blue
            )
            bitmap.setPixel(x,y,color)
        }
    }

    return bitmap
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="210dp"
        android:layout_marginTop="8dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Original Bitmap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="210dp"
        android:layout_marginTop="16dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView2">

        <TextView
            android:id="@+id/tvIntensity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:gravity="center_horizontal"
            android:text="Channels Intensity"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <SeekBar
            android:id="@+id/seekBarRed"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="200"
            android:progress="100"
            android:progressBackgroundTint="#ff0000"
            android:progressTint="#ff0000"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvIntensity" />

        <SeekBar
            android:id="@+id/seekBarGreen"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="200"
            android:progress="100"
            android:progressBackgroundTint="#00ff00"
            android:progressTint="#00ff00"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarRed" />

        <SeekBar
            android:id="@+id/seekBarBlue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="200"
            android:progress="100"
            android:progressBackgroundTint="#0000ff"
            android:progressTint="#0000ff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarGreen" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials