2 * Copyright (C) 2013 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.graphics.Bitmap;
20 import android.graphics.Color;
21 import android.graphics.Matrix;
23 import com.android.gallery3d.R;
24 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
26 import android.graphics.Bitmap;
27 import android.graphics.Color;
28 import android.graphics.Matrix;
29 import android.support.v8.renderscript.Allocation;
30 import android.support.v8.renderscript.Element;
31 import android.support.v8.renderscript.RenderScript;
32 import android.support.v8.renderscript.Script.LaunchOptions;
33 import android.support.v8.renderscript.Type;
34 import android.util.Log;
36 import com.android.gallery3d.R;
37 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
39 public class ImageFilterGrad extends ImageFilterRS {
40 private static final String LOGTAG = "ImageFilterGrad";
41 private ScriptC_grad mScript;
42 private Bitmap mSourceBitmap;
43 private static final int RADIUS_SCALE_FACTOR = 160;
45 private static final int STRIP_SIZE = 64;
47 FilterGradRepresentation mParameters = new FilterGradRepresentation();
48 private Bitmap mOverlayBitmap;
50 public ImageFilterGrad() {
55 public FilterRepresentation getDefaultRepresentation() {
56 return new FilterGradRepresentation();
60 public void useRepresentation(FilterRepresentation representation) {
61 mParameters = (FilterGradRepresentation) representation;
65 protected void resetAllocations() {
70 public void resetScripts() {
71 if (mScript != null) {
77 protected void createFilter(android.content.res.Resources res, float scaleFactor,
79 createFilter(res, scaleFactor, quality, getInPixelsAllocation());
83 protected void createFilter(android.content.res.Resources res, float scaleFactor,
84 int quality, Allocation in) {
85 RenderScript rsCtx = getRenderScriptContext();
87 Type.Builder tb_float = new Type.Builder(rsCtx, Element.F32_4(rsCtx));
88 tb_float.setX(in.getType().getX());
89 tb_float.setY(in.getType().getY());
90 mScript = new ScriptC_grad(rsCtx);
94 private Bitmap getSourceBitmap() {
95 assert (mSourceBitmap != null);
100 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
101 if (SIMPLE_ICONS && FilterEnvironment.QUALITY_ICON == quality) {
105 mSourceBitmap = bitmap;
106 Bitmap ret = super.apply(bitmap, scaleFactor, quality);
107 mSourceBitmap = null;
113 protected void bindScriptValues() {
114 int width = getInPixelsAllocation().getType().getX();
115 int height = getInPixelsAllocation().getType().getY();
116 mScript.set_inputWidth(width);
117 mScript.set_inputHeight(height);
121 protected void runFilter() {
122 int[] x1 = mParameters.getXPos1();
123 int[] y1 = mParameters.getYPos1();
124 int[] x2 = mParameters.getXPos2();
125 int[] y2 = mParameters.getYPos2();
127 int width = getInPixelsAllocation().getType().getX();
128 int height = getInPixelsAllocation().getType().getY();
129 Matrix m = getOriginalToScreenMatrix(width, height);
130 float[] coord = new float[2];
131 for (int i = 0; i < x1.length; i++) {
135 x1[i] = (int) coord[0];
136 y1[i] = (int) coord[1];
140 x2[i] = (int) coord[0];
141 y2[i] = (int) coord[1];
144 mScript.set_mask(mParameters.getMask());
145 mScript.set_xPos1(x1);
146 mScript.set_yPos1(y1);
147 mScript.set_xPos2(x2);
148 mScript.set_yPos2(y2);
150 mScript.set_brightness(mParameters.getBrightness());
151 mScript.set_contrast(mParameters.getContrast());
152 mScript.set_saturation(mParameters.getSaturation());
154 mScript.invoke_setupGradParams();
156 getInPixelsAllocation(), getOutPixelsAllocation());
160 private void runSelectiveAdjust(Allocation in, Allocation out) {
161 int width = in.getType().getX();
162 int height = in.getType().getY();
164 LaunchOptions options = new LaunchOptions();
166 options.setX(0, width);
168 for (ty = 0; ty < height; ty += STRIP_SIZE) {
169 int endy = ty + STRIP_SIZE;
173 options.setY(ty, endy);
174 mScript.forEach_selectiveAdjust(in, out, options);
181 private boolean checkStop() {
182 RenderScript rsCtx = getRenderScriptContext();
184 if (getEnvironment().needsStop()) {