Jack Yoo | a02710a | 2016-04-06 16:07:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 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 | |
| 17 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | IF 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 |
| 40 | extern "C" { |
| 41 | #endif |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 42 | JNIEXPORT jint JNICALL Java_com_android_camera_imageprocessor_PostProcessor_nativeFlipNV21( |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 43 | JNIEnv* env, jobject thiz, jbyteArray yvuBytes, jint stride, jint height, jint gap, jboolean isVertical); |
Jack Yoo | a02710a | 2016-04-06 16:07:22 -0700 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | typedef unsigned char uint8_t; |
| 49 | |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 50 | jint JNICALL Java_com_android_camera_imageprocessor_PostProcessor_nativeFlipNV21( |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 51 | JNIEnv* env, jobject thiz, jbyteArray yvuBytes, jint stride, jint height, jint gap, jboolean isVertical) |
Jack Yoo | 7d80bdb | 2016-07-13 14:49:22 -0700 | [diff] [blame] | 52 | { |
jinwu | 8045fd4 | 2018-02-09 14:54:16 +0800 | [diff] [blame] | 53 | (void)thiz; |
Jack Yoo | 7d80bdb | 2016-07-13 14:49:22 -0700 | [diff] [blame] | 54 | jbyte* imageDataNV21Array = env->GetByteArrayElements(yvuBytes, NULL); |
| 55 | uint8_t *buf = (uint8_t *)imageDataNV21Array; |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 56 | int ysize = stride * height; |
Jack Yoo | 7d80bdb | 2016-07-13 14:49:22 -0700 | [diff] [blame] | 57 | uint8_t temp1, temp2; |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 58 | |
| 59 | if(isVertical) { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 60 | for (int x = 0; x < stride; x++) { |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 61 | for (int y = 0; y < height / 2; y++) { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 62 | 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 Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 67 | for (int x = 0; x < stride; x += 2) { |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 68 | for (int y = 0; y < height / 4; y++) { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 69 | 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 Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } else { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 78 | int width = stride - gap; |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 79 | for (int x = 0; x < width/2; x++) { |
| 80 | for (int y = 0; y < height; y++) { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 81 | 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 Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | for (int x = 0; x < width/2; x += 2) { |
| 87 | for (int y = 0; y < height / 2; y++) { |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 88 | 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 Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 94 | } |
Jack Yoo | 7d80bdb | 2016-07-13 14:49:22 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
Jack Yoo | aa24e96 | 2016-09-26 13:21:57 -0700 | [diff] [blame] | 97 | |
Jack Yoo | 7d80bdb | 2016-07-13 14:49:22 -0700 | [diff] [blame] | 98 | env->ReleaseByteArrayElements(yvuBytes, imageDataNV21Array, JNI_ABORT); |
| 99 | return 0; |
Jack Yoo | 392d833 | 2016-09-29 12:35:56 -0700 | [diff] [blame] | 100 | } |