1d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford/*
2d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Copyright (C) 2012 The Android Open Source Project
3d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
4d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * you may not use this file except in compliance with the License.
6d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * You may obtain a copy of the License at
7d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
8d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
10d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Unless required by applicable law or agreed to in writing, software
11d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * See the License for the specific language governing permissions and
14d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * limitations under the License.
15d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford */
16d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
17bf97d3aaeddfba06f6a00ee7abb23fcd28eb2e7dJohn Hoford#include <math.h>
18d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford#include "filters.h"
19d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
20d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hofordunsigned char clamp(int c)
21d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford{
22d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    int N = 255;
23d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    c &= ~(c >> 31);
24d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    c -= N;
25d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    c &= (c >> 31);
26d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    c += N;
27d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    return  (unsigned char) c;
28d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford}
29d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
30d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hofordvoid JNIFUNCF(ImageFilterContrast, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat bright)
31d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford{
32d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    char* destination = 0;
33d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
34d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    unsigned char * rgb = (unsigned char * )destination;
35d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    int i;
36d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    int len = width * height * 4;
37d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    float m =  (float)pow(2, bright/100.);
38d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    float c =  127-m*127;
39d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
40d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    for (i = 0; i < len; i+=4) {
41d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford        rgb[RED]   = clamp((int)(m*rgb[RED]+c));
42d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford        rgb[GREEN] = clamp((int)(m*rgb[GREEN]+c));
43d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford        rgb[BLUE]  = clamp((int)(m*rgb[BLUE]+c));
44d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    }
45d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    AndroidBitmap_unlockPixels(env, bitmap);
46d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford}
47d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
48