2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 static int* gVignetteMap = 0;
20 static int gVignetteWidth = 0;
21 static int gVignetteHeight = 0;
23 __inline__ void createVignetteMap(int w, int h)
25 if (gVignetteMap && (gVignetteWidth != w || gVignetteHeight != h))
30 if (gVignetteMap == 0)
39 gVignetteMap = malloc(w * h * sizeof(int));
40 float maxDistance = cx * cx * 2.0f;
41 for (i = 0; i < w; i++)
43 for (j = 0; j < h; j++)
45 float distance = (cx - i) * (cx - i) + (cy - j) * (cy - j);
46 gVignetteMap[j * w + i] = (int) (distance / maxDistance * 255);
52 void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat strength)
54 char* destination = 0;
55 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
56 createVignetteMap(width, height);
58 int len = width * height * 4;
61 for (i = 0; i < len; i += 4)
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);
68 AndroidBitmap_unlockPixels(env, bitmap);