• 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 "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 
Java_com_android_photoeditor_filters_ImageUtils_nativeDuotone(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jint color1,jint color2)29 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeDuotone(
30     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jint color1, jint color2) {
31    pDuotoneType f = (pDuotoneType)JNIFunc[JNI_Duotone].func_ptr;
32    return f(env, obj, src_bitmap, dst_bitmap, color1, color2);
33 }
34 
Duotone(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jint color1,jint color2)35 extern "C" void Duotone(
36     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jint color1, jint color2) {
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 Duotone failed, error=%d", ret);
46     return;
47   }
48 
49   uint8_t red1 = static_cast<uint8_t>(color1 >> 16) & 0xff;
50   uint8_t green1 = static_cast<uint8_t>(color1 >> 8) & 0xff;
51   uint8_t blue1 = static_cast<uint8_t>(color1) & 0xff;
52 
53   uint8_t red2 = (uint8_t)(color2 >> 16) & 0xff;
54   uint8_t green2 = (uint8_t)(color2 >> 8) & 0xff;
55   uint8_t blue2 = (uint8_t)color2 & 0xff;
56 
57   int r_delta = red2 - red1;
58   int g_delta = green2 - green1;
59   int b_delta = blue2 - blue1;
60 
61   const uint32_t kEnergyLevels = 255 * 3 + 1;
62   uint8_t r_table[kEnergyLevels];
63   uint8_t g_table[kEnergyLevels];
64   uint8_t b_table[kEnergyLevels];
65   for (uint32_t energy = 0; energy < kEnergyLevels; energy++) {
66     r_table[energy] = (uint8_t)(red1 + energy * r_delta / kEnergyLevels);
67     g_table[energy] = (uint8_t)(green1 + energy * g_delta / kEnergyLevels);
68     b_table[energy] = (uint8_t)(blue1 + energy * b_delta / kEnergyLevels);
69   }
70 
71   for (uint32_t scan_line = 0; scan_line < dst_info.height; scan_line++) {
72     uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
73     pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels);
74     pixel32_t* src_line_end = src + src_info.width;
75 
76     while (src < src_line_end) {
77       uint32_t energy = src->rgba8[0] + src->rgba8[1] + src->rgba8[2];
78 
79       int dst_red = r_table[energy];
80       int dst_green = g_table[energy];
81       int dst_blue = b_table[energy];
82 
83       *dst = (src->rgba8[3] << 24) | (dst_blue << 16) | (dst_green << 8) | dst_red;
84       dst++;
85       src++;
86     }
87     dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride;
88     src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
89   }
90 
91   UnlockBitmaps(env, src_bitmap, dst_bitmap);
92 }
93 
94 }  // namespace
95