OSDN Git Service

am a1db2653: Dismiss settings when showing popup in Video mode.
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilter.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.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.Matrix;
22 import android.support.v8.renderscript.Allocation;
23 import android.widget.Toast;
24
25 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
26 import com.android.gallery3d.filtershow.presets.FilterEnvironment;
27 import com.android.gallery3d.filtershow.presets.ImagePreset;
28
29 public abstract class ImageFilter implements Cloneable {
30     private FilterEnvironment mEnvironment = null;
31
32     protected String mName = "Original";
33     private final String LOGTAG = "ImageFilter";
34     protected static final boolean SIMPLE_ICONS = true;
35     // TODO: Temporary, for dogfood note memory issues with toasts for better
36     // feedback. Remove this when filters actually work in low memory
37     // situations.
38     private static Activity sActivity = null;
39
40     public static void setActivityForMemoryToasts(Activity activity) {
41         sActivity = activity;
42     }
43
44     public static void resetStatics() {
45         sActivity = null;
46     }
47
48     public void freeResources() {}
49
50     public void displayLowMemoryToast() {
51         if (sActivity != null) {
52             sActivity.runOnUiThread(new Runnable() {
53                 public void run() {
54                     Toast.makeText(sActivity, "Memory too low for filter " + getName() +
55                             ", please file a bug report", Toast.LENGTH_SHORT).show();
56                 }
57             });
58         }
59     }
60
61     public void setName(String name) {
62         mName = name;
63     }
64
65     public String getName() {
66         return mName;
67     }
68
69     public boolean supportsAllocationInput() { return false; }
70
71     public void apply(Allocation in, Allocation out) {
72         setGeneralParameters();
73     }
74
75     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
76         // do nothing here, subclasses will implement filtering here
77         setGeneralParameters();
78         return bitmap;
79     }
80
81     public abstract void useRepresentation(FilterRepresentation representation);
82
83     native protected void nativeApplyGradientFilter(Bitmap bitmap, int w, int h,
84             int[] redGradient, int[] greenGradient, int[] blueGradient);
85
86     public FilterRepresentation getDefaultRepresentation() {
87         return null;
88     }
89
90     protected Matrix getOriginalToScreenMatrix(int w, int h) {
91         ImagePreset preset = getEnvironment().getImagePreset();
92         GeometryMetadata geo = getEnvironment().getImagePreset().mGeoData;
93         Matrix originalToScreen = geo.getOriginalToScreen(true,
94                 preset.getImageLoader().getOriginalBounds().width(),
95                 preset.getImageLoader().getOriginalBounds().height(),
96                 w, h);
97         return originalToScreen;
98     }
99
100     public void setEnvironment(FilterEnvironment environment) {
101         mEnvironment = environment;
102     }
103
104     public FilterEnvironment getEnvironment() {
105         return mEnvironment;
106     }
107
108     public void setGeneralParameters() {
109         // should implement in subclass which like to transport
110         // some information to other filters. (like the style setting from RetroLux
111         // and Film to FixedFrame)
112         mEnvironment.clearGeneralParameters();
113     }
114 }