@@ -44,20 +44,43 @@ import com.github.awxkee.avifcoil.HeifDecoder
4444import com.radzivon.bartoshyk.avif.coder.HeifCoder
4545import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4646import com.radzivon.bartoshyk.avif.coder.ScaleMode
47+ import com.radzivon.bartoshyk.avif.coder.ToneMapper
4748import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
49+ import com.radzivon.bartoshyk.avif.databinding.BindingImageViewBinding
4850import kotlinx.coroutines.Dispatchers
4951import kotlinx.coroutines.launch
52+ import okio.FileNotFoundException
5053import okio.buffer
5154import okio.sink
5255import okio.source
5356import java.io.File
5457import java.io.FileOutputStream
58+ import java.io.IOException
5559import kotlin.system.measureTimeMillis
5660
5761class 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() {
71942023-11-05 20:23:51.574 24181-24181 AVIF com.radzivon.bartoshyk.avif I execution time 821
72952023-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
0 commit comments