• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.test.silkfx.hdr
18 
19 import android.content.Context
20 import android.graphics.Bitmap
21 import android.graphics.ImageDecoder
22 import android.graphics.Matrix
23 import android.util.AttributeSet
24 import android.widget.Button
25 import android.widget.ImageView
26 import android.widget.LinearLayout
27 import android.widget.TextView
28 import com.android.test.silkfx.R
29 
30 class GainmapTransformsTest(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
31 
32     private val sourceImage = loadSample()
33 
34     private fun loadSample(): Bitmap {
35         val source = ImageDecoder.createSource(resources.assets,
36                 "gainmaps/${context.assets.list("gainmaps")!![0]}")
37 
38         return ImageDecoder.decodeBitmap(source) { decoder, info, source ->
39             decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
40         }
41     }
42 
43     private fun process(transform: (Bitmap) -> Bitmap) {
44         val result = transform(sourceImage)
45 
46         val gainmapContents = result.gainmap?.let { gainmapVisualizer(it) }
47         val sdrBitmap = result.also { it.gainmap = null }
48 
49         findViewById<ImageView>(R.id.sdr_source)!!.setImageBitmap(sdrBitmap)
50         findViewById<TextView>(R.id.sdr_label)!!.text =
51                 "SDR Size: ${sdrBitmap.width}x${sdrBitmap.height}"
52 
53         findViewById<ImageView>(R.id.gainmap)!!.setImageBitmap(gainmapContents)
54         findViewById<TextView>(R.id.gainmap_label)!!.text =
55                 "Gainmap Size: ${gainmapContents?.width ?: 0}x${gainmapContents?.height ?: 0}"
56     }
57 
58     override fun onFinishInflate() {
59         super.onFinishInflate()
60         val sourceInfo = findViewById<TextView>(R.id.source_info)!!
61         sourceInfo.text = "Original size ${sourceImage.width}x${sourceImage.height}"
62         process { it.copy(Bitmap.Config.ARGB_8888, false) }
63 
64         findViewById<Button>(R.id.original)!!.setOnClickListener {
65             process { it.copy(Bitmap.Config.ARGB_8888, false) }
66         }
67 
68         findViewById<Button>(R.id.scaled)!!.setOnClickListener {
69             process { Bitmap.createScaledBitmap(it, it.width / 3, it.height / 3, true) }
70         }
71 
72         findViewById<Button>(R.id.rotate_90)!!.setOnClickListener {
73             process {
74                 val width: Int = it.width
75                 val height: Int = it.height
76 
77                 val m = Matrix()
78                 m.setRotate(90.0f, (width / 2).toFloat(), (height / 2).toFloat())
79                 Bitmap.createBitmap(it, 0, 0, width, height, m, false)
80             }
81         }
82 
83         findViewById<Button>(R.id.rotate_90_scaled)!!.setOnClickListener {
84             process {
85                 val width: Int = it.width
86                 val height: Int = it.height
87 
88                 val m = Matrix()
89                 m.setRotate(90.0f, (width / 2).toFloat(), (height / 2).toFloat())
90                 m.preScale(.3f, .3f)
91                 Bitmap.createBitmap(it, 0, 0, width, height, m, false)
92             }
93         }
94 
95         findViewById<Button>(R.id.crop)!!.setOnClickListener {
96             process {
97                 val width: Int = it.width
98                 val height: Int = it.height
99                 Bitmap.createBitmap(it, width / 2, height / 2,
100                         width / 4, height / 4, null, false)
101             }
102         }
103 
104         findViewById<Button>(R.id.crop_200)!!.setOnClickListener {
105             process {
106                 val width: Int = it.width
107                 val height: Int = it.height
108 
109                 val m = Matrix()
110                 m.setRotate(200.0f, (width / 2).toFloat(), (height / 2).toFloat())
111                 Bitmap.createBitmap(it, width / 2, height / 2,
112                         width / 4, height / 4, m, false)
113             }
114         }
115     }
116 }