16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennOpenCV for Android NDK
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCopyright (c) 2006-2009 SIProp Project http://www.siprop.org/
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennThis software is provided 'as-is', without any express or implied warranty.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennIn no event will the authors be held liable for any damages arising from the use of this software.
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennPermission is granted to anyone to use this software for any purpose,
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennincluding commercial applications, and to alter it and redistribute it freely,
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennsubject to the following restrictions:
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn3. This notice may not be removed or altered from any source distribution.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*/
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <stdlib.h>
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <string.h>
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <jni.h>
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include <android/log.h>
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "cv.h"
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "cxcore.h"
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "cvaux.h"
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "highgui.h"
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "ml.h"
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "utils.h"
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "WLNonFileByteStream.h"
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "grfmt_bmp.h"
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define LOGV(...) __android_log_print(ANDROID_LOG_SILENT, LOG_TAG, __VA_ARGS__)
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//ANDROID_LOG_UNKNOWN, ANDROID_LOG_DEFAULT, ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_SILENT
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//LOGV(ANDROID_LOG_DEBUG, "JNI", "");
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define ANDROID_LOG_VERBOSE ANDROID_LOG_DEBUG
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define LOG_TAG "CVJNI"
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define INVALID_ARGUMENT -18456
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define		SAFE_DELETE(p)			{ if(p){ delete (p); (p)=0; } }
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define		SAFE_DELETE_ARRAY(p)	{ if(p){ delete [](p); (p)=0; } }
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#define IMAGE( i, x, y, n )   *(( unsigned char * )(( i )->imageData      \
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                    + ( x ) * sizeof( unsigned char ) * 3 \
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                                    + ( y ) * ( i )->widthStep ) + ( n ))
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// CV Objects
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic const char* fmtSignBmp = "BM";
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvCapture *m_capture = 0;
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvHaarClassifierCascade *m_cascade = 0;
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennIplImage *m_sourceImage = 0;
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennIplImage *m_grayImage = 0;
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennIplImage *m_smallImage = 0;
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvMemStorage *m_storage = 0;
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvSeq *m_facesFound = 0;
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvRect m_faceCropArea;
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvSize m_smallestFaceSize;
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef __cplusplus
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennextern "C" {
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjboolean
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_createSocketCapture(JNIEnv* env,
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												  jobject thiz,
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												  jstring address_str,
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												  jstring port_str,
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												  jint width,
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												  jint height);
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_releaseSocketCapture(JNIEnv* env,
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												   jobject thiz);
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjboolean
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_grabSourceImageFromCapture(JNIEnv* env,
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn														 jobject thiz);
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennjbooleanArray
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_getSourceImage(JNIEnv* env,
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jobject thiz);
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjboolean
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_setSourceImage(JNIEnv* env,
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn									    	 jobject thiz,
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jintArray photo_data,
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jint width,
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jint height);
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennjbooleanArray
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_findContours(JNIEnv* env,
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn										jobject thiz,
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn										jint width,
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn										jint height);
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjboolean
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_initFaceDetection(JNIEnv* env,
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												jobject thiz,
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn												jstring cascade_path_str);
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_releaseFaceDetection(JNIEnv* env,
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn									       	  	   jobject thiz);
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjboolean
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_highlightFaces(JNIEnv* env,
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jobject thiz);
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennjobjectArray
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_findAllFaces(JNIEnv* env,
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn									       jobject thiz);
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNIEXPORT
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjobject
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJNICALL
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennJava_org_siprop_opencv_OpenCV_findSingleFace(JNIEnv* env,
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn											 jobject thiz);
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef __cplusplus
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennIplImage* loadPixels(int* pixels, int width, int height) {
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	int x, y;
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	IplImage *img = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	for ( y = 0; y < height; y++ ) {
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for ( x = 0; x < width; x++ ) {
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            // blue
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            IMAGE( img, x, y, 0 ) = pixels[x+y*width] & 0xFF;
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            // green
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            IMAGE( img, x, y, 1 ) = pixels[x+y*width] >> 8 & 0xFF;
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            // red
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            IMAGE( img, x, y, 2 ) = pixels[x+y*width] >> 16 & 0xFF;
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return img;
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid loadImageBytes(const uchar* data,
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    int step,
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    int width,
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    int height,
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    int depth,
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    int channels,
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    WLNonFileByteStream* m_strm) {
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int fileStep = (width*channels + 3) & -4;
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    uchar zeropad[] = "\0\0\0\0";
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char log_str[100];
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    assert( data && width > 0 && height > 0 && step >= fileStep );
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  bitmapHeaderSize = 40;
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  paletteSize = channels > 1 ? 0 : 1024;
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int  headerSize = 14 /* fileheader */ + bitmapHeaderSize + paletteSize;
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    PaletteEntry palette[256];
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int testSize = fileStep*height + headerSize;
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->Open(testSize);
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	sprintf(log_str, "fileStep*height + headerSize=%i", testSize);
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV(log_str);
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // write signature 'BM'
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutBytes( fmtSignBmp, (int)strlen(fmtSignBmp) );
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // write file header
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( fileStep*height + headerSize ); // file size
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( headerSize );
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // write bitmap header
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( bitmapHeaderSize );
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( width );
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( height );
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutWord( 1 );
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutWord( channels << 3 );
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( BMP_RGB );
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_strm->PutDWord( 0 );
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( channels == 1 )
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        FillGrayPalette( palette, 8 );
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        m_strm->PutBytes( palette, sizeof(palette));
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    width *= channels;
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    data += step*(height - 1);
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( ; height--; data -= step )
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        m_strm->PutBytes( data, width );
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( fileStep > width )
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            m_strm->PutBytes( zeropad, fileStep - width );
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name, const char* field_type) {
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in is_NULL_field_JavaObj!");
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass clazz = env->GetObjectClass(java_obj);
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	// get field
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jfieldID fid = env->GetFieldID(clazz, field_name, field_type);
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jobject obj = env->GetObjectField(java_obj, fid);
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	if(obj == 0) {
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn		LOGV("Object is NULL!");
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn		return true;
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	}
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return false;
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_vec_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Vector3;");
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_point_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Point3;");
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_axis_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Axis;");
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_pivot_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Pivot3;");
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_quat_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Quaternion;");
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_mat3x3_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Matrix3x3;");
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool is_NULL_mat3x1_field_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return is_NULL_field_JavaObj(env, java_obj, field_name, "Lorg/siprop/opencv/util/Matrix3x1;");
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid set_JavaObj_int(JNIEnv* env, jobject java_obj, const char* field_name, jint val) {
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in set_JavaObj_int!");
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass clazz = env->GetObjectClass(java_obj);
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jfieldID fid = env->GetFieldID(clazz, field_name, "I");
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	env->SetIntField(java_obj, fid, val);
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint get_id_by_JavaObj(JNIEnv* env, jobject java_obj) {
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in get_id_by_JavaObj!");
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass method_clazz = env->GetObjectClass(java_obj);
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jmethodID get_type_mid = env->GetMethodID(method_clazz, "getID", "()I");
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return env->CallIntMethod(java_obj, get_type_mid);
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint get_type_by_JavaObj(JNIEnv* env, jobject java_obj) {
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in get_type_by_JavaObj!");
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass method_clazz = env->GetObjectClass(java_obj);
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jmethodID get_type_mid = env->GetMethodID(method_clazz, "getType", "()I");
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return env->CallIntMethod(java_obj, get_type_mid);
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint get_int_by_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in get_int_by_JavaObj!");
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass clazz = env->GetObjectClass(java_obj);
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jfieldID int_fid = env->GetFieldID(clazz, field_name, "I");
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return env->GetIntField(java_obj, int_fid);
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennfloat get_float_by_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name) {
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in get_float_by_JavaObj!");
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass clazz = env->GetObjectClass(java_obj);
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jfieldID float_fid = env->GetFieldID(clazz, field_name, "F");
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return env->GetFloatField(java_obj, float_fid);
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennjobject get_obj_by_JavaObj(JNIEnv* env, jobject java_obj, const char* field_name, const char* obj_type) {
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	LOGV("in get_obj_by_JavaObj!");
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jclass clazz = env->GetObjectClass(java_obj);
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	jfieldID obj_fid = env->GetFieldID(clazz, field_name, obj_type);
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn	return env->GetObjectField(java_obj, obj_fid);
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
343