#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;

extern "C" {
  JNIEXPORT void JNICALL Java_com_lego_minddroid_SampleView_FindLight(JNIEnv* env, jobject thiz, jlong _imgInp, jlong _imgRes, jintArray array)
{
    jint*  _array = env->GetIntArrayElements(array, 0);

    Mat* pMatInp=(Mat*)_imgInp;
    Mat* pMatRes=(Mat*)_imgRes;
    Mat m = Mat(pMatInp->rows,pMatInp->cols,CV_8UC4);
    vector<Mat> planes;
    //Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
    //Please make attention about BGRA byte order
    //ARGB stored in java as int array becomes BGRA at native level
    cvtColor(*pMatInp, m, CV_BGR2HSV, 4);

    split(*pMatRes,planes);
    for(int i=0; i<4; ++i ){
      planes[i] = Mat::zeros(planes[i].rows,planes[i].cols, CV_8UC1); //Mat::eye(planes[i].rows,planes[i].cols, CV_8UC1)*255;
    }

    Scalar lightLower = Scalar(0, 0, 220);
    Scalar lightUpper = Scalar(360, 10, 255);

    //for(int i=0; i<4; ++i ){
      inRange(m, lightLower, lightUpper, planes[0]);
      //}

    merge(planes,*pMatRes);

    circle(*pMatRes, Point(100, 50), 10, Scalar(255,0,255,255));
    _array[1] = 2*_array[0];

    env->ReleaseIntArrayElements(array, _array, 0);

}

}
