Mat.cpp 15.6 KB
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
#include <jni.h>
/*
#include <android/log.h>
#define TEGRA_LOG_TAG "MAT_CPP"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, TEGRA_LOG_TAG, __VA_ARGS__))
*/


#include "opencv2/core/core.hpp"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nType
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->type(  );
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nRows
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->rows;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nCols
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->cols;
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nData
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return (jlong) me->data;
}

JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_nIsEmpty
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->empty();
}

JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nSize
	(JNIEnv* env, jclass cls, jlong self)
{
    try {
#ifdef DEBUG
        LOGD("core::Mat::nSize()");
#endif // DEBUG
        cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
        cv::Size _retval_ = me->size();

        jdoubleArray _da_retval_ = env->NewDoubleArray(2);
	jdouble _tmp_retval_[4] = {_retval_.width, _retval_.height};
	env->SetDoubleArrayRegion(_da_retval_, 0, 2, _tmp_retval_);
        return _da_retval_;
    } catch(cv::Exception e) {
#ifdef DEBUG
        LOGD("core::Mat::nSize() catched cv::Exception: %s", e.what());
#endif // DEBUG
        jclass je = env->FindClass("org/opencv/core/CvException");
        if(!je) je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, e.what());
        return 0;
    } catch (...) {
#ifdef DEBUG
        LOGD("core::Mat::nSize() catched unknown exception (...)");
#endif // DEBUG
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Unknown exception in JNI code {core::n_1mean__JJ()}");
        return 0;
    }
}

JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_nIsCont
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->isContinuous();
}

JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_nIsSubmat
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return me->isSubmatrix();
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nSubmat
	(JNIEnv* env, jclass cls, jlong self, jint r1, jint r2, jint c1, jint c2)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    return (jlong) new cv::Mat(*me, cv::Range(r1, r2>0 ? r2 : me->rows), cv::Range(c1, c2>0 ? c2 : me->cols));
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nClone
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    cv::Mat* it = new cv::Mat();
	me->copyTo(*it);
    return (jlong) it;
}

// unlike other nPut()-s this one (with double[]) should convert input values to correct type
#define PUT_ITEM(T, R, C) { T*dst = (T*)me->ptr(R, C); for(int ch=0; ch<me->channels() && count>0; count--,ch++,src++,dst++) *dst = cv::saturate_cast<T>(*src); }

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jdoubleArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(!me || !me->data) return 0;  // no native object behind
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range

	int rest = ((me->rows - row) * me->cols - col) * me->channels();
	if(count>rest) count = rest;
	int res = count;
	double* values = (double*)env->GetPrimitiveArrayCritical(vals, 0);
	double* src = values;
	int r, c;
	for(c=col; c<me->cols && count>0; c++)
	{
		switch(me->depth()) {
			case CV_8U:  PUT_ITEM(uchar,  row, c); break;
			case CV_8S:  PUT_ITEM(schar,  row, c); break;
			case CV_16U: PUT_ITEM(ushort, row, c); break;
			case CV_16S: PUT_ITEM(short,  row, c); break;
			case CV_32S: PUT_ITEM(int,    row, c); break;
			case CV_32F: PUT_ITEM(float,  row, c); break;
			case CV_64F: PUT_ITEM(double, row, c); break;
		}
	}

	for(r=row+1; r<me->rows && count>0; r++)
		for(c=0; c<me->cols && count>0; c++)
		{
			switch(me->depth()) {
				case CV_8U:  PUT_ITEM(uchar,  r, c); break;
				case CV_8S:  PUT_ITEM(schar,  r, c); break;
				case CV_16U: PUT_ITEM(ushort, r, c); break;
				case CV_16S: PUT_ITEM(short,  r, c); break;
				case CV_32S: PUT_ITEM(int,    r, c); break;
				case CV_32F: PUT_ITEM(float,  r, c); break;
				case CV_64F: PUT_ITEM(double, r, c); break;
			}
		}

	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}


#ifdef __cplusplus
}
#endif

template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, char* buff)
{
	if(! m) return 0;
	if(! buff) return 0;

	count *= sizeof(T);
	int rest = ((m->rows - row) * m->cols - col) * m->channels() * sizeof(T);
	if(count>rest) count = rest;
	int res = count;

	if( m->isContinuous() )
	{
		memcpy(m->ptr(row, col), buff, count);
	} else {
		// row by row
		int num = (m->cols - col - 1) * m->channels() * sizeof(T); // 1st partial row
		if(count<num) num = count;
		uchar* data = m->ptr(row++, col);
		while(count>0){
			memcpy(data, buff, num);
			count -= num;
			buff += num;
			num = m->cols * m->channels() * sizeof(T);
			if(count<num) num = count;
			data = m->ptr(row++, 0);
		}
	}
	return res;
}


#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jbyteArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_put<char>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jshortArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_put<short>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jintArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_32S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_put<int>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jfloatArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_32F) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_put<float>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}


#ifdef __cplusplus
}
#endif


template<typename T> int mat_get(cv::Mat* m, int row, int col, int count, char* buff)
{
	if(! m) return 0;
	if(! buff) return 0;

	count *= sizeof(T);
	int rest = ((m->rows - row) * m->cols - col) * m->channels() * sizeof(T);
	if(count>rest) count = rest;
	int res = count;

	if( m->isContinuous() )
	{
		memcpy(buff, m->ptr(row, col), count);
	} else {
		// row by row
		int num = (m->cols - col - 1) * m->channels() * sizeof(T); // 1st partial row
		if(count<num) num = count;
		uchar* data = m->ptr(row++, col);
		while(count>0){//TODO: recheck this cycle for the case col!=0
			memcpy(buff, data, num);
			count -= num;
			buff += num;
			num = m->cols * m->channels() * sizeof(T);
			if(count<num) num = count;
			data = m->ptr(row++, 0);
		}
	}
	return res;
}

#ifdef __cplusplus
extern "C" {
#endif


JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jbyteArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_get<char>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jshortArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_get<short>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jintArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_32S) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_get<int>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jfloatArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_32F) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_get<float>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count, jdoubleArray vals)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->depth() != CV_64F) return 0; // incompatible type
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
	
	char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
	int res = mat_get<double>(me, row, col, count, values);
	env->ReleasePrimitiveArrayCritical(vals, values, 0);
	return res;
}

JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
	(JNIEnv* env, jclass cls, jlong self, jint row, jint col, jint count)
{
	cv::Mat* me = (cv::Mat*) self;
	if(! self) return 0; // no native object behind
	if(me->rows<=row || me->cols<=col) return 0; // indexes out of range

	jdoubleArray res = env->NewDoubleArray(me->channels());
	if(res){
		jdouble buff[me->channels()];
		int i;
		switch(me->depth()){
			case CV_8U:  for(i=0; i<me->channels(); i++) buff[i] = *((unsigned char*) me->ptr(row, col) + i); break;
			case CV_8S:  for(i=0; i<me->channels(); i++) buff[i] = *((signed char*)   me->ptr(row, col) + i); break;
			case CV_16U: for(i=0; i<me->channels(); i++) buff[i] = *((unsigned short*)me->ptr(row, col) + i); break;
			case CV_16S: for(i=0; i<me->channels(); i++) buff[i] = *((signed short*)  me->ptr(row, col) + i); break;
			case CV_32S: for(i=0; i<me->channels(); i++) buff[i] = *((int*)           me->ptr(row, col) + i); break;
			case CV_32F: for(i=0; i<me->channels(); i++) buff[i] = *((float*)         me->ptr(row, col) + i); break;
			case CV_64F: for(i=0; i<me->channels(); i++) buff[i] = *((double*)        me->ptr(row, col) + i); break;
		}
		env->SetDoubleArrayRegion(res, 0, me->channels(), buff);
	}
	return res;
}

JNIEXPORT void JNICALL Java_org_opencv_core_Mat_nSetTo
  (JNIEnv* env, jclass cls, jlong self, jdouble v0, jdouble v1, jdouble v2, jdouble v3)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    me->setTo( cv::Scalar(v0, v1, v2, v3) );
}

JNIEXPORT void JNICALL Java_org_opencv_core_Mat_nCopyTo
  (JNIEnv* env, jclass cls, jlong self, jlong m)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    cv::Mat* _m = (cv::Mat*) m; //TODO: check for NULL
    me->copyTo( *_m );
}

JNIEXPORT jdouble JNICALL Java_org_opencv_core_Mat_nDot
  (JNIEnv* env, jclass cls, jlong self, jlong m)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    cv::Mat* _m = (cv::Mat*) m; //TODO: check for NULL
    return me->dot( *_m );
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nCross
	(JNIEnv* env, jclass cls, jlong self, jlong m)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    cv::Mat* _m = (cv::Mat*) m; //TODO: check for NULL
    return (jlong) new cv::Mat(me->cross( *_m ));
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nInv
	(JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL    
	return (jlong) new cv::Mat(me->inv());
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nCreateMat__
  (JNIEnv* env, jclass cls)
{
    return (jlong) new cv::Mat();
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nEye
    (JNIEnv* env, jclass cls, jint _rows, jint _cols, jint _type)
{
    return (jlong) new cv::Mat(cv::Mat::eye( _rows, _cols, _type ));
}

JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
  (JNIEnv *env, jclass cls, jlong self)
{
	cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    std::stringstream s;
    s << *me;
	return env->NewStringUTF(s.str().c_str());
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nCreateMat__III
  (JNIEnv* env, jclass cls, jint _rows, jint _cols, jint _type)
{
    //LOGD("called with r=%d, c=%d", _rows, _cols);
    return (jlong) new cv::Mat( _rows, _cols, _type );;
}

JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_nCreateMat__IIIDDDD
  (JNIEnv* env, jclass cls, jint _rows, jint _cols, jint _type, jdouble v0, jdouble v1, jdouble v2, jdouble v3)
{
    return (jlong) new cv::Mat( _rows, _cols, _type, cv::Scalar(v0, v1, v2, v3) );
}

JNIEXPORT void JNICALL Java_org_opencv_core_Mat_nDelete
  (JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    delete me;
}

JNIEXPORT void JNICALL Java_org_opencv_core_Mat_nRelease
  (JNIEnv* env, jclass cls, jlong self)
{
    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
    me->release();
}

#ifdef __cplusplus
}
#endif