안녕하세요.
이미지 변환을 하기 위해 ndk를 이용하려 합니다. 자바단에서 처리하려니 한 장당 약 0.5~1초의 속도를 소모하게 되어 어려움이 있습니다.
혹시나 싶어 native String 함수를 만들어 출력하니 그 값은 잘나옵니다.
yuv->rgb 함수는 해외에서 돌아다니는것을 참고하였는데, 함수 내부문제일까요?
--- 패키지명.ndk.YUV2GB.java ---
/**
* yuv -> rgb 변환
* @param rgb output
* @param yuv input
* @param width size
* @param height size
*/
public native void yuv2rgb(int[] rgb, byte[] yuv, int width, int height);
--- yuv2rgb_conver.c---
#include <jni.h>
int* rgbData;
int rgbDataSize = 0;
JNIEXPORT void JNICALL Java_패키지명_ndk_YUV2RGB_yuv2rgb(JNIEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
int sz;
int i;
int j;
int Y;
int Cr = 0;
int Cb = 0;
int pixPtr = 0;
int jDiv2 = 0;
int R = 0;
int G = 0;
int B = 0;
int cOff;
int w = width;
int h = height;
sz = w * h;
jbyte* yuv = yuv420sp;
if(rgbDataSize < sz) {
int tmp[sz];
rgbData = &tmp[0];
rgbDataSize = sz;
}
for(j = 0; j < h; j++) {
pixPtr = j * w;
jDiv2 = j >> 1;
for(i = 0; i < w; i++) {
Y = yuv[pixPtr];
if(Y < 0) Y += 255;
if((i & 0x1) != 1) {
cOff = sz + jDiv2 * w + (i >> 1) * 2;
Cb = yuv[cOff];
if(Cb < 0) Cb += 127; else Cb -= 128;
Cr = yuv[cOff + 1];
if(Cr < 0) Cr += 127; else Cr -= 128;
}
R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if(R < 0) R = 0; else if(R > 255) R = 255;
G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if(G < 0) G = 0; else if(G > 255) G = 255;
B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if(B < 0) B = 0; else if(B > 255) B = 255;
rgbData[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}
(*env)->SetIntArrayRegion(env, rgb, 0, sz, ( jint * ) &rgbData[0] );
}
-- Error Log.. --
01-01 14:07:39.124: D/dalvikvm(13999): Trying to load lib /data/app-lib/패키지명-185/libyuv420sp2rgb.so 0x42bb8b18
01-01 14:07:39.134: E/ViewRootImpl(13999): sendUserActionEvent() mView == null
01-01 14:07:39.134: D/dalvikvm(13999): Added shared lib /data/app-lib/패키지명-185/libyuv420sp2rgb.so 0x42bb8b18
01-01 14:07:39.134: D/dalvikvm(13999): No JNI_OnLoad found in /data/app-lib/패키지명-185/libyuv420sp2rgb.so 0x42bb8b18, skipping init
01-01 14:07:39.139: A/libc(13999): Fatal signal 11 (SIGSEGV) at 0x1da00009 (code=1), thread 14301 (Thread-9433)
01-01 14:07:40.424: D/dalvikvm(14317): GC_FOR_ALLOC freed 61K, 8% free 8372K/9060K, paused 20ms, total 21ms
01-01 14:07:40.439: I/dalvikvm-heap(14317): Grow heap (frag case) to 18.161MB for 9830416-byte allocation
01-01 14:07:40.459: D/dalvikvm(14317): GC_FOR_ALLOC freed <1K, 4% free 17972K/18664K, paused 18ms, total 18ms