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__inline__ int interp(unsigned char *src, int p , int *off ,float dr,float dg, float db){ 20 21 float fr00 = (src[p+off[0]])*(1-dr)+(src[p+off[1]])*dr; 22 float fr01 = (src[p+off[2]])*(1-dr)+(src[p+off[3]])*dr; 23 float fr10 = (src[p+off[4]])*(1-dr)+(src[p+off[5]])*dr; 24 float fr11 = (src[p+off[6]])*(1-dr)+(src[p+off[7]])*dr; 25 float frb0 = fr00 * (1-db)+fr01*db; 26 float frb1 = fr10 * (1-db)+fr11*db; 27 float frbg = frb0 * (1-dg)+frb1*dg; 28 29 return (int)frbg ; 30} 31 32void JNIFUNCF(ImageFilterFx, nativeApplyFilter, jobject bitmap, jint width, jint height, jobject lutbitmap,jint lutwidth, jint lutheight ) 33{ 34 char* destination = 0; 35 char* lut = 0; 36 AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); 37 AndroidBitmap_lockPixels(env, lutbitmap, (void**) &lut); 38 unsigned char * rgb = (unsigned char * )destination; 39 unsigned char * lutrgb = (unsigned char * )lut; 40 int lutdim_r = lutheight; 41 int lutdim_g = lutheight;; 42 int lutdim_b = lutwidth/lutheight;; 43 int STEP = 4; 44 45 int off[8] = { 46 0, 47 STEP*1, 48 STEP*lutdim_r, 49 STEP*(lutdim_r + 1), 50 STEP*(lutdim_r*lutdim_b), 51 STEP*(lutdim_r*lutdim_b+1), 52 STEP*(lutdim_r*lutdim_b+lutdim_r), 53 STEP*(lutdim_r*lutdim_b+lutdim_r + 1) 54 }; 55 56 float scale_R = (lutdim_r-1.f)/256.f; 57 float scale_G = (lutdim_g-1.f)/256.f; 58 float scale_B = (lutdim_b-1.f)/256.f; 59 60 int i; 61 int len = width * height * STEP; 62 63 for (i = 0; i < len; i+=STEP) 64 { 65 int r = rgb[RED]; 66 int g = rgb[GREEN]; 67 int b = rgb[BLUE]; 68 69 float fb = b*scale_B; 70 float fg = g*scale_G; 71 float fr = r*scale_R; 72 int lut_b = (int)fb; 73 int lut_g = (int)fg; 74 int lut_r = (int)fr; 75 int p = lut_r+lut_b*lutdim_r+lut_g*lutdim_r*lutdim_b; 76 p*=STEP; 77 float dr = fr-lut_r; 78 float dg = fg-lut_g; 79 float db = fb-lut_b; 80 rgb[RED] = clamp(interp(lutrgb,p ,off,dr,dg,db)); 81 rgb[GREEN] = clamp(interp(lutrgb,p+1,off,dr,dg,db)); 82 rgb[BLUE] = clamp(interp(lutrgb,p+2,off,dr,dg,db)); 83 84 } 85 86 AndroidBitmap_unlockPixels(env, bitmap); 87 AndroidBitmap_unlockPixels(env, lutbitmap); 88} 89