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