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.BitmapFactory;
     22 import android.graphics.Canvas;
     23 import android.graphics.Rect;
     24 import android.graphics.drawable.BitmapDrawable;
     25 import android.graphics.drawable.Drawable;
     26 
     27 import java.util.HashMap;
     28 
     29 public class ImageFilterBorder extends ImageFilter {
     30     private static final float NINEPATCH_ICON_SCALING = 10;
     31     private static final float BITMAP_ICON_SCALING = 1 / 3.0f;
     32     private FilterImageBorderRepresentation mParameters = null;
     33     private Resources mResources = null;
     34 
     35     private HashMap<Integer, Drawable> mDrawables = new HashMap<Integer, Drawable>();
     36 
     37     public ImageFilterBorder() {
     38         mName = "Border";
     39     }
     40 
     41     public void useRepresentation(FilterRepresentation representation) {
     42         FilterImageBorderRepresentation parameters = (FilterImageBorderRepresentation) representation;
     43         mParameters = parameters;
     44     }
     45 
     46     public FilterImageBorderRepresentation getParameters() {
     47         return mParameters;
     48     }
     49 
     50     public void freeResources() {
     51        mDrawables.clear();
     52     }
     53 
     54     public Bitmap applyHelper(Bitmap bitmap, float scale1, float scale2 ) {
     55         int w = bitmap.getWidth();
     56         int h = bitmap.getHeight();
     57         Rect bounds = new Rect(0, 0, (int) (w * scale1), (int) (h * scale1));
     58         Canvas canvas = new Canvas(bitmap);
     59         canvas.scale(scale2, scale2);
     60         Drawable drawable = getDrawable(getParameters().getDrawableResource());
     61         drawable.setBounds(bounds);
     62         drawable.draw(canvas);
     63         return bitmap;
     64     }
     65 
     66     @Override
     67     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     68         if (getParameters() == null || getParameters().getDrawableResource() == 0) {
     69             return bitmap;
     70         }
     71         float scale2 = scaleFactor * 2.0f;
     72         float scale1 = 1 / scale2;
     73         return applyHelper(bitmap, scale1, scale2);
     74     }
     75 
     76     public void setResources(Resources resources) {
     77         if (mResources != resources) {
     78             mResources = resources;
     79             mDrawables.clear();
     80         }
     81     }
     82 
     83     public Drawable getDrawable(int rsc) {
     84         Drawable drawable = mDrawables.get(rsc);
     85         if (drawable == null && mResources != null && rsc != 0) {
     86             drawable = new BitmapDrawable(mResources, BitmapFactory.decodeResource(mResources, rsc));
     87             mDrawables.put(rsc, drawable);
     88         }
     89         return drawable;
     90     }
     91 
     92 }
     93