ImageFilter.java revision 1cfbaca7bef29b7d510c5e1de424e68cd8804022
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
17package com.android.gallery3d.filtershow.filters;
18
19import android.app.Activity;
20import android.graphics.Bitmap;
21import android.graphics.Matrix;
22import android.support.v8.renderscript.Allocation;
23import android.widget.Toast;
24
25import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
26import com.android.gallery3d.filtershow.presets.FilterEnvironment;
27import com.android.gallery3d.filtershow.presets.ImagePreset;
28
29public 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    private boolean mReInitNeeded = false;
36
37    // TODO: Temporary, for dogfood note memory issues with toasts for better
38    // feedback. Remove this when filters actually work in low memory
39    // situations.
40    private static Activity sActivity = null;
41
42    public static void setActivityForMemoryToasts(Activity activity) {
43        sActivity = activity;
44    }
45
46    public static void resetStatics() {
47        sActivity = null;
48    }
49
50    public void freeResources() {}
51
52    public void displayLowMemoryToast() {
53        if (sActivity != null) {
54            sActivity.runOnUiThread(new Runnable() {
55                public void run() {
56                    Toast.makeText(sActivity, "Memory too low for filter " + getName() +
57                            ", please file a bug report", Toast.LENGTH_SHORT).show();
58                }
59            });
60        }
61    }
62
63    public void setName(String name) {
64        mName = name;
65    }
66
67    public String getName() {
68        return mName;
69    }
70
71    public boolean supportsAllocationInput() { return false; }
72
73    public void apply(Allocation in, Allocation out) {
74    }
75
76    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
77        // do nothing here, subclasses will implement filtering here
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 boolean getReInitNeeded() {
109        return mReInitNeeded;
110    }
111
112    public void setReInitNeeded(boolean reInitNeeded) {
113        mReInitNeeded = reInitNeeded;
114    }
115}
116