2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.filters;
19 import android.app.Activity;
20 import android.graphics.Bitmap;
21 import android.graphics.Matrix;
22 import android.support.v8.renderscript.Allocation;
23 import android.widget.Toast;
25 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
26 import com.android.gallery3d.filtershow.imageshow.MasterImage;
27 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
28 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
30 public abstract class ImageFilter implements Cloneable {
31 private FilterEnvironment mEnvironment = null;
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
39 private static Activity sActivity = null;
41 public static void setActivityForMemoryToasts(Activity activity) {
45 public static void resetStatics() {
49 public void freeResources() {}
51 public void displayLowMemoryToast() {
52 if (sActivity != null) {
53 sActivity.runOnUiThread(new Runnable() {
55 Toast.makeText(sActivity, "Memory too low for filter " + getName() +
56 ", please file a bug report", Toast.LENGTH_SHORT).show();
62 public void setName(String name) {
66 public String getName() {
70 public boolean supportsAllocationInput() { return false; }
72 public void apply(Allocation in, Allocation out) {
73 setGeneralParameters();
76 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
77 // do nothing here, subclasses will implement filtering here
78 setGeneralParameters();
82 public abstract void useRepresentation(FilterRepresentation representation);
84 native protected void nativeApplyGradientFilter(Bitmap bitmap, int w, int h,
85 int[] redGradient, int[] greenGradient, int[] blueGradient);
87 public FilterRepresentation getDefaultRepresentation() {
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(),
98 return originalToScreen;
101 public void setEnvironment(FilterEnvironment environment) {
102 mEnvironment = environment;
105 public FilterEnvironment getEnvironment() {
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();