OSDN Git Service

Merge "Import translations. DO NOT MERGE" into jb-mr1-aah-dev
[android-x86/packages-apps-Gallery2.git] / jni / filters / vignette.c
1 /*
2  * Copyright (C) 2012 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 "filters.h"
18
19 static int* gVignetteMap = 0;
20 static int gVignetteWidth = 0;
21 static int gVignetteHeight = 0;
22
23 __inline__ void createVignetteMap(int w, int h)
24 {
25     if (gVignetteMap && (gVignetteWidth != w || gVignetteHeight != h))
26     {
27         free(gVignetteMap);
28         gVignetteMap = 0;
29     }
30     if (gVignetteMap == 0)
31     {
32         gVignetteWidth = w;
33         gVignetteHeight = h;
34
35         int cx = w / 2;
36         int cy = h / 2;
37         int i, j;
38
39         gVignetteMap = malloc(w * h * sizeof(int));
40         float maxDistance = cx * cx * 2.0f;
41         for (i = 0; i < w; i++)
42         {
43             for (j = 0; j < h; j++)
44             {
45                 float distance = (cx - i) * (cx - i) + (cy - j) * (cy - j);
46                 gVignetteMap[j * w + i] = (int) (distance / maxDistance * 255);
47             }
48         }
49     }
50 }
51
52 void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat strength)
53 {
54     char* destination = 0;
55     AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
56     createVignetteMap(width, height);
57     int i;
58     int len = width * height * 4;
59     int vignette = 0;
60
61     for (i = 0; i < len; i += 4)
62     {
63         vignette = (int) (strength * gVignetteMap[i / 4]);
64         destination[RED] = CLAMP(destination[RED] - vignette);
65         destination[GREEN] = CLAMP(destination[GREEN] - vignette);
66         destination[BLUE] = CLAMP(destination[BLUE] - vignette);
67     }
68     AndroidBitmap_unlockPixels(env, bitmap);
69 }