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
JNIFUNCF(ImageFilterBW,nativeApplyFilter,jobject bitmap,jint width,jint height)19 void JNIFUNCF(ImageFilterBW, nativeApplyFilter, jobject bitmap, jint width, jint height)
20 {
21 char* destination = 0;
22 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
23 int i;
24 int len = width * height * 4;
25 float Rf = 0.2999f;
26 float Gf = 0.587f;
27 float Bf = 0.114f;
28
29 for (i = 0; i < len; i+=4)
30 {
31 int r = destination[RED];
32 int g = destination[GREEN];
33 int b = destination[BLUE];
34 int t = CLAMP(Rf * r + Gf * g + Bf *b);
35
36 destination[RED] = t;
37 destination[GREEN] = t;
38 destination[BLUE] = t;
39 }
40 AndroidBitmap_unlockPixels(env, bitmap);
41 }
42
JNIFUNCF(ImageFilterBWRed,nativeApplyFilter,jobject bitmap,jint width,jint height)43 void JNIFUNCF(ImageFilterBWRed, nativeApplyFilter, jobject bitmap, jint width, jint height)
44 {
45 char* destination = 0;
46 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
47 int i;
48 int len = width * height * 4;
49 for (i = 0; i < len; i+=4)
50 {
51 int r = destination[RED];
52 int g = destination[GREEN];
53 int b = destination[BLUE];
54 int t = (g + b) / 2;
55 r = t;
56 g = t;
57 b = t;
58 destination[RED] = r;
59 destination[GREEN] = g;
60 destination[BLUE] = b;
61 }
62 AndroidBitmap_unlockPixels(env, bitmap);
63 }
64
JNIFUNCF(ImageFilterBWGreen,nativeApplyFilter,jobject bitmap,jint width,jint height)65 void JNIFUNCF(ImageFilterBWGreen, nativeApplyFilter, jobject bitmap, jint width, jint height)
66 {
67 char* destination = 0;
68 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
69 int i;
70 int len = width * height * 4;
71 for (i = 0; i < len; i+=4)
72 {
73 int r = destination[RED];
74 int g = destination[GREEN];
75 int b = destination[BLUE];
76 int t = (r + b) / 2;
77 r = t;
78 g = t;
79 b = t;
80 destination[RED] = r;
81 destination[GREEN] = g;
82 destination[BLUE] = b;
83 }
84 AndroidBitmap_unlockPixels(env, bitmap);
85 }
86
JNIFUNCF(ImageFilterBWBlue,nativeApplyFilter,jobject bitmap,jint width,jint height)87 void JNIFUNCF(ImageFilterBWBlue, nativeApplyFilter, jobject bitmap, jint width, jint height)
88 {
89 char* destination = 0;
90 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
91 int i;
92 int len = width * height * 4;
93 for (i = 0; i < len; i+=4)
94 {
95 int r = destination[RED];
96 int g = destination[GREEN];
97 int b = destination[BLUE];
98 int t = (r + g) / 2;
99 r = t;
100 g = t;
101 b = t;
102 destination[RED] = r;
103 destination[GREEN] = g;
104 destination[BLUE] = b;
105 }
106 AndroidBitmap_unlockPixels(env, bitmap);
107 }
108