blur.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 "convolution.h"
21#include "utils.h"
22#include "_jni.h"
23
24using android::apps::photoeditor::convolution::SpecialConvolution;
25using android::apps::photoeditor::utils::LockBitmaps;
26using android::apps::photoeditor::utils::UnlockBitmaps;
27
28namespace {
29
30extern "C" JNIEXPORT void JNICALL Java_com_android_photoeditor_filters_ImageUtils_nativeBlur(
31    JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat scale) {
32   pBlurType f = (pBlurType)JNIFunc[JNI_Blur].func_ptr;
33   return f(env, obj, src_bitmap, dst_bitmap, scale);
34}
35
36extern "C" void Blur(
37    JNIEnv *env, jobject obj, jobject src_bitmap, jobject dst_bitmap, jfloat scale) {
38  AndroidBitmapInfo src_info;
39  AndroidBitmapInfo dst_info;
40  void* src_pixels;
41  void* dst_pixels;
42
43  int ret = LockBitmaps(
44      env, src_bitmap, dst_bitmap, &src_info, &dst_info, &src_pixels, &dst_pixels);
45  if (ret < 0) {
46    LOGE("LockBitmaps in Sharpen failed, error=%d", ret);
47    return;
48  }
49
50  /* Divide blur scale by a factor of 5 which comes from trials. Due to a small (3x3)
51   * kernel is used for convolution, small blur factor causes double edge. */
52  SpecialConvolution(&src_info, &dst_info, src_pixels, dst_pixels, scale / 5);
53
54  UnlockBitmaps(env, src_bitmap, dst_bitmap);
55}
56
57}  // namespace
58