Skip to content

[Sample] [Graphic] - Implementation of "Visualizing an UltraHDR Gainmap" #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Shows the recommended flow to request single runtime permissions
The sample demonstrates the importance of having proper labels for
- [TextSpan](user-interface/text/src/main/java/com/example/platform/ui/text/TextSpan.kt):
buildSpannedString is useful for quickly building a rich text.
- [Visualizing an UltraHDR Gainmap](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/VisualizingAnUltraHDRGainmap.kt):
This sample demonstrates visualizing the underlying gainmap of an UltraHDR
- [WindowInsetsAnimation](user-interface/window-insets/src/main/java/com/example/platform/ui/insets/WindowInsetsAnimation.kt):
Shows how to react to the on-screen keyboard (IME) changing visibility, and also controlling the IME's visibility.
- [WindowManager](user-interface/windowmanager/src/main/java/com/example/platform/ui/windowmanager/demos/WindowDemosActivity.kt):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.graphics.ultrahdr.display

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.ColorMatrixColorFilter
import android.graphics.Gainmap
import android.graphics.Paint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.example.platform.graphics.ultrahdr.databinding.VisualizingAnUltrahdrGainmapBinding
import com.google.android.catalog.framework.annotations.Sample

@Sample(
name = "Visualizing an UltraHDR Gainmap",
description = "This sample demonstrates visualizing the underlying gainmap of an UltraHDR " +
"image, which reveals which parts of the image are enhanced by the gainmap.",
documentation = "https://developer.android.com/guide/topics/media/hdr-image-format",
tags = ["ultrahdr"],
)
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)

class VisualizingAnUltraHDRGainmap : Fragment() {
private enum class Type(val value: Int) {
JPEG(0),
GAINMAP(1),
COMBINED(2);

companion object {
fun fromInt(value: Int) = Type.values().first { it.value == value }
}
}

/**
* Android ViewBinding.
*/
private var _binding: VisualizingAnUltrahdrGainmapBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = VisualizingAnUltrahdrGainmapBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// The ColorModeControls Class contain the necessary function to change the activities
// ColorMode to HDR, which allows and UltraHDRs images gain map to be used to enhance the
// image.
binding.colorModeControls.setWindow(requireActivity().window)
binding.modeSelectionGroup.setOnCheckedChangeListener { group, i ->
val selected = group.findViewById<RadioButton>(i)
val index = group.indexOfChild(selected)
updateDisplayedImage(Type.fromInt(index))
}

binding.modeJpeg.isChecked = true
}

/**
* Updated the currently displayed image. This allows users to visualize the SDR image alone,
* the gainmap as well as the result of the both of them displayed together.
*/
private fun updateDisplayedImage(type: Type) {
// Clear previous bitmap source.
val imageview = binding.imageContainer
imageview.setImageResource(0)

// Decode ultra hdr image into bitmap data.
val stream = context?.assets?.open(ULTRA_HDR_IMAGE_FOUNTAIN)
val imageBitmap = BitmapFactory.decodeStream(stream)

// Based on the type, setting the appropriate bitmap.
imageview.setImageBitmap(
when (type) {
// Intentionally drop the gainmap in order to display 8-bit jpeg sdr image. This
// demonstrates that even if the activity is in HDR mode, SDR images will
// remain unchanged.
Type.JPEG -> imageBitmap.apply { gainmap = null }

// Create a visual version of the gainmap where you will be able to see exactly
// which parts of the UltraHDR image are being enhanced by the gainmap.
Type.GAINMAP -> visualizeGainmap(imageBitmap.gainmap!!)

// Do nothing.
Type.COMBINED -> imageBitmap
},
)
}

/**
* Creates a monochrome representation of the [Gainmap] of an UltraHDR image and returns it as
* a [Bitmap].
*/
private fun visualizeGainmap(gainmap: Gainmap): Bitmap {
val contents = gainmap.gainmapContents
if (contents.config != Bitmap.Config.ALPHA_8) return contents

val visual = Bitmap.createBitmap(
contents.width, contents.height,
Bitmap.Config.ARGB_8888,
)

val canvas = Canvas(visual)
val paint = Paint()
paint.colorFilter = ColorMatrixColorFilter(
floatArrayOf(
0f, 0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f, 0f,
0f, 0f, 0f, 0f, 255f,
),
)
canvas.drawBitmap(contents, 0f, 0f, paint)
canvas.setBitmap(null)
return visual
}

override fun onDetach() {
super.onDetach()
binding.colorModeControls.detach()
}

companion object {
/**
* Sample UltraHDR image path
*/
private const val ULTRA_HDR_IMAGE_FOUNTAIN = "gainmaps/fountain_night.jpg"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2023 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.example.platform.graphics.ultrahdr.common.ColorModeControls
android:id="@+id/color_mode_controls"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RadioGroup
android:id="@+id/mode_selection_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_marginTop="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
android:orientation="horizontal">

<RadioButton
android:id="@+id/mode_jpeg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_jpeg" />

<RadioButton
android:id="@+id/mode_gainmap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_gainmap" />

<RadioButton
android:id="@+id/mode_both"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/visualizing_ultrahdr_gainmap_mode_title_both" />
</RadioGroup>

<ImageView
android:id="@+id/image_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@null" />

</LinearLayout>
5 changes: 5 additions & 0 deletions samples/graphics/ultrahdr/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<string name="displaying_ultrahdr_option_title_canyon">Grand Canyon</string>
<string name="displaying_ultrahdr_option_title_train_station">Train Station</string>

<!-- Visualizing An UltraHDR Gainmap String -->
<string name="visualizing_ultrahdr_gainmap_mode_title_jpeg">JPEG</string>
<string name="visualizing_ultrahdr_gainmap_mode_title_gainmap">Gainmap</string>
<string name="visualizing_ultrahdr_gainmap_mode_title_both">Both</string>

<!-- ColorModeControls String -->
<string name="color_mode_sdr">SDR</string>
<string name="color_mode_hdr">HDR</string>
Expand Down