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 package com.android.gallery3d.filtershow.filters;
     18 
     19 import android.content.res.Resources;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Matrix;
     23 import android.graphics.Rect;
     24 import com.android.gallery3d.R;
     25 import com.android.gallery3d.filtershow.presets.ImagePreset;
     26 
     27 public class ImageFilterVignette extends SimpleImageFilter {
     28     private static final String LOGTAG = "ImageFilterVignette";
     29     private Bitmap mOverlayBitmap;
     30 
     31     public ImageFilterVignette() {
     32         mName = "Vignette";
     33     }
     34 
     35     @Override
     36     public FilterRepresentation getDefaultRepresentation() {
     37         FilterVignetteRepresentation representation = new FilterVignetteRepresentation();
     38         return representation;
     39     }
     40 
     41     native protected void nativeApplyFilter(
     42             Bitmap bitmap, int w, int h, int cx, int cy, float radx, float rady, float strength);
     43 
     44     private float calcRadius(float cx, float cy, int w, int h) {
     45         float d = cx;
     46         if (d < (w - cx)) {
     47             d = w - cx;
     48         }
     49         if (d < cy) {
     50             d = cy;
     51         }
     52         if (d < (h - cy)) {
     53             d = h - cy;
     54         }
     55         return d * d * 2.0f;
     56     }
     57 
     58     @Override
     59     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     60         if (SIMPLE_ICONS && ImagePreset.QUALITY_ICON == quality) {
     61             if (mOverlayBitmap == null) {
     62                 Resources res = getEnvironment().getCachingPipeline().getResources();
     63                 mOverlayBitmap = IconUtilities.getFXBitmap(res,
     64                         R.drawable.filtershow_icon_vignette);
     65             }
     66             Canvas c = new Canvas(bitmap);
     67             int dim = Math.max(bitmap.getWidth(), bitmap.getHeight());
     68             Rect r = new Rect(0, 0, dim, dim);
     69             c.drawBitmap(mOverlayBitmap, null, r, null);
     70             return bitmap;
     71         }
     72         FilterVignetteRepresentation rep = (FilterVignetteRepresentation) getParameters();
     73         if (rep == null) {
     74             return bitmap;
     75         }
     76         int w = bitmap.getWidth();
     77         int h = bitmap.getHeight();
     78         float value = rep.getValue() / 100.0f;
     79         float cx = w / 2;
     80         float cy = h / 2;
     81         float r = calcRadius(cx, cy, w, h);
     82         float rx = r;
     83         float ry = r;
     84         if (rep.isCenterSet()) {
     85             Matrix m = getOriginalToScreenMatrix(w, h);
     86             cx = rep.getCenterX();
     87             cy = rep.getCenterY();
     88             float[] center = new float[] { cx, cy };
     89             m.mapPoints(center);
     90             cx = center[0];
     91             cy = center[1];
     92             rx = m.mapRadius(rep.getRadiusX());
     93             ry = m.mapRadius(rep.getRadiusY());
     94          }
     95         nativeApplyFilter(bitmap, w, h, (int) cx, (int) cy, rx, ry, value);
     96         return bitmap;
     97     }
     98 }
     99