@@ -34,11 +34,13 @@ import android.os.Bundle
3434import android.util.Log
3535import android.util.Size
3636import androidx.appcompat.app.AppCompatActivity
37+ import androidx.core.graphics.scale
3738import androidx.lifecycle.lifecycleScope
3839import com.radzivon.bartoshyk.avif.coder.HeifCoder
3940import com.radzivon.bartoshyk.avif.coder.PreciseMode
4041import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4142import com.radzivon.bartoshyk.avif.coder.ScaleMode
43+ import com.radzivon.bartoshyk.avif.coder.ScalingQuality
4244import com.radzivon.bartoshyk.avif.coder.ToneMapper
4345import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
4446import com.radzivon.bartoshyk.avif.databinding.BindingImageViewBinding
@@ -101,19 +103,21 @@ class MainActivity : AppCompatActivity() {
101103// imageView.root.setImageBitmap(decoded100)
102104// binding.scrollViewContainer.addView(imageView.root)
103105// }
104- val coder = HeifCoder (null , ToneMapper .REC2408 )
105- val allFiles1 = getAllFilesFromAssets().filter { it.contains(" .avif" ) || it.contains(" .heic" ) }
106- val allFiles2 = getAllFilesFromAssets(path = " hdr" ).filter { it.contains(" .avif" ) || it.contains(" .heic" ) }
106+ val coder = HeifCoder ( ToneMapper .REC2408 )
107+ val allFiles1 =
108+ getAllFilesFromAssets().filter { it.contains(" .avif" ) || it.contains(" .heic" ) }
109+ val allFiles2 =
110+ getAllFilesFromAssets(path = " hdr" ).filter { it.contains(" .avif" ) || it.contains(" .heic" ) }
107111 var allFiles = mutableListOf<String >()
108112 allFiles.addAll(allFiles2)
109113 allFiles.addAll(allFiles1)
110- allFiles = allFiles.filter { it.contains(" federico-beccari .avif" ) }.toMutableList()
114+ allFiles = allFiles.filter { it.contains(" WJS01456-Enhanced-NR-2 .avif" ) }.toMutableList()
111115// allFiles = allFiles.filter { it.contains("bbb_alpha_inverted.avif") }.toMutableList()
112116 for (file in allFiles) {
113117 try {
114118 Log .d(" AVIF" , " start processing $file " )
115119 val buffer = this @MainActivity.assets.open(file).source().buffer()
116- .readByteArray()
120+ .readByteArray()
117121 val size = coder.getSize(buffer)
118122 if (size != null ) {
119123// val bitmap = coder.decodeSampled(
@@ -126,28 +130,51 @@ class MainActivity : AppCompatActivity() {
126130
127131 val start = System .currentTimeMillis()
128132
129- val bitmap0 = coder.decode(
130- buffer,
133+ var bitmap0 = coder.decodeSampled(
134+ byteArray = buffer,
135+ scaledWidth = size.width / 2 ,
136+ scaledHeight = size.height / 2 ,
131137 preferredColorConfig = PreferredColorConfig .RGBA_8888 ,
138+ scaleQuality = ScalingQuality .HIGH ,
132139 )
133140
134141// bitmap0.setColorSpace(ColorSpace.getFromDataSpace(DataSpace.DATASPACE_BT2020_PQ)!!)
135142
136143 Log .i(" AVIF" , " Decoding time ${System .currentTimeMillis() - start} " )
137144
138- val encode = coder.encodeAvif(bitmap = bitmap0, quality = 64 )
139- val bitmap = coder.decode(encode)
145+ val bmp1 = bitmap0.scale(
146+ if (bitmap0.width % 2 != 0 ) {
147+ bitmap0.width + 1
148+ } else {
149+ bitmap0.width
150+ }, if (bitmap0.height % 2 != 0 ) {
151+ bitmap0.height + 1
152+ } else {
153+ bitmap0.height
154+ }
155+ )
156+
157+ // val encode = coder.encodeAvif(bitmap = bmp1, quality = 55)
158+ // val bitmap = coder.decode(encode)
140159
141160 lifecycleScope.launch(Dispatchers .Main ) {
142- val imageView = BindingImageViewBinding .inflate(layoutInflater, binding.scrollViewContainer, false )
161+ val imageView = BindingImageViewBinding .inflate(
162+ layoutInflater,
163+ binding.scrollViewContainer,
164+ false
165+ )
143166 imageView.root.setImageBitmap(bitmap0)
144167 binding.scrollViewContainer.addView(imageView.root)
145168 }
146- lifecycleScope.launch(Dispatchers .Main ) {
147- val imageView = BindingImageViewBinding .inflate(layoutInflater, binding.scrollViewContainer, false )
148- imageView.root.setImageBitmap(bitmap)
149- binding.scrollViewContainer.addView(imageView.root)
150- }
169+ // lifecycleScope.launch(Dispatchers.Main) {
170+ // val imageView = BindingImageViewBinding.inflate(
171+ // layoutInflater,
172+ // binding.scrollViewContainer,
173+ // false
174+ // )
175+ // imageView.root.setImageBitmap(bitmap)
176+ // binding.scrollViewContainer.addView(imageView.root)
177+ // }
151178 }
152179 } catch (e: Exception ) {
153180 Log .d(" AVIF" , e.toString())
@@ -231,7 +258,7 @@ class MainActivity : AppCompatActivity() {
231258 }
232259
233260 private fun testEncoder (assetName : String ) {
234- val coder = HeifCoder (this )
261+ val coder = HeifCoder ()
235262 val buffer = this .assets.open(assetName).source().buffer().readByteArray()
236263 val opts = BitmapFactory .Options ()
237264 opts.inMutable = true
@@ -296,7 +323,7 @@ class MainActivity : AppCompatActivity() {
296323 }
297324
298325 private fun writeHevc (bitmap : Bitmap ) {
299- val bytes = HeifCoder (this ).encodeHeic(bitmap)
326+ val bytes = HeifCoder ().encodeHeic(bitmap)
300327 val ff = File (this .filesDir, " result.heic" )
301328 ff.delete()
302329 val output = FileOutputStream (ff)
0 commit comments