Home | History | Annotate | Download | only in filters
      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 #include <math.h>
     19 
     20 static int* gVignetteMap = 0;
     21 static int gVignetteWidth = 0;
     22 static int gVignetteHeight = 0;
     23 
     24 void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jint centerx, jint centery, jfloat radiusx, jfloat radiusy, jfloat strength)
     25 {
     26     char* destination = 0;
     27     AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
     28     int i;
     29     int len = width * height * 4;
     30     int vignette = 0;
     31     float d = centerx;
     32     if (radiusx == 0) radiusx = 10;
     33     if (radiusy == 0) radiusy = 10;
     34     float scalex = 1/radiusx;
     35     float scaley = 1/radiusy;
     36 
     37     for (i = 0; i < len; i += 4)
     38     {
     39         int p = i/4;
     40         float x = ((p%width)-centerx)*scalex;
     41         float y = ((p/width)-centery)*scaley;
     42         float dist = sqrt(x*x+y*y)-1;
     43         vignette = (int) (strength*256*MAX(dist,0));
     44         destination[RED] = CLAMP(destination[RED] - vignette);
     45         destination[GREEN] = CLAMP(destination[GREEN] - vignette);
     46         destination[BLUE] = CLAMP(destination[BLUE] - vignette);
     47     }
     48     AndroidBitmap_unlockPixels(env, bitmap);
     49 }
     50