1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <mbgl/style/image.hpp>
#include <mbgl/util/exception.hpp>
#include "image.hpp"
namespace mbgl {
namespace android {
mbgl::style::Image Image::getImage(jni::JNIEnv& env, jni::Object<Image> image) {
static auto widthField = Image::javaClass.GetField<jni::jint>(env, "width");
static auto heightField = Image::javaClass.GetField<jni::jint>(env, "height");
static auto pixelRatioField = Image::javaClass.GetField<jni::jfloat>(env, "pixelRatio");
static auto bufferField = Image::javaClass.GetField<jni::Array<jbyte>>(env, "buffer");
static auto nameField = Image::javaClass.GetField<jni::String>(env, "name");
auto height = image.Get(env, heightField);
auto width = image.Get(env, widthField);
auto pixelRatio = image.Get(env, pixelRatioField);
auto pixels = image.Get(env, bufferField);
auto name = jni::Make<std::string>(env, image.Get(env, nameField));
jni::NullCheck(env, &pixels);
std::size_t size = pixels.Length(env);
mbgl::PremultipliedImage premultipliedImage({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) });
if (premultipliedImage.bytes() != uint32_t(size)) {
throw mbgl::util::SpriteImageException("Sprite image pixel count mismatch");
}
jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
return mbgl::style::Image {name, std::move(premultipliedImage), pixelRatio};
}
void Image::registerNative(jni::JNIEnv &env) {
// Lookup the class
Image::javaClass = *jni::Class<Image>::Find(env).NewGlobalRef(env).release();
}
jni::Class<Image> Image::javaClass;
} // namespace android
} // namespace mb
|