blob: 4be55d16b36d2dba7e76f952d2a131b65a8980b9 [file] [log] [blame]
Jack Yooa02710a2016-04-06 16:07:22 -07001/*
2Copyright (c) 2016, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include <jni.h>
31#include <assert.h>
32#include <stdlib.h>
33
34#ifdef __ANDROID__
35#include "android/log.h"
36#define printf(...) __android_log_print( ANDROID_LOG_ERROR, "ImageUtil", __VA_ARGS__ )
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
Jack Yooaa24e962016-09-26 13:21:57 -070042JNIEXPORT jint JNICALL Java_com_android_camera_imageprocessor_PostProcessor_nativeFlipNV21(
Jack Yoo392d8332016-09-29 12:35:56 -070043 JNIEnv* env, jobject thiz, jbyteArray yvuBytes, jint stride, jint height, jint gap, jboolean isVertical);
Jack Yooa02710a2016-04-06 16:07:22 -070044#ifdef __cplusplus
45}
46#endif
47
48typedef unsigned char uint8_t;
49
Jack Yooaa24e962016-09-26 13:21:57 -070050jint JNICALL Java_com_android_camera_imageprocessor_PostProcessor_nativeFlipNV21(
Jack Yoo392d8332016-09-29 12:35:56 -070051 JNIEnv* env, jobject thiz, jbyteArray yvuBytes, jint stride, jint height, jint gap, jboolean isVertical)
Jack Yoo7d80bdb2016-07-13 14:49:22 -070052{
jinwu8045fd42018-02-09 14:54:16 +080053 (void)thiz;
Jack Yoo7d80bdb2016-07-13 14:49:22 -070054 jbyte* imageDataNV21Array = env->GetByteArrayElements(yvuBytes, NULL);
55 uint8_t *buf = (uint8_t *)imageDataNV21Array;
Jack Yoo392d8332016-09-29 12:35:56 -070056 int ysize = stride * height;
Jack Yoo7d80bdb2016-07-13 14:49:22 -070057 uint8_t temp1, temp2;
Jack Yooaa24e962016-09-26 13:21:57 -070058
59 if(isVertical) {
Jack Yoo392d8332016-09-29 12:35:56 -070060 for (int x = 0; x < stride; x++) {
Jack Yooaa24e962016-09-26 13:21:57 -070061 for (int y = 0; y < height / 2; y++) {
Jack Yoo392d8332016-09-29 12:35:56 -070062 temp1 = buf[y * stride + x];
63 buf[y * stride + x] = buf[(height - 1 - y) * stride + x];
64 buf[(height - 1 - y) * stride + x] = temp1;
Jack Yooaa24e962016-09-26 13:21:57 -070065 }
66 }
Jack Yoo392d8332016-09-29 12:35:56 -070067 for (int x = 0; x < stride; x += 2) {
Jack Yooaa24e962016-09-26 13:21:57 -070068 for (int y = 0; y < height / 4; y++) {
Jack Yoo392d8332016-09-29 12:35:56 -070069 temp1 = buf[ysize + y * stride + x];
70 temp2 = buf[ysize + y * stride + x + 1];
71 buf[ysize + y * stride + x] = buf[ysize + (height / 2 - 1 - y) * stride + x];
72 buf[ysize + y * stride + x + 1] = buf[ysize + (height / 2 - 1 - y) * stride + x + 1];
73 buf[ysize + (height / 2 - 1 - y) * stride + x] = temp1;
74 buf[ysize + (height / 2 - 1 - y) * stride + x + 1] = temp2;
Jack Yooaa24e962016-09-26 13:21:57 -070075 }
76 }
77 } else {
Jack Yoo392d8332016-09-29 12:35:56 -070078 int width = stride - gap;
Jack Yooaa24e962016-09-26 13:21:57 -070079 for (int x = 0; x < width/2; x++) {
80 for (int y = 0; y < height; y++) {
Jack Yoo392d8332016-09-29 12:35:56 -070081 temp1 = buf[y * stride + x];
82 buf[y * stride + x] = buf[y * stride + (width - 1 - x)];
83 buf[y * stride + (width - 1 - x)] = temp1;
Jack Yooaa24e962016-09-26 13:21:57 -070084 }
85 }
86 for (int x = 0; x < width/2; x += 2) {
87 for (int y = 0; y < height / 2; y++) {
Jack Yoo392d8332016-09-29 12:35:56 -070088 temp1 = buf[ysize + y * stride + x];
89 temp2 = buf[ysize + y * stride + x + 1];
90 buf[ysize + y * stride + x] = buf[ysize + y * stride + (width - 1 - x - 1)];
91 buf[ysize + y * stride + x + 1] = buf[ysize + y * stride + (width - 1 - x)];
92 buf[ysize + y * stride + (width - 1 - x - 1)] = temp1;
93 buf[ysize + y * stride + (width - 1 - x)] = temp2;
Jack Yooaa24e962016-09-26 13:21:57 -070094 }
Jack Yoo7d80bdb2016-07-13 14:49:22 -070095 }
96 }
Jack Yooaa24e962016-09-26 13:21:57 -070097
Jack Yoo7d80bdb2016-07-13 14:49:22 -070098 env->ReleaseByteArrayElements(yvuBytes, imageDataNV21Array, JNI_ABORT);
99 return 0;
Jack Yoo392d8332016-09-29 12:35:56 -0700100}