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 "utils.h"
     21 #include "_jni.h"
     22 
     23 using android::apps::photoeditor::utils::LockBitmaps;
     24 using android::apps::photoeditor::utils::pixel32_t;
     25 using android::apps::photoeditor::utils::UnlockBitmaps;
     26 
     27 namespace {
     28 
     29 const int k256Multiply255 = 65280;
     30 
     31 /*
     32  * @param scale ranges from -0.5 to 0.5.
     33  */
     34 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeColorTemp(
     35     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat scale) {
     36    pColorTempType f = (pColorTempType)JNIFunc[JNI_ColorTemp].func_ptr;
     37    return f(env, obj, src_bitmap, dst_bitmap, scale);
     38 }
     39 
     40 extern "C" void ColorTemp(
     41     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat scale) {
     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 ColorTemp failed, error=%d", ret);
     51     return;
     52   }
     53 
     54   int curve[256];
     55   for (int i = 0; i < 256; i++) {
     56     curve[i] = i * (256 - i);
     57   }
     58 
     59   int ics = static_cast<int>(scale * 256);
     60   int icsr = ics;
     61   int icsg = ics > 0 ? ics : 0;
     62   int icsb = ics;
     63 
     64   for (uint32_t scan_line = 0; scan_line < dst_info.height; scan_line++) {
     65     uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
     66     pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels);
     67     pixel32_t* src_line_end = src + src_info.width;
     68 
     69     while (src < src_line_end) {
     70       int32_t src_red = src->rgba8[0];
     71       int32_t src_green = src->rgba8[1];
     72       int32_t src_blue = src->rgba8[2];
     73       int32_t src_alpha = src->rgba8[3];
     74 
     75       int32_t dst_red = src_red + ((curve[src_red] * icsr) >> 15);
     76       int32_t dst_green = src_green + ((curve[src_green] * icsg) >> 17);
     77       int32_t dst_blue = src_blue - ((curve[src_blue] * icsb) >> 15);
     78 
     79       int rgb_max = MAX3(dst_red, dst_green, dst_blue);
     80       if (rgb_max > 255) {
     81         int invmax = k256Multiply255 / rgb_max;
     82         dst_red = dst_red * invmax >> 8;
     83         dst_green = dst_green * invmax >> 8;
     84         dst_blue = dst_blue * invmax >> 8;
     85       }
     86 
     87       *dst = (src_alpha << 24) | (dst_blue << 16) | (dst_green << 8) | dst_red;
     88       dst++;
     89       src++;
     90     }
     91     dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride;
     92     src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
     93   }
     94 
     95   UnlockBitmaps(env, src_bitmap, dst_bitmap);
     96 }
     97 
     98 }  // namespace
     99