Home | History | Annotate | Download | only in jni
      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::pixel32_t;
     28 using android::apps::photoeditor::utils::UnlockBitmaps;
     29 
     30 namespace {
     31 
     32 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeRedEye(
     33     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap,
     34     jobjectArray redeye_positions, jfloat radius, jfloat intensity) {
     35    pRedEyeType f = (pRedEyeType)JNIFunc[JNI_RedEye].func_ptr;
     36    return f(env, obj, src_bitmap, dst_bitmap, redeye_positions, radius, intensity);
     37 }
     38 
     39 extern "C" void RedEye(
     40     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap,
     41     jobjectArray redeye_positions, jfloat radius, jfloat intensity) {
     42   AndroidBitmapInfo src_info;
     43   AndroidBitmapInfo dst_info;
     44   void* src_pixels;
     45   void* dst_pixels;
     46 
     47   int ret = LockBitmaps(
     48       env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
     49   if (ret < 0) {
     50     LOGE("LockBitmaps in RedEye failed, error=%d", ret);
     51     return;
     52   }
     53 
     54   memcpy(dst_pixels, src_pixels, src_info.stride * src_info.height);
     55 
     56   // get the class reference.
     57   jclass cls = env->FindClass("android/graphics/PointF");
     58   jfieldID xid = env->GetFieldID(cls, "x", "F");
     59   jfieldID yid = env->GetFieldID(cls, "y", "F");
     60   int eye_number = env->GetArrayLength(redeye_positions);
     61   for (int eye = 0; eye < eye_number; eye++) {
     62     jobject point_f = env->GetObjectArrayElement(redeye_positions, eye);
     63     float focus_x = env->GetFloatField(point_f, xid);
     64     float focus_y = env->GetFloatField(point_f, yid);
     65     if (focus_x < 0.0 || focus_x > 1.0 || focus_y < 0.0 || focus_y > 1.0) {
     66       // did not tap on the picture
     67       continue;
     68     }
     69 
     70     float x = focus_x * src_info.width;
     71     float y = focus_y * src_info.height;
     72     uint32_t bound_y = y + radius;
     73     if (bound_y > dst_info.height) {
     74       bound_y = dst_info.height;
     75     }
     76     uint32_t start_x = (x > radius) ? x - radius: 0;
     77     uint32_t bound_x = x + radius;
     78     if (bound_x > dst_info.width) {
     79       bound_x = dst_info.width;
     80     }
     81     uint32_t dst_y = (y > radius) ? y - radius : 0;
     82     for (; dst_y < bound_y; dst_y++) {
     83       uint32_t *dst = reinterpret_cast<uint32_t*>(
     84           reinterpret_cast<char*>(dst_pixels) + dst_info.stride * dst_y) + start_x;
     85       pixel32_t *src = reinterpret_cast<pixel32_t*>(
     86           reinterpret_cast<char*>(src_pixels) + src_info.stride * dst_y) + start_x;
     87       for (uint32_t dst_x = start_x; dst_x < bound_x; dst_x++) {
     88         if (hypotf(dst_x - x, dst_y - y) <= radius) {
     89           int32_t red = src->rgba8[0];
     90           int32_t green = src->rgba8[1];
     91           int32_t blue = src->rgba8[2];
     92           int32_t alpha = src->rgba8[3];
     93 
     94           float redIntensity = static_cast<float>(red) / (green + blue);
     95           if (redIntensity > intensity) {
     96             red = (green + blue) / 2.0f;
     97             *dst = alpha << 24 | (blue << 16) | (green << 8) | red;
     98           }
     99         }
    100         dst++;
    101         src++;
    102       }  // dst_x
    103     }  // dst_y
    104   }
    105 
    106   UnlockBitmaps(env, src_bitmap, dst_bitmap);
    107 }
    108 
    109 }  // namespace
    110