1c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford/*
2c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * Copyright (C) 2012 The Android Open Source Project
3c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford *
4c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * you may not use this file except in compliance with the License.
6c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * You may obtain a copy of the License at
7c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford *
8c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford *
10c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * Unless required by applicable law or agreed to in writing, software
11c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * See the License for the specific language governing permissions and
14c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford * limitations under the License.
15c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford */
16c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford
17c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford#include <math.h>
18c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford#include "filters.h"
19c7b2c287bfbe3f53a4d65800c3884b0082ad683cJohn Hoford
2090b1d251973bfa748d435896fc277cb4024451adJohn Hofordvoid JNIFUNCF(ImageFilterShadows, nativeApplyFilter, jobject bitmap, jint width, jint height, float scale){
2190b1d251973bfa748d435896fc277cb4024451adJohn Hoford    double shadowFilterMap[] = {
2290b1d251973bfa748d435896fc277cb4024451adJohn Hoford            -0.00591,  0.0001,
2390b1d251973bfa748d435896fc277cb4024451adJohn Hoford             1.16488,  0.01668,
2490b1d251973bfa748d435896fc277cb4024451adJohn Hoford            -0.18027, -0.06791,
2590b1d251973bfa748d435896fc277cb4024451adJohn Hoford            -0.12625,  0.09001,
2690b1d251973bfa748d435896fc277cb4024451adJohn Hoford             0.15065, -0.03897
2790b1d251973bfa748d435896fc277cb4024451adJohn Hoford    };
2890b1d251973bfa748d435896fc277cb4024451adJohn Hoford
2990b1d251973bfa748d435896fc277cb4024451adJohn Hoford    char* destination = 0;
3090b1d251973bfa748d435896fc277cb4024451adJohn Hoford    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
3190b1d251973bfa748d435896fc277cb4024451adJohn Hoford    unsigned char * rgb = (unsigned char * )destination;
3290b1d251973bfa748d435896fc277cb4024451adJohn Hoford    int i;
3390b1d251973bfa748d435896fc277cb4024451adJohn Hoford    double s = (scale>=0)?scale:scale/5;
3490b1d251973bfa748d435896fc277cb4024451adJohn Hoford    int len = width * height * 4;
3590b1d251973bfa748d435896fc277cb4024451adJohn Hoford
3690b1d251973bfa748d435896fc277cb4024451adJohn Hoford    double *poly = (double *) malloc(5*sizeof(double));
3790b1d251973bfa748d435896fc277cb4024451adJohn Hoford    for (i = 0; i < 5; i++) {
3890b1d251973bfa748d435896fc277cb4024451adJohn Hoford        poly[i] = fastevalPoly(shadowFilterMap+i*2,2 , s);
3990b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
4090b1d251973bfa748d435896fc277cb4024451adJohn Hoford
4190b1d251973bfa748d435896fc277cb4024451adJohn Hoford    unsigned short * hsv = (unsigned short *)malloc(3*sizeof(short));
4290b1d251973bfa748d435896fc277cb4024451adJohn Hoford
4390b1d251973bfa748d435896fc277cb4024451adJohn Hoford    for (i = 0; i < len; i+=4)
4490b1d251973bfa748d435896fc277cb4024451adJohn Hoford    {
4590b1d251973bfa748d435896fc277cb4024451adJohn Hoford        rgb2hsv(rgb,i,hsv,0);
4690b1d251973bfa748d435896fc277cb4024451adJohn Hoford
4790b1d251973bfa748d435896fc277cb4024451adJohn Hoford        double v = (fastevalPoly(poly,5,hsv[0]/4080.)*4080);
4890b1d251973bfa748d435896fc277cb4024451adJohn Hoford        if (v>4080) v = 4080;
4990b1d251973bfa748d435896fc277cb4024451adJohn Hoford        hsv[0] = (unsigned short) ((v>0)?v:0);
5090b1d251973bfa748d435896fc277cb4024451adJohn Hoford
5190b1d251973bfa748d435896fc277cb4024451adJohn Hoford        hsv2rgb(hsv,0, rgb,i);
5290b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
5390b1d251973bfa748d435896fc277cb4024451adJohn Hoford
5490b1d251973bfa748d435896fc277cb4024451adJohn Hoford    free(poly);
5590b1d251973bfa748d435896fc277cb4024451adJohn Hoford    free(hsv);
5690b1d251973bfa748d435896fc277cb4024451adJohn Hoford    AndroidBitmap_unlockPixels(env, bitmap);
5790b1d251973bfa748d435896fc277cb4024451adJohn Hoford}
58