Skip to content

[Sample] [Graphics] UltraHDR Image Edit Operations #88

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 2 commits into from
Sep 1, 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 @@ -26,6 +26,8 @@ Demonstrates how to implement data access auditing for your app to identify
This sample demonstrates displaying an UltraHDR image.
- [Displaying UltraHDR (Glide)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsingGlide.kt):
This sample demonstrates using the Glide image loading library to detect the
- [Editing UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt):
This sample demonstrates editing an UltraHDR image. Spatial edit operations like crop, rotate, scale are supported.
- [Downloadable Fonts](user-interface/text/src/main/java/com/example/platform/ui/text/DownloadableFonts.kt):
Download fonts instead of bundling them in the app resources.
- [Drag and Drop](user-interface/draganddrop/src/main/java/com/example/platform/ui/draganddrop/DragAndDrop.kt):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* 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.edit

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.ColorMatrixColorFilter
import android.graphics.Matrix
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 androidx.lifecycle.lifecycleScope
import com.example.platform.graphics.ultrahdr.databinding.EditingUltrahdrBinding
import com.google.android.catalog.framework.annotations.Sample
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.math.roundToInt


@Sample(
name = "Editing UltraHDR",
description = "This sample demonstrates editing an UltraHDR image and the resulting gainmap as well. Spatial edit operations like crop, rotate, scale are supported",
documentation = "https://developer.android.com/guide/topics/media/hdr-image-format",
tags = ["UltraHDR"],
)
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
class EditingUltraHDR : Fragment() {


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

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = EditingUltrahdrBinding.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)

// Setup editing options and on listeners
initializedImagesOrGainmap()
}

private fun initializedImagesOrGainmap() {
displayOriginalUltraHDRImage()

//Set default editing for type image and operation rotate
var editingType: Int = ULTRA_HDR_DISPLAY_EDITED_IMAGE
var editOperation = ULTRA_HDR_IMAGE_ROTATE

//Select which type to edit : Image or Gainmap (for visualization purposes only)
binding.imageOptionVisualization.setOnCheckedChangeListener { group, i ->
val selected = group.findViewById(i)
editingType = group.indexOfChild(selected)
}

//Select the edit operation : crop, rotate, scale
binding.imageOptionsEditingGroup.setOnCheckedChangeListener { group, i ->
val selected = group.findViewById(i)
editOperation = group.indexOfChild(selected)
editSelectedUltraHDRImage(editOperation, editingType)
}

//Initialize refresh button
binding.refreshUltrahdrEditedImage.setOnClickListener {
editSelectedUltraHDRImage(editOperation,editingType)
}

}
/**
* Updated the currently displayed UltraHDR ORIGINAL image.
*/
private fun displayOriginalUltraHDRImage() {

val stream = context?.assets?.open(ULTRA_HDR_IMAGE_TRAIN_STATION)
val bitmap = BitmapFactory.decodeStream(stream)
binding.imageContainer.setImageBitmap(bitmap)
}

/**
* Edit currently displayed UltraHDR image
*/
private fun editSelectedUltraHDRImage(editOperation: Int, editingType: Int) =
lifecycleScope.launch(Dispatchers.IO) {
//Performing edit operations on a background thread

val stream = context?.assets?.open(ULTRA_HDR_IMAGE_TRAIN_STATION)
val bitmap = BitmapFactory.decodeStream(stream)

//first display original image, then perform any edit operations
displayOriginalUltraHDRImage()

//Perform Edit operation
lateinit var editedBitmap: Bitmap

when (editOperation) {
ULTRA_HDR_IMAGE_CROP -> editedBitmap = bitmap.crop()
ULTRA_HDR_IMAGE_ROTATE -> editedBitmap = bitmap.rotate()
ULTRA_HDR_IMAGE_SCALE -> editedBitmap = bitmap.scale()
}

//Display edited image or gainmap, run on main thread to update the UI
withContext(Dispatchers.Main) {
when (editingType) {
ULTRA_HDR_DISPLAY_EDITED_IMAGE -> binding.imageContainer.setImageBitmap(editedBitmap)
ULTRA_HDR_DISPLAY_EDITED_GAINMAP -> binding.imageContainer.setImageBitmap(
visualizeEditedGainmap(editedBitmap),
)
}
//Release memory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for comments just have a space after the "//"

bitmap.recycle()
}
}

private fun Bitmap.rotate(): Bitmap {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove space

val matrix = Matrix().apply { postRotate(ULTRA_HDR_IMAGE_ROTATE_DEGREE) }
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

private fun Bitmap.scale(): Bitmap {

//For simplicity : Resize the image to X% of existing dimensions
return Bitmap.createScaledBitmap(
this, (this.width * ULTRA_HDR_IMAGE_SCALE_RATIO).roundToInt(),
(this.height * ULTRA_HDR_IMAGE_SCALE_RATIO).roundToInt(), true,
)
}

private fun Bitmap.crop(): Bitmap {

//For simplicity : Crop to square shape
val dimension: Int = this.width.coerceAtMost(this.height)
return Bitmap.createBitmap(this, 0, 0, dimension, dimension)
}

private suspend fun visualizeEditedGainmap(bitmap: Bitmap): Bitmap =
withContext(Dispatchers.IO) {
bitmap.gainmap?.let {
val contents = it.gainmapContents
if (contents.config != Bitmap.Config.ALPHA_8) return@withContext 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@withContext visual
}
return@withContext bitmap
}

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

companion object {
/**
* Sample UltraHDR images paths
*/
private const val ULTRA_HDR_IMAGE_TRAIN_STATION = "gainmaps/train_station_night.jpg"

/**
* Sample UltraHDR images operations
*/
private const val ULTRA_HDR_IMAGE_CROP = 0
private const val ULTRA_HDR_IMAGE_ROTATE = 1
private const val ULTRA_HDR_IMAGE_SCALE = 2

/**
* Visualization types for editing
*/
private const val ULTRA_HDR_DISPLAY_EDITED_IMAGE = 0
private const val ULTRA_HDR_DISPLAY_EDITED_GAINMAP = 1

private const val ULTRA_HDR_IMAGE_ROTATE_DEGREE = 180F
private const val ULTRA_HDR_IMAGE_SCALE_RATIO = 0.2
}

}

95 changes: 95 additions & 0 deletions samples/graphics/ultrahdr/src/main/res/layout/editing_ultrahdr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">

android:id="@+id/color_mode_controls"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

android:id="@+id/refresh_ultrahdr_edited_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/ultrahdr_color_mode_current_mode_padding"
android:layout_marginEnd="@dimen/ultrahdr_color_mode_current_mode_padding"
android:text="@string/refresh_ultrahdr_edited_image"
/>

android:id="@+id/image_option_visualization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ultrahdr_color_mode_current_mode_padding"
android:orientation="horizontal">

android:id="@+id/option_edited_ultrahdr"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="@string/displaying_edited_ultrahdr" />

android:id="@+id/option_edited_gainmap"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="@string/displaying_edited_gainmap" />

android:id="@+id/image_options_editing_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ultrahdr_color_mode_current_mode_padding"
android:orientation="horizontal">

android:id="@+id/option_crop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/displaying_ultrahdr_option_title_crop" />

android:id="@+id/option_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/displaying_ultrahdr_option_title_rotate" />

android:id="@+id/option_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/displaying_ultrahdr_option_title_scale" />

android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null" />

2 changes: 1 addition & 1 deletion samples/graphics/ultrahdr/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
-->

16dp
8dp
11 changes: 11 additions & 0 deletions samples/graphics/ultrahdr/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@
UltraHDR
Not UltraHDR

Crop
Rotate
Scale
Edit UltraHDR Image
Edit Gainmap


SDR
HDR
HDR | SDR/HDR Ratio = %f
Unknown

Refresh