Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / java / generator / src / cpp / Mat.cpp
1 #define LOG_TAG "org.opencv.core.Mat"
2
3 #include <stdexcept>
4
5 #include "common.h"
6 #include "opencv2/core/core.hpp"
7
8 using namespace cv;
9
10 /// throw java exception
11 static void throwJavaException(JNIEnv *env, const std::exception *e, const char *method) {
12   std::string what = "unknown exception";
13   jclass je = 0;
14   
15   if(e) {
16     std::string exception_type = "std::exception";
17     
18     if(dynamic_cast<const cv::Exception*>(e)) {
19       exception_type = "cv::Exception";
20       je = env->FindClass("org/opencv/core/CvException");
21     }
22
23     what = exception_type + ": " + e->what();
24   }
25   
26   if(!je) je = env->FindClass("java/lang/Exception");
27   env->ThrowNew(je, what.c_str());
28   
29   LOGE("%s caught %s", method, what.c_str());
30   (void)method;        // avoid "unused" warning
31 }
32
33 extern "C" {
34   
35
36 //
37 //   MatXXX::MatXXX()
38 //
39
40
41 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__
42   (JNIEnv*, jclass);
43
44 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__
45   (JNIEnv*, jclass)
46 {
47     LOGD("Mat::n_1Mat__()");
48     return (jlong) new cv::Mat();
49 }
50
51
52
53 //
54 //   Mat::Mat(int rows, int cols, int type)
55 //
56
57 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__III
58   (JNIEnv* env, jclass, jint rows, jint cols, jint type);
59
60 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__III
61   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
62 {
63     static const char method_name[] = "Mat::n_1Mat__III()";
64     try {
65         LOGD("%s", method_name);
66         return (jlong) new Mat( rows, cols, type );
67     } catch(const std::exception &e) {
68         throwJavaException(env, &e, method_name);
69     } catch (...) {
70         throwJavaException(env, 0, method_name);
71     }
72     
73     return 0;
74 }
75
76
77
78 //
79 //   Mat::Mat(Size size, int type)
80 //
81
82 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDI
83   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type);
84
85 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDI
86   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
87 {
88     static const char method_name[] = "Mat::n_1Mat__DDI()";
89     try {
90         LOGD("%s", method_name);
91         Size size((int)size_width, (int)size_height);
92         return (jlong) new Mat( size, type );
93     } catch(const std::exception &e) {
94         throwJavaException(env, &e, method_name);
95     } catch (...) {
96         throwJavaException(env, 0, method_name);
97     }
98     
99     return 0;
100 }
101
102
103
104 //
105 //   Mat::Mat(int rows, int cols, int type, Scalar s)
106 //
107
108 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIDDDD
109   (JNIEnv* env, jclass, jint rows, jint cols, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3);
110
111
112 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIDDDD
113   (JNIEnv* env, jclass, jint rows, jint cols, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
114 {
115     static const char method_name[] = "Mat::n_1Mat__IIIDDDD()";
116     try {
117         LOGD("%s", method_name);
118         Scalar s(s_val0, s_val1, s_val2, s_val3);
119         return (jlong) new Mat( rows, cols, type, s );
120     } catch(const std::exception &e) {
121         throwJavaException(env, &e, method_name);
122     } catch (...) {
123         throwJavaException(env, 0, method_name);
124     }
125     
126     return 0;
127 }
128
129
130
131 //
132 //   Mat::Mat(Size size, int type, Scalar s)
133 //
134
135 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDIDDDD
136   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3);
137
138 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDIDDDD
139   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
140 {
141     static const char method_name[] = "Mat::n_1Mat__DDIDDDD()";
142     try {
143         LOGD("%s", method_name);
144         Size size((int)size_width, (int)size_height);
145         Scalar s(s_val0, s_val1, s_val2, s_val3);
146         return (jlong) new Mat( size, type, s );
147     } catch(const std::exception &e) {
148         throwJavaException(env, &e, method_name);
149     } catch (...) {
150         throwJavaException(env, 0, method_name);
151     }
152     
153     return 0;
154 }
155
156
157
158 //
159 //   Mat::Mat(Mat m, Range rowRange, Range colRange = Range::all())
160 //
161
162 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JIIII
163   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end);
164
165 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JIIII
166   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end)
167 {
168     static const char method_name[] = "Mat::n_1Mat__JIIII()";
169     try {
170         LOGD("%s", method_name);
171         Range rowRange(rowRange_start, rowRange_end);
172         Range colRange(colRange_start, colRange_end);
173         return (jlong) new Mat( (*(Mat*)m_nativeObj), rowRange, colRange );
174     } catch(const std::exception &e) {
175         throwJavaException(env, &e, method_name);
176     } catch (...) {
177         throwJavaException(env, 0, method_name);
178     }
179     
180     return 0;
181 }
182
183
184 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JII
185   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end);
186
187
188 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JII
189   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end)
190 {
191     static const char method_name[] = "Mat::n_1Mat__JII()";
192     try {
193         LOGD("%s", method_name);
194         Range rowRange(rowRange_start, rowRange_end);
195         return (jlong) new Mat( (*(Mat*)m_nativeObj), rowRange );
196     } catch(const std::exception &e) {
197         throwJavaException(env, &e, method_name);
198     } catch (...) {
199         throwJavaException(env, 0, method_name);
200     }
201     
202     return 0;
203 }
204
205
206 //
207 //  Mat Mat::adjustROI(int dtop, int dbottom, int dleft, int dright)
208 //
209
210 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1adjustROI
211   (JNIEnv* env, jclass, jlong self, jint dtop, jint dbottom, jint dleft, jint dright);
212
213 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1adjustROI
214   (JNIEnv* env, jclass, jlong self, jint dtop, jint dbottom, jint dleft, jint dright)
215 {
216     static const char method_name[] = "Mat::n_1adjustROI()";
217     try {
218         LOGD("%s", method_name);
219         Mat* me = (Mat*) self; //TODO: check for NULL
220         Mat _retval_ = me->adjustROI( dtop, dbottom, dleft, dright );
221         return (jlong) new Mat(_retval_);
222     } catch(const std::exception &e) {
223         throwJavaException(env, &e, method_name);
224     } catch (...) {
225         throwJavaException(env, 0, method_name);
226     }
227     
228     return 0;
229 }
230
231
232
233 //
234 //  void Mat::assignTo(Mat m, int type = -1)
235 //
236
237 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI
238   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint type);
239
240 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI
241   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint type)
242 {
243     static const char method_name[] = "Mat::n_1assignTo__JJI()";
244     try {
245         LOGD("%s", method_name);
246         Mat* me = (Mat*) self; //TODO: check for NULL
247         me->assignTo( (*(Mat*)m_nativeObj), type );
248     } catch(const std::exception &e) {
249         throwJavaException(env, &e, method_name);
250     } catch (...) {
251         throwJavaException(env, 0, method_name);
252     }
253 }
254
255
256 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJ
257   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
258
259 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJ
260   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
261 {
262     static const char method_name[] = "Mat::n_1assignTo__JJ()";
263     try {
264         LOGD("%s", method_name);
265         Mat* me = (Mat*) self; //TODO: check for NULL
266         me->assignTo( (*(Mat*)m_nativeObj) );
267     } catch(const std::exception &e) {
268         throwJavaException(env, &e, method_name);
269     } catch (...) {
270         throwJavaException(env, 0, method_name);
271     }
272 }
273
274
275
276 //
277 //  int Mat::channels()
278 //
279
280 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1channels
281   (JNIEnv* env, jclass, jlong self);
282
283 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1channels
284   (JNIEnv* env, jclass, jlong self)
285 {
286     static const char method_name[] = "Mat::n_1channels()";
287     try {
288         LOGD("%s", method_name);
289         Mat* me = (Mat*) self; //TODO: check for NULL
290         return me->channels(  );
291     } catch(const std::exception &e) {
292         throwJavaException(env, &e, method_name);
293     } catch (...) {
294         throwJavaException(env, 0, method_name);
295     }
296     
297     return 0;
298 }
299
300
301
302 //
303 //  int Mat::checkVector(int elemChannels, int depth = -1, bool requireContinuous = true)
304 //
305
306 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ
307   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth, jboolean requireContinuous);
308
309 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ
310   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth, jboolean requireContinuous)
311 {
312     static const char method_name[] = "Mat::n_1checkVector__JIIZ()";
313     try {
314         LOGD("%s", method_name);
315         Mat* me = (Mat*) self; //TODO: check for NULL
316         return me->checkVector( elemChannels, depth, requireContinuous );
317     } catch(const std::exception &e) {
318         throwJavaException(env, &e, method_name);
319     } catch (...) {
320         throwJavaException(env, 0, method_name);
321     }
322     
323     return 0;
324 }
325
326
327
328 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JII
329   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth);
330
331 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JII
332   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth)
333 {
334     static const char method_name[] = "Mat::n_1checkVector__JII()";
335     try {
336         LOGD("%s", method_name);
337         Mat* me = (Mat*) self; //TODO: check for NULL
338         return me->checkVector( elemChannels, depth );
339     } catch(const std::exception &e) {
340         throwJavaException(env, &e, method_name);
341     } catch (...) {
342         throwJavaException(env, 0, method_name);
343     }
344     
345     return 0;
346 }
347
348
349 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JI
350   (JNIEnv* env, jclass, jlong self, jint elemChannels);
351
352
353 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JI
354   (JNIEnv* env, jclass, jlong self, jint elemChannels)
355 {
356     static const char method_name[] = "Mat::n_1checkVector__JI()";
357     try {
358         LOGD("%s", method_name);
359         Mat* me = (Mat*) self; //TODO: check for NULL
360         return me->checkVector( elemChannels );
361     } catch(const std::exception &e) {
362         throwJavaException(env, &e, method_name);
363     } catch (...) {
364         throwJavaException(env, 0, method_name);
365     }
366     
367     return 0;
368 }
369
370
371
372 //
373 //  Mat Mat::clone()
374 //
375
376 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1clone
377   (JNIEnv* env, jclass, jlong self);
378
379
380 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1clone
381   (JNIEnv* env, jclass, jlong self)
382 {
383     static const char method_name[] = "Mat::n_1clone()";
384     try {
385         LOGD("%s", method_name);
386         Mat* me = (Mat*) self; //TODO: check for NULL
387         Mat _retval_ = me->clone(  );
388         return (jlong) new Mat(_retval_);
389     } catch(const std::exception &e) {
390         throwJavaException(env, &e, method_name);
391     } catch (...) {
392         throwJavaException(env, 0, method_name);
393     }
394     
395     return 0;
396 }
397
398
399
400 //
401 //  Mat Mat::col(int x)
402 //
403
404 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1col
405   (JNIEnv* env, jclass, jlong self, jint x);
406
407 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1col
408   (JNIEnv* env, jclass, jlong self, jint x)
409 {
410     static const char method_name[] = "Mat::n_1col()";
411     try {
412         LOGD("%s", method_name);
413         Mat* me = (Mat*) self; //TODO: check for NULL
414         Mat _retval_ = me->col( x );
415         return (jlong) new Mat(_retval_);
416     } catch(const std::exception &e) {
417         throwJavaException(env, &e, method_name);
418     } catch (...) {
419         throwJavaException(env, 0, method_name);
420     }
421     
422     return 0;
423 }
424
425
426
427 //
428 //  Mat Mat::colRange(int startcol, int endcol)
429 //
430
431 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1colRange
432   (JNIEnv* env, jclass, jlong self, jint startcol, jint endcol);
433
434 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1colRange
435   (JNIEnv* env, jclass, jlong self, jint startcol, jint endcol)
436 {
437     static const char method_name[] = "Mat::n_1colRange()";
438     try {
439         LOGD("%s", method_name);
440         Mat* me = (Mat*) self; //TODO: check for NULL
441         Mat _retval_ = me->colRange( startcol, endcol );
442         return (jlong) new Mat(_retval_);
443     } catch(const std::exception &e) {
444         throwJavaException(env, &e, method_name);
445     } catch (...) {
446         throwJavaException(env, 0, method_name);
447     }
448     
449     return 0;
450 }
451
452
453
454 //
455 //  int Mat::dims()
456 //
457   
458 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1dims
459   (JNIEnv* env, jclass, jlong self);
460
461 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1dims
462   (JNIEnv* env, jclass, jlong self)
463 {
464     static const char method_name[] = "Mat::n_1dims()";
465     try {
466         LOGD("%s", method_name);
467         Mat* me = (Mat*) self; //TODO: check for NULL
468         return me->dims;
469     } catch(cv::Exception e) {
470         throwJavaException(env, &e, method_name);
471     } catch (...) {
472         throwJavaException(env, 0, method_name);
473     }
474     
475     return 0;
476 }
477
478
479
480 //
481 //  int Mat::cols()
482 //
483
484 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1cols
485   (JNIEnv* env, jclass, jlong self);
486
487 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1cols
488   (JNIEnv* env, jclass, jlong self)
489 {
490     static const char method_name[] = "Mat::n_1cols()";
491     try {
492         LOGD("%s", method_name);
493         Mat* me = (Mat*) self; //TODO: check for NULL
494         return me->cols;
495     } catch(const std::exception &e) {
496         throwJavaException(env, &e, method_name);
497     } catch (...) {
498         throwJavaException(env, 0, method_name);
499     }
500     
501     return 0;
502 }
503
504
505
506 //
507 //  void Mat::convertTo(Mat& m, int rtype, double alpha = 1, double beta = 0)
508 //
509
510 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD
511   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha, jdouble beta);
512
513 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD
514   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha, jdouble beta)
515 {
516     static const char method_name[] = "Mat::n_1convertTo__JJIDD()";
517     try {
518         LOGD("%s", method_name);
519         Mat* me = (Mat*) self; //TODO: check for NULL
520         Mat& m = *((Mat*)m_nativeObj);
521         me->convertTo( m, rtype, alpha, beta );
522     } catch(const std::exception &e) {
523         throwJavaException(env, &e, method_name);
524     } catch (...) {
525         throwJavaException(env, 0, method_name);
526     }
527 }
528
529
530 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID
531   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha);
532
533 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID
534   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha)
535 {
536     static const char method_name[] = "Mat::n_1convertTo__JJID()";
537     try {
538         LOGD("%s", method_name);
539         Mat* me = (Mat*) self; //TODO: check for NULL
540         Mat& m = *((Mat*)m_nativeObj);
541         me->convertTo( m, rtype, alpha );
542     } catch(const std::exception &e) {
543         throwJavaException(env, &e, method_name);
544     } catch (...) {
545         throwJavaException(env, 0, method_name);
546     }
547 }
548
549
550 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJI
551   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype);
552
553 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJI
554   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype)
555 {
556     static const char method_name[] = "Mat::n_1convertTo__JJI()";
557     try {
558         LOGD("%s", method_name);
559         Mat* me = (Mat*) self; //TODO: check for NULL
560         Mat& m = *((Mat*)m_nativeObj);
561         me->convertTo( m, rtype );
562     } catch(const std::exception &e) {
563         throwJavaException(env, &e, method_name);
564     } catch (...) {
565         throwJavaException(env, 0, method_name);
566     }
567 }
568
569
570
571 //
572 //  void Mat::copyTo(Mat& m)
573 //
574
575 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJ
576   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
577
578 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJ
579   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
580 {
581     static const char method_name[] = "Mat::n_1copyTo__JJ()";
582     try {
583         LOGD("%s", method_name);
584         Mat* me = (Mat*) self; //TODO: check for NULL
585         Mat& m = *((Mat*)m_nativeObj);
586         me->copyTo( m );
587     } catch(const std::exception &e) {
588         throwJavaException(env, &e, method_name);
589     } catch (...) {
590         throwJavaException(env, 0, method_name);
591     }
592 }
593
594
595
596 //
597 //  void Mat::copyTo(Mat& m, Mat mask)
598 //
599
600 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJJ
601   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jlong mask_nativeObj);
602
603 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJJ
604   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jlong mask_nativeObj)
605 {
606     static const char method_name[] = "Mat::n_1copyTo__JJJ()";
607     try {
608         LOGD("%s", method_name);
609         Mat* me = (Mat*) self; //TODO: check for NULL
610         Mat& m = *((Mat*)m_nativeObj);
611         Mat& mask = *((Mat*)mask_nativeObj);
612         me->copyTo( m, mask );
613     } catch(const std::exception &e) {
614         throwJavaException(env, &e, method_name);
615     } catch (...) {
616         throwJavaException(env, 0, method_name);
617     }
618 }
619
620
621
622 //
623 //  void Mat::create(int rows, int cols, int type)
624 //
625
626 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JIII
627   (JNIEnv* env, jclass, jlong self, jint rows, jint cols, jint type);
628
629 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JIII
630   (JNIEnv* env, jclass, jlong self, jint rows, jint cols, jint type)
631 {
632     static const char method_name[] = "Mat::n_1create__JIII()";
633     try {
634         LOGD("%s", method_name);
635         Mat* me = (Mat*) self; //TODO: check for NULL
636         me->create( rows, cols, type );
637     } catch(const std::exception &e) {
638         throwJavaException(env, &e, method_name);
639     } catch (...) {
640         throwJavaException(env, 0, method_name);
641     }
642 }
643
644
645
646 //
647 //  void Mat::create(Size size, int type)
648 //
649
650 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JDDI
651   (JNIEnv* env, jclass, jlong self, jdouble size_width, jdouble size_height, jint type);
652
653 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JDDI
654   (JNIEnv* env, jclass, jlong self, jdouble size_width, jdouble size_height, jint type)
655 {
656     static const char method_name[] = "Mat::n_1create__JDDI()";
657     try {
658         LOGD("%s", method_name);
659         Mat* me = (Mat*) self; //TODO: check for NULL
660         Size size((int)size_width, (int)size_height);
661         me->create( size, type );
662     } catch(const std::exception &e) {
663         throwJavaException(env, &e, method_name);
664     } catch (...) {
665         throwJavaException(env, 0, method_name);
666     }
667 }
668
669
670
671 //
672 //  Mat Mat::cross(Mat m)
673 //
674
675 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1cross
676   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
677
678 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1cross
679   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
680 {
681     static const char method_name[] = "Mat::n_1cross()";
682     try {
683         LOGD("%s", method_name);
684         Mat* me = (Mat*) self; //TODO: check for NULL
685         Mat& m = *((Mat*)m_nativeObj);
686         Mat _retval_ = me->cross( m );
687         return (jlong) new Mat(_retval_);
688     } catch(const std::exception &e) {
689         throwJavaException(env, &e, method_name);
690     } catch (...) {
691         throwJavaException(env, 0, method_name);
692     }
693     
694     return 0;
695 }
696
697
698
699 //
700 //  long Mat::dataAddr()
701 //
702
703 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1dataAddr
704   (JNIEnv*, jclass, jlong self);
705
706 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1dataAddr
707   (JNIEnv*, jclass, jlong self)
708 {
709     LOGD("Mat::n_1dataAddr()");
710     Mat* me = (Mat*) self; //TODO: check for NULL
711     return (jlong) me->data;
712 }
713
714
715
716 //
717 //  int Mat::depth()
718 //
719
720 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1depth
721   (JNIEnv* env, jclass, jlong self);
722
723 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1depth
724   (JNIEnv* env, jclass, jlong self)
725 {
726     static const char method_name[] = "Mat::n_1depth()";
727     try {
728         LOGD("%s", method_name);
729         Mat* me = (Mat*) self; //TODO: check for NULL
730         return me->depth(  );
731     } catch(const std::exception &e) {
732         throwJavaException(env, &e, method_name);
733     } catch (...) {
734         throwJavaException(env, 0, method_name);
735     }
736     
737     return 0;
738 }
739
740
741
742 //
743 //  Mat Mat::diag(int d = 0)
744 //
745
746 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__JI
747   (JNIEnv* env, jclass, jlong self, jint d);
748
749 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__JI
750   (JNIEnv* env, jclass, jlong self, jint d)
751 {
752     static const char method_name[] = "Mat::n_1diag__JI()";
753     try {
754         LOGD("%s", method_name);
755         Mat* me = (Mat*) self; //TODO: check for NULL
756         Mat _retval_ = me->diag( d );
757         return (jlong) new Mat(_retval_);
758     } catch(const std::exception &e) {
759         throwJavaException(env, &e, method_name);
760     } catch (...) {
761         throwJavaException(env, 0, method_name);
762     }
763     
764     return 0;
765 }
766
767
768
769
770 //
771 // static Mat Mat::diag(Mat d)
772 //
773
774 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__J
775   (JNIEnv* env, jclass, jlong d_nativeObj);
776
777 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__J
778   (JNIEnv* env, jclass, jlong d_nativeObj)
779 {
780     static const char method_name[] = "Mat::n_1diag__J()";
781     try {
782         LOGD("%s", method_name);
783         Mat _retval_ = Mat::diag( (*(Mat*)d_nativeObj) );
784         return (jlong) new Mat(_retval_);
785     } catch(const std::exception &e) {
786         throwJavaException(env, &e, method_name);
787     } catch (...) {
788         throwJavaException(env, 0, method_name);
789     }
790     
791     return 0;
792 }
793
794
795
796 //
797 //  double Mat::dot(Mat m)
798 //
799
800 JNIEXPORT jdouble JNICALL Java_org_opencv_core_Mat_n_1dot
801   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
802
803 JNIEXPORT jdouble JNICALL Java_org_opencv_core_Mat_n_1dot
804   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
805 {
806     static const char method_name[] = "Mat::n_1dot()";
807     try {
808         LOGD("%s", method_name);
809         Mat* me = (Mat*) self; //TODO: check for NULL
810         Mat& m = *((Mat*)m_nativeObj);
811         return me->dot( m );
812     } catch(const std::exception &e) {
813         throwJavaException(env, &e, method_name);
814     } catch (...) {
815         throwJavaException(env, 0, method_name);
816     }
817     
818     return 0;
819 }
820
821
822
823 //
824 //  size_t Mat::elemSize()
825 //
826
827 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize
828   (JNIEnv* env, jclass, jlong self);
829
830 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize
831   (JNIEnv* env, jclass, jlong self)
832 {
833     static const char method_name[] = "Mat::n_1elemSize()";
834     try {
835         LOGD("%s", method_name);
836         Mat* me = (Mat*) self; //TODO: check for NULL
837         return me->elemSize(  );
838     } catch(const std::exception &e) {
839         throwJavaException(env, &e, method_name);
840     } catch (...) {
841         throwJavaException(env, 0, method_name);
842     }
843     
844     return 0;
845 }
846
847
848
849 //
850 //  size_t Mat::elemSize1()
851 //
852
853 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize1
854   (JNIEnv* env, jclass, jlong self);
855
856 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize1
857   (JNIEnv* env, jclass, jlong self)
858 {
859     static const char method_name[] = "Mat::n_1elemSize1()";
860     try {
861         LOGD("%s", method_name);
862         Mat* me = (Mat*) self; //TODO: check for NULL
863         return me->elemSize1(  );
864     } catch(const std::exception &e) {
865         throwJavaException(env, &e, method_name);
866     } catch (...) {
867         throwJavaException(env, 0, method_name);
868     }
869     
870     return 0;
871 }
872
873
874
875 //
876 //  bool Mat::empty()
877 //
878
879 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1empty
880   (JNIEnv* env, jclass, jlong self);
881
882 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1empty
883   (JNIEnv* env, jclass, jlong self)
884 {
885     static const char method_name[] = "Mat::n_1empty()";
886     try {
887         LOGD("%s", method_name);
888         Mat* me = (Mat*) self; //TODO: check for NULL
889         return me->empty(  );
890     } catch(const std::exception &e) {
891         throwJavaException(env, &e, method_name);
892     } catch (...) {
893         throwJavaException(env, 0, method_name);
894     }
895     
896     return 0;
897 }
898
899
900
901 //
902 // static Mat Mat::eye(int rows, int cols, int type)
903 //
904
905 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__III
906   (JNIEnv* env, jclass, jint rows, jint cols, jint type);
907
908 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__III
909   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
910 {
911     static const char method_name[] = "Mat::n_1eye__III()";
912     try {
913         LOGD("%s", method_name);
914         Mat _retval_ = Mat::eye( rows, cols, type );
915         return (jlong) new Mat(_retval_);
916     } catch(const std::exception &e) {
917         throwJavaException(env, &e, method_name);
918     } catch (...) {
919         throwJavaException(env, 0, method_name);
920     }
921     
922     return 0;
923 }
924
925
926
927 //
928 // static Mat Mat::eye(Size size, int type)
929 //
930
931 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__DDI
932   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type);
933
934 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__DDI
935   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
936 {
937     static const char method_name[] = "Mat::n_1eye__DDI()";
938     try {
939         LOGD("%s", method_name);
940         Size size((int)size_width, (int)size_height);
941         Mat _retval_ = Mat::eye( size, type );
942         return (jlong) new Mat(_retval_);
943     } catch(const std::exception &e) {
944         throwJavaException(env, &e, method_name);
945     } catch (...) {
946         throwJavaException(env, 0, method_name);
947     }
948     
949     return 0;
950 }
951
952
953
954 //
955 //  Mat Mat::inv(int method = DECOMP_LU)
956 //
957
958 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__JI
959   (JNIEnv* env, jclass, jlong self, jint method);
960
961 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__JI
962   (JNIEnv* env, jclass, jlong self, jint method)
963 {
964     static const char method_name[] = "Mat::n_1inv__JI()";
965     try {
966         LOGD("%s", method_name);
967         Mat* me = (Mat*) self; //TODO: check for NULL
968         Mat _retval_ = me->inv( method );
969         return (jlong) new Mat(_retval_);
970     } catch(const std::exception &e) {
971         throwJavaException(env, &e, method_name);
972     } catch (...) {
973         throwJavaException(env, 0, method_name);
974     }
975     
976     return 0;
977 }
978
979
980 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__J
981   (JNIEnv* env, jclass, jlong self);
982
983 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__J
984   (JNIEnv* env, jclass, jlong self)
985 {
986     static const char method_name[] = "Mat::n_1inv__J()";
987     try {
988         LOGD("%s", method_name);
989         Mat* me = (Mat*) self; //TODO: check for NULL
990         Mat _retval_ = me->inv(  );
991         return (jlong) new Mat(_retval_);
992     } catch(const std::exception &e) {
993         throwJavaException(env, &e, method_name);
994     } catch (...) {
995         throwJavaException(env, 0, method_name);
996     }
997     
998     return 0;
999 }
1000
1001
1002
1003 //
1004 //  bool Mat::isContinuous()
1005 //
1006
1007 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isContinuous
1008   (JNIEnv* env, jclass, jlong self);
1009
1010 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isContinuous
1011   (JNIEnv* env, jclass, jlong self)
1012 {
1013     static const char method_name[] = "Mat::n_1isContinuous()";
1014     try {
1015         LOGD("%s", method_name);
1016         Mat* me = (Mat*) self; //TODO: check for NULL
1017         return me->isContinuous(  );
1018     } catch(const std::exception &e) {
1019         throwJavaException(env, &e, method_name);
1020     } catch (...) {
1021         throwJavaException(env, 0, method_name);
1022     }
1023     
1024     return 0;
1025 }
1026
1027
1028
1029 //
1030 //  bool Mat::isSubmatrix()
1031 //
1032
1033 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isSubmatrix
1034   (JNIEnv* env, jclass, jlong self);
1035
1036 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isSubmatrix
1037   (JNIEnv* env, jclass, jlong self)
1038 {
1039     static const char method_name[] = "Mat::n_1isSubmatrix()";
1040     try {
1041         LOGD("%s", method_name);
1042         Mat* me = (Mat*) self; //TODO: check for NULL
1043         return me->isSubmatrix(  );
1044     } catch(const std::exception &e) {
1045         throwJavaException(env, &e, method_name);
1046     } catch (...) {
1047         throwJavaException(env, 0, method_name);
1048     }
1049     
1050     return 0;
1051 }
1052
1053
1054
1055 //
1056 //  void Mat::locateROI(Size wholeSize, Point ofs)
1057 //
1058
1059 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_locateROI_10
1060   (JNIEnv* env, jclass, jlong self, jdoubleArray wholeSize_out, jdoubleArray ofs_out);
1061
1062 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_locateROI_10
1063   (JNIEnv* env, jclass, jlong self, jdoubleArray wholeSize_out, jdoubleArray ofs_out)
1064 {
1065     static const char method_name[] = "core::locateROI_10()";
1066     try {
1067         LOGD("%s", method_name);
1068         Mat* me = (Mat*) self; //TODO: check for NULL
1069         Size wholeSize;
1070         Point ofs;
1071         me->locateROI( wholeSize, ofs );
1072         jdouble tmp_wholeSize[2] = {wholeSize.width, wholeSize.height}; env->SetDoubleArrayRegion(wholeSize_out, 0, 2, tmp_wholeSize);  jdouble tmp_ofs[2] = {ofs.x, ofs.y}; env->SetDoubleArrayRegion(ofs_out, 0, 2, tmp_ofs);
1073     } catch(const std::exception &e) {
1074         throwJavaException(env, &e, method_name);
1075     } catch (...) {
1076         throwJavaException(env, 0, method_name);
1077     }
1078 }
1079
1080
1081
1082 //
1083 //  Mat Mat::mul(Mat m, double scale = 1)
1084 //
1085
1086 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJD
1087   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jdouble scale);
1088
1089 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJD
1090   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jdouble scale)
1091 {
1092     static const char method_name[] = "Mat::n_1mul__JJD()";
1093     try {
1094         LOGD("%s", method_name);
1095         Mat* me = (Mat*) self; //TODO: check for NULL
1096         Mat& m = *((Mat*)m_nativeObj);
1097         Mat _retval_ = me->mul( m, scale );
1098         return (jlong) new Mat(_retval_);
1099     } catch(const std::exception &e) {
1100         throwJavaException(env, &e, method_name);
1101     } catch (...) {
1102         throwJavaException(env, 0, method_name);
1103     }
1104     
1105     return 0;
1106 }
1107
1108
1109
1110 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJ
1111   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
1112
1113 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJ
1114   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
1115 {
1116     static const char method_name[] = "Mat::n_1mul__JJ()";
1117     try {
1118         LOGD("%s", method_name);
1119         Mat* me = (Mat*) self; //TODO: check for NULL
1120         Mat& m = *((Mat*)m_nativeObj);
1121         Mat _retval_ = me->mul( m );
1122         return (jlong) new Mat(_retval_);
1123     } catch(const std::exception &e) {
1124         throwJavaException(env, &e, method_name);
1125     } catch (...) {
1126         throwJavaException(env, 0, method_name);
1127     }
1128
1129     return 0;
1130 }
1131
1132
1133
1134 //
1135 // static Mat Mat::ones(int rows, int cols, int type)
1136 //
1137
1138 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__III
1139   (JNIEnv* env, jclass, jint rows, jint cols, jint type);
1140
1141 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__III
1142   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
1143 {
1144     static const char method_name[] = "Mat::n_1ones__III()";
1145     try {
1146         LOGD("%s", method_name);
1147         Mat _retval_ = Mat::ones( rows, cols, type );
1148         return (jlong) new Mat(_retval_);
1149     } catch(const std::exception &e) {
1150         throwJavaException(env, &e, method_name);
1151     } catch (...) {
1152         throwJavaException(env, 0, method_name);
1153     }
1154
1155     return 0;
1156 }
1157
1158
1159
1160 //
1161 // static Mat Mat::ones(Size size, int type)
1162 //
1163
1164 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__DDI
1165   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type);
1166
1167 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__DDI
1168   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
1169 {
1170     static const char method_name[] = "Mat::n_1ones__DDI()";
1171     try {
1172         LOGD("%s", method_name);
1173         Size size((int)size_width, (int)size_height);
1174         Mat _retval_ = Mat::ones( size, type );
1175         return (jlong) new Mat(_retval_);
1176     } catch(const std::exception &e) {
1177         throwJavaException(env, &e, method_name);
1178     } catch (...) {
1179         throwJavaException(env, 0, method_name);
1180     }
1181
1182     return 0;
1183 }
1184
1185
1186
1187 //
1188 //  void Mat::push_back(Mat m)
1189 //
1190
1191 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1push_1back
1192   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj);
1193
1194 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1push_1back
1195   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
1196 {
1197     static const char method_name[] = "Mat::n_1push_1back()";
1198     try {
1199         LOGD("%s", method_name);
1200         Mat* me = (Mat*) self; //TODO: check for NULL
1201         me->push_back( (*(Mat*)m_nativeObj) );
1202     } catch(const std::exception &e) {
1203         throwJavaException(env, &e, method_name);
1204     } catch (...) {
1205         throwJavaException(env, 0, method_name);
1206     }
1207 }
1208
1209
1210
1211 //
1212 //  void Mat::release()
1213 //
1214
1215 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1release
1216   (JNIEnv* env, jclass, jlong self);
1217
1218 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1release
1219   (JNIEnv* env, jclass, jlong self)
1220 {
1221     static const char method_name[] = "Mat::n_1release()";
1222     try {
1223         LOGD("%s", method_name);
1224         Mat* me = (Mat*) self; //TODO: check for NULL
1225         me->release(  );
1226     } catch(const std::exception &e) {
1227         throwJavaException(env, &e, method_name);
1228     } catch (...) {
1229         throwJavaException(env, 0, method_name);
1230     }
1231 }
1232
1233
1234
1235 //
1236 //  Mat Mat::reshape(int cn, int rows = 0)
1237 //
1238
1239 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JII
1240   (JNIEnv* env, jclass, jlong self, jint cn, jint rows);
1241
1242 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JII
1243   (JNIEnv* env, jclass, jlong self, jint cn, jint rows)
1244 {
1245     static const char method_name[] = "Mat::n_1reshape__JII()";
1246     try {
1247         LOGD("%s", method_name);
1248         Mat* me = (Mat*) self; //TODO: check for NULL
1249         Mat _retval_ = me->reshape( cn, rows );
1250         return (jlong) new Mat(_retval_);
1251     } catch(const std::exception &e) {
1252         throwJavaException(env, &e, method_name);
1253     } catch (...) {
1254         throwJavaException(env, 0, method_name);
1255     }
1256     
1257     return 0;
1258 }
1259
1260
1261
1262 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JI
1263   (JNIEnv* env, jclass, jlong self, jint cn);
1264
1265 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JI
1266   (JNIEnv* env, jclass, jlong self, jint cn)
1267 {
1268     static const char method_name[] = "Mat::n_1reshape__JI()";
1269     try {
1270         LOGD("%s", method_name);
1271         Mat* me = (Mat*) self; //TODO: check for NULL
1272         Mat _retval_ = me->reshape( cn );
1273         return (jlong) new Mat(_retval_);
1274     } catch(const std::exception &e) {
1275         throwJavaException(env, &e, method_name);
1276     } catch (...) {
1277         throwJavaException(env, 0, method_name);
1278     }
1279     
1280     return 0;
1281 }
1282
1283
1284
1285 //
1286 //  Mat Mat::row(int y)
1287 //
1288
1289 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1row
1290   (JNIEnv* env, jclass, jlong self, jint y);
1291
1292 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1row
1293   (JNIEnv* env, jclass, jlong self, jint y)
1294 {
1295     static const char method_name[] = "Mat::n_1row()";
1296     try {
1297         LOGD("%s", method_name);
1298         Mat* me = (Mat*) self; //TODO: check for NULL
1299         Mat _retval_ = me->row( y );
1300         return (jlong) new Mat(_retval_);
1301     } catch(const std::exception &e) {
1302         throwJavaException(env, &e, method_name);
1303     } catch (...) {
1304         throwJavaException(env, 0, method_name);
1305     }
1306     
1307     return 0;
1308 }
1309
1310
1311
1312 //
1313 //  Mat Mat::rowRange(int startrow, int endrow)
1314 //
1315
1316 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1rowRange
1317   (JNIEnv* env, jclass, jlong self, jint startrow, jint endrow);
1318
1319 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1rowRange
1320   (JNIEnv* env, jclass, jlong self, jint startrow, jint endrow)
1321 {
1322     static const char method_name[] = "Mat::n_1rowRange()";
1323     try {
1324         LOGD("%s", method_name);
1325         Mat* me = (Mat*) self; //TODO: check for NULL
1326         Mat _retval_ = me->rowRange( startrow, endrow );
1327         return (jlong) new Mat(_retval_);
1328     } catch(const std::exception &e) {
1329         throwJavaException(env, &e, method_name);
1330     } catch (...) {
1331         throwJavaException(env, 0, method_name);
1332     }
1333
1334     return 0;
1335 }
1336
1337
1338
1339 //
1340 //  int Mat::rows()
1341 //
1342
1343 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1rows
1344   (JNIEnv* env, jclass, jlong self);
1345
1346 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1rows
1347   (JNIEnv* env, jclass, jlong self)
1348 {
1349     static const char method_name[] = "Mat::n_1rows()";
1350     try {
1351         LOGD("%s", method_name);
1352         Mat* me = (Mat*) self; //TODO: check for NULL
1353         return me->rows;
1354     } catch(const std::exception &e) {
1355         throwJavaException(env, &e, method_name);
1356     } catch (...) {
1357         throwJavaException(env, 0, method_name);
1358     }
1359
1360     return 0;
1361 }
1362
1363
1364
1365 //
1366 //  Mat Mat::operator =(Scalar s)
1367 //
1368
1369 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDD
1370   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3);
1371
1372 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDD
1373   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
1374 {
1375     static const char method_name[] = "Mat::n_1setTo__JDDDD()";
1376     try {
1377         LOGD("%s", method_name);
1378         Mat* me = (Mat*) self; //TODO: check for NULL
1379         Scalar s(s_val0, s_val1, s_val2, s_val3);
1380         Mat _retval_ = me->operator =( s );
1381         return (jlong) new Mat(_retval_);
1382     } catch(const std::exception &e) {
1383         throwJavaException(env, &e, method_name);
1384     } catch (...) {
1385         throwJavaException(env, 0, method_name);
1386     }
1387
1388     return 0;
1389 }
1390
1391
1392
1393 //
1394 //  Mat Mat::setTo(Scalar value, Mat mask = Mat())
1395 //
1396
1397 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDDJ
1398   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3, jlong mask_nativeObj);
1399
1400 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDDJ
1401   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3, jlong mask_nativeObj)
1402 {
1403     static const char method_name[] = "Mat::n_1setTo__JDDDDJ()";
1404     try {
1405         LOGD("%s", method_name);
1406         Mat* me = (Mat*) self; //TODO: check for NULL
1407         Scalar s(s_val0, s_val1, s_val2, s_val3);
1408         Mat& mask = *((Mat*)mask_nativeObj);
1409         Mat _retval_ = me->setTo( s, mask );
1410         return (jlong) new Mat(_retval_);
1411     } catch(const std::exception &e) {
1412         throwJavaException(env, &e, method_name);
1413     } catch (...) {
1414         throwJavaException(env, 0, method_name);
1415     }
1416
1417     return 0;
1418 }
1419
1420
1421
1422 //
1423 //  Mat Mat::setTo(Mat value, Mat mask = Mat())
1424 //
1425
1426 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJJ
1427   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj, jlong mask_nativeObj);
1428
1429 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJJ
1430   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj, jlong mask_nativeObj)
1431 {
1432     static const char method_name[] = "Mat::n_1setTo__JJJ()";
1433     try {
1434         LOGD("%s", method_name);
1435         Mat* me = (Mat*) self; //TODO: check for NULL
1436         Mat& value = *((Mat*)value_nativeObj);
1437         Mat& mask = *((Mat*)mask_nativeObj);
1438         Mat _retval_ = me->setTo( value, mask );
1439         return (jlong) new Mat(_retval_);
1440     } catch(const std::exception &e) {
1441         throwJavaException(env, &e, method_name);
1442     } catch (...) {
1443         throwJavaException(env, 0, method_name);
1444     }
1445
1446     return 0;
1447 }
1448
1449
1450
1451 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJ
1452   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj);
1453
1454 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJ
1455   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj)
1456 {
1457     static const char method_name[] = "Mat::n_1setTo__JJ()";
1458     try {
1459         LOGD("%s", method_name);
1460         Mat* me = (Mat*) self; //TODO: check for NULL
1461         Mat& value = *((Mat*)value_nativeObj);
1462         Mat _retval_ = me->setTo( value );
1463         return (jlong) new Mat(_retval_);
1464     } catch(const std::exception &e) {
1465         throwJavaException(env, &e, method_name);
1466     } catch (...) {
1467         throwJavaException(env, 0, method_name);
1468     }
1469
1470     return 0;
1471 }
1472
1473
1474
1475 //
1476 //  Size Mat::size()
1477 //
1478
1479 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_n_1size
1480   (JNIEnv* env, jclass, jlong self);
1481
1482 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_n_1size
1483   (JNIEnv* env, jclass, jlong self)
1484 {
1485     static const char method_name[] = "Mat::n_1size()";
1486     try {
1487         LOGD("%s", method_name);
1488         Mat* me = (Mat*) self; //TODO: check for NULL
1489         Size _retval_ = me->size(  );
1490         jdoubleArray _da_retval_ = env->NewDoubleArray(2);  
1491         jdouble _tmp_retval_[2] = {_retval_.width, _retval_.height};
1492         env->SetDoubleArrayRegion(_da_retval_, 0, 2, _tmp_retval_);
1493         return _da_retval_;
1494     } catch(const std::exception &e) {
1495         throwJavaException(env, &e, method_name);
1496     } catch (...) {
1497         throwJavaException(env, 0, method_name);
1498     }
1499
1500     return 0;
1501 }
1502
1503
1504
1505 //
1506 //  size_t Mat::step1(int i = 0)
1507 //
1508
1509 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__JI
1510   (JNIEnv* env, jclass, jlong self, jint i);
1511
1512 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__JI
1513   (JNIEnv* env, jclass, jlong self, jint i)
1514 {
1515     static const char method_name[] = "Mat::n_1step1__JI()";
1516     try {
1517         LOGD("%s", method_name);
1518         Mat* me = (Mat*) self; //TODO: check for NULL
1519         return me->step1( i );
1520     } catch(const std::exception &e) {
1521         throwJavaException(env, &e, method_name);
1522     } catch (...) {
1523         throwJavaException(env, 0, method_name);
1524     }
1525
1526     return 0;
1527 }
1528
1529
1530
1531 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__J
1532   (JNIEnv* env, jclass, jlong self);
1533
1534 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__J
1535   (JNIEnv* env, jclass, jlong self)
1536 {
1537     static const char method_name[] = "Mat::n_1step1__J()";
1538     try {
1539         LOGD("%s", method_name);
1540         Mat* me = (Mat*) self; //TODO: check for NULL
1541         return me->step1(  );
1542     } catch(const std::exception &e) {
1543         throwJavaException(env, &e, method_name);
1544     } catch (...) {
1545         throwJavaException(env, 0, method_name);
1546     }
1547
1548     return 0;
1549 }
1550
1551 //
1552 //  Mat Mat::operator()(Range rowRange, Range colRange)
1553 //
1554
1555 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat_1rr
1556   (JNIEnv* env, jclass, jlong self, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end);
1557
1558 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat_1rr
1559   (JNIEnv* env, jclass, jlong self, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end)
1560 {
1561     static const char method_name[] = "Mat::n_1submat_1rr()";
1562     try {
1563         LOGD("%s", method_name);
1564         Mat* me = (Mat*) self; //TODO: check for NULL
1565         Range rowRange(rowRange_start, rowRange_end);
1566         Range colRange(colRange_start, colRange_end);
1567         Mat _retval_ = me->operator()( rowRange, colRange );
1568         return (jlong) new Mat(_retval_);
1569     } catch(const std::exception &e) {
1570         throwJavaException(env, &e, method_name);
1571     } catch (...) {
1572         throwJavaException(env, 0, method_name);
1573     }
1574
1575     return 0;
1576 }
1577
1578
1579
1580 //
1581 //  Mat Mat::operator()(Rect roi)
1582 //
1583
1584 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat
1585   (JNIEnv* env, jclass, jlong self, jint roi_x, jint roi_y, jint roi_width, jint roi_height);
1586
1587 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat
1588   (JNIEnv* env, jclass, jlong self, jint roi_x, jint roi_y, jint roi_width, jint roi_height)
1589 {
1590     static const char method_name[] = "Mat::n_1submat()";
1591     try {
1592         LOGD("%s", method_name);
1593         Mat* me = (Mat*) self; //TODO: check for NULL
1594         Rect roi(roi_x, roi_y, roi_width, roi_height);
1595         Mat _retval_ = me->operator()( roi );
1596         return (jlong) new Mat(_retval_);
1597     } catch(const std::exception &e) {
1598         throwJavaException(env, &e, method_name);
1599     } catch (...) {
1600         throwJavaException(env, 0, method_name);
1601     }
1602
1603     return 0;
1604 }
1605
1606
1607
1608 //
1609 //  Mat Mat::t()
1610 //
1611
1612 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1t
1613   (JNIEnv* env, jclass, jlong self);
1614
1615 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1t
1616   (JNIEnv* env, jclass, jlong self)
1617 {
1618     static const char method_name[] = "Mat::n_1t()";
1619     try {
1620         LOGD("%s", method_name);
1621         Mat* me = (Mat*) self; //TODO: check for NULL
1622         Mat _retval_ = me->t(  );
1623         return (jlong) new Mat(_retval_);
1624     } catch(const std::exception &e) {
1625         throwJavaException(env, &e, method_name);
1626     } catch (...) {
1627         throwJavaException(env, 0, method_name);
1628     }
1629
1630     return 0;
1631 }
1632
1633
1634
1635 //
1636 //  size_t Mat::total()
1637 //
1638
1639 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1total
1640   (JNIEnv* env, jclass, jlong self);
1641
1642 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1total
1643   (JNIEnv* env, jclass, jlong self)
1644 {
1645     static const char method_name[] = "Mat::n_1total()";
1646     try {
1647         LOGD("%s", method_name);
1648         Mat* me = (Mat*) self; //TODO: check for NULL
1649         return me->total(  );
1650     } catch(const std::exception &e) {
1651         throwJavaException(env, &e, method_name);
1652     } catch (...) {
1653         throwJavaException(env, 0, method_name);
1654     }
1655
1656     return 0;
1657 }
1658
1659
1660
1661 //
1662 //  int Mat::type()
1663 //
1664
1665 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1type
1666   (JNIEnv* env, jclass, jlong self);
1667
1668 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1type
1669   (JNIEnv* env, jclass, jlong self)
1670 {
1671     static const char method_name[] = "Mat::n_1type()";
1672     try {
1673         LOGD("%s", method_name);
1674         Mat* me = (Mat*) self; //TODO: check for NULL
1675         return me->type(  );
1676     } catch(const std::exception &e) {
1677         throwJavaException(env, &e, method_name);
1678     } catch (...) {
1679         throwJavaException(env, 0, method_name);
1680     }
1681
1682     return 0;
1683 }
1684
1685
1686
1687 //
1688 // static Mat Mat::zeros(int rows, int cols, int type)
1689 //
1690
1691 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__III
1692   (JNIEnv* env, jclass, jint rows, jint cols, jint type);
1693
1694 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__III
1695   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
1696 {
1697     static const char method_name[] = "Mat::n_1zeros__III()";
1698     try {
1699         LOGD("%s", method_name);
1700         Mat _retval_ = Mat::zeros( rows, cols, type );
1701         return (jlong) new Mat(_retval_);
1702     } catch(const std::exception &e) {
1703         throwJavaException(env, &e, method_name);
1704     } catch (...) {
1705         throwJavaException(env, 0, method_name);
1706     }
1707
1708     return 0;
1709 }
1710
1711
1712
1713 //
1714 // static Mat Mat::zeros(Size size, int type)
1715 //
1716
1717 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__DDI
1718   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type);
1719
1720 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__DDI
1721   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
1722 {
1723     static const char method_name[] = "Mat::n_1zeros__DDI()";
1724     try {
1725         LOGD("%s", method_name);
1726         Size size((int)size_width, (int)size_height);
1727         Mat _retval_ = Mat::zeros( size, type );
1728         return (jlong) new Mat(_retval_);
1729     } catch(const std::exception &e) {
1730         throwJavaException(env, &e, method_name);
1731     } catch (...) {
1732         throwJavaException(env, 0, method_name);
1733     }
1734
1735     return 0;
1736 }
1737
1738
1739
1740 //
1741 //  native support for java finalize()
1742 //  static void Mat::n_delete( __int64 self )
1743 //
1744
1745 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1delete
1746   (JNIEnv*, jclass, jlong self);
1747
1748 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1delete
1749   (JNIEnv*, jclass, jlong self)
1750 {
1751     delete (Mat*) self;
1752 }
1753
1754 // unlike other nPut()-s this one (with double[]) should convert input values to correct type
1755 #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); }
1756
1757 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
1758     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals);
1759
1760 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
1761     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals)
1762 {
1763     static const char method_name[] = "Mat::nPutD()";
1764     try {
1765         LOGD("%s", method_name);
1766         cv::Mat* me = (cv::Mat*) self;
1767         if(!me || !me->data) return 0;  // no native object behind
1768         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
1769
1770         int rest = ((me->rows - row) * me->cols - col) * me->channels();
1771         if(count>rest) count = rest;
1772         int res = count;
1773         double* values = (double*)env->GetPrimitiveArrayCritical(vals, 0);
1774         double* src = values;
1775         int r, c;
1776         for(c=col; c<me->cols && count>0; c++)
1777         {
1778             switch(me->depth()) {
1779                 case CV_8U:  PUT_ITEM(uchar,  row, c); break;
1780                 case CV_8S:  PUT_ITEM(schar,  row, c); break;
1781                 case CV_16U: PUT_ITEM(ushort, row, c); break;
1782                 case CV_16S: PUT_ITEM(short,  row, c); break;
1783                 case CV_32S: PUT_ITEM(int,    row, c); break;
1784                 case CV_32F: PUT_ITEM(float,  row, c); break;
1785                 case CV_64F: PUT_ITEM(double, row, c); break;
1786             }
1787         }
1788
1789         for(r=row+1; r<me->rows && count>0; r++)
1790             for(c=0; c<me->cols && count>0; c++)
1791             {
1792                 switch(me->depth()) {
1793                     case CV_8U:  PUT_ITEM(uchar,  r, c); break;
1794                     case CV_8S:  PUT_ITEM(schar,  r, c); break;
1795                     case CV_16U: PUT_ITEM(ushort, r, c); break;
1796                     case CV_16S: PUT_ITEM(short,  r, c); break;
1797                     case CV_32S: PUT_ITEM(int,    r, c); break;
1798                     case CV_32F: PUT_ITEM(float,  r, c); break;
1799                     case CV_64F: PUT_ITEM(double, r, c); break;
1800                 }
1801             }
1802
1803         env->ReleasePrimitiveArrayCritical(vals, values, 0);
1804         return res;
1805     } catch(const std::exception &e) {
1806         throwJavaException(env, &e, method_name);
1807     } catch (...) {
1808         throwJavaException(env, 0, method_name);
1809     }
1810
1811     return 0;
1812 }
1813
1814
1815 } // extern "C"
1816
1817 template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, char* buff)
1818 {
1819     if(! m) return 0;
1820     if(! buff) return 0;
1821
1822     count *= sizeof(T);
1823     int rest = ((m->rows - row) * m->cols - col) * (int)m->elemSize();
1824     if(count>rest) count = rest;
1825     int res = count;
1826
1827     if( m->isContinuous() )
1828     {
1829         memcpy(m->ptr(row, col), buff, count);
1830     } else {
1831         // row by row
1832         int num = (m->cols - col) * (int)m->elemSize(); // 1st partial row
1833         if(count<num) num = count;
1834         uchar* data = m->ptr(row++, col);
1835         while(count>0){
1836             memcpy(data, buff, num);
1837             count -= num;
1838             buff += num;
1839             num = m->cols * (int)m->elemSize();
1840             if(count<num) num = count;
1841             data = m->ptr(row++, 0);
1842         }
1843     }
1844     return res;
1845 }
1846
1847
1848 extern "C" {
1849
1850 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
1851     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals);
1852
1853 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
1854     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals)
1855 {
1856     static const char method_name[] = "Mat::nPutB()";
1857     try {
1858         LOGD("%s", method_name);
1859         cv::Mat* me = (cv::Mat*) self;
1860         if(! self) return 0; // no native object behind
1861         if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
1862         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
1863
1864         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
1865         int res = mat_put<char>(me, row, col, count, values);
1866         env->ReleasePrimitiveArrayCritical(vals, values, 0);
1867         return res;
1868     } catch(const std::exception &e) {
1869         throwJavaException(env, &e, method_name);
1870     } catch (...) {
1871         throwJavaException(env, 0, method_name);
1872     }
1873
1874     return 0;
1875 }
1876
1877 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
1878     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals);
1879
1880 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
1881     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
1882 {
1883     static const char method_name[] = "Mat::nPutS()";
1884     try {
1885         LOGD("%s", method_name);
1886         cv::Mat* me = (cv::Mat*) self;
1887         if(! self) return 0; // no native object behind
1888         if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
1889         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
1890
1891         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
1892         int res = mat_put<short>(me, row, col, count, values);
1893         env->ReleasePrimitiveArrayCritical(vals, values, 0);
1894         return res;
1895     } catch(const std::exception &e) {
1896         throwJavaException(env, &e, method_name);
1897     } catch (...) {
1898         throwJavaException(env, 0, method_name);
1899     }
1900
1901     return 0;
1902 }
1903
1904 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
1905     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals);
1906
1907 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
1908     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
1909 {
1910     static const char method_name[] = "Mat::nPutI()";
1911     try {
1912         LOGD("%s", method_name);
1913         cv::Mat* me = (cv::Mat*) self;
1914         if(! self) return 0; // no native object behind
1915         if(me->depth() != CV_32S) return 0; // incompatible type
1916         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
1917
1918         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
1919         int res = mat_put<int>(me, row, col, count, values);
1920         env->ReleasePrimitiveArrayCritical(vals, values, 0);
1921         return res;
1922     } catch(const std::exception &e) {
1923         throwJavaException(env, &e, method_name);
1924     } catch (...) {
1925         throwJavaException(env, 0, method_name);
1926     }
1927
1928     return 0;
1929 }
1930
1931 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
1932     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals);
1933
1934 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
1935     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
1936 {
1937     static const char method_name[] = "Mat::nPutF()";
1938     try {
1939         LOGD("%s", method_name);
1940         cv::Mat* me = (cv::Mat*) self;
1941         if(! self) return 0; // no native object behind
1942         if(me->depth() != CV_32F) return 0; // incompatible type
1943         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
1944
1945         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
1946         int res = mat_put<float>(me, row, col, count, values);
1947         env->ReleasePrimitiveArrayCritical(vals, values, 0);
1948         return res;
1949     } catch(const std::exception &e) {
1950         throwJavaException(env, &e, method_name);
1951     } catch (...) {
1952         throwJavaException(env, 0, method_name);
1953     }
1954
1955     return 0;
1956 }
1957
1958
1959 } // extern "C"
1960
1961 template<typename T> int mat_get(cv::Mat* m, int row, int col, int count, char* buff)
1962 {
1963     if(! m) return 0;
1964     if(! buff) return 0;
1965
1966     int bytesToCopy = count * sizeof(T);
1967     int bytesRestInMat = ((m->rows - row) * m->cols - col) * (int)m->elemSize();
1968     if(bytesToCopy > bytesRestInMat) bytesToCopy = bytesRestInMat;
1969     int res = bytesToCopy;
1970
1971     if( m->isContinuous() )
1972     {
1973         memcpy(buff, m->ptr(row, col), bytesToCopy);
1974     } else {
1975         // row by row
1976         int bytesInRow = (m->cols - col) * (int)m->elemSize(); // 1st partial row
1977         while(bytesToCopy > 0)
1978         {
1979             int len = std::min(bytesToCopy, bytesInRow);
1980             memcpy(buff, m->ptr(row, col), len);
1981             bytesToCopy -= len;
1982             buff += len;
1983             row++;
1984             col = 0;
1985             bytesInRow = m->cols * (int)m->elemSize();
1986         }
1987     }
1988     return res;
1989 }
1990
1991 extern "C" {
1992
1993 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
1994     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals);
1995
1996 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
1997     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals)
1998 {
1999     static const char method_name[] = "Mat::nGetB()";
2000     try {
2001         LOGD("%s", method_name);
2002         cv::Mat* me = (cv::Mat*) self;
2003         if(! self) return 0; // no native object behind
2004         if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
2005         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2006
2007         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
2008         int res = mat_get<char>(me, row, col, count, values);
2009         env->ReleasePrimitiveArrayCritical(vals, values, 0);
2010         return res;
2011     } catch(const std::exception &e) {
2012         throwJavaException(env, &e, method_name);
2013     } catch (...) {
2014         throwJavaException(env, 0, method_name);
2015     }
2016
2017     return 0;
2018 }
2019
2020 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
2021     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals);
2022
2023 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
2024     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
2025 {
2026     static const char method_name[] = "Mat::nGetS()";
2027     try {
2028         LOGD("%s", method_name);
2029         cv::Mat* me = (cv::Mat*) self;
2030         if(! self) return 0; // no native object behind
2031         if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
2032         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2033
2034         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
2035         int res = mat_get<short>(me, row, col, count, values);
2036         env->ReleasePrimitiveArrayCritical(vals, values, 0);
2037         return res;
2038     } catch(const std::exception &e) {
2039         throwJavaException(env, &e, method_name);
2040     } catch (...) {
2041         throwJavaException(env, 0, method_name);
2042     }
2043
2044     return 0;
2045 }
2046
2047 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
2048     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals);
2049
2050 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
2051     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
2052 {
2053     static const char method_name[] = "Mat::nGetI()";
2054     try {
2055         LOGD("%s", method_name);
2056         cv::Mat* me = (cv::Mat*) self;
2057         if(! self) return 0; // no native object behind
2058         if(me->depth() != CV_32S) return 0; // incompatible type
2059         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2060
2061         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
2062         int res = mat_get<int>(me, row, col, count, values);
2063         env->ReleasePrimitiveArrayCritical(vals, values, 0);
2064         return res;
2065     } catch(const std::exception &e) {
2066         throwJavaException(env, &e, method_name);
2067     } catch (...) {
2068         throwJavaException(env, 0, method_name);
2069     }
2070
2071     return 0;
2072 }
2073
2074 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
2075     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals);
2076
2077 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
2078     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
2079 {
2080     static const char method_name[] = "Mat::nGetF()";
2081     try {
2082         LOGD("%s", method_name);
2083         cv::Mat* me = (cv::Mat*) self;
2084         if(! self) return 0; // no native object behind
2085         if(me->depth() != CV_32F) return 0; // incompatible type
2086         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2087
2088         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
2089         int res = mat_get<float>(me, row, col, count, values);
2090         env->ReleasePrimitiveArrayCritical(vals, values, 0);
2091         return res;
2092     } catch(const std::exception &e) {
2093         throwJavaException(env, &e, method_name);
2094     } catch (...) {
2095         throwJavaException(env, 0, method_name);
2096     }
2097
2098     return 0;
2099 }
2100
2101 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
2102     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals);
2103
2104 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
2105     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals)
2106 {
2107     static const char method_name[] = "Mat::nGetD()";
2108     try {
2109         LOGD("%s", method_name);
2110         cv::Mat* me = (cv::Mat*) self;
2111         if(! self) return 0; // no native object behind
2112         if(me->depth() != CV_64F) return 0; // incompatible type
2113         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2114
2115         char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
2116         int res = mat_get<double>(me, row, col, count, values);
2117         env->ReleasePrimitiveArrayCritical(vals, values, 0);
2118         return res;
2119     } catch(const std::exception &e) {
2120         throwJavaException(env, &e, method_name);
2121     } catch (...) {
2122         throwJavaException(env, 0, method_name);
2123     }
2124
2125     return 0;
2126 }
2127
2128 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
2129     (JNIEnv* env, jclass, jlong self, jint row, jint col);
2130
2131 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
2132     (JNIEnv* env, jclass, jlong self, jint row, jint col)
2133 {
2134     static const char method_name[] = "Mat::nGet()";
2135     try {
2136         LOGD("%s", method_name);
2137         cv::Mat* me = (cv::Mat*) self;
2138         if(! self) return 0; // no native object behind
2139         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
2140
2141         jdoubleArray res = env->NewDoubleArray(me->channels());
2142         if(res){
2143             jdouble buff[CV_CN_MAX];//me->channels()
2144             int i;
2145             switch(me->depth()){
2146                 case CV_8U:  for(i=0; i<me->channels(); i++) buff[i] = *((unsigned char*) me->ptr(row, col) + i); break;
2147                 case CV_8S:  for(i=0; i<me->channels(); i++) buff[i] = *((signed char*)   me->ptr(row, col) + i); break;
2148                 case CV_16U: for(i=0; i<me->channels(); i++) buff[i] = *((unsigned short*)me->ptr(row, col) + i); break;
2149                 case CV_16S: for(i=0; i<me->channels(); i++) buff[i] = *((signed short*)  me->ptr(row, col) + i); break;
2150                 case CV_32S: for(i=0; i<me->channels(); i++) buff[i] = *((int*)           me->ptr(row, col) + i); break;
2151                 case CV_32F: for(i=0; i<me->channels(); i++) buff[i] = *((float*)         me->ptr(row, col) + i); break;
2152                 case CV_64F: for(i=0; i<me->channels(); i++) buff[i] = *((double*)        me->ptr(row, col) + i); break;
2153             }
2154             env->SetDoubleArrayRegion(res, 0, me->channels(), buff);
2155         }
2156         return res;
2157     } catch(const std::exception &e) {
2158         throwJavaException(env, &e, method_name);
2159     } catch (...) {
2160         throwJavaException(env, 0, method_name);
2161     }
2162
2163     return 0;
2164 }
2165
2166 JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
2167   (JNIEnv *env, jclass, jlong self);
2168
2169 JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
2170   (JNIEnv *env, jclass, jlong self)
2171 {
2172     static const char method_name[] = "Mat::nDump()";
2173     try {
2174         LOGD("%s", method_name);
2175         cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
2176         std::stringstream s;
2177         s << *me;
2178         std::string str = s.str();
2179         return env->NewStringUTF(str.c_str());
2180     } catch(const std::exception &e) {
2181         throwJavaException(env, &e, method_name);
2182     } catch (...) {
2183         throwJavaException(env, 0, method_name);
2184     }
2185
2186     return 0;
2187 }
2188
2189
2190 } // extern "C"