java rgb565_[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#ifndef eprintf

#define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)

#endif

#define RGB565_R(p) ((((p) & 0xF800) >> 11) <

#define RGB565_G(p) ((((p) & 0x7E0 ) >> 5)  <

#define RGB565_B(p) ( ((p) & 0x1F  )        <

#define MAKE_RGB565(r,g,b) ((((r) >> 3) <> 2) <> 3))

#define RGBA_A(p) (((p) & 0xFF000000) >> 24)

#define RGBA_R(p) (((p) & 0x00FF0000) >> 16)

#define RGBA_G(p) (((p) & 0x0000FF00) >>  8)

#define RGBA_B(p)  ((p) & 0x000000FF)

#define MAKE_RGBA(r,g,b,a) (((a) <

JNIEXPORT void JNICALL Java_com_yxcorp_hello_Effect_update

(JNIEnv *env, jclass clazz, jobject zBitmap) {

JNIEnv J = *env;

if (zBitmap == NULL) {

eprintf("bitmap is null\n");

return;

}

// Get bitmap info

AndroidBitmapInfo info;

memset(&info, 0, sizeof(info));

AndroidBitmap_getInfo(env, zBitmap, &info);

// Check format, only RGB565 & RGBA are supported

if (info.width <= 0 || info.height <= 0 ||

(info.format != ANDROID_BITMAP_FORMAT_RGB_565 && info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {

eprintf("invalid bitmap\n");

J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");

return;

}

// Lock the bitmap to get the buffer

void * pixels = NULL;

int res = AndroidBitmap_lockPixels(env, zBitmap, &pixels);

if (pixels == NULL) {

eprintf("fail to lock bitmap: %d\n", res);

J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");

return;

}

eprintf("Effect: %dx%d, %d\n", info.width, info.height, info.format);

int x = 0, y = 0;

// From top to bottom

for (y = 0; y 

// From left to right

for (x = 0; x 

int a = 0, r = 0, g = 0, b = 0;

void *pixel = NULL;

// Get each pixel by format

if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {

pixel = ((uint16_t *)pixels) + y * info.width + x;

uint16_t v = *(uint16_t *)pixel;

r = RGB565_R(v);

g = RGB565_G(v);

b = RGB565_B(v);

} else {// RGBA

pixel = ((uint32_t *)pixels) + y * info.width + x;

uint32_t v = *(uint32_t *)pixel;

a = RGBA_A(v);

r = RGBA_R(v);

g = RGBA_G(v);

b = RGBA_B(v);

}

// Grayscale

int gray = (r * 38 + g * 75 + b * 15) >> 7;

// Write the pixel back

if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {

*((uint16_t *)pixel) = MAKE_RGB565(gray, gray, gray);

} else {// RGBA

*((uint32_t *)pixel) = MAKE_RGBA(gray, gray, gray, a);

}

}

}

AndroidBitmap_unlockPixels(env, zBitmap);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值