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.renderscript.Allocation;
23 import android.widget.Toast;
24 
25 import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils;
26 import com.android.gallery3d.filtershow.imageshow.MasterImage;
27 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
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 
setActivityForMemoryToasts(Activity activity)40     public static void setActivityForMemoryToasts(Activity activity) {
41         sActivity = activity;
42     }
43 
resetStatics()44     public static void resetStatics() {
45         sActivity = null;
46     }
47 
freeResources()48     public void freeResources() {}
49 
displayLowMemoryToast()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 
setName(String name)61     public void setName(String name) {
62         mName = name;
63     }
64 
getName()65     public String getName() {
66         return mName;
67     }
68 
supportsAllocationInput()69     public boolean supportsAllocationInput() { return false; }
70 
apply(Allocation in, Allocation out)71     public void apply(Allocation in, Allocation out) {
72         setGeneralParameters();
73     }
74 
apply(Bitmap bitmap, float scaleFactor, int quality)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 
useRepresentation(FilterRepresentation representation)81     public abstract void useRepresentation(FilterRepresentation representation);
82 
nativeApplyGradientFilter(Bitmap bitmap, int w, int h, int[] redGradient, int[] greenGradient, int[] blueGradient)83     native protected void nativeApplyGradientFilter(Bitmap bitmap, int w, int h,
84             int[] redGradient, int[] greenGradient, int[] blueGradient);
85 
getDefaultRepresentation()86     public FilterRepresentation getDefaultRepresentation() {
87         return null;
88     }
89 
getOriginalToScreenMatrix(int w, int h)90     protected Matrix getOriginalToScreenMatrix(int w, int h) {
91         return GeometryMathUtils.getImageToScreenMatrix(getEnvironment().getImagePreset()
92                 .getGeometryFilters(), true, MasterImage.getImage().getOriginalBounds(), w, h);
93     }
94 
setEnvironment(FilterEnvironment environment)95     public void setEnvironment(FilterEnvironment environment) {
96         mEnvironment = environment;
97     }
98 
getEnvironment()99     public FilterEnvironment getEnvironment() {
100         return mEnvironment;
101     }
102 
setGeneralParameters()103     public void setGeneralParameters() {
104         // should implement in subclass which like to transport
105         // some information to other filters. (like the style setting from RetroLux
106         // and Film to FixedFrame)
107         mEnvironment.clearGeneralParameters();
108     }
109 }
110