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.Canvas;
22 import android.graphics.Matrix;
23 import android.graphics.Rect;
24 import com.android.gallery3d.R;
25 import com.android.gallery3d.filtershow.imageshow.MasterImage;
26 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
27 import android.renderscript.Allocation;
28 import android.renderscript.Element;
29 import android.renderscript.RenderScript;
30 import android.renderscript.Script.LaunchOptions;
31 import android.renderscript.Type;
32 import android.util.Log;
34 public class ImageFilterVignette extends ImageFilterRS {
35 private static final String LOGTAG = "ImageFilterVignette";
36 private Bitmap mOverlayBitmap;
37 private ScriptC_vignette mScript;
38 FilterVignetteRepresentation mParameters;
39 public static final int MODE_VIGNETTE = FilterVignetteRepresentation.MODE_VIGNETTE;
40 public static final int MODE_EXPOSURE = FilterVignetteRepresentation.MODE_EXPOSURE;
41 public static final int MODE_SATURATION = FilterVignetteRepresentation.MODE_SATURATION;
42 public static final int MODE_CONTRAST = FilterVignetteRepresentation.MODE_CONTRAST;
43 public static final int MODE_FALLOFF = FilterVignetteRepresentation.MODE_FALLOFF;
45 public ImageFilterVignette() {
50 public FilterRepresentation getDefaultRepresentation() {
51 FilterVignetteRepresentation representation = new FilterVignetteRepresentation();
52 return representation;
56 public void useRepresentation(FilterRepresentation representation) {
57 mParameters = (FilterVignetteRepresentation) representation;
60 native protected void nativeApplyFilter(
61 Bitmap bitmap, int w, int h, int cx, int cy, float radx, float rady,
62 float strength, float finalValue);
64 private float calcRadius(float cx, float cy, int w, int h) {
79 protected void createFilter(Resources res, float scaleFactor, int quality) {
80 RenderScript rsCtx = getRenderScriptContext();
82 mScript = new ScriptC_vignette(rsCtx);
86 protected void runFilter() {
88 int w = getInPixelsAllocation().getType().getX();
89 int h = getInPixelsAllocation().getType().getY();
93 float r = calcRadius(cx, cy, w, h);
97 float[]c = new float[2];
98 if (mParameters.isCenterSet()) {
99 Matrix m = getOriginalToScreenMatrix(w, h);
100 Rect bounds = MasterImage.getImage().getOriginalBounds();
101 c[0] = bounds.right * mParameters.getCenterX();
102 c[1] = bounds.bottom * mParameters.getCenterY();
106 c[0] = bounds.right * mParameters.getRadiusX();
107 c[1] = bounds.bottom * mParameters.getRadiusY();
113 mScript.set_inputWidth(w);
114 mScript.set_inputHeight(h);
115 int v = mParameters.getValue(MODE_VIGNETTE);
116 mScript.set_finalSubtract((v < 0) ? v : 0);
117 mScript.set_finalBright((v > 0) ? -v : 0);
118 mScript.set_finalSaturation(mParameters.getValue(MODE_SATURATION));
119 mScript.set_finalContrast(mParameters.getValue(MODE_CONTRAST));
120 mScript.set_centerx(cx);
121 mScript.set_centery(cy);
122 mScript.set_radiusx(rx);
123 mScript.set_radiusy(ry);
124 mScript.set_strength(mParameters.getValue(MODE_FALLOFF)/10.f);
125 mScript.invoke_setupVignetteParams();
126 mScript.forEach_vignette(getInPixelsAllocation(), getOutPixelsAllocation());
130 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
131 if (SIMPLE_ICONS && FilterEnvironment.QUALITY_ICON == quality) {
132 if (mOverlayBitmap == null) {
133 Resources res = getEnvironment().getPipeline().getResources();
134 mOverlayBitmap = IconUtilities.getFXBitmap(res,
135 R.drawable.filtershow_icon_vignette);
138 Canvas c = new Canvas(bitmap);
139 int dim = Math.max(bitmap.getWidth(), bitmap.getHeight());
140 Rect r = new Rect(0, 0, dim, dim);
141 c.drawBitmap(mOverlayBitmap, null, r, null);
144 Bitmap ret = super.apply(bitmap, scaleFactor, quality);
150 protected void resetAllocations() {
155 public void resetScripts() {
160 protected void bindScriptValues() {
161 int width = getInPixelsAllocation().getType().getX();
162 int height = getInPixelsAllocation().getType().getY();
163 mScript.set_inputWidth(width);
164 mScript.set_inputHeight(height);