Skip to content

Commit f836b14

Browse files
committed
Lot of bugfixes + R8 configuration
1 parent 0943f50 commit f836b14

File tree

15 files changed

+93
-50
lines changed

15 files changed

+93
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ val bitmap: Bitmap = decodeSampled(byteArray, scaledWidth, scaledHeight)
2424
```
2525

2626
```groovy
27-
implementation 'com.github.awxkee:avif-coder:1.0.13' // or any version above picker from release tags
27+
implementation 'com.github.awxkee:avif-coder:1.0.14' // or any version above picker from release tags
2828
```
2929

3030
# Also supports coil integration

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

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity
99
import android.os.Bundle
1010
import android.util.Log
1111
import android.util.Size
12+
import androidx.core.graphics.scale
1213
import androidx.lifecycle.lifecycleScope
1314
import com.radzivon.bartoshyk.avif.coder.HeifCoder
1415
import com.radzivon.bartoshyk.avif.databinding.ActivityMainBinding
@@ -32,50 +33,56 @@ class MainActivity : AppCompatActivity() {
3233

3334
// Example of a call to a native method
3435

35-
val buffer = this.assets.open("test_avif.avif").source().buffer().readByteArray()
36-
assert(HeifCoder().isAvif(buffer))
37-
val bitmap = HeifCoder().decode(buffer)
38-
val opts = BitmapFactory.Options()
39-
opts.inMutable = true
40-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
41-
opts.inPreferredConfig = Bitmap.Config.RGBA_F16
42-
}
43-
val decodedBitmap = BitmapFactory.decodeResource(resources, R.drawable.test_png)
44-
val cc16 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
45-
decodedBitmap.copy(Bitmap.Config.RGBA_F16, true)
36+
// val buffer = this.assets.open("test_avif.avif").source().buffer().readByteArray()
37+
// assert(HeifCoder().isAvif(buffer))
38+
// val bitmap = HeifCoder().decode(buffer)
39+
// val opts = BitmapFactory.Options()
40+
// opts.inMutable = true
41+
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
42+
// opts.inPreferredConfig = Bitmap.Config.RGBA_F16
43+
// }
44+
val decodedBitmap = BitmapFactory.decodeResource(resources, R.drawable.test_png_with_alpha)
45+
var cc16 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
46+
decodedBitmap.copy(Bitmap.Config.RGB_565, true)
4647
} else {
4748
decodedBitmap.copy(Bitmap.Config.ARGB_8888, true)
4849
}
49-
binding.imageView.setImageBitmap(decodedBitmap)
50-
binding.imageView.setImageBitmap(bitmap)
50+
val rescaledSize =
51+
aspectScale(Size(decodedBitmap.width, decodedBitmap.height), Size(1400, 720))
52+
val cc16Rescaled = cc16.scale(rescaledSize.width, rescaledSize.height, true)
53+
cc16.recycle()
54+
cc16 = cc16Rescaled
5155
binding.imageView.setImageBitmap(cc16)
52-
val avif12DepthBuffer =
53-
this.assets.open("test_avif_12_bitdepth.avif").source().buffer().readByteArray()
54-
assert(HeifCoder().isAvif(avif12DepthBuffer))
55-
val avifHDRBitmap = HeifCoder().decode(avif12DepthBuffer)
56-
binding.imageView.setImageBitmap(avifHDRBitmap)
57-
val heicBuffer = this.assets.open("pexels-heif.heif").source().buffer().readByteArray()
58-
assert(HeifCoder().isHeif(heicBuffer))
59-
val heicBitmap = HeifCoder().decode(heicBuffer)
60-
binding.imageView.setImageBitmap(heicBitmap)
61-
assert(HeifCoder().getSize(heicBuffer) != null)
62-
assert(HeifCoder().getSize(buffer) != null)
63-
val heicScaled = HeifCoder().decodeSampled(heicBuffer, 350, 900)
64-
binding.imageView.setImageBitmap(heicScaled)
65-
val extremlyLargeBitmapBuffer =
66-
this.assets.open("extremly_large.avif").source().buffer().readByteArray()
67-
assert(HeifCoder().isAvif(extremlyLargeBitmapBuffer))
68-
val extremlyLargeBitmap = HeifCoder().decode(extremlyLargeBitmapBuffer)
69-
binding.imageView.setImageBitmap(extremlyLargeBitmap)
56+
// binding.imageView.setImageBitmap(decodedBitmap)
57+
// binding.imageView.setImageBitmap(bitmap)
58+
// binding.imageView.setImageBitmap(cc16)
59+
// val avif12DepthBuffer =
60+
// this.assets.open("test_avif_12_bitdepth.avif").source().buffer().readByteArray()
61+
// assert(HeifCoder().isAvif(avif12DepthBuffer))
62+
// val avifHDRBitmap = HeifCoder().decode(avif12DepthBuffer)
63+
// binding.imageView.setImageBitmap(avifHDRBitmap)
64+
// val heicBuffer = this.assets.open("pexels-heif.heif").source().buffer().readByteArray()
65+
// assert(HeifCoder().isHeif(heicBuffer))
66+
// val heicBitmap = HeifCoder().decode(heicBuffer)
67+
// binding.imageView.setImageBitmap(heicBitmap)
68+
// assert(HeifCoder().getSize(heicBuffer) != null)
69+
// assert(HeifCoder().getSize(buffer) != null)
70+
// val heicScaled = HeifCoder().decodeSampled(heicBuffer, 350, 900)
71+
// binding.imageView.setImageBitmap(heicScaled)
72+
// val extremlyLargeBitmapBuffer =
73+
// this.assets.open("extremly_large.avif").source().buffer().readByteArray()
74+
// assert(HeifCoder().isAvif(extremlyLargeBitmapBuffer))
75+
// val extremlyLargeBitmap = HeifCoder().decode(extremlyLargeBitmapBuffer)
76+
// binding.imageView.setImageBitmap(extremlyLargeBitmap)
7077

71-
// val bytes = HeifCoder().encodeAvif(cc16)
72-
// val ff = File(this.filesDir, "result.avif")
73-
// ff.delete()
74-
// val output = FileOutputStream(ff)
75-
// output.sink().buffer().use {
76-
// it.write(bytes)
77-
// it.flush()
78-
// }
78+
val bytes = HeifCoder().encodeAvif(cc16)
79+
val ff = File(this.filesDir, "result.avif")
80+
ff.delete()
81+
val output = FileOutputStream(ff)
82+
output.sink().buffer().use {
83+
it.write(bytes)
84+
it.flush()
85+
}
7986
// output.close()
8087
// Log.d("p", bytes.size.toString())
8188
// writeHevc(decodedBitmap)
@@ -94,17 +101,24 @@ class MainActivity : AppCompatActivity() {
94101
// output.close()
95102
// Log.d("p", bytes.size.toString())
96103
// writeHevc(decodedBitmap)
97-
// val numbers = IntArray(5) { 1 * (it + 1) }
98-
// numbers.forEach {
99-
// testEncoder("test_${it}.jpg")
100-
// }
104+
val numbers = IntArray(5) { 1 * (it + 1) }
105+
numbers.forEach {
106+
testEncoder("test_${it}.jpg")
107+
}
101108
}
102109

103110
private fun testEncoder(assetName: String) {
104111
val buffer = this.assets.open(assetName).source().buffer().readByteArray()
105112
val opts = BitmapFactory.Options()
106113
opts.inMutable = true
107-
val bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.size, opts)
114+
var bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.size, opts)
115+
val rr = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
116+
bitmap.copy(Bitmap.Config.RGBA_F16, true)
117+
} else {
118+
bitmap.copy(Bitmap.Config.ARGB_8888, true)
119+
}
120+
bitmap.recycle()
121+
bitmap = rr
108122
// val newBitmap = bitmap.aspectFit(bitmap.width, bitmap.height)
109123
// bitmap.recycle()
110124
val bytes = HeifCoder().encodeAvif(bitmap)
3.2 MB
Loading

avif-coder-coil/src/main/java/com/github/awxkee/avifcoil/HeifDecoder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class HeifDecoder(
4242
)
4343
}
4444
val originalImage = HeifCoder().decode(byteArray)
45-
val resizedBitmap = resizeAspectFill(originalImage, originalSize, Size(dstWidth, dstHeight))
45+
val resizedBitmap = resizeAspectFill(originalImage, Size(dstWidth, dstHeight))
4646
originalImage.recycle()
4747
return@runInterruptible DecodeResult(
4848
BitmapDrawable(
@@ -52,10 +52,10 @@ class HeifDecoder(
5252
)
5353
}
5454

55-
private fun resizeAspectFill(sourceBitmap: Bitmap, sourceSize: Size, dstSize: Size): Bitmap {
55+
private fun resizeAspectFill(sourceBitmap: Bitmap,dstSize: Size): Bitmap {
5656
val background = Bitmap.createBitmap(dstSize.width, dstSize.height, Bitmap.Config.ARGB_8888)
57-
val originalWidth: Float = sourceSize.width.toFloat()
58-
val originalHeight: Float = sourceSize.height.toFloat()
57+
val originalWidth: Float = background.width.toFloat()
58+
val originalHeight: Float = background.height.toFloat()
5959
val canvas = Canvas(background)
6060

6161
val scale: Float =

avif-coder/consumer-rules.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-keepclasseswithmembernames class com.radzivon.bartoshyk.avif.coder.** { *; }

avif-coder/src/main/cpp/coder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
101101
} else if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
102102
libyuv::RGB565ToARGB(reinterpret_cast<const uint8_t *>(addr), (int) info.stride, imgData,
103103
stride, (int) info.width, (int) info.height);
104+
libyuv::ARGBToABGR(imgData, stride, imgData, stride, (int) info.width, (int) info.height);
104105
} else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_F16) {
105106
std::shared_ptr<char> dstARGB(
106107
static_cast<char *>(malloc(info.width * info.height * 4 * sizeof(uint16_t))),
@@ -113,7 +114,8 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
113114
auto *dataPtr = reinterpret_cast<uint16_t *>(dstARGB.get());
114115
auto *data64Ptr = reinterpret_cast<uint64_t *>(dstARGB.get());
115116
const float maxColors = (float) pow(2.0, bitDepth) - 1;
116-
for (int i = 0, k = 0; i < info.stride * info.height; i += 4, k += 1) {
117+
for (int i = 0, k = 0; i < std::min(info.stride * info.height,
118+
info.width * info.height * 4); i += 4, k += 1) {
117119
tmpR = (uint16_t) (srcData[i] * maxColors);
118120
tmpG = (uint16_t) (srcData[i + 1] * maxColors);
119121
tmpB = (uint16_t) (srcData[i + 2] * maxColors);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.radzivon.bartoshyk.avif.coder
22

3+
import androidx.annotation.Keep
4+
5+
@Keep
36
class CantCreateCodecException: Exception("Can't create decoder") {
47
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.radzivon.bartoshyk.avif.coder
22

3+
import androidx.annotation.Keep
4+
5+
@Keep
36
class CantDecoderImageException: Exception("Can't decode image exception") {
47
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.radzivon.bartoshyk.avif.coder
22

3+
import androidx.annotation.Keep
4+
5+
@Keep
36
class CantEncodeImageException: Exception("Can't encode image exception") {
47

58
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.radzivon.bartoshyk.avif.coder
22

3+
import androidx.annotation.Keep
4+
5+
@Keep
36
class CantReadHeifFileException: Exception("Can't read heif file exception") {
47
}

0 commit comments

Comments
 (0)