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.content.res.Resources;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import com.android.gallery3d.app.Log;
24 public class ImageFilterFx extends ImageFilter {
25 private static final String LOGTAG = "ImageFilterFx";
26 private FilterFxRepresentation mParameters = null;
27 private Bitmap mFxBitmap = null;
28 private Resources mResources = null;
29 private int mFxBitmapId = 0;
31 public ImageFilterFx() {
35 public void freeResources() {
36 if (mFxBitmap != null) mFxBitmap.recycle();
41 public FilterRepresentation getDefaultRepresentation() {
45 public void useRepresentation(FilterRepresentation representation) {
46 FilterFxRepresentation parameters = (FilterFxRepresentation) representation;
47 mParameters = parameters;
50 public FilterFxRepresentation getParameters() {
54 native protected void nativeApplyFilter(Bitmap bitmap, int w, int h,
55 Bitmap fxBitmap, int fxw, int fxh,
59 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
60 if (getParameters() == null || mResources == null) {
64 int w = bitmap.getWidth();
65 int h = bitmap.getHeight();
67 int bitmapResourceId = getParameters().getBitmapResource();
68 if (bitmapResourceId == 0) { // null filter fx
72 if (mFxBitmap == null || mFxBitmapId != bitmapResourceId) {
73 BitmapFactory.Options o = new BitmapFactory.Options();
75 mFxBitmapId = bitmapResourceId;
76 if (mFxBitmapId != 0) {
77 mFxBitmap = BitmapFactory.decodeResource(mResources, mFxBitmapId, o);
79 Log.w(LOGTAG, "bad resource for filter: " + mName);
83 if (mFxBitmap == null) {
87 int fxw = mFxBitmap.getWidth();
88 int fxh = mFxBitmap.getHeight();
92 int increment = stride * 256; // 256 lines
93 for (int i = 0; i < max; i += increment) {
95 int end = i + increment;
99 if (!getEnvironment().needsStop()) {
100 nativeApplyFilter(bitmap, w, h, mFxBitmap, fxw, fxh, start, end);
107 public void setResources(Resources resources) {
108 mResources = resources;