sepia.cpp revision 0f8a40e4cfdc5f6cd47c22e81f69ed0446067c54
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
23using android::apps::photoeditor::utils::LockBitmaps;
24using android::apps::photoeditor::utils::pixel32_t;
25using android::apps::photoeditor::utils::UnlockBitmaps;
26
27namespace {
28
29extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeSepia(
30    JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
31   pSepiaType f = (pSepiaType)JNIFunc[JNI_Sepia].func_ptr;
32   return f(env, obj, src_bitmap, dst_bitmap);
33}
34
35extern "C" void Sepia(
36    JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap) {
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 Sepia failed, error=%d", ret);
46    return;
47  }
48
49  for (uint32_t scan_line = 0; scan_line < dst_info.height; scan_line++) {
50    uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
51    pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels);
52    pixel32_t* src_line_end = src + src_info.width;
53    while (src < src_line_end) {
54      int dst_red = (src->rgba8[0] * 805 + src->rgba8[1] * 1575 + src->rgba8[2] * 387) >> 11;
55      int dst_green = (src->rgba8[0] * 715 + src->rgba8[1] * 1405 + src->rgba8[2] * 344) >> 11;
56      int dst_blue = (src->rgba8[0] * 557 + src->rgba8[1] * 1094 + src->rgba8[2] * 268) >> 11;
57
58      if (dst_red > 255) {
59        dst_red = 255;
60      }
61      if (dst_green > 255) {
62        dst_green = 255;
63      }
64      if (dst_blue > 255) {
65        dst_blue = 255;
66      }
67
68      *dst = (src->rgba8[3] << 24) | (dst_blue << 16) | (dst_green << 8) | dst_red;
69      dst++;
70      src++;
71    }
72    dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info.stride;
73    src_pixels = reinterpret_cast<char*>(src_pixels) + src_info.stride;
74  }
75
76  UnlockBitmaps(env, src_bitmap, dst_bitmap);
77}
78
79}  // namespace
80