Skip to content

Commit dad1b13

Browse files
committed
Big rework to support SVT, libavif, kvazaar
1 parent a3756a4 commit dad1b13

Some content is hidden

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

50 files changed

+2880
-576
lines changed

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ import android.os.Bundle
3434
import android.util.Log
3535
import android.util.Size
3636
import androidx.appcompat.app.AppCompatActivity
37+
import androidx.core.graphics.scale
3738
import androidx.lifecycle.lifecycleScope
3839
import com.radzivon.bartoshyk.avif.coder.HeifCoder
3940
import com.radzivon.bartoshyk.avif.coder.PreciseMode
4041
import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4142
import com.radzivon.bartoshyk.avif.coder.ScaleMode
43+
import com.radzivon.bartoshyk.avif.coder.ScalingQuality
4244
import com.radzivon.bartoshyk.avif.coder.ToneMapper
4345
import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
4446
import 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)

app/src/main/java/com/radzivon/bartoshyk/avif/glide/AvifCoderByteBufferDecoder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ import com.bumptech.glide.request.target.Target
4242
import com.radzivon.bartoshyk.avif.coder.HeifCoder
4343
import com.radzivon.bartoshyk.avif.coder.PreferredColorConfig
4444
import com.radzivon.bartoshyk.avif.coder.ScaleMode
45+
import com.radzivon.bartoshyk.avif.coder.ScalingQuality
4546
import java.nio.ByteBuffer
4647

4748
class AvifCoderByteBufferDecoder(private val context: Context, private val bitmapPool: BitmapPool) :
4849
ResourceDecoder<ByteBuffer, Bitmap> {
4950

50-
private val coder = HeifCoder(context)
51+
private val coder = HeifCoder()
5152

5253
override fun handles(source: ByteBuffer, options: Options): Boolean {
5354
return coder.isSupportedImage(source)
@@ -83,7 +84,7 @@ class AvifCoderByteBufferDecoder(private val context: Context, private val bitma
8384
}
8485

8586
val bitmap =
86-
coder.decodeSampled(src, idealWidth, idealHeight, preferredColorConfig, ScaleMode.FIT)
87+
coder.decodeSampled(src, idealWidth, idealHeight, preferredColorConfig, ScaleMode.FIT, ScalingQuality.HIGH)
8788

8889
return BitmapResource.obtain(bitmap, bitmapPool)
8990
}

0 commit comments

Comments
 (0)