Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7feadd85 authored by John Reck's avatar John Reck
Browse files

Fix wrong gainmap info

Fixes: 295897350
Test: repro in bug
Change-Id: I8d2e607c00d6f870635b0f0939c37b3f78af9071
parent fc5bf263
Loading
Loading
Loading
Loading
+11 −48
Original line number Diff line number Diff line
@@ -16,10 +16,6 @@

package com.android.test.silkfx.hdr

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
@@ -46,7 +42,7 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context
    private var selectedImage = -1
    private var outputMode = R.id.output_hdr
    private var bitmap: Bitmap? = null
    private var gainmap: Gainmap? = null
    private var originalGainmap: Gainmap? = null
    private var gainmapVisualizer: Bitmap? = null
    private lateinit var imageView: SubsamplingScaleImageView
    private lateinit var gainmapMetadataEditor: GainmapMetadataEditor
@@ -70,7 +66,6 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context
            it.check(outputMode)
            it.setOnCheckedChangeListener { _, checkedId ->
                outputMode = checkedId
                // Intentionally don't do anything fancy so that mode A/B comparisons are easy
                updateDisplay()
            }
        }
@@ -101,41 +96,10 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context

        imageView.apply {
            isClickable = true
            // Example of animating between SDR and HDR using gainmap params; animates HDR->SDR->HDR
            // with a brief pause on SDR. The key thing here is that the gainmap's
            // minDisplayRatioForHdrTransition is animated between its original value (for full HDR)
            // and displayRatioForFullHdr (for full SDR). The view must also be invalidated during
            // the animation for the updates to take effect.
            setOnClickListener {
                if (gainmap != null && (outputMode == R.id.output_hdr ||
                                        outputMode == R.id.output_hdr_test)) {
                    val animationLengthMs: Long = 500
                    val updateListener = object : AnimatorUpdateListener {
                        override fun onAnimationUpdate(animation: ValueAnimator) {
                            imageView.invalidate()
                        }
                    }
                    val hdrToSdr = ObjectAnimator.ofFloat(
                      gainmap, "minDisplayRatioForHdrTransition",
                      gainmap!!.minDisplayRatioForHdrTransition,
                      gainmap!!.displayRatioForFullHdr).apply {
                        duration = animationLengthMs
                        addUpdateListener(updateListener)
                    }
                    val sdrToHdr = ObjectAnimator.ofFloat(
                      gainmap, "minDisplayRatioForHdrTransition",
                      gainmap!!.displayRatioForFullHdr,
                      gainmap!!.minDisplayRatioForHdrTransition).apply {
                        duration = animationLengthMs
                        addUpdateListener(updateListener)
                    }

                    AnimatorSet().apply {
                        play(hdrToSdr)
                        play(sdrToHdr).after(animationLengthMs)
                        start()
                    }
                }
                animate().alpha(.5f).withEndAction {
                    animate().alpha(1f).start()
                }.start()
            }
        }
    }
@@ -149,7 +113,7 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context
    }

    private fun doDecode(source: ImageDecoder.Source) {
        gainmap = null
        originalGainmap = null
        bitmap = ImageDecoder.decodeBitmap(source) { decoder, info, source ->
            decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
        }
@@ -167,9 +131,10 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context
            findViewById<TextView>(R.id.error_msg)!!.visibility = View.GONE
            findViewById<RadioGroup>(R.id.output_mode)!!.visibility = View.VISIBLE

            gainmap = bitmap!!.gainmap
            gainmapMetadataEditor.setGainmap(gainmap)
            val map = gainmap!!.gainmapContents
            val gainmap = bitmap!!.gainmap!!
            originalGainmap = gainmap
            gainmapMetadataEditor.setGainmap(Gainmap(gainmap, gainmap.gainmapContents))
            val map = gainmap.gainmapContents
            if (map.config != Bitmap.Config.ALPHA_8) {
                gainmapVisualizer = map
            } else {
@@ -198,14 +163,12 @@ class GainmapImage(context: Context, attrs: AttributeSet?) : FrameLayout(context

        imageView.setImage(ImageSource.cachedBitmap(when (outputMode) {
            R.id.output_hdr -> {
                gainmapMetadataEditor.useOriginalMetadata()
                bitmap!!.gainmap = gainmap
                bitmap!!.gainmap = originalGainmap
                bitmap!!
            }

            R.id.output_hdr_test -> {
                gainmapMetadataEditor.useEditMetadata()
                bitmap!!.gainmap = gainmap
                bitmap!!.gainmap = gainmapMetadataEditor.editedGainmap()
                bitmap!!
            }

+67 −82
Original line number Diff line number Diff line
@@ -37,9 +37,13 @@ data class GainmapMetadata(
        var offsetHdr: Float
)

/**
 * Note: This can only handle single-channel gainmaps nicely. It will force all 3-channel
 * metadata to have the same value single value and is not intended to be a robust demonstration
 * of gainmap metadata editing
 */
class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
    private var gainmap: Gainmap? = null
    private var showingEdits = false
    private lateinit var gainmap: Gainmap

    private var metadataPopup: PopupWindow? = null

@@ -61,23 +65,18 @@ class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
    private val maxGamma = 3.0f
    // Min and max offsets are 0.0 and 1.0 respectively

    fun setGainmap(newGainmap: Gainmap?) {
    fun setGainmap(newGainmap: Gainmap) {
        gainmap = newGainmap
        originalMetadata = GainmapMetadata(gainmap!!.getRatioMin()[0],
            gainmap!!.getRatioMax()[0], gainmap!!.getMinDisplayRatioForHdrTransition(),
            gainmap!!.getDisplayRatioForFullHdr(), gainmap!!.getGamma()[0],
            gainmap!!.getEpsilonSdr()[0], gainmap!!.getEpsilonHdr()[0])
        originalMetadata = GainmapMetadata(gainmap.getRatioMin()[0],
                gainmap.getRatioMax()[0], gainmap.getMinDisplayRatioForHdrTransition(),
                gainmap.getDisplayRatioForFullHdr(), gainmap.getGamma()[0],
                gainmap.getEpsilonSdr()[0], gainmap.getEpsilonHdr()[0])
        currentMetadata = originalMetadata.copy()
    }

    fun useOriginalMetadata() {
        showingEdits = false
        applyMetadata(originalMetadata)
    }

    fun useEditMetadata() {
        showingEdits = true
    fun editedGainmap(): Gainmap {
        applyMetadata(currentMetadata)
        return gainmap
    }

    fun closeEditor() {
@@ -189,13 +188,13 @@ class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
    }

    private fun applyMetadata(newMetadata: GainmapMetadata) {
        gainmap!!.setRatioMin(newMetadata.ratioMin, newMetadata.ratioMin, newMetadata.ratioMin)
        gainmap!!.setRatioMax(newMetadata.ratioMax, newMetadata.ratioMax, newMetadata.ratioMax)
        gainmap!!.setMinDisplayRatioForHdrTransition(newMetadata.capacityMin)
        gainmap!!.setDisplayRatioForFullHdr(newMetadata.capacityMax)
        gainmap!!.setGamma(newMetadata.gamma, newMetadata.gamma, newMetadata.gamma)
        gainmap!!.setEpsilonSdr(newMetadata.offsetSdr, newMetadata.offsetSdr, newMetadata.offsetSdr)
        gainmap!!.setEpsilonHdr(newMetadata.offsetHdr, newMetadata.offsetHdr, newMetadata.offsetHdr)
        gainmap.setRatioMin(newMetadata.ratioMin, newMetadata.ratioMin, newMetadata.ratioMin)
        gainmap.setRatioMax(newMetadata.ratioMax, newMetadata.ratioMax, newMetadata.ratioMax)
        gainmap.setMinDisplayRatioForHdrTransition(newMetadata.capacityMin)
        gainmap.setDisplayRatioForFullHdr(newMetadata.capacityMax)
        gainmap.setGamma(newMetadata.gamma, newMetadata.gamma, newMetadata.gamma)
        gainmap.setEpsilonSdr(newMetadata.offsetSdr, newMetadata.offsetSdr, newMetadata.offsetSdr)
        gainmap.setEpsilonHdr(newMetadata.offsetHdr, newMetadata.offsetHdr, newMetadata.offsetHdr)
        renderView.invalidate()
    }

@@ -204,55 +203,45 @@ class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
        parent.findViewById<TextView>(R.id.gainmap_metadata_gainmapmin_val)!!.setText(
                "%.3f".format(newValue))
        currentMetadata.ratioMin = newValue
        if (showingEdits) {
            gainmap!!.setRatioMin(newValue, newValue, newValue)
        gainmap.setRatioMin(newValue, newValue, newValue)
        renderView.invalidate()
    }
    }

    private fun updateGainmapMax(normalized: Float) {
        val newValue = minRatioMax + normalized * (maxRatioMax - minRatioMax)
        parent.findViewById<TextView>(R.id.gainmap_metadata_gainmapmax_val)!!.setText(
                "%.3f".format(newValue))
        currentMetadata.ratioMax = newValue
        if (showingEdits) {
            gainmap!!.setRatioMax(newValue, newValue, newValue)
        gainmap.setRatioMax(newValue, newValue, newValue)
        renderView.invalidate()
    }
    }

    private fun updateCapacityMin(normalized: Float) {
        val newValue = minCapacityMin + normalized * (maxCapacityMin - minCapacityMin)
        parent.findViewById<TextView>(R.id.gainmap_metadata_capacitymin_val)!!.setText(
                "%.3f".format(newValue))
        currentMetadata.capacityMin = newValue
        if (showingEdits) {
            gainmap!!.setMinDisplayRatioForHdrTransition(newValue)
        gainmap.setMinDisplayRatioForHdrTransition(newValue)
        renderView.invalidate()
    }
    }

    private fun updateCapacityMax(normalized: Float) {
        val newValue = minCapacityMax + normalized * (maxCapacityMax - minCapacityMax)
        parent.findViewById<TextView>(R.id.gainmap_metadata_capacitymax_val)!!.setText(
                "%.3f".format(newValue))
        currentMetadata.capacityMax = newValue
        if (showingEdits) {
            gainmap!!.setDisplayRatioForFullHdr(newValue)
        gainmap.setDisplayRatioForFullHdr(newValue)
        renderView.invalidate()
    }
    }

    private fun updateGamma(normalized: Float) {
        val newValue = minGamma + normalized * (maxGamma - minGamma)
        parent.findViewById<TextView>(R.id.gainmap_metadata_gamma_val)!!.setText(
                "%.3f".format(newValue))
        currentMetadata.gamma = newValue
        if (showingEdits) {
            gainmap!!.setGamma(newValue, newValue, newValue)
        gainmap.setGamma(newValue, newValue, newValue)
        renderView.invalidate()
    }
    }

    private fun updateOffsetSdr(normalized: Float) {
        var newValue = 0.0f
@@ -262,11 +251,9 @@ class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
        parent.findViewById<TextView>(R.id.gainmap_metadata_offsetsdr_val)!!.setText(
                "%.5f".format(newValue))
        currentMetadata.offsetSdr = newValue
        if (showingEdits) {
            gainmap!!.setEpsilonSdr(newValue, newValue, newValue)
        gainmap.setEpsilonSdr(newValue, newValue, newValue)
        renderView.invalidate()
    }
    }

    private fun updateOffsetHdr(normalized: Float) {
        var newValue = 0.0f
@@ -276,9 +263,7 @@ class GainmapMetadataEditor(val parent: ViewGroup, val renderView: View) {
        parent.findViewById<TextView>(R.id.gainmap_metadata_offsethdr_val)!!.setText(
                "%.5f".format(newValue))
        currentMetadata.offsetHdr = newValue
        if (showingEdits) {
            gainmap!!.setEpsilonHdr(newValue, newValue, newValue)
        gainmap.setEpsilonHdr(newValue, newValue, newValue)
        renderView.invalidate()
    }
}
}