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 <cmath>
21
22 #include "utils.h"
23 #include "_jni.h"
24
25 using android::apps::photoeditor::utils::LockBitmaps;
26 using android::apps::photoeditor::utils::pixel32_t;
27 using android::apps::photoeditor::utils::UnlockBitmaps;
28
29 namespace {
30
31 /**
32 * @param range the position of the significant falloff of light (50% luminosity).
33 */
Java_com_android_photoeditor_filters_ImageUtils_nativeVignetting(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jfloat range)34 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeVignetting(
35 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat range) {
36 pVignettingType f = (pVignettingType)JNIFunc[JNI_Vignetting].func_ptr;
37 return f(env, obj, src_bitmap, dst_bitmap, range);
38 }
39
Vignetting(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jfloat range)40 extern "C" void Vignetting(
41 JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat range) {
42 AndroidBitmapInfo src_info;
43 AndroidBitmapInfo dst_info;
44 void* src_pixels;
45 void* dst_pixels;
46
47 const float slope = 20.0f; // how fast the light changes, the bigger the faster.
48 range *= slope;
49 const int kShiftBits = 10;
50 const uint32_t shade = 0.85 * (1 << kShiftBits); // the intensity of the shade
51 const uint32_t shade_offset = (1 << kShiftBits) - shade;
52
53 int ret = LockBitmaps(
54 env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
55 if (ret < 0) {
56 LOGE("LockBitmaps in Vignetting failed, error=%d", ret);
57 return;
58 }
59
60 const uint32_t center_x = src_info.width / 2;
61 const uint32_t center_y = src_info.height / 2;
62 const uint32_t hypot = sqrt(center_x * center_x + center_y * center_y) / slope;
63
64 for (uint32_t dst_y = 0; dst_y < dst_info.height; dst_y++) {
65 uint32_t *dst = reinterpret_cast<uint32_t*>(
66 reinterpret_cast<char*>(dst_pixels) + dst_info.stride * dst_y);
67 pixel32_t *src = reinterpret_cast<pixel32_t*>(
68 reinterpret_cast<char*>(src_pixels) + src_info.stride * dst_y);
69 int dy = dst_y - center_y;
70 for (uint32_t dst_x = 0; dst_x < dst_info.width; dst_x++) {
71 int dx = dst_x - center_x;
72 int dist_square = dx * dx + dy * dy;
73 uint32_t luminousity = shade / (1 + exp(sqrt(dist_square) / hypot - range)) +
74 shade_offset;
75 uint32_t red = (luminousity * src->rgba8[0]) >> kShiftBits;
76 uint32_t green = (luminousity * src->rgba8[1]) >> kShiftBits;
77 uint32_t blue = (luminousity * src->rgba8[2]) >> kShiftBits;
78 uint32_t alpha = src->rgba8[3];
79 *dst = (alpha << 24) | (blue << 16) | (green << 8) | red;
80 dst++;
81 src++;
82 } // dst_x
83 } // dst_y
84
85 UnlockBitmaps(env, src_bitmap, dst_bitmap);
86 }
87
88 } // namespace
89