-
Notifications
You must be signed in to change notification settings - Fork 343
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
...ics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/edit/EditingUltraHDR.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
||
editingType = group.indexOfChild(selected) | ||
} | ||
|
||
//Select the edit operation : crop, rotate, scale | ||
binding.imageOptionsEditingGroup.setOnCheckedChangeListener { group, i -> | ||
val selected = group.findViewById |
||
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 | ||
bitmap.recycle() | ||
} | ||
} | ||
|
||
private fun Bitmap.rotate(): Bitmap { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
95
samples/graphics/ultrahdr/src/main/res/layout/editing_ultrahdr.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> | ||
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,5 @@ | |
--> | ||
|
||
|
||
|
||
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 "//"