|
| 1 | +/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +// This file binds the native image utility code to the Java class |
| 17 | +// which exposes them. |
| 18 | + |
| 19 | +#include <jni.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | + |
| 23 | +#include "rgb2yuv.h" |
| 24 | +#include "yuv2rgb.h" |
| 25 | + |
| 26 | +#define IMAGEUTILS_METHOD(METHOD_NAME) \ |
| 27 | + Java_tensorflow_detector_spc_env_ImageUtils_##METHOD_NAME // NOLINT |
| 28 | + |
| 29 | +#ifdef __cplusplus |
| 30 | +extern "C" { |
| 31 | +#endif |
| 32 | + |
| 33 | +JNIEXPORT void JNICALL |
| 34 | +IMAGEUTILS_METHOD(convertYUV420SPToARGB8888)( |
| 35 | + JNIEnv* env, jclass clazz, jbyteArray input, jintArray output, |
| 36 | + jint width, jint height, jboolean halfSize); |
| 37 | + |
| 38 | +JNIEXPORT void JNICALL IMAGEUTILS_METHOD(convertYUV420ToARGB8888)( |
| 39 | + JNIEnv* env, jclass clazz, jbyteArray y, jbyteArray u, jbyteArray v, |
| 40 | + jintArray output, jint width, jint height, jint y_row_stride, |
| 41 | + jint uv_row_stride, jint uv_pixel_stride, jboolean halfSize); |
| 42 | + |
| 43 | +JNIEXPORT void JNICALL IMAGEUTILS_METHOD(convertYUV420SPToRGB565)( |
| 44 | + JNIEnv* env, jclass clazz, jbyteArray input, jbyteArray output, jint width, |
| 45 | + jint height); |
| 46 | + |
| 47 | +JNIEXPORT void JNICALL |
| 48 | +IMAGEUTILS_METHOD(convertARGB8888ToYUV420SP)( |
| 49 | + JNIEnv* env, jclass clazz, jintArray input, jbyteArray output, |
| 50 | + jint width, jint height); |
| 51 | + |
| 52 | +JNIEXPORT void JNICALL |
| 53 | +IMAGEUTILS_METHOD(convertRGB565ToYUV420SP)( |
| 54 | + JNIEnv* env, jclass clazz, jbyteArray input, jbyteArray output, |
| 55 | + jint width, jint height); |
| 56 | + |
| 57 | +#ifdef __cplusplus |
| 58 | +} |
| 59 | +#endif |
| 60 | + |
| 61 | +JNIEXPORT void JNICALL |
| 62 | +IMAGEUTILS_METHOD(convertYUV420SPToARGB8888)( |
| 63 | + JNIEnv* env, jclass clazz, jbyteArray input, jintArray output, |
| 64 | + jint width, jint height, jboolean halfSize) { |
| 65 | + jboolean inputCopy = JNI_FALSE; |
| 66 | + jbyte* const i = env->GetByteArrayElements(input, &inputCopy); |
| 67 | + |
| 68 | + jboolean outputCopy = JNI_FALSE; |
| 69 | + jint* const o = env->GetIntArrayElements(output, &outputCopy); |
| 70 | + |
| 71 | + if (halfSize) { |
| 72 | + ConvertYUV420SPToARGB8888HalfSize(reinterpret_cast<uint8_t*>(i), |
| 73 | + reinterpret_cast<uint32_t*>(o), width, |
| 74 | + height); |
| 75 | + } else { |
| 76 | + ConvertYUV420SPToARGB8888(reinterpret_cast<uint8_t*>(i), |
| 77 | + reinterpret_cast<uint8_t*>(i) + width * height, |
| 78 | + reinterpret_cast<uint32_t*>(o), width, height); |
| 79 | + } |
| 80 | + |
| 81 | + env->ReleaseByteArrayElements(input, i, JNI_ABORT); |
| 82 | + env->ReleaseIntArrayElements(output, o, 0); |
| 83 | +} |
| 84 | + |
| 85 | +JNIEXPORT void JNICALL IMAGEUTILS_METHOD(convertYUV420ToARGB8888)( |
| 86 | + JNIEnv* env, jclass clazz, jbyteArray y, jbyteArray u, jbyteArray v, |
| 87 | + jintArray output, jint width, jint height, jint y_row_stride, |
| 88 | + jint uv_row_stride, jint uv_pixel_stride, jboolean halfSize) { |
| 89 | + jboolean inputCopy = JNI_FALSE; |
| 90 | + jbyte* const y_buff = env->GetByteArrayElements(y, &inputCopy); |
| 91 | + jboolean outputCopy = JNI_FALSE; |
| 92 | + jint* const o = env->GetIntArrayElements(output, &outputCopy); |
| 93 | + |
| 94 | + if (halfSize) { |
| 95 | + ConvertYUV420SPToARGB8888HalfSize(reinterpret_cast<uint8_t*>(y_buff), |
| 96 | + reinterpret_cast<uint32_t*>(o), width, |
| 97 | + height); |
| 98 | + } else { |
| 99 | + jbyte* const u_buff = env->GetByteArrayElements(u, &inputCopy); |
| 100 | + jbyte* const v_buff = env->GetByteArrayElements(v, &inputCopy); |
| 101 | + |
| 102 | + ConvertYUV420ToARGB8888( |
| 103 | + reinterpret_cast<uint8_t*>(y_buff), reinterpret_cast<uint8_t*>(u_buff), |
| 104 | + reinterpret_cast<uint8_t*>(v_buff), reinterpret_cast<uint32_t*>(o), |
| 105 | + width, height, y_row_stride, uv_row_stride, uv_pixel_stride); |
| 106 | + |
| 107 | + env->ReleaseByteArrayElements(u, u_buff, JNI_ABORT); |
| 108 | + env->ReleaseByteArrayElements(v, v_buff, JNI_ABORT); |
| 109 | + } |
| 110 | + |
| 111 | + env->ReleaseByteArrayElements(y, y_buff, JNI_ABORT); |
| 112 | + env->ReleaseIntArrayElements(output, o, 0); |
| 113 | +} |
| 114 | + |
| 115 | +JNIEXPORT void JNICALL IMAGEUTILS_METHOD(convertYUV420SPToRGB565)( |
| 116 | + JNIEnv* env, jclass clazz, jbyteArray input, jbyteArray output, jint width, |
| 117 | + jint height) { |
| 118 | + jboolean inputCopy = JNI_FALSE; |
| 119 | + jbyte* const i = env->GetByteArrayElements(input, &inputCopy); |
| 120 | + |
| 121 | + jboolean outputCopy = JNI_FALSE; |
| 122 | + jbyte* const o = env->GetByteArrayElements(output, &outputCopy); |
| 123 | + |
| 124 | + ConvertYUV420SPToRGB565(reinterpret_cast<uint8_t*>(i), |
| 125 | + reinterpret_cast<uint16_t*>(o), width, height); |
| 126 | + |
| 127 | + env->ReleaseByteArrayElements(input, i, JNI_ABORT); |
| 128 | + env->ReleaseByteArrayElements(output, o, 0); |
| 129 | +} |
| 130 | + |
| 131 | +JNIEXPORT void JNICALL |
| 132 | +IMAGEUTILS_METHOD(convertARGB8888ToYUV420SP)( |
| 133 | + JNIEnv* env, jclass clazz, jintArray input, jbyteArray output, |
| 134 | + jint width, jint height) { |
| 135 | + jboolean inputCopy = JNI_FALSE; |
| 136 | + jint* const i = env->GetIntArrayElements(input, &inputCopy); |
| 137 | + |
| 138 | + jboolean outputCopy = JNI_FALSE; |
| 139 | + jbyte* const o = env->GetByteArrayElements(output, &outputCopy); |
| 140 | + |
| 141 | + ConvertARGB8888ToYUV420SP(reinterpret_cast<uint32_t*>(i), |
| 142 | + reinterpret_cast<uint8_t*>(o), width, height); |
| 143 | + |
| 144 | + env->ReleaseIntArrayElements(input, i, JNI_ABORT); |
| 145 | + env->ReleaseByteArrayElements(output, o, 0); |
| 146 | +} |
| 147 | + |
| 148 | +JNIEXPORT void JNICALL |
| 149 | +IMAGEUTILS_METHOD(convertRGB565ToYUV420SP)( |
| 150 | + JNIEnv* env, jclass clazz, jbyteArray input, jbyteArray output, |
| 151 | + jint width, jint height) { |
| 152 | + jboolean inputCopy = JNI_FALSE; |
| 153 | + jbyte* const i = env->GetByteArrayElements(input, &inputCopy); |
| 154 | + |
| 155 | + jboolean outputCopy = JNI_FALSE; |
| 156 | + jbyte* const o = env->GetByteArrayElements(output, &outputCopy); |
| 157 | + |
| 158 | + ConvertRGB565ToYUV420SP(reinterpret_cast<uint16_t*>(i), |
| 159 | + reinterpret_cast<uint8_t*>(o), width, height); |
| 160 | + |
| 161 | + env->ReleaseByteArrayElements(input, i, JNI_ABORT); |
| 162 | + env->ReleaseByteArrayElements(output, o, 0); |
| 163 | +} |
0 commit comments