1 /*
2 * Copyright (C) 2010 The Android Open Source Project
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 #include <android/bitmap.h>
18 #include <jni.h>
19
20 #include "utils.h"
21 #include "_jni.h"
22
23 using android::apps::photoeditor::utils::LockBitmaps;
24 using android::apps::photoeditor::utils::UnlockBitmaps;
25
26 namespace {
27
Java_com_android_photoeditor_filters_ImageUtils_nativeFlipHorizontal(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)28 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeFlipHorizontal(
29 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
30 pFlipHorizontalType f = (pFlipHorizontalType)JNIFunc[JNI_FlipHorizontal].func_ptr;
31 return f(env, obj, src_bitmap, dst_bitmap);
32 }
33
FlipHorizontal(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)34 extern "C" void FlipHorizontal(
35 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
36
37 AndroidBitmapInfo src_info;
38 AndroidBitmapInfo dst_info;
39 void* src_pixels;
40 void* dst_pixels;
41
42 int ret = LockBitmaps(
43 env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
44 if (ret < 0) {
45 LOGE("LockBitmaps in FlipHorizontal failed, error=%d", ret);
46 return;
47 }
48
49 for (uint32_t scan_line = 0; scan_line < src_info.height; scan_line++) {
50 uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels) + src_info.width - 1;
51 uint32_t* src = reinterpret_cast<uint32_t*>(src_pixels);
52 uint32_t* src_line_end = src + src_info.width;
53
54 while (src < src_line_end) {
55 *dst = *src;
56 src++;
57 dst--;
58 }
59 src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
60 dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride;
61 }
62
63 UnlockBitmaps(env, src_bitmap, dst_bitmap);
64 }
65
Java_com_android_photoeditor_filters_ImageUtils_nativeFlipVertical(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)66 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeFlipVertical(
67 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
68 pFlipVerticalType f = (pFlipVerticalType)JNIFunc[JNI_FlipVertical].func_ptr;
69 return f(env, obj, src_bitmap, dst_bitmap);
70 }
71
FlipVertical(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)72 extern "C" void FlipVertical(
73 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
74 AndroidBitmapInfo src_info;
75 AndroidBitmapInfo dst_info;
76 void* src_pixels;
77 void* dst_pixels;
78
79 int ret = LockBitmaps(
80 env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
81 if (ret < 0) {
82 LOGE("LockBitmaps in FlipVertical failed, error=%d", ret);
83 return;
84 }
85
86 dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride * (dst_info.height - 1);
87 for (uint32_t scan_line = 0; scan_line < src_info.height; scan_line++) {
88 uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
89 uint32_t* src = reinterpret_cast<uint32_t*>(src_pixels);
90 uint32_t* src_line_end = src + src_info.width;
91
92 while (src < src_line_end) {
93 *dst = *src;
94 src++;
95 dst++;
96 }
97 src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
98 dst_pixels = reinterpret_cast<char*>(dst_pixels) - dst_info.stride;
99 }
100
101 UnlockBitmaps(env, src_bitmap, dst_bitmap);
102 }
103
Java_com_android_photoeditor_filters_ImageUtils_nativeFlipBoth(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)104 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeFlipBoth(
105 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
106 pFlipBothType f = (pFlipBothType)JNIFunc[JNI_FlipBoth].func_ptr;
107 return f(env, obj, src_bitmap, dst_bitmap);
108 }
109
FlipBoth(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap)110 extern "C" void FlipBoth(
111 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
112 AndroidBitmapInfo src_info;
113 AndroidBitmapInfo dst_info;
114 void* src_pixels;
115 void* dst_pixels;
116
117 int ret = LockBitmaps(
118 env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
119 if (ret < 0) {
120 LOGE("LockBitmaps in FlipBoth failed, error=%d", ret);
121 return;
122 }
123
124 dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride * (dst_info.height - 1);
125 for (uint32_t scan_line = 0; scan_line < src_info.height; scan_line++) {
126 uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels) + src_info.width - 1;
127 uint32_t* src = reinterpret_cast<uint32_t*>(src_pixels);
128 uint32_t* src_line_end = src + src_info.width;
129
130 while (src < src_line_end) {
131 *dst = *src;
132 src++;
133 dst--;
134 }
135 src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
136 dst_pixels = reinterpret_cast<char*>(dst_pixels) - dst_info.stride;
137 }
138
139 UnlockBitmaps(env, src_bitmap, dst_bitmap);
140 }
141
142 } // namespace
143