• 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_nativeTint(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jint tint)29 extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeTint(
30     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jint tint) {
31    pTintType f = (pTintType)JNIFunc[JNI_Tint].func_ptr;
32    return f(env, obj, src_bitmap, dst_bitmap, tint);
33 }
34 
Tint(JNIEnv * env,jobject obj,jobject src_bitmap,jobject dst_bitmap,jint tint)35 extern "C" void Tint(
36     JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jint tint) {
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 Tint failed, error=%d", ret);
46     return;
47   }
48 
49   const int kShiftBits = 10;
50   const int kRedRatio = static_cast<int>(0.8f * (1 << kShiftBits) * 0.21f);
51   const int kGreenRatio = static_cast<int>(0.8f * (1 << kShiftBits) * 0.71f);
52   const int kBlueRatio = static_cast<int>(0.8f * (1 << kShiftBits) * 0.07f);
53   const int kTintRatio = static_cast<int>(0.2f * (1 << kShiftBits));
54 
55   uint32_t tint_red = kTintRatio * ((tint >> 16) & 0xff);
56   uint32_t tint_green = kTintRatio * ((tint >> 8) & 0xff);
57   uint32_t tint_blue = kTintRatio * (tint & 0xff);
58 
59   for (uint32_t scan_line = 0; scan_line < dst_info.height; scan_line++) {
60     uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
61     pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels);
62     pixel32_t* src_line_end = src + src_info.width;
63     while (src < src_line_end) {
64       int32_t avg = kRedRatio * src->rgba8[0] + kGreenRatio * src->rgba8[1] +
65           kBlueRatio * src->rgba8[2];
66 
67       int32_t dst_red = (avg + tint_red) >> kShiftBits;
68       int32_t dst_green = (avg + tint_green) >> kShiftBits;
69       int32_t dst_blue = (avg + tint_blue) >> kShiftBits;
70 
71       if (dst_red > 255) {
72         dst_red = 255;
73       }
74       if (dst_green > 255) {
75         dst_green = 255;
76       }
77       if (dst_blue > 255) {
78         dst_blue = 255;
79       }
80 
81       *dst = (src->rgba8[3] << 24) | (dst_blue << 16) | (dst_green << 8) | dst_red;
82       dst++;
83       src++;
84     }
85     dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride;
86     src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
87   }
88 
89   UnlockBitmaps(env, src_bitmap, dst_bitmap);
90 }
91 
92 }  // namespace
93