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.Matrix;
21 import android.support.v8.renderscript.Allocation;
22 import android.support.v8.renderscript.Element;
23 import android.support.v8.renderscript.RenderScript;
24 import android.support.v8.renderscript.Script.LaunchOptions;
25 import android.support.v8.renderscript.Type;
27 import com.android.gallery3d.R;
28 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment;
30 public class ImageFilterChanSat extends ImageFilterRS {
31 private static final String LOGTAG = "ImageFilterChanSat";
32 private ScriptC_saturation mScript;
33 private Bitmap mSourceBitmap;
35 private static final int STRIP_SIZE = 64;
37 FilterChanSatRepresentation mParameters = new FilterChanSatRepresentation();
38 private Bitmap mOverlayBitmap;
40 public ImageFilterChanSat() {
45 public FilterRepresentation getDefaultRepresentation() {
46 return new FilterChanSatRepresentation();
50 public void useRepresentation(FilterRepresentation representation) {
51 mParameters = (FilterChanSatRepresentation) representation;
55 protected void resetAllocations() {
60 public void resetScripts() {
61 if (mScript != null) {
67 protected void createFilter(android.content.res.Resources res, float scaleFactor,
69 createFilter(res, scaleFactor, quality, getInPixelsAllocation());
73 protected void createFilter(android.content.res.Resources res, float scaleFactor,
74 int quality, Allocation in) {
75 RenderScript rsCtx = getRenderScriptContext();
77 Type.Builder tb_float = new Type.Builder(rsCtx, Element.F32_4(rsCtx));
78 tb_float.setX(in.getType().getX());
79 tb_float.setY(in.getType().getY());
80 mScript = new ScriptC_saturation(rsCtx);
84 private Bitmap getSourceBitmap() {
85 assert (mSourceBitmap != null);
90 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
91 if (SIMPLE_ICONS && FilterEnvironment.QUALITY_ICON == quality) {
95 mSourceBitmap = bitmap;
96 Bitmap ret = super.apply(bitmap, scaleFactor, quality);
103 protected void bindScriptValues() {
104 int width = getInPixelsAllocation().getType().getX();
105 int height = getInPixelsAllocation().getType().getY();
111 protected void runFilter() {
112 int []sat = new int[7];
113 for(int i = 0;i<sat.length ;i ++){
114 sat[i] = mParameters.getValue(i);
118 int width = getInPixelsAllocation().getType().getX();
119 int height = getInPixelsAllocation().getType().getY();
120 Matrix m = getOriginalToScreenMatrix(width, height);
123 mScript.set_saturation(sat);
125 mScript.invoke_setupGradParams();
127 getInPixelsAllocation(), getOutPixelsAllocation());
131 private void runSelectiveAdjust(Allocation in, Allocation out) {
132 int width = in.getType().getX();
133 int height = in.getType().getY();
135 LaunchOptions options = new LaunchOptions();
137 options.setX(0, width);
139 for (ty = 0; ty < height; ty += STRIP_SIZE) {
140 int endy = ty + STRIP_SIZE;
144 options.setY(ty, endy);
145 mScript.forEach_selectiveAdjust(in, out, options);
152 private boolean checkStop() {
153 RenderScript rsCtx = getRenderScriptContext();
155 if (getEnvironment().needsStop()) {