Skip to content

Commit

Permalink
Merge pull request #117 from android/ultrahdr_compression
Browse files Browse the repository at this point in the history
[Graphics] [Sample] UltraHDR Image compression
  • Loading branch information
MayuriKhinvasara authored Dec 4, 2023
2 parents 4cd7c58 + b81daf0 commit 2b8d309
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 1 deletion.
4 changes: 3 additions & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This sample demonstrates the importance of proper color contrast and how to
This sample shows how to use audio manager to for Communication application that self-manage the call.
- [Companion Device Manager Sample](connectivity/bluetooth/companion/src/main/java/com/example/platform/connectivity/bluetooth/cdm/CompanionDeviceManagerSample.kt):
This samples shows how to use the CDM to pair and connect with BLE devices
- [Compressing UltraHDR Images](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/CompressingUltraHDRImages.kt):
This sample demonstrates displaying an UltraHDR image in a Compose View and an Android View
- [Connect to a GATT server](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/ConnectGATTSample.kt):
Shows how to connect to a GATT server hosted by the BLE device and perform simple operations
- [ConstraintLayout - 1. Centering Views](user-interface/constraintlayout/src/main/java/com/example/platform/ui/constraintlayout/ConstraintLayout.kt):
Expand All @@ -25,7 +27,7 @@ Demonstrates how to implement data access auditing for your app to identify
- [Displaying UltraHDR](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDR.kt):
This sample demonstrates displaying an UltraHDR image.
- [Displaying UltraHDR (3P Libraries)](graphics/ultrahdr/src/main/java/com/example/platform/graphics/ultrahdr/display/DisplayingUltraHDRUsing3PLibrary.kt):
This sample demonstrates using the various popular image loading libraries to display Ultra HDR images
This sample demonstrates using the various popular image loading library to
- [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
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,159 @@
/*
* 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.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.example.platform.graphics.ultrahdr.databinding.CompressUltrahdrBinding
import com.google.android.catalog.framework.annotations.Sample
import java.io.ByteArrayOutputStream


@RequiresApi(34)
@Sample(
name = "Compressing UltraHDR Images",
description = "This sample demonstrates displaying an UltraHDR image in a Compose View and an Android View",
documentation = "https://developer.android.com/guide/topics/media/hdr-image-format",
tags = ["UltraHDR"],
)

class CompressingUltraHDRImages : Fragment() {
/**
* Android ViewBinding.
*/
private var _binding: CompressUltrahdrBinding? = null
private val binding get() = _binding!!
var orignalImageSize: Long = 0

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

// Set original ultra hdr image
binding.optionOrignalUltrahdr.isChecked = true
binding.optionOrignalUltrahdr.callOnClick()
}

private fun initializedUltraHDRImageButtonListeners() {

//Select the edit operation : crop, rotate, scale
binding.optionCompressedImage.setOnClickListener {
compressAndDisplayUltraHDRImage()
}

//Select the edit operation : crop, rotate, scale
binding.optionOrignalUltrahdr.setOnClickListener {
displayOriginalUltraHDRImage()
}
}

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

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

// display original size of image
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val outputStreamSize = outputStream.toByteArray().size.toLong()

// convert from bytes to MB for displaying text
orignalImageSize = (outputStreamSize / (1024 * 1024))

" Displayed Original Image size : $orignalImageSize MB".also {
binding.ultrahdrImageSizeText.text = it
}
}

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

//open original image
val stream = context?.assets?.open(ULTRA_HDR_IMAGE)
val bitmap = BitmapFactory.decodeStream(stream)

//Compress ultra-hdr image
val outputStream = ByteArrayOutputStream()
bitmap.compress(
Bitmap.CompressFormat.JPEG,
ULTRA_HDR_IMAGE_COMPRESSION_QUALITY,
outputStream,
)


// Display size of compressed image. For demo purposes only
val imageInByte: ByteArray = outputStream.toByteArray()
//convert fom bytes to megabytes
val compressedImageSizeInMegabytes: Long = imageInByte.size.toLong() / (1024 * 1024)

// Convert compressed stream into a bitmap
val compressedByteArray: ByteArray = imageInByte
val compressedBitmap =
BitmapFactory
.decodeByteArray(compressedByteArray, 0, compressedByteArray.size)


//Load the compressed bitmap
binding.imageContainer.setImageBitmap(compressedBitmap)

//Set compressed image details as text for informational purposes
("Compressed Image Quality : ${ULTRA_HDR_IMAGE_COMPRESSION_QUALITY}% \n Displayed Compressed Image Size : ${compressedImageSizeInMegabytes} MB ").also {
binding.ultrahdrImageSizeText.text = it
}
}

companion object {
/**
* Sample UltraHDR images paths
*/
private const val ULTRA_HDR_IMAGE = "ultrahdr/ultrahdr_cityscape.jpg"

//Image compression quality from 0-100. Refer: https://developer.android.com/reference/android/graphics/Bitmap.CompressFormat
private const val ULTRA_HDR_IMAGE_COMPRESSION_QUALITY = 40
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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/image_operation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ultrahdr_color_mode_current_mode_padding"
android:orientation="horizontal">

<RadioButton
android:id="@+id/option_orignal_ultrahdr"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="@string/displaying_orignal_ultrahdr" />

<RadioButton
android:id="@+id/option_compressed_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="@string/displaying_compressed_image" />
</RadioGroup>

<TextView
android:id="@+id/ultrahdr_image_size_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:lines="3"
android:padding="@dimen/ultrahdr_color_mode_current_mode_padding"
android:text="@string/ultra_hdr_image_size"
tools:text="@string/ultra_hdr_image_size" />


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

</LinearLayout>
3 changes: 3 additions & 0 deletions samples/graphics/ultrahdr/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<string name="displaying_ultrahdr_option_title_scale">Scale</string>
<string name="displaying_edited_ultrahdr">Edit UltraHDR Image</string>
<string name="displaying_edited_gainmap">Edit Gainmap</string>
<string name="displaying_orignal_ultrahdr">Original UltraHDR Image</string>
<string name="displaying_compressed_image">Compressed UltraHDR image </string>
<string name="ultra_hdr_image_size">Displayed image size</string>

<!-- ColorModeControls Strings -->
<string name="color_mode_sdr">SDR</string>
Expand Down

0 comments on commit 2b8d309

Please sign in to comment.