PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/jni/opticalflow/optical_flow-jni.cpp

http://eyes-free.googlecode.com/
C++ | 274 lines | 205 code | 50 blank | 19 comment | 9 complexity | 113caaf5e3e0d6d8128f61cf7aacf8df MD5 | raw file
  1/*
  2 * Copyright 2011, Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *     http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17// Author: Andrew Harp
 18
 19#include <time.h>
 20#include <stdlib.h>
 21#include <string.h>
 22#include <jni.h>
 23#include "types.h"
 24#include "optical_flow_utils.h"
 25#include "time_log.h"
 26#include "image.h"
 27#include "optical_flow.h"
 28
 29namespace flow {
 30
 31#ifdef __cplusplus
 32extern "C" {
 33#endif
 34
 35  JNIEXPORT
 36  void
 37  JNICALL
 38  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_initNative(
 39      JNIEnv* env,
 40      jobject thiz,
 41      jint width,
 42      jint height,
 43      jint downsample_factor);
 44
 45  JNIEXPORT
 46  void
 47  JNICALL
 48  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addFrameNative(
 49      JNIEnv* env,
 50      jobject thiz,
 51      jbyteArray photo_data,
 52      jlong timestamp);
 53
 54  JNIEXPORT
 55  void
 56  JNICALL
 57  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFeaturesNative(
 58      JNIEnv* env,
 59      jobject thiz,
 60      jboolean cached_ok);
 61
 62  JNIEXPORT
 63  void
 64  JNICALL
 65  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFlowNative(
 66      JNIEnv* env,
 67      jobject thiz);
 68
 69  JNIEXPORT
 70  void
 71  JNICALL
 72  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_printInfoNative(
 73      JNIEnv* env,
 74      jobject thiz);
 75
 76  JNIEXPORT
 77  void
 78  JNICALL
 79  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addInterestRegionNative(
 80      JNIEnv* env,
 81      jobject thiz,
 82      jint num_x, jint num_y,
 83      jfloat left, jfloat top, jfloat right, jfloat bottom);
 84
 85  JNIEXPORT
 86  jfloatArray
 87  JNICALL
 88  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getFeaturesNative(
 89      JNIEnv* env,
 90      jobject thiz,
 91      jboolean only_found_);
 92
 93  JNIEXPORT
 94  void
 95  JNICALL
 96  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getAccumulatedDeltaNative(
 97      JNIEnv* env,
 98      jobject thiz,
 99      jlong timestamp,
100      jfloat position_x,
101      jfloat position_y,
102      jfloat radius,
103      jfloatArray delta);
104
105  JNIEXPORT
106  void
107  JNICALL
108  Java_com_googlecode_eyesfree_opticflow_OpticalFlow_resetNative(
109      JNIEnv* env,
110      jobject thiz);
111
112#ifdef __cplusplus
113}
114#endif
115
116OpticalFlow* optical_flow = NULL;
117
118
119JNIEXPORT
120void
121JNICALL
122Java_com_googlecode_eyesfree_opticflow_OpticalFlow_initNative(
123    JNIEnv* env,
124    jobject thiz,
125    jint width,
126    jint height,
127    jint downsample_factor) {
128  SAFE_DELETE(optical_flow);
129
130  LOGI("Initializing optical flow. %dx%d, %d",
131       width, height, downsample_factor);
132  optical_flow = new OpticalFlow(width, height, downsample_factor);
133}
134
135
136JNIEXPORT
137void
138JNICALL
139Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addFrameNative(
140    JNIEnv* env,
141    jobject thiz,
142    jbyteArray photo_data,
143    jlong timestamp) {
144  CHECK(optical_flow != NULL, "Optical flow not initialized!");
145
146  resetTimeLog();
147  timeLog("Starting optical flow");
148
149  // Copy image into currFrame.
150  jboolean iCopied = JNI_FALSE;
151  jbyte* pixels = env->GetByteArrayElements(photo_data, &iCopied);
152
153  timeLog("Got elements");
154
155  // Add the frame to the optical flow object.
156  optical_flow->nextFrame(reinterpret_cast<uint8*>(pixels), timestamp);
157
158  env->ReleaseByteArrayElements(photo_data, pixels, JNI_ABORT);
159  timeLog("Released elements");
160}
161
162
163JNIEXPORT
164void
165JNICALL
166Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFeaturesNative(
167    JNIEnv* env,
168    jobject thiz,
169    jboolean cached_ok) {
170  CHECK(optical_flow != NULL, "Optical flow not initialized!");
171
172  optical_flow->computeFeatures(cached_ok);
173}
174
175
176JNIEXPORT
177void
178JNICALL
179Java_com_googlecode_eyesfree_opticflow_OpticalFlow_computeFlowNative(
180    JNIEnv* env,
181    jobject thiz) {
182  CHECK(optical_flow != NULL, "Optical flow not initialized!");
183
184  optical_flow->computeFlow();
185}
186
187
188JNIEXPORT
189void
190JNICALL
191Java_com_googlecode_eyesfree_opticflow_OpticalFlow_printInfoNative(
192    JNIEnv* env,
193    jobject thiz) {
194  CHECK(optical_flow != NULL, "Optical flow not initialized!");
195
196  printTimeLog();
197  optical_flow->printInfo();
198}
199
200
201JNIEXPORT
202void
203JNICALL
204Java_com_googlecode_eyesfree_opticflow_OpticalFlow_addInterestRegionNative(
205    JNIEnv* env,
206    jobject thiz,
207    jint num_x, jint num_y,
208    jfloat left, jfloat top, jfloat right, jfloat bottom) {
209  CHECK(optical_flow != NULL, "Optical flow not initialized!");
210
211  optical_flow->addInterestRegion(num_x, num_y, left, top, right, bottom);
212  timeLog("Added interest region.");
213}
214
215
216JNIEXPORT
217jfloatArray
218JNICALL
219Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getFeaturesNative(
220    JNIEnv* env,
221    jobject thiz,
222    jboolean only_found) {
223  CHECK(optical_flow != NULL, "Optical flow not initialized!");
224
225  jfloat feature_arr[MAX_FEATURES * FEATURE_STEP];
226
227  const int32 number_of_features =
228      optical_flow->getFeatures(only_found, feature_arr);
229
230  // Create and return the array that will be passed back to Java.
231  jfloatArray features = env->NewFloatArray(number_of_features * FEATURE_STEP);
232  if (features == NULL) {
233    LOGE("null array!");
234    return NULL;
235  }
236  env->SetFloatArrayRegion(
237      features, 0, number_of_features * FEATURE_STEP, feature_arr);
238
239  return features;
240}
241
242
243JNIEXPORT
244void
245JNICALL
246Java_com_googlecode_eyesfree_opticflow_OpticalFlow_getAccumulatedDeltaNative(
247    JNIEnv* env,
248    jobject thiz,
249    jlong timestamp,
250    jfloat position_x,
251    jfloat position_y,
252    jfloat radius,
253    jfloatArray delta) {
254  CHECK(optical_flow != NULL, "Optical flow not initialized!");
255
256  const Point2D query_position(position_x, position_y);
257  const Point2D query_delta =
258      optical_flow->getAccumulatedDelta(query_position, radius, timestamp);
259  const jfloat point_arr[] = { query_delta.x, query_delta.y };
260
261  env->SetFloatArrayRegion(delta, 0, 2, point_arr);
262}
263
264
265JNIEXPORT
266void
267JNICALL
268Java_com_googlecode_eyesfree_opticflow_OpticalFlow_resetNative(
269    JNIEnv* env, jobject thiz) {
270  LOGI("Cleaning up optical flow.");
271  SAFE_DELETE(optical_flow);
272}
273
274}  // namespace flow