|
7 | 7 | #include <vector> |
8 | 8 | #include <float.h> |
9 | 9 | #include <arm_fp16.h> |
10 | | - |
11 | | -extern "C" JNIEXPORT jstring JNICALL |
12 | | -Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_stringFromJNI( |
13 | | - JNIEnv *env, |
14 | | - jobject /* this */) { |
15 | | - std::string hello = "Hello from C++"; |
16 | | - return env->NewStringUTF(hello.c_str()); |
17 | | -} |
18 | | - |
19 | | -jint throwCannotReadFileException(JNIEnv *env) { |
20 | | - jclass exClass; |
21 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/CantReadHeifFileException"); |
22 | | - return env->ThrowNew(exClass, ""); |
23 | | -} |
24 | | - |
25 | | -jint throwBitDepthException(JNIEnv *env) { |
26 | | - jclass exClass; |
27 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/CorruptedBitDepthException"); |
28 | | - return env->ThrowNew(exClass, ""); |
29 | | -} |
30 | | - |
31 | | -jint throwCoderCreationException(JNIEnv *env) { |
32 | | - jclass exClass; |
33 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/CantCreateCodecException"); |
34 | | - return env->ThrowNew(exClass, ""); |
35 | | -} |
36 | | - |
37 | | -jint throwCantDecodeImageException(JNIEnv *env) { |
38 | | - jclass exClass; |
39 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/CantDecoderImageException"); |
40 | | - return env->ThrowNew(exClass, ""); |
41 | | -} |
42 | | - |
43 | | -jint throwInvalidScale(JNIEnv *env) { |
44 | | - jclass exClass; |
45 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/HeifCantScaleException"); |
46 | | - return env->ThrowNew(exClass, ""); |
47 | | -} |
48 | | - |
49 | | -jint throwCantEncodeImageException(JNIEnv *env) { |
50 | | - jclass exClass; |
51 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/CantEncodeImageException"); |
52 | | - return env->ThrowNew(exClass, ""); |
53 | | -} |
54 | | - |
55 | | -jint throwInvalidPixelsFormat(JNIEnv *env) { |
56 | | - jclass exClass; |
57 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/UnsupportedImageFormatException"); |
58 | | - return env->ThrowNew(exClass, ""); |
59 | | -} |
60 | | - |
61 | | -jint throwPixelsException(JNIEnv *env) { |
62 | | - jclass exClass; |
63 | | - exClass = env->FindClass("com/radzivon/bartoshyk/avif/coder/GetPixelsException"); |
64 | | - return env->ThrowNew(exClass, ""); |
65 | | -} |
| 10 | +#include "jni_exception.h" |
| 11 | +#include "scaler.h" |
66 | 12 |
|
67 | 13 | struct AvifMemEncoder { |
68 | 14 | std::vector<char> buffer; |
@@ -322,7 +268,7 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_getSizeImpl(JNIEnv *env, jobjec |
322 | 268 | }); |
323 | 269 | options->convert_hdr_to_8bit = true; |
324 | 270 | result = heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, |
325 | | - nullptr); |
| 271 | + options.get()); |
326 | 272 | options.reset(); |
327 | 273 | if (result.code != heif_error_Ok) { |
328 | 274 | heif_image_handle_release(handle); |
@@ -392,6 +338,19 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeImpl(JNIEnv *env, jobject |
392 | 338 | return static_cast<jobject>(nullptr); |
393 | 339 | } |
394 | 340 |
|
| 341 | +// auto totalMemoryImageSize = heif_image_get_width(img, heif_channel_interleaved) * |
| 342 | +// heif_image_get_height(img, heif_channel_interleaved) * 4; |
| 343 | +// auto maxAvailableMemory = 34 * 1024 * 1024; |
| 344 | +// |
| 345 | +// if (scaledHeight <= 0 && scaledWidth <= 0 && totalMemoryImageSize > maxAvailableMemory) { |
| 346 | +// auto scaledSize = resizeAspect( |
| 347 | +// std::pair<int, int>(heif_image_get_width(img, heif_channel_interleaved), |
| 348 | +// heif_image_get_height(img, heif_channel_interleaved)), |
| 349 | +// std::pair<int, int>(2550, 1440)); |
| 350 | +// scaledWidth = scaledSize.first; |
| 351 | +// scaledHeight = scaledSize.second; |
| 352 | +// } |
| 353 | + |
395 | 354 | if (scaledHeight > 0 && scaledWidth > 0) { |
396 | 355 | heif_image *scaledImg; |
397 | 356 | result = heif_image_scale_image(img, &scaledImg, scaledWidth, scaledHeight, nullptr); |
@@ -445,9 +404,9 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeImpl(JNIEnv *env, jobject |
445 | 404 | "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); |
446 | 405 | jobject bitmapObj = env->CallStaticObjectMethod(bitmapClass, createBitmapMethodID, |
447 | 406 | imageWidth, imageHeight, rgba8888Obj); |
448 | | - auto returningLength = stride * imageHeight; |
449 | | - jintArray pixels = env->NewIntArray(stride * imageHeight); |
450 | | - env->SetIntArrayRegion(pixels, 0, (jsize) returningLength / sizeof(uint32_t), |
| 407 | + auto returningLength = stride * imageHeight / sizeof(uint32_t); |
| 408 | + jintArray pixels = env->NewIntArray((jsize) returningLength); |
| 409 | + env->SetIntArrayRegion(pixels, 0, (jsize) returningLength, |
451 | 410 | reinterpret_cast<const jint *>(dstARGB.get())); |
452 | 411 | dstARGB.reset(); |
453 | 412 | jmethodID setPixelsMid = env->GetMethodID(bitmapClass, "setPixels", "([IIIIIII)V"); |
|
0 commit comments