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 308921c28c7333ad2b4d34f013904ad4737044f366John Hofordint clampMax(int c,int max) 318921c28c7333ad2b4d34f013904ad4737044f366John Hoford{ 328921c28c7333ad2b4d34f013904ad4737044f366John Hoford c &= ~(c >> 31); 338921c28c7333ad2b4d34f013904ad4737044f366John Hoford c -= max; 348921c28c7333ad2b4d34f013904ad4737044f366John Hoford c &= (c >> 31); 358921c28c7333ad2b4d34f013904ad4737044f366John Hoford c += max; 368921c28c7333ad2b4d34f013904ad4737044f366John Hoford return c; 378921c28c7333ad2b4d34f013904ad4737044f366John Hoford} 388921c28c7333ad2b4d34f013904ad4737044f366John Hoford 39d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hofordvoid JNIFUNCF(ImageFilterContrast, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat bright) 40d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford{ 41d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford char* destination = 0; 42d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); 43d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford unsigned char * rgb = (unsigned char * )destination; 44d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford int i; 45d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford int len = width * height * 4; 46d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford float m = (float)pow(2, bright/100.); 47d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford float c = 127-m*127; 48d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford 49d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford for (i = 0; i < len; i+=4) { 50d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford rgb[RED] = clamp((int)(m*rgb[RED]+c)); 51d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford rgb[GREEN] = clamp((int)(m*rgb[GREEN]+c)); 52d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford rgb[BLUE] = clamp((int)(m*rgb[BLUE]+c)); 53d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford } 54d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford AndroidBitmap_unlockPixels(env, bitmap); 55d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford} 56d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford 57