extern "C" JNIEXPORT jintArray JNICALL Java_com_almalence_plugins_processing_objectremoval_AlmaCLRShot_NV21toARGB
(
	JNIEnv* env,
	jobject thiz,
	jint inptr,
	jobject srcSize,
	jobject rect,
	jobject dstSize
)
{
	LOGD("NV21toARGB - start");

	Uint32 * pixels;
	jintArray jpixels = NULL;

	jclass src_size = env->GetObjectClass(srcSize);
	jfieldID id_srcW = env->GetFieldID(src_size, "width", "I");
	jint srcW = env->GetIntField(srcSize,id_srcW);
	jfieldID id_srcH = env->GetFieldID(src_size, "height", "I");
	jint srcH = env->GetIntField(srcSize,id_srcH);

	jclass class_rect = env->GetObjectClass(rect);
	jfieldID id_left = env->GetFieldID(class_rect, "left", "I");
	jint left = env->GetIntField(rect,id_left);
	jfieldID id_top = env->GetFieldID(class_rect, "top", "I");
	jint top = env->GetIntField(rect,id_top);
	jfieldID id_right = env->GetFieldID(class_rect, "right", "I");
	jint right = env->GetIntField(rect,id_right);
	jfieldID id_bottom = env->GetFieldID(class_rect, "bottom", "I");
	jint bottom = env->GetIntField(rect,id_bottom);

	jclass dst_size = env->GetObjectClass(dstSize);
	jfieldID id_dstW = env->GetFieldID(dst_size, "width", "I");
	jint dstW = env->GetIntField(dstSize,id_dstW);
	jfieldID id_dstH = env->GetFieldID(dst_size, "height", "I");
	jint dstH = env->GetIntField(dstSize,id_dstH);

	LOGD("inptr = %d srcW = %d srcH = %d ", inptr, srcW, srcH);
	LOGD("left = %d top = %d right = %d bottom = %d ", left, top, right, bottom);
	LOGD("dstW = %d dstH = %d", dstW, dstH);

	jpixels = env->NewIntArray(dstW*dstH);
	LOGD("Memory alloc size = %d * %d", dstW, dstH);
	pixels = (Uint32 *)env->GetIntArrayElements(jpixels, NULL);

	NV21_to_RGB_scaled((Uint8 *)inptr, srcW, srcH, left, top, right - left, bottom - top, dstW, dstH, 4, (Uint8 *)pixels);

	env->ReleaseIntArrayElements(jpixels, (jint*)pixels, 0);

	LOGD("NV21toARGB - end");

	return jpixels;
}
extern "C" JNIEXPORT jbyte* JNICALL Java_com_almalence_plugins_processing_groupshot_AlmaShotSeamless_Preview
(
	JNIEnv* env,
	jobject thiz,
	jint baseFrame,
	jint inWidth,
	jint inHeight,
	jint outWidth,
	jint outHeight,
	jbyteArray jlayout
)
{
	LOGD("Preview - start");

	Uint8 *layout;
	int crop[4];
	int x,y;

	layout = (Uint8 *)env->GetByteArrayElements(jlayout, NULL);

	outBuffer = (Uint8 *)malloc(inWidth * inHeight * 3 / 2);
	LOGD("alloc %d byte yvu memory", inWidth * inHeight * 3 / 2);
	LOGD("base frame = %d", baseFrame);

	Seamless_Process(instance,
			outBuffer,
			layout, NULL,				// prescribed layout
			baseFrame,							// base frame (from which to take background)
			// Set it to non-zero to get very quick stitched result
			2,					// quick method
			&crop[0], &crop[1], &crop[2], &crop[3]);

	env->ReleaseByteArrayElements(jlayout, (jbyte*)layout, JNI_ABORT);

	jbyteArray jpixels = NULL;
	Uint8 * pixels;

	jpixels = env->NewByteArray(outWidth * outHeight * 4);
	LOGD("alloc %d byte argb memory", outWidth * outHeight * 4);
	pixels = (Uint8 *)env->GetByteArrayElements(jpixels, NULL);

	NV21_to_RGB_scaled(outBuffer, inWidth, inHeight, 0, 0, inWidth, inHeight, outWidth, outHeight, 4, (Uint8 *)pixels);

	free(outBuffer);

	env->ReleaseByteArrayElements(jpixels, (jbyte*)pixels, JNI_ABORT);

	LOGD("Preview - end");
	return (jbyte *)jpixels;
}