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 com.android.gallery3d.app.Log;
     23 
     24 public class ImageFilterFx extends ImageFilter {
     25     private static final String LOGTAG = "ImageFilterFx";
     26     private FilterFxRepresentation mParameters = null;
     27     private Bitmap mFxBitmap = null;
     28     private Resources mResources = null;
     29     private int mFxBitmapId = 0;
     30 
     31     public ImageFilterFx() {
     32     }
     33 
     34     @Override
     35     public void freeResources() {
     36         if (mFxBitmap != null) mFxBitmap.recycle();
     37         mFxBitmap = null;
     38     }
     39 
     40     public void useRepresentation(FilterRepresentation representation) {
     41         FilterFxRepresentation parameters = (FilterFxRepresentation) representation;
     42         mParameters = parameters;
     43     }
     44 
     45     public FilterFxRepresentation getParameters() {
     46         return mParameters;
     47     }
     48 
     49     native protected void nativeApplyFilter(Bitmap bitmap, int w, int h,Bitmap  fxBitmap, int fxw, int fxh);
     50 
     51     @Override
     52     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     53         if (getParameters() == null || mResources == null) {
     54             return bitmap;
     55         }
     56 
     57         int w = bitmap.getWidth();
     58         int h = bitmap.getHeight();
     59 
     60         int bitmapResourceId = getParameters().getBitmapResource();
     61         if (bitmapResourceId == 0) { // null filter fx
     62             return bitmap;
     63         }
     64 
     65         if (mFxBitmap == null || mFxBitmapId != bitmapResourceId) {
     66             BitmapFactory.Options o = new BitmapFactory.Options();
     67             o.inScaled = false;
     68             mFxBitmapId = bitmapResourceId;
     69             if (mFxBitmapId != 0) {
     70                 mFxBitmap = BitmapFactory.decodeResource(mResources, mFxBitmapId, o);
     71             } else {
     72                 Log.w(LOGTAG, "bad resource for filter: " + mName);
     73             }
     74         }
     75 
     76         if (mFxBitmap == null) {
     77             return bitmap;
     78         }
     79 
     80         int fxw = mFxBitmap.getWidth();
     81         int fxh = mFxBitmap.getHeight();
     82 
     83         nativeApplyFilter(bitmap, w, h, mFxBitmap, fxw, fxh);
     84         return bitmap;
     85     }
     86 
     87     public void setResources(Resources resources) {
     88         mResources = resources;
     89     }
     90 }
     91