@@ -13,10 +13,11 @@ tsdr_lib_t tsdr_instance;
1313
1414struct java_context {
1515 jobject obj ;
16+ jobject obj_pixels ;
1617 jclass cls ;
1718 jfieldID fid_pixels ;
18- jfieldID fid_width ;
19- jfieldID fid_height ;
19+ int pic_width ;
20+ int pic_height ;
2021 jmethodID fixSize ;
2122 jmethodID notifyCallbacks ;
2223 jint * pixels ;
@@ -97,15 +98,17 @@ void read_async(float *buf, int width, int height, void *ctx) {
9798 if ((* jvm )-> GetEnv (jvm , (void * * )& env , javaversion ) == JNI_EDETACHED )
9899 (* jvm )-> AttachCurrentThread (jvm , (void * * ) & env , 0 );
99100
100- jint i_width = ( * env ) -> GetIntField ( env , context -> obj , context -> fid_width );
101- jint i_height = (* env )-> GetIntField (env , context -> obj , context -> fid_height );
101+ if ( context -> pic_width != width || context -> pic_height != height ) {
102+ (* env )-> DeleteLocalRef (env , context -> obj_pixels );
102103
103- if (i_width != width || i_height != height ) {
104- // fixSize(200, 200);
105104 (* env )-> CallVoidMethod (env , context -> obj , context -> fixSize , width , height );
106105
106+ context -> obj_pixels = (* env )-> GetObjectField (env , context -> obj , context -> fid_pixels );
107+
107108 context -> pixelsize = width * height ;
108109 context -> pixels = (jint * ) realloc ((void * ) context -> pixels , sizeof (jint ) * context -> pixelsize );
110+ context -> pic_width = width ;
111+ context -> pic_height = height ;
109112 }
110113
111114 jint * data = context -> pixels ;
@@ -118,7 +121,7 @@ void read_async(float *buf, int width, int height, void *ctx) {
118121 }
119122
120123 // release elements
121- (* env )-> SetIntArrayRegion (env , ( * env ) -> GetObjectField ( env , context -> obj , context -> fid_pixels ) , 0 , context -> pixelsize , context -> pixels );
124+ (* env )-> SetIntArrayRegion (env , context -> obj_pixels , 0 , context -> pixelsize , context -> pixels );
122125
123126 // notifyCallbacks();
124127 (* env )-> CallVoidMethod (env , context -> obj , context -> notifyCallbacks );
@@ -132,15 +135,12 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv
132135 (* env )-> DeleteLocalRef (env , obj );
133136 context -> cls = (jclass ) (* env )-> NewGlobalRef (env , (* env )-> GetObjectClass (env , context -> obj ));
134137 context -> fid_pixels = (* env )-> GetFieldID (env , context -> cls , "pixels" , "[I" );
135- context -> fid_width = (* env )-> GetFieldID (env , context -> cls , "width" , "I" );
136- context -> fid_height = (* env )-> GetFieldID (env , context -> cls , "height" , "I" );
137138 context -> fixSize = (* env )-> GetMethodID (env , context -> cls , "fixSize" , "(II)V" );
138139 context -> notifyCallbacks = (* env )-> GetMethodID (env , context -> cls , "notifyCallbacks" , "()V" );
139140
140- jint i_width = (* env )-> GetIntField (env , context -> obj , context -> fid_width );
141- jint i_height = (* env )-> GetIntField (env , context -> obj , context -> fid_height );
142-
143- context -> pixelsize = i_width * i_height ;
141+ context -> pic_width = 0 ;
142+ context -> pic_height = 0 ;
143+ context -> pixelsize = 1 ;
144144 context -> pixels = (jint * ) malloc (sizeof (jint ) * context -> pixelsize );
145145
146146 const char * npath = (* env )-> GetStringUTFChars (env , path , 0 );
@@ -152,6 +152,7 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv
152152 (* env )-> ReleaseStringUTFChars (env , params , nparams );
153153 (* env )-> DeleteGlobalRef (env , context -> obj );
154154 (* env )-> DeleteGlobalRef (env , context -> cls );
155+
155156 free (context );
156157 free (context -> pixels );
157158}
0 commit comments