1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.gallery3d.filtershow.filters; 18 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; 26 27 import com.android.gallery3d.R; 28 import com.android.gallery3d.filtershow.pipeline.FilterEnvironment; 29 30 public class ImageFilterChanSat extends ImageFilterRS { 31 private static final String LOGTAG = "ImageFilterChanSat"; 32 private ScriptC_saturation mScript; 33 private Bitmap mSourceBitmap; 34 35 private static final int STRIP_SIZE = 64; 36 37 FilterChanSatRepresentation mParameters = new FilterChanSatRepresentation(); 38 private Bitmap mOverlayBitmap; 39 40 public ImageFilterChanSat() { 41 mName = "ChannelSat"; 42 } 43 44 @Override 45 public FilterRepresentation getDefaultRepresentation() { 46 return new FilterChanSatRepresentation(); 47 } 48 49 @Override 50 public void useRepresentation(FilterRepresentation representation) { 51 mParameters = (FilterChanSatRepresentation) representation; 52 } 53 54 @Override 55 protected void resetAllocations() { 56 57 } 58 59 @Override 60 public void resetScripts() { 61 if (mScript != null) { 62 mScript.destroy(); 63 mScript = null; 64 } 65 } 66 @Override 67 protected void createFilter(android.content.res.Resources res, float scaleFactor, 68 int quality) { 69 createFilter(res, scaleFactor, quality, getInPixelsAllocation()); 70 } 71 72 @Override 73 protected void createFilter(android.content.res.Resources res, float scaleFactor, 74 int quality, Allocation in) { 75 RenderScript rsCtx = getRenderScriptContext(); 76 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, res, R.raw.saturation); 81 } 82 83 84 private Bitmap getSourceBitmap() { 85 assert (mSourceBitmap != null); 86 return mSourceBitmap; 87 } 88 89 @Override 90 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) { 91 if (SIMPLE_ICONS && FilterEnvironment.QUALITY_ICON == quality) { 92 return bitmap; 93 } 94 95 mSourceBitmap = bitmap; 96 Bitmap ret = super.apply(bitmap, scaleFactor, quality); 97 mSourceBitmap = null; 98 99 return ret; 100 } 101 102 @Override 103 protected void bindScriptValues() { 104 int width = getInPixelsAllocation().getType().getX(); 105 int height = getInPixelsAllocation().getType().getY(); 106 } 107 108 109 110 @Override 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); 115 } 116 117 118 int width = getInPixelsAllocation().getType().getX(); 119 int height = getInPixelsAllocation().getType().getY(); 120 Matrix m = getOriginalToScreenMatrix(width, height); 121 122 123 mScript.set_saturation(sat); 124 125 mScript.invoke_setupGradParams(); 126 runSelectiveAdjust( 127 getInPixelsAllocation(), getOutPixelsAllocation()); 128 129 } 130 131 private void runSelectiveAdjust(Allocation in, Allocation out) { 132 int width = in.getType().getX(); 133 int height = in.getType().getY(); 134 135 LaunchOptions options = new LaunchOptions(); 136 int ty; 137 options.setX(0, width); 138 139 for (ty = 0; ty < height; ty += STRIP_SIZE) { 140 int endy = ty + STRIP_SIZE; 141 if (endy > height) { 142 endy = height; 143 } 144 options.setY(ty, endy); 145 mScript.forEach_selectiveAdjust(in, out, options); 146 if (checkStop()) { 147 return; 148 } 149 } 150 } 151 152 private boolean checkStop() { 153 RenderScript rsCtx = getRenderScriptContext(); 154 rsCtx.finish(); 155 if (getEnvironment().needsStop()) { 156 return true; 157 } 158 return false; 159 } 160 } 161 162