• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstdlib>
22 
23 #include "utils.h"
24 #include "_jni.h"
25 
26 using android::apps::photoeditor::utils::LockBitmaps;
27 using android::apps::photoeditor::utils::UnlockBitmaps;
28 
29 namespace {
30 
BilinearPixelInterpolation(float x,float y,uint8_t * p00,uint8_t * p01,uint8_t * p10,uint8_t * p11)31 uint32_t BilinearPixelInterpolation(
32     float x, float y, uint8_t *p00, uint8_t *p01, uint8_t *p10, uint8_t *p11) {
33 
34   float coef4 = x * y;
35   float coef2 = x - coef4; // x * (1 - y)
36   float coef3 = y - coef4; // (1 - x) * y
37   float coef1 = 1 - x - coef3; // (1 - x) * (1 - y)
38 
39   int dst_red = p00[0] * coef1 + p10[0] * coef2 + p01[0] * coef3 + p11[0] * coef4;
40   int dst_green = p00[1] * coef1 + p10[1] * coef2 + p01[1] * coef3 + p11[1] * coef4;
41   int dst_blue = p00[2] * coef1 + p10[2] * coef2 + p01[2] * coef3 + p11[2] * coef4;
42   if (dst_red > 255) {
43     dst_red = 255;
44   }
45   if (dst_green > 255) {
46     dst_green = 255;
47   }
48   if (dst_blue > 255) {
49     dst_blue = 255;
50   }
51 
52   // alpha is not calculated, directly from any point should do.
53   return p00[3] << 24 | (dst_blue << 16) | (dst_green << 8) | dst_red;
54 }
55 
FisheyeMapPixels(float px,float py,uint32_t x,uint32_t y,AndroidBitmapInfo * src_info,AndroidBitmapInfo * dst_info,void * src_pixels,void * dst_pixels)56 void FisheyeMapPixels(
57     float px, float py, uint32_t x, uint32_t y, AndroidBitmapInfo *src_info,
58     AndroidBitmapInfo *dst_info, void *src_pixels, void *dst_pixels) {
59   if (x >= src_info->width || y >= src_info->height) {
60     return;
61   }
62 
63   uint32_t px_floor = floor(px);
64   uint32_t py_floor = floor(py);
65 
66   uint32_t *p00 = (uint32_t*)((char*)src_pixels + src_info->stride * py_floor) + px_floor;
67   uint32_t *p01, *p10, *p11;
68   if (py_floor + 1 < src_info->height) {
69     p01 = (uint32_t*)((char*)p00 + src_info->stride);
70   } else {
71     p01 = p00;
72   }
73   if (py_floor + 1 < src_info->width) {
74     p10 = p00 + 1;
75     p11 = p01 + 1;
76   } else {
77     p10 = p00;
78     p11 = p01;
79   }
80 
81   uint32_t *dst = (uint32_t*)((char*)dst_pixels + dst_info->stride * y) + x;
82   *dst = BilinearPixelInterpolation(px - px_floor, py - py_floor,
83                                     (uint8_t*)p00, (uint8_t*)p01, (uint8_t*)p10, (uint8_t*)p11);
84 }
85 
86 /*
87  * @param scale ranges from 0.0 to 1.0.
88  * @param focus_x is the X-coord of the center of the projection ranging from 0 to 1.
89  * @param focus_y is the Y-coord of the center of the projection ranging from 0 to 1.
90  */
Java_com_android_photoeditor_filters_ImageUtils_nativeFisheye(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jfloat focus_x,jfloat focus_y,jfloat scale)91 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeFisheye(
92     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap,
93     jfloat focus_x, jfloat focus_y, jfloat scale) {
94    pFisheyeType f = (pFisheyeType)JNIFunc[JNI_Fisheye].func_ptr;
95    return f(env, obj, src_bitmap, dst_bitmap, focus_x, focus_y, scale);
96 }
97 
Fisheye(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jfloat focus_x,jfloat focus_y,jfloat scale)98 extern "C" void Fisheye(
99     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap,
100     jfloat focus_x, jfloat focus_y, jfloat scale) {
101   AndroidBitmapInfo src_info;
102   AndroidBitmapInfo dst_info;
103   void* src_pixels;
104   void* dst_pixels;
105 
106   int ret = LockBitmaps(
107       env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
108   if (ret < 0) {
109     LOGE("LockBitmaps in FishEye failed, error=%d", ret);
110     return;
111   }
112 
113   if (scale == 0) {
114     memcpy(dst_pixels, src_pixels, src_info.stride * src_info.height);
115     return;
116   }
117 
118   float far_point = hypotf(src_info.height * focus_y, src_info.width * focus_x);
119   const float r = far_point * 1.15; // radius
120   const float r2 = r * r;
121   const uint32_t center_x = round(src_info.width * focus_x);
122   const uint32_t center_y = round(src_info.height * focus_y);
123   float alpha = 0.75 + scale * 2.0;
124   float linear_scale = far_point /
125       (M_PI_2 - atan(alpha * sqrtf(r2 - far_point * far_point) / far_point));
126 
127   for (uint32_t scan_line = 0; scan_line <= center_y; scan_line++) {
128 
129     int y = scan_line - center_y;
130     int y2 = y * y;
131 
132     for (uint32_t dst_x = 0; dst_x <= center_x; dst_x++) {
133       int x = dst_x - center_x;
134       float xy2 = (x * x + y2);
135       float s_xy2 = sqrtf(xy2);
136       float r_scale = linear_scale * (M_PI_2 - atan(alpha * sqrtf(r2 - xy2) / s_xy2)) / s_xy2;
137       float scaled_x = x * r_scale;
138       float scaled_y = y * r_scale;
139       int nx = center_x - x;
140       int ny = center_y - y;
141       float fpx = center_x + scaled_x;
142       float fnx = center_x - scaled_x;
143       float fpy = center_y + scaled_y;
144       float fny = center_y - scaled_y;
145 
146       FisheyeMapPixels(fpx, fpy, dst_x, scan_line, &src_info, &dst_info, src_pixels, dst_pixels);
147       FisheyeMapPixels(fpx, fny, dst_x, ny, &src_info, &dst_info, src_pixels, dst_pixels);
148       FisheyeMapPixels(fnx, fpy, nx, scan_line, &src_info, &dst_info, src_pixels, dst_pixels);
149       FisheyeMapPixels(fnx, fny, nx, ny, &src_info, &dst_info, src_pixels, dst_pixels);
150     }
151   }
152   // Deal with the condition when x = 0 and y = 0 (dst_x = center_x and
153   // scan_line = center_y.
154   uint32_t *dst = (uint32_t*)((char*)dst_pixels + dst_info.stride * center_y) + center_x;
155   *dst = *((uint32_t*)((char*)src_pixels + src_info.stride * center_y) + center_x);
156 
157   UnlockBitmaps(env, src_bitmap, dst_bitmap);
158 }
159 
160 }  // namespace
161