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,
33        jobject lutbitmap, jint lutwidth, jint lutheight,
34        jint start, jint end)
35{
36    char* destination = 0;
37    char* lut = 0;
38    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
39    AndroidBitmap_lockPixels(env, lutbitmap, (void**) &lut);
40    unsigned char * rgb = (unsigned char * )destination;
41    unsigned char * lutrgb = (unsigned char * )lut;
42    int lutdim_r   = lutheight;
43    int lutdim_g   = lutheight;;
44    int lutdim_b   = lutwidth/lutheight;;
45    int STEP = 4;
46
47    int off[8] =  {
48            0,
49            STEP*1,
50            STEP*lutdim_r,
51            STEP*(lutdim_r + 1),
52            STEP*(lutdim_r*lutdim_b),
53            STEP*(lutdim_r*lutdim_b+1),
54            STEP*(lutdim_r*lutdim_b+lutdim_r),
55            STEP*(lutdim_r*lutdim_b+lutdim_r + 1)
56    };
57
58    float scale_R = (lutdim_r-1.f)/256.f;
59    float scale_G = (lutdim_g-1.f)/256.f;
60    float scale_B = (lutdim_b-1.f)/256.f;
61
62    int i;
63    for (i = start; i < end; 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