OSDN Git Service

Add stop check in ImageFilterFX
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilterFx.java
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     @Override
41     public FilterRepresentation getDefaultRepresentation() {
42         return null;
43     }
44
45     public void useRepresentation(FilterRepresentation representation) {
46         FilterFxRepresentation parameters = (FilterFxRepresentation) representation;
47         mParameters = parameters;
48     }
49
50     public FilterFxRepresentation getParameters() {
51         return mParameters;
52     }
53
54     native protected void nativeApplyFilter(Bitmap bitmap, int w, int h,
55                                             Bitmap fxBitmap, int fxw, int fxh,
56                                             int start, int end);
57
58     @Override
59     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
60         if (getParameters() == null || mResources == null) {
61             return bitmap;
62         }
63
64         int w = bitmap.getWidth();
65         int h = bitmap.getHeight();
66
67         int bitmapResourceId = getParameters().getBitmapResource();
68         if (bitmapResourceId == 0) { // null filter fx
69             return bitmap;
70         }
71
72         if (mFxBitmap == null || mFxBitmapId != bitmapResourceId) {
73             BitmapFactory.Options o = new BitmapFactory.Options();
74             o.inScaled = false;
75             mFxBitmapId = bitmapResourceId;
76             if (mFxBitmapId != 0) {
77                 mFxBitmap = BitmapFactory.decodeResource(mResources, mFxBitmapId, o);
78             } else {
79                 Log.w(LOGTAG, "bad resource for filter: " + mName);
80             }
81         }
82
83         if (mFxBitmap == null) {
84             return bitmap;
85         }
86
87         int fxw = mFxBitmap.getWidth();
88         int fxh = mFxBitmap.getHeight();
89
90         int stride = w * 4;
91         int max = stride * h;
92         int increment = stride * 256; // 256 lines
93         for (int i = 0; i < max; i += increment) {
94             int start = i;
95             int end = i + increment;
96             if (end > max) {
97                 end = max;
98             }
99             if (!getEnvironment().needsStop()) {
100                 nativeApplyFilter(bitmap, w, h, mFxBitmap, fxw, fxh, start, end);
101             }
102         }
103
104         return bitmap;
105     }
106
107     public void setResources(Resources resources) {
108         mResources = resources;
109     }
110
111 }