@@ -8,9 +8,10 @@ namespace android {
|
||||
|
||||
class PixelGuard {
|
||||
public:
|
||||
PixelGuard(jni::JNIEnv& env_, const jni::Object<Bitmap>& bitmap_) : env(env_), bitmap(bitmap_) {
|
||||
const int result = AndroidBitmap_lockPixels(&env, jni::Unwrap(*bitmap),
|
||||
reinterpret_cast<void**>(&address));
|
||||
PixelGuard(jni::JNIEnv& env_, const jni::Object<Bitmap>& bitmap_)
|
||||
: env(env_),
|
||||
bitmap(bitmap_) {
|
||||
const int result = AndroidBitmap_lockPixels(&env, jni::Unwrap(*bitmap), reinterpret_cast<void**>(&address));
|
||||
if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
|
||||
throw std::runtime_error("bitmap decoding: could not lock pixels");
|
||||
}
|
||||
@@ -23,9 +24,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
auto* get() {
|
||||
return address;
|
||||
}
|
||||
auto* get() { return address; }
|
||||
|
||||
private:
|
||||
jni::JNIEnv& env;
|
||||
@@ -36,16 +35,16 @@ private:
|
||||
jni::Local<jni::Object<Bitmap::Config>> Bitmap::Config::Create(jni::JNIEnv& env, Value value) {
|
||||
static auto& _class = jni::Class<Config>::Singleton(env);
|
||||
switch (value) {
|
||||
case ALPHA_8:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ALPHA_8"));
|
||||
case ARGB_4444:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ARGB_4444"));
|
||||
case ARGB_8888:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ARGB_8888"));
|
||||
case RGB_565:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "RGB_565"));
|
||||
default:
|
||||
throw std::runtime_error("invalid enum value for Bitmap.Config");
|
||||
case ALPHA_8:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ALPHA_8"));
|
||||
case ARGB_4444:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ARGB_4444"));
|
||||
case ARGB_8888:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "ARGB_8888"));
|
||||
case RGB_565:
|
||||
return _class.Get(env, _class.GetStaticField<jni::Object<Config>>(env, "RGB_565"));
|
||||
default:
|
||||
throw std::runtime_error("invalid enum value for Bitmap.Config");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +58,8 @@ jni::Local<jni::Object<Bitmap>> Bitmap::CreateBitmap(jni::JNIEnv& env,
|
||||
jni::jint height,
|
||||
const jni::Object<Config>& config) {
|
||||
static auto& _class = jni::Class<Bitmap>::Singleton(env);
|
||||
static auto method = _class.GetStaticMethod<jni::Object<Bitmap> (jni::jint, jni::jint, jni::Object<Config>)>(env, "createBitmap");
|
||||
static auto method = _class.GetStaticMethod<jni::Object<Bitmap>(jni::jint, jni::jint, jni::Object<Config>)>(
|
||||
env, "createBitmap");
|
||||
|
||||
return _class.Call(env, method, width, height, config);
|
||||
}
|
||||
@@ -77,15 +77,15 @@ PremultipliedImage Bitmap::GetImage(jni::JNIEnv& env, const jni::Object<Bitmap>&
|
||||
PixelGuard guard(env, bitmap);
|
||||
|
||||
// Copy the Android Bitmap into the PremultipliedImage.
|
||||
auto pixels =
|
||||
std::make_unique<uint8_t[]>(info.width * info.height * PremultipliedImage::channels);
|
||||
auto pixels = std::make_unique<uint8_t[]>(info.width * info.height * PremultipliedImage::channels);
|
||||
for (uint32_t y = 0; y < info.height; y++) {
|
||||
auto begin = guard.get() + y * info.stride;
|
||||
std::copy(begin, begin + info.width * PremultipliedImage::channels,
|
||||
pixels.get() + y * info.width * PremultipliedImage::channels);
|
||||
std::copy(begin,
|
||||
begin + info.width * PremultipliedImage::channels,
|
||||
pixels.get() + y * info.width * PremultipliedImage::channels);
|
||||
}
|
||||
|
||||
return { Size{ info.width, info.height }, std::move(pixels) };
|
||||
return {Size{info.width, info.height}, std::move(pixels)};
|
||||
}
|
||||
|
||||
jni::Local<jni::Object<Bitmap>> Bitmap::CreateBitmap(jni::JNIEnv& env, const PremultipliedImage& image) {
|
||||
@@ -115,7 +115,7 @@ jni::Local<jni::Object<Bitmap>> Bitmap::CreateBitmap(jni::JNIEnv& env, const Pre
|
||||
|
||||
jni::Local<jni::Object<Bitmap>> Bitmap::Copy(jni::JNIEnv& env, const jni::Object<Bitmap>& bitmap) {
|
||||
static auto& klass = jni::Class<Bitmap>::Singleton(env);
|
||||
static auto copy = klass.GetMethod<jni::Object<Bitmap> (jni::Object<Config>, jni::jboolean)>(env, "copy");
|
||||
static auto copy = klass.GetMethod<jni::Object<Bitmap>(jni::Object<Config>, jni::jboolean)>(env, "copy");
|
||||
|
||||
return bitmap.Call(env, copy, Bitmap::Config::Create(env, Bitmap::Config::Value::ARGB_8888), jni::jni_false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user