Skip to content

Commit fbe28eb

Browse files
committed
Added RGBA_1010102 pixel format
1 parent f836b14 commit fbe28eb

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class MainActivity : AppCompatActivity() {
4242
// opts.inPreferredConfig = Bitmap.Config.RGBA_F16
4343
// }
4444
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)
45+
var cc16 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
46+
decodedBitmap.copy(Bitmap.Config.RGBA_1010102, true)
4747
} else {
4848
decodedBitmap.copy(Bitmap.Config.ARGB_8888, true)
4949
}
@@ -76,13 +76,15 @@ class MainActivity : AppCompatActivity() {
7676
// binding.imageView.setImageBitmap(extremlyLargeBitmap)
7777

7878
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-
}
79+
val decodedBytes = HeifCoder().decode(bytes)
80+
binding.imageView.setImageBitmap(decodedBytes)
81+
// val ff = File(this.filesDir, "result.avif")
82+
// ff.delete()
83+
// val output = FileOutputStream(ff)
84+
// output.sink().buffer().use {
85+
// it.write(bytes)
86+
// it.flush()
87+
// }
8688
// output.close()
8789
// Log.d("p", bytes.size.toString())
8890
// writeHevc(decodedBitmap)
@@ -101,10 +103,10 @@ class MainActivity : AppCompatActivity() {
101103
// output.close()
102104
// Log.d("p", bytes.size.toString())
103105
// writeHevc(decodedBitmap)
104-
val numbers = IntArray(5) { 1 * (it + 1) }
105-
numbers.forEach {
106-
testEncoder("test_${it}.jpg")
107-
}
106+
// val numbers = IntArray(5) { 1 * (it + 1) }
107+
// numbers.forEach {
108+
// testEncoder("test_${it}.jpg")
109+
// }
108110
}
109111

110112
private fun testEncoder(assetName: String) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
5656

5757
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
5858
info.format != ANDROID_BITMAP_FORMAT_RGB_565 &&
59-
info.format != ANDROID_BITMAP_FORMAT_RGBA_F16) {
59+
info.format != ANDROID_BITMAP_FORMAT_RGBA_F16 &&
60+
info.format != ANDROID_BITMAP_FORMAT_RGBA_1010102) {
6061
throwInvalidPixelsFormat(env);
6162
return static_cast<jbyteArray>(nullptr);
6263
}
@@ -85,7 +86,10 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
8586
bitDepth = 8;
8687
} else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_F16) {
8788
bitDepth = 10;
89+
} else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_1010102) {
90+
bitDepth = 10;
8891
}
92+
8993
result = heif_image_add_plane(image, heif_channel_interleaved, (int) info.width,
9094
(int) info.height, bitDepth);
9195
if (result.code != heif_error_Ok) {
@@ -102,6 +106,14 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
102106
libyuv::RGB565ToARGB(reinterpret_cast<const uint8_t *>(addr), (int) info.stride, imgData,
103107
stride, (int) info.width, (int) info.height);
104108
libyuv::ARGBToABGR(imgData, stride, imgData, stride, (int) info.width, (int) info.height);
109+
} else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_1010102) {
110+
auto dstY = (char *) imgData;
111+
auto srcY = (char *) addr;
112+
for (int y = 0; y < info.height; ++y) {
113+
memcpy(dstY, srcY, info.width * 4 * sizeof(uint32_t));
114+
srcY += info.width * sizeof(uint64_t);
115+
dstY += stride;
116+
}
105117
} else if (info.format == ANDROID_BITMAP_FORMAT_RGBA_F16) {
106118
std::shared_ptr<char> dstARGB(
107119
static_cast<char *>(malloc(info.width * info.height * 4 * sizeof(uint16_t))),

avif-coder/src/main/java/com/radzivon/bartoshyk/avif/coder/UnsupportedImageFormatException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import androidx.annotation.Keep
44

55
@Keep
66
class UnsupportedImageFormatException :
7-
Exception("Currently support only RGBA_8888, RGB_565, RGBA_F16 image format") {
7+
Exception("Currently support only RGBA_8888, RGB_565, RGBA_F16, RGBA_1010102 image format") {
88
}

0 commit comments

Comments
 (0)