ImageFilter.java revision 4433a05abfc33e99223d89fcc28b924cdd754104
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    // 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    }
73
74    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
75        // do nothing here, subclasses will implement filtering here
76        return bitmap;
77    }
78
79    public abstract void useRepresentation(FilterRepresentation representation);
80
81    native protected void nativeApplyGradientFilter(Bitmap bitmap, int w, int h,
82            int[] redGradient, int[] greenGradient, int[] blueGradient);
83
84    public FilterRepresentation getDefaultRepresentation() {
85        return null;
86    }
87
88    protected Matrix getOriginalToScreenMatrix(int w, int h) {
89        ImagePreset preset = getEnvironment().getImagePreset();
90        GeometryMetadata geo = getEnvironment().getImagePreset().mGeoData;
91        Matrix originalToScreen = geo.getOriginalToScreen(true,
92                preset.getImageLoader().getOriginalBounds().width(),
93                preset.getImageLoader().getOriginalBounds().height(),
94                w, h);
95        return originalToScreen;
96    }
97
98    public void setEnvironment(FilterEnvironment environment) {
99        mEnvironment = environment;
100    }
101
102    public FilterEnvironment getEnvironment() {
103        return mEnvironment;
104    }
105}
106