From 0e3d55fc656b4e7f4fe23e10bb5fc466f14e22c5 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 22 Oct 2024 16:12:12 +0300 Subject: [PATCH] WIP: add JNI wrappers --- .../simplex/common/platform/Images.android.kt | 20 +++++++++--------- .../src/commonMain/cpp/android/simplex-api.c | 9 ++++++++ .../src/commonMain/cpp/desktop/simplex-api.c | 9 ++++++++ .../chat/simplex/common/platform/Core.kt | 1 + .../chat/simplex/common/platform/Images.kt | 1 + .../simplex/common/platform/Images.desktop.kt | 21 ++++++++++--------- 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt index fc323f6ffd..f3a89dd664 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Images.android.kt @@ -14,7 +14,9 @@ import boofcv.android.ConvertBitmap import boofcv.struct.image.GrayU8 import chat.simplex.common.R import chat.simplex.common.views.helpers.errorBitmap +import chat.simplex.common.views.helpers.generateNewFileName import chat.simplex.common.views.helpers.getFileName +import chat.simplex.common.views.helpers.removeFile import java.io.ByteArrayOutputStream import java.io.InputStream import java.net.URI @@ -35,16 +37,14 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap { } actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String { - var img = image - var str = compressImageStr(img) - while (str.length > maxDataSize) { - val ratio = sqrt(str.length.toDouble() / maxDataSize.toDouble()) - val clippedRatio = min(ratio, 2.0) - val width = (img.width.toDouble() / clippedRatio).toInt() - val height = img.height * width / img.width - img = Bitmap.createScaledBitmap(img.asAndroidBitmap(), width, height, true).asImageBitmap() - str = compressImageStr(img) - } + val tmpFileName = generateNewFileName("IMG", "png", tmpDir) + val tmpFile = File(tmpDir, tmpFileName) + val output = FileOutputStream(tmpFile) + compressImageData(image, true).writeTo(output) + output.flush() + output.close() + var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt()) + removeFile(tmpFileName) return str } diff --git a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c index b9b5277aeb..72d8b0e3a1 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/android/simplex-api.c @@ -71,6 +71,7 @@ extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int le extern char *chat_read_file(const char *path, const char *key, const char *nonce); extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path); extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path); +extern char *chat_resize_image_to_str_size(const char *from_path, int max_size); JNIEXPORT jobjectArray JNICALL Java_chat_simplex_common_platform_CoreKt_chatMigrateInit(JNIEnv *env, __unused jclass clazz, jstring dbPath, jstring dbKey, jstring confirm) { @@ -244,3 +245,11 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla (*env)->ReleaseStringUTFChars(env, to_path, _to_path); return res; } + +JNIEXPORT jstring JNICALL +Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) { + const char *_from_path = encode_to_utf8_chars(env, from_path); + jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size)); + (*env)->ReleaseStringUTFChars(env, from_path, _from_path); + return res; +} diff --git a/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c b/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c index 5c921c400d..0febec3d2e 100644 --- a/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c +++ b/apps/multiplatform/common/src/commonMain/cpp/desktop/simplex-api.c @@ -44,6 +44,7 @@ extern char *chat_write_file(chat_ctrl ctrl, const char *path, char *ptr, int le extern char *chat_read_file(const char *path, const char *key, const char *nonce); extern char *chat_encrypt_file(chat_ctrl ctrl, const char *from_path, const char *to_path); extern char *chat_decrypt_file(const char *from_path, const char *key, const char *nonce, const char *to_path); +extern char *chat_resize_image_to_str_size(const char *from_path, int max_size); // As a reference: https://stackoverflow.com/a/60002045 jstring decode_to_utf8_string(JNIEnv *env, char *string) { @@ -254,3 +255,11 @@ Java_chat_simplex_common_platform_CoreKt_chatDecryptFile(JNIEnv *env, jclass cla (*env)->ReleaseStringUTFChars(env, to_path, _to_path); return res; } + +JNIEXPORT jstring JNICALL +Java_chat_simplex_common_platform_CoreKt_chatResizeImageToStrSize(JNIEnv *env, jclass clazz, jstring from_path, jint max_size) { + const char *_from_path = encode_to_utf8_chars(env, from_path); + jstring res = decode_to_utf8_string(env, chat_resize_image_to_str_size(_from_path, max_size)); + (*env)->ReleaseStringUTFChars(env, from_path, _from_path); + return res; +} diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt index 57b93d4d6e..1cf9172595 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Core.kt @@ -35,6 +35,7 @@ external fun chatWriteFile(ctrl: ChatCtrl, path: String, buffer: ByteBuffer): St external fun chatReadFile(path: String, key: String, nonce: String): Array external fun chatEncryptFile(ctrl: ChatCtrl, fromPath: String, toPath: String): String external fun chatDecryptFile(fromPath: String, key: String, nonce: String, toPath: String): String +external fun chatResizeImageToStrSize(fromPath: String, maxSize: Int): String val chatModel: ChatModel get() = chatController.chatModel diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Images.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Images.kt index fca69d5398..449fcb4396 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Images.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Images.kt @@ -7,6 +7,7 @@ import java.io.InputStream import java.net.URI expect fun base64ToBitmap(base64ImageString: String): ImageBitmap +// XXX: Not a part of platform services anymore? expect fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String expect fun resizeImageToDataSize(image: ImageBitmap, usePng: Boolean, maxDataSize: Long): ByteArrayOutputStream expect fun cropToSquare(image: ImageBitmap): ImageBitmap diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt index e65adea70e..236cf63345 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/Images.desktop.kt @@ -3,6 +3,8 @@ package chat.simplex.common.platform import androidx.compose.ui.graphics.* import boofcv.io.image.ConvertBufferedImage import boofcv.struct.image.GrayU8 +import chat.simplex.common.views.helpers.generateNewFileName +import chat.simplex.common.views.helpers.removeFile import chat.simplex.res.MR import org.jetbrains.skia.Image import java.awt.RenderingHints @@ -32,18 +34,17 @@ actual fun base64ToBitmap(base64ImageString: String): ImageBitmap { } actual fun resizeImageToStrSize(image: ImageBitmap, maxDataSize: Long): String { - var img = image - var str = compressImageStr(img) - while (str.length > maxDataSize) { - val ratio = sqrt(str.length.toDouble() / maxDataSize.toDouble()) - val clippedRatio = kotlin.math.min(ratio, 2.0) - val width = (img.width.toDouble() / clippedRatio).toInt() - val height = img.height * width / img.width - img = img.scale(width, height) - str = compressImageStr(img) - } + val tmpFileName = generateNewFileName("IMG", "png", tmpDir) + val tmpFile = File(tmpDir, tmpFileName) + val output = FileOutputStream(tmpFile) + compressImageData(image, true).writeTo(output) + output.flush() + output.close() + var str = chatResizeImageToStrSize(tmpFileName, maxDataSize.toInt()) + removeFile(tmpFileName) return str } + actual fun resizeImageToDataSize(image: ImageBitmap, usePng: Boolean, maxDataSize: Long): ByteArrayOutputStream { var img = image var stream = compressImageData(img, usePng)