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