1 /* 2 * Copyright (C) 2012 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.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; 33 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; 44 45 public ImageFilterVignette() { 46 mName = "Vignette"; 47 } 48 49 @Override 50 public FilterRepresentation getDefaultRepresentation() { 51 FilterVignetteRepresentation representation = new FilterVignetteRepresentation(); 52 return representation; 53 } 54 55 @Override 56 public void useRepresentation(FilterRepresentation representation) { 57 mParameters = (FilterVignetteRepresentation) representation; 58 } 59 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); 63 64 private float calcRadius(float cx, float cy, int w, int h) { 65 float d = cx; 66 if (d < (w - cx)) { 67 d = w - cx; 68 } 69 if (d < cy) { 70 d = cy; 71 } 72 if (d < (h - cy)) { 73 d = h - cy; 74 } 75 return d * d * 2.0f; 76 } 77 78 @Override 79 protected void createFilter(Resources res, float scaleFactor, int quality) { 80 RenderScript rsCtx = getRenderScriptContext(); 81 82 mScript = new ScriptC_vignette(rsCtx); 83 } 84 85 @Override 86 protected void runFilter() { 87 88 int w = getInPixelsAllocation().getType().getX(); 89 int h = getInPixelsAllocation().getType().getY(); 90 91 float cx = w / 2; 92 float cy = h / 2; 93 float r = calcRadius(cx, cy, w, h); 94 float rx = r; 95 float ry = r; 96 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(); 103 m.mapPoints(c); 104 cx = c[0]; 105 cy = c[1]; 106 c[0] = bounds.right * mParameters.getRadiusX(); 107 c[1] = bounds.bottom * mParameters.getRadiusY(); 108 m.mapVectors(c); 109 rx = c[0]; 110 ry = c[1]; 111 } 112 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()); 127 } 128 129 @Override 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); 136 } 137 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); 142 return bitmap; 143 } 144 Bitmap ret = super.apply(bitmap, scaleFactor, quality); 145 return bitmap; 146 } 147 148 149 @Override 150 protected void resetAllocations() { 151 152 } 153 154 @Override 155 public void resetScripts() { 156 157 } 158 159 @Override 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); 165 } 166 } 167