Fix odd scaling issues during decoding

This commit is contained in:
Cody Henthorne 2023-10-31 10:50:41 +01:00 committed by Oscar Mira
parent c805095cea
commit 68b77dd77e

View file

@ -1,6 +1,8 @@
#include <android/log.h>
#include <jni.h> #include <jni.h>
#include <webp/demux.h> #include <webp/demux.h>
#include <android/log.h>
#include <algorithm>
#include <string> #include <string>
#define TAG "WebpResourceDecoder" #define TAG "WebpResourceDecoder"
@ -16,7 +18,6 @@ jobject createBitmap(JNIEnv *env, int width, int height, const uint8_t *pixels)
jobject argb8888Config = env->GetStaticObjectField(jbitmapConfigClass, jbitmapConfigARGB8888Field); jobject argb8888Config = env->GetStaticObjectField(jbitmapConfigClass, jbitmapConfigARGB8888Field);
jobject jbitmap = env->CallStaticObjectMethod(jbitmapClass, jbitmapCreateBitmapMethod, intArray, 0, width, width, height, argb8888Config); jobject jbitmap = env->CallStaticObjectMethod(jbitmapClass, jbitmapCreateBitmapMethod, intArray, 0, width, width, height, argb8888Config);
env->DeleteLocalRef(argb8888Config);
return jbitmap; return jbitmap;
} }
@ -29,14 +30,14 @@ jobject nativeDecodeBitmapScaled(JNIEnv *env, jobject, jbyteArray data, jint req
WebPBitstreamFeatures features; WebPBitstreamFeatures features;
if (WebPGetFeatures(buffer, bufferLength, &features) != VP8_STATUS_OK) { if (WebPGetFeatures(buffer, bufferLength, &features) != VP8_STATUS_OK) {
__android_log_write(ANDROID_LOG_WARN, TAG, "GetFeatures"); __android_log_write(ANDROID_LOG_WARN, TAG, "GetFeatures");
env->ReleaseByteArrayElements(data, javaBytes, 0); env->ReleaseByteArrayElements(data, javaBytes, JNI_ABORT);
return nullptr; return nullptr;
} }
WebPDecoderConfig config; WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config)) { if (!WebPInitDecoderConfig(&config)) {
__android_log_write(ANDROID_LOG_WARN, TAG, "Init decoder config"); __android_log_write(ANDROID_LOG_WARN, TAG, "Init decoder config");
env->ReleaseByteArrayElements(data, javaBytes, 0); env->ReleaseByteArrayElements(data, javaBytes, JNI_ABORT);
return nullptr; return nullptr;
} }
@ -44,17 +45,13 @@ jobject nativeDecodeBitmapScaled(JNIEnv *env, jobject, jbyteArray data, jint req
config.output.colorspace = MODE_BGRA; config.output.colorspace = MODE_BGRA;
if (requestedWidth > 0 && requestedHeight > 0 && features.width > 0 && features.height > 0 && (requestedWidth < features.width || requestedHeight < features.height)) { if (requestedWidth > 0 && requestedHeight > 0 && features.width > 0 && features.height > 0 && (requestedWidth < features.width || requestedHeight < features.height)) {
float hRatio = 1.0; double widthScale = static_cast<double>(requestedWidth) / features.width;
float vRatio = 1.0; double heightScale = static_cast<double>(requestedHeight) / features.height;
if (features.width >= features.height) { double requiredScale = std::min(widthScale, heightScale);
vRatio = static_cast<float>(features.height) / static_cast<float>(features.width);
} else {
hRatio = static_cast<float>(features.width) / static_cast<float>(features.height);
}
config.options.use_scaling = 1; config.options.use_scaling = 1;
config.options.scaled_width = static_cast<int>(static_cast<float>(requestedWidth) * hRatio); config.options.scaled_width = static_cast<int>(requiredScale * features.width);
config.options.scaled_height = static_cast<int>(static_cast<float>(requestedHeight) * vRatio); config.options.scaled_height = static_cast<int>(requiredScale * features.height);
} }
uint8_t *pixels = nullptr; uint8_t *pixels = nullptr;
@ -63,8 +60,7 @@ jobject nativeDecodeBitmapScaled(JNIEnv *env, jobject, jbyteArray data, jint req
VP8StatusCode result = WebPDecode(buffer, bufferLength, &config); VP8StatusCode result = WebPDecode(buffer, bufferLength, &config);
if (result != VP8_STATUS_OK) { if (result != VP8_STATUS_OK) {
__android_log_write(ANDROID_LOG_WARN, TAG, ("Scaled WebPDecode failed (" + std::to_string(result) + ") Trying without scaled").c_str()); __android_log_write(ANDROID_LOG_WARN, TAG, ("Scaled WebPDecode failed (" + std::to_string(result) + ")").c_str());
pixels = WebPDecodeBGRA(buffer, bufferLength, &width, &height);
} else { } else {
pixels = config.output.u.RGBA.rgba; pixels = config.output.u.RGBA.rgba;
width = config.output.width; width = config.output.width;
@ -77,7 +73,7 @@ jobject nativeDecodeBitmapScaled(JNIEnv *env, jobject, jbyteArray data, jint req
} }
WebPFree(pixels); WebPFree(pixels);
env->ReleaseByteArrayElements(data, javaBytes, 0); env->ReleaseByteArrayElements(data, javaBytes, JNI_ABORT);
return jbitmap; return jbitmap;
} }