Skip to content

Commit 9869563

Browse files
committed
Improve HDR render, some optimizations and bugxfixes
1 parent b10bc57 commit 9869563

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4944
-1083
lines changed

app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt

Lines changed: 48 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,43 @@ import com.github.awxkee.avifcoil.HeifDecoder
4444
import com.radzivon.bartoshyk.avif.coder.HeifCoder
4545
import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4646
import com.radzivon.bartoshyk.avif.coder.ScaleMode
47+
import com.radzivon.bartoshyk.avif.coder.ToneMapper
4748
import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
49+
import com.radzivon.bartoshyk.avif.databinding.BindingImageViewBinding
4850
import kotlinx.coroutines.Dispatchers
4951
import kotlinx.coroutines.launch
52+
import okio.FileNotFoundException
5053
import okio.buffer
5154
import okio.sink
5255
import okio.source
5356
import java.io.File
5457
import java.io.FileOutputStream
58+
import java.io.IOException
5559
import kotlin.system.measureTimeMillis
5660

5761
class MainActivity : AppCompatActivity() {
5862

5963
private lateinit var binding: ActivityMainBinding
6064

65+
fun getAllFilesFromAssets(): List<String> {
66+
val assetManager = assets
67+
val fileList: MutableList<String> = mutableListOf()
68+
69+
try {
70+
// List all files in the "assets" folder
71+
val files = assetManager.list("") ?: arrayOf()
72+
73+
// Add each file to the list
74+
for (file in files) {
75+
fileList.add(file)
76+
}
77+
} catch (e: IOException) {
78+
e.printStackTrace()
79+
}
80+
81+
return fileList
82+
}
83+
6184
override fun onCreate(savedInstanceState: Bundle?) {
6285
super.onCreate(savedInstanceState)
6386

@@ -71,194 +94,34 @@ class MainActivity : AppCompatActivity() {
7194
2023-11-05 20:23:51.574 24181-24181 AVIF com.radzivon.bartoshyk.avif I execution time 821
7295
2023-11-05 20:23:52.341 24181-24181 AVIF com.radzivon.bartoshyk.avif I execution time 767
7396
*/
74-
val coder = HeifCoder()
75-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
76-
val buffer = this.assets.open("hdr/castle-hdr.avif").source().buffer().readByteArray()
77-
val size = coder.getSize(buffer)!!
78-
assert(size != null)
79-
val bitmap = coder.decodeSampled(
80-
buffer,
81-
size.width * 2,
82-
size.height * 2,
83-
PreferredColorConfig.RGBA_F16,
84-
ScaleMode.RESIZE
85-
)
86-
binding.imageView.setImageBitmap(bitmap)
87-
}
88-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
89-
val buffer = this.assets.open("hdr/future city.avif").source().buffer().readByteArray()
90-
val size = coder.getSize(buffer)!!
91-
assert(size != null)
92-
val executionTime = measureTimeMillis {
93-
val bitmap = coder.decodeSampled(
94-
buffer,
95-
size.width,
96-
size.height,
97-
PreferredColorConfig.RGBA_F16,
98-
ScaleMode.RESIZE
99-
)
100-
binding.imageView1.setImageBitmap(bitmap)
101-
}
102-
Log.i("AVIF", "execution time $executionTime")
103-
}
104-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
105-
val buffer = this.assets.open("hdr/Sea of Umbrellas at Shibuya Crossing-hdr.avif").source().buffer().readByteArray()
106-
val size = coder.getSize(buffer)!!
107-
assert(size != null)
108-
val bitmap = coder.decodeSampled(
109-
buffer,
110-
size.width * 2,
111-
size.height * 2,
112-
PreferredColorConfig.RGB_565,
113-
ScaleMode.RESIZE
114-
)
115-
binding.imageView2.setImageBitmap(bitmap)
116-
}
117-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
118-
val buffer = this.assets.open("hdr/Elevate-hdr.avif").source().buffer().readByteArray()
119-
val size = coder.getSize(buffer)!!
120-
assert(size != null)
121-
val executionTime = measureTimeMillis {
122-
val bitmap = coder.decodeSampled(
123-
buffer,
124-
size.width * 2,
125-
size.height * 2,
126-
PreferredColorConfig.RGBA_F16,
127-
ScaleMode.RESIZE
128-
)
129-
binding.imageView3.setImageBitmap(bitmap)
130-
}
131-
Log.i("AVIF", "execution time $executionTime")
132-
}
133-
134-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
135-
val buffer = this.assets.open("hdr/house on lake.avif").source().buffer().readByteArray()
136-
val size = coder.getSize(buffer)!!
137-
assert(size != null)
138-
val executionTime = measureTimeMillis {
139-
val bitmap = coder.decodeSampled(
140-
buffer,
141-
size.width * 2,
142-
size.height * 2,
143-
PreferredColorConfig.RGB_565,
144-
ScaleMode.RESIZE
145-
)
146-
binding.imageView4.setImageBitmap(bitmap)
147-
}
148-
Log.i("AVIF", "execution time $executionTime")
149-
}
15097

151-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
152-
val buffer = this.assets.open("hdr/Triad-hdr.avif").source().buffer().readByteArray()
153-
val size = coder.getSize(buffer)!!
154-
assert(size != null)
155-
val executionTime = measureTimeMillis {
156-
val bitmap = coder.decodeSampled(
157-
buffer,
158-
size.width * 2,
159-
size.height * 2,
160-
PreferredColorConfig.RGB_565,
161-
ScaleMode.RESIZE
162-
)
163-
binding.imageView5.setImageBitmap(bitmap)
164-
}
165-
Log.i("AVIF", "execution time $executionTime")
166-
}
167-
168-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
169-
val buffer = this.assets.open("hdr/GUM Mall Lights-hdr.avif").source().buffer().readByteArray()
170-
val size = coder.getSize(buffer)!!
171-
assert(size != null)
172-
val executionTime = measureTimeMillis {
173-
val bitmap = coder.decodeSampled(
174-
buffer,
175-
size.width * 2,
176-
size.height * 2,
177-
PreferredColorConfig.RGBA_1010102,
178-
ScaleMode.RESIZE
179-
)
180-
binding.imageView6.setImageBitmap(bitmap)
181-
}
182-
Log.i("AVIF", "execution time $executionTime")
183-
}
184-
185-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
186-
val buffer = this.assets.open("bt_2020_pq.avif").source().buffer().readByteArray()
187-
val size = coder.getSize(buffer)!!
188-
assert(size != null)
189-
val bitmap = coder.decodeSampled(
190-
buffer,
191-
size.width / 3,
192-
size.height / 3,
193-
PreferredColorConfig.RGBA_8888,
194-
ScaleMode.RESIZE
195-
)
196-
binding.imageView7.setImageBitmap(bitmap)
197-
}
198-
199-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
200-
val buffer = this.assets.open("federico-beccari.avif").source().buffer().readByteArray()
201-
val size = coder.getSize(buffer)!!
202-
assert(size != null)
203-
val executionTime = measureTimeMillis {
204-
val bitmap = coder.decodeSampled(
205-
buffer,
206-
size.width / 4,
207-
size.height / 4,
208-
PreferredColorConfig.RGBA_1010102,
209-
ScaleMode.RESIZE
210-
)
211-
binding.imageView8.setImageBitmap(bitmap)
212-
}
213-
Log.i("AVIF", "execution time $executionTime")
214-
}
215-
216-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
217-
val buffer = this.assets.open("happy_colly.avif").source().buffer().readByteArray()
218-
val size = coder.getSize(buffer)!!
219-
assert(size != null)
220-
val executionTime = measureTimeMillis {
221-
val bitmap = coder.decodeSampled(
222-
buffer,
223-
size.width,
224-
size.height,
225-
PreferredColorConfig.RGBA_8888,
226-
ScaleMode.RESIZE
227-
)
228-
binding.imageView10.setImageBitmap(bitmap)
229-
}
230-
Log.i("AVIF", "execution time $executionTime")
231-
}
232-
233-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
234-
val buffer = this.assets.open("blue_lights.avif").source().buffer().readByteArray()
235-
val size = coder.getSize(buffer)!!
236-
assert(size != null)
237-
val executionTime = measureTimeMillis {
238-
val bitmap = coder.decodeSampled(
239-
buffer,
240-
size.width,
241-
size.height,
242-
PreferredColorConfig.RGB_565,
243-
ScaleMode.RESIZE
244-
)
245-
binding.imageView11.setImageBitmap(bitmap)
246-
}
247-
Log.i("AVIF", "execution time $executionTime")
248-
}
98+
// HDR EXAMPLES - https://us.zonerama.com/williamskeaguidingphotography/Photo/1000120226/1004888131
99+
val coder = HeifCoder(this, toneMapper = ToneMapper.LOGARITHMIC)
100+
val allFiles = getAllFilesFromAssets().filter { it.contains(".avif") || it.contains(".heic") }
101+
for (file in allFiles) {
102+
try {
103+
val imageView = BindingImageViewBinding.inflate(layoutInflater, binding.scrollViewContainer, false)
104+
val buffer = this.assets.open(file).source().buffer()
105+
.readByteArray()
106+
val size = coder.getSize(buffer)
107+
if (size != null) {
108+
val bitmap = coder.decodeSampled(
109+
buffer,
110+
size.width / 2,
111+
size.height / 2,
112+
PreferredColorConfig.HARDWARE,
113+
ScaleMode.RESIZE
114+
)
115+
imageView.root.setImageBitmap(bitmap)
116+
binding.scrollViewContainer.addView(imageView.root)
117+
}
118+
} catch (e: Exception) {
119+
if (e is FileNotFoundException || e is java.io.FileNotFoundException) {
249120

250-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
251-
val buffer = this.assets.open("blue_lights.avif").source().buffer().readByteArray()
252-
val size = coder.getSize(buffer)!!
253-
assert(size != null)
254-
val executionTime = measureTimeMillis {
255-
val bitmap = coder.decode(
256-
buffer,
257-
PreferredColorConfig.RGBA_1010102
258-
)
259-
binding.imageView9.setImageBitmap(bitmap)
121+
} else {
122+
throw e
123+
}
260124
}
261-
Log.i("AVIF", "execution time $executionTime")
262125
}
263126

264127
// https://wh.aimuse.online/creatives/IMUSE_03617fe2db82a584166_27/TT_a9d21ff1061d785347935fef/68f06252.avif

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -44,128 +44,9 @@
4444
<LinearLayout
4545
android:layout_width="match_parent"
4646
android:orientation="vertical"
47+
android:id="@+id/scrollViewContainer"
4748
android:layout_height="wrap_content">
4849

49-
<ImageView
50-
android:id="@+id/imageView7"
51-
android:layout_width="match_parent"
52-
android:layout_height="wrap_content"
53-
android:scaleType="fitCenter"
54-
app:layout_constraintBottom_toBottomOf="parent"
55-
app:layout_constraintEnd_toEndOf="parent"
56-
app:layout_constraintStart_toStartOf="parent"
57-
app:layout_constraintTop_toTopOf="parent" />
58-
59-
<ImageView
60-
android:id="@+id/imageView11"
61-
android:layout_width="match_parent"
62-
android:layout_height="wrap_content"
63-
android:scaleType="fitCenter"
64-
app:layout_constraintBottom_toBottomOf="parent"
65-
app:layout_constraintEnd_toEndOf="parent"
66-
app:layout_constraintStart_toStartOf="parent"
67-
app:layout_constraintTop_toTopOf="parent" />
68-
69-
<ImageView
70-
android:id="@+id/imageView10"
71-
android:layout_width="match_parent"
72-
android:layout_height="wrap_content"
73-
android:scaleType="fitCenter"
74-
app:layout_constraintBottom_toBottomOf="parent"
75-
app:layout_constraintEnd_toEndOf="parent"
76-
app:layout_constraintStart_toStartOf="parent"
77-
app:layout_constraintTop_toTopOf="parent" />
78-
79-
<ImageView
80-
android:id="@+id/imageView8"
81-
android:layout_width="match_parent"
82-
android:layout_height="wrap_content"
83-
android:scaleType="fitCenter"
84-
app:layout_constraintBottom_toBottomOf="parent"
85-
app:layout_constraintEnd_toEndOf="parent"
86-
app:layout_constraintStart_toStartOf="parent"
87-
app:layout_constraintTop_toTopOf="parent" />
88-
89-
<ImageView
90-
android:id="@+id/imageView9"
91-
android:layout_width="match_parent"
92-
android:layout_height="wrap_content"
93-
android:scaleType="fitCenter"
94-
app:layout_constraintBottom_toBottomOf="parent"
95-
app:layout_constraintEnd_toEndOf="parent"
96-
app:layout_constraintStart_toStartOf="parent"
97-
app:layout_constraintTop_toTopOf="parent" />
98-
99-
<ImageView
100-
android:id="@+id/imageView"
101-
android:layout_width="match_parent"
102-
android:layout_height="wrap_content"
103-
android:scaleType="fitCenter"
104-
app:layout_constraintBottom_toBottomOf="parent"
105-
app:layout_constraintEnd_toEndOf="parent"
106-
app:layout_constraintStart_toStartOf="parent"
107-
app:layout_constraintTop_toTopOf="parent" />
108-
109-
<ImageView
110-
android:id="@+id/imageView1"
111-
android:layout_width="match_parent"
112-
android:layout_height="wrap_content"
113-
android:scaleType="fitCenter"
114-
app:layout_constraintBottom_toBottomOf="parent"
115-
app:layout_constraintEnd_toEndOf="parent"
116-
app:layout_constraintStart_toStartOf="parent"
117-
app:layout_constraintTop_toTopOf="parent" />
118-
119-
<ImageView
120-
android:id="@+id/imageView2"
121-
android:layout_width="match_parent"
122-
android:layout_height="wrap_content"
123-
android:scaleType="fitCenter"
124-
app:layout_constraintBottom_toBottomOf="parent"
125-
app:layout_constraintEnd_toEndOf="parent"
126-
app:layout_constraintStart_toStartOf="parent"
127-
app:layout_constraintTop_toTopOf="parent" />
128-
129-
<ImageView
130-
android:id="@+id/imageView3"
131-
android:layout_width="match_parent"
132-
android:layout_height="wrap_content"
133-
android:scaleType="fitCenter"
134-
app:layout_constraintBottom_toBottomOf="parent"
135-
app:layout_constraintEnd_toEndOf="parent"
136-
app:layout_constraintStart_toStartOf="parent"
137-
app:layout_constraintTop_toTopOf="parent" />
138-
139-
<ImageView
140-
android:id="@+id/imageView4"
141-
android:layout_width="match_parent"
142-
android:layout_height="wrap_content"
143-
android:scaleType="fitCenter"
144-
app:layout_constraintBottom_toBottomOf="parent"
145-
app:layout_constraintEnd_toEndOf="parent"
146-
app:layout_constraintStart_toStartOf="parent"
147-
app:layout_constraintTop_toTopOf="parent" />
148-
149-
<ImageView
150-
android:id="@+id/imageView5"
151-
android:layout_width="match_parent"
152-
android:layout_height="wrap_content"
153-
android:scaleType="fitCenter"
154-
app:layout_constraintBottom_toBottomOf="parent"
155-
app:layout_constraintEnd_toEndOf="parent"
156-
app:layout_constraintStart_toStartOf="parent"
157-
app:layout_constraintTop_toTopOf="parent" />
158-
159-
<ImageView
160-
android:id="@+id/imageView6"
161-
android:layout_width="match_parent"
162-
android:layout_height="wrap_content"
163-
android:scaleType="fitCenter"
164-
app:layout_constraintBottom_toBottomOf="parent"
165-
app:layout_constraintEnd_toEndOf="parent"
166-
app:layout_constraintStart_toStartOf="parent"
167-
app:layout_constraintTop_toTopOf="parent" />
168-
16950
</LinearLayout>
17051

17152
</ScrollView>

0 commit comments

Comments
 (0)