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.rs.levels; 18 19 import android.app.Activity; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.graphics.Canvas; 23 import android.os.Bundle; 24 import android.graphics.SurfaceTexture; 25 import android.renderscript.Allocation; 26 import android.renderscript.Matrix3f; 27 import android.renderscript.RenderScript; 28 import android.util.Log; 29 import android.view.TextureView; 30 import android.view.View; 31 import android.widget.ImageView; 32 import android.widget.SeekBar; 33 import android.widget.TextView; 34 35 public class LevelsRSActivity extends Activity 36 implements SeekBar.OnSeekBarChangeListener, 37 TextureView.SurfaceTextureListener 38 { 39 private final String TAG = "Img"; 40 private Bitmap mBitmapIn; 41 private float mInBlack = 0.0f; 42 private SeekBar mInBlackSeekBar; 43 private float mOutBlack = 0.0f; 44 private SeekBar mOutBlackSeekBar; 45 private float mInWhite = 255.0f; 46 private SeekBar mInWhiteSeekBar; 47 private float mOutWhite = 255.0f; 48 private SeekBar mOutWhiteSeekBar; 49 private float mGamma = 1.0f; 50 private SeekBar mGammaSeekBar; 51 private float mSaturation = 1.0f; 52 private SeekBar mSaturationSeekBar; 53 private TextView mBenchmarkResult; 54 private TextureView mDisplayView; 55 56 Matrix3f satMatrix = new Matrix3f(); 57 float mInWMinInB; 58 float mOutWMinOutB; 59 float mOverInWMinInB; 60 61 private RenderScript mRS; 62 private Allocation mInPixelsAllocation; 63 private Allocation mOutPixelsAllocation; 64 private ScriptC_levels mScript; 65 setLevels()66 private void setLevels() { 67 mInWMinInB = mInWhite - mInBlack; 68 mOutWMinOutB = mOutWhite - mOutBlack; 69 mOverInWMinInB = 1.f / mInWMinInB; 70 71 mScript.set_inBlack(mInBlack); 72 mScript.set_outBlack(mOutBlack); 73 mScript.set_inWMinInB(mInWMinInB); 74 mScript.set_outWMinOutB(mOutWMinOutB); 75 mScript.set_overInWMinInB(mOverInWMinInB); 76 } 77 setSaturation()78 private void setSaturation() { 79 float rWeight = 0.299f; 80 float gWeight = 0.587f; 81 float bWeight = 0.114f; 82 float oneMinusS = 1.0f - mSaturation; 83 84 satMatrix.set(0, 0, oneMinusS * rWeight + mSaturation); 85 satMatrix.set(0, 1, oneMinusS * rWeight); 86 satMatrix.set(0, 2, oneMinusS * rWeight); 87 satMatrix.set(1, 0, oneMinusS * gWeight); 88 satMatrix.set(1, 1, oneMinusS * gWeight + mSaturation); 89 satMatrix.set(1, 2, oneMinusS * gWeight); 90 satMatrix.set(2, 0, oneMinusS * bWeight); 91 satMatrix.set(2, 1, oneMinusS * bWeight); 92 satMatrix.set(2, 2, oneMinusS * bWeight + mSaturation); 93 mScript.set_colorMat(satMatrix); 94 } 95 onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)96 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 97 if (fromUser) { 98 if (seekBar == mInBlackSeekBar) { 99 mInBlack = (float)progress; 100 setLevels(); 101 } else if (seekBar == mOutBlackSeekBar) { 102 mOutBlack = (float)progress; 103 setLevels(); 104 } else if (seekBar == mInWhiteSeekBar) { 105 mInWhite = (float)progress + 127.0f; 106 setLevels(); 107 } else if (seekBar == mOutWhiteSeekBar) { 108 mOutWhite = (float)progress + 127.0f; 109 setLevels(); 110 } else if (seekBar == mGammaSeekBar) { 111 mGamma = (float)progress/100.0f; 112 mGamma = Math.max(mGamma, 0.1f); 113 mGamma = 1.0f / mGamma; 114 mScript.set_gamma(mGamma); 115 } else if (seekBar == mSaturationSeekBar) { 116 mSaturation = (float)progress / 50.0f; 117 setSaturation(); 118 } 119 120 filter(); 121 mDisplayView.invalidate(); 122 } 123 } 124 onStartTrackingTouch(SeekBar seekBar)125 public void onStartTrackingTouch(SeekBar seekBar) { 126 } 127 onStopTrackingTouch(SeekBar seekBar)128 public void onStopTrackingTouch(SeekBar seekBar) { 129 } 130 131 @Override onCreate(Bundle savedInstanceState)132 protected void onCreate(Bundle savedInstanceState) { 133 super.onCreate(savedInstanceState); 134 setContentView(R.layout.rs); 135 136 mBitmapIn = loadBitmap(R.drawable.city); 137 mDisplayView = (TextureView) findViewById(R.id.display); 138 139 mInBlackSeekBar = (SeekBar)findViewById(R.id.inBlack); 140 mInBlackSeekBar.setOnSeekBarChangeListener(this); 141 mInBlackSeekBar.setMax(128); 142 mInBlackSeekBar.setProgress(0); 143 mOutBlackSeekBar = (SeekBar)findViewById(R.id.outBlack); 144 mOutBlackSeekBar.setOnSeekBarChangeListener(this); 145 mOutBlackSeekBar.setMax(128); 146 mOutBlackSeekBar.setProgress(0); 147 148 mInWhiteSeekBar = (SeekBar)findViewById(R.id.inWhite); 149 mInWhiteSeekBar.setOnSeekBarChangeListener(this); 150 mInWhiteSeekBar.setMax(128); 151 mInWhiteSeekBar.setProgress(128); 152 mOutWhiteSeekBar = (SeekBar)findViewById(R.id.outWhite); 153 mOutWhiteSeekBar.setOnSeekBarChangeListener(this); 154 mOutWhiteSeekBar.setMax(128); 155 mOutWhiteSeekBar.setProgress(128); 156 157 mGammaSeekBar = (SeekBar)findViewById(R.id.inGamma); 158 mGammaSeekBar.setOnSeekBarChangeListener(this); 159 mGammaSeekBar.setMax(150); 160 mGammaSeekBar.setProgress(100); 161 162 mSaturationSeekBar = (SeekBar)findViewById(R.id.inSaturation); 163 mSaturationSeekBar.setOnSeekBarChangeListener(this); 164 mSaturationSeekBar.setProgress(50); 165 166 mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); 167 mBenchmarkResult.setText("Result: not run"); 168 169 mRS = RenderScript.create(this); 170 mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, 171 Allocation.MipmapControl.MIPMAP_NONE, 172 Allocation.USAGE_SCRIPT); 173 mOutPixelsAllocation = Allocation.createTyped(mRS, mInPixelsAllocation.getType(), 174 Allocation.USAGE_SCRIPT | 175 Allocation.USAGE_IO_OUTPUT); 176 mDisplayView.setSurfaceTextureListener(this); 177 178 mScript = new ScriptC_levels(mRS, getResources(), R.raw.levels); 179 mScript.set_gamma(mGamma); 180 181 setSaturation(); 182 setLevels(); 183 } 184 loadBitmap(int resource)185 private Bitmap loadBitmap(int resource) { 186 final BitmapFactory.Options options = new BitmapFactory.Options(); 187 options.inPreferredConfig = Bitmap.Config.ARGB_8888; 188 Bitmap b = BitmapFactory.decodeResource(getResources(), resource, options); 189 Bitmap b2 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig()); 190 Canvas c = new Canvas(b2); 191 c.drawBitmap(b, 0, 0, null); 192 b.recycle(); 193 return b2; 194 } 195 filter()196 private void filter() { 197 mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation); 198 mOutPixelsAllocation.ioSendOutput(); 199 mRS.finish(); 200 } 201 benchmark(View v)202 public void benchmark(View v) { 203 filter(); 204 long t = java.lang.System.currentTimeMillis(); 205 filter(); 206 t = java.lang.System.currentTimeMillis() - t; 207 mDisplayView.invalidate(); 208 mBenchmarkResult.setText("Result: " + t + " ms"); 209 } 210 211 212 @Override onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)213 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 214 mOutPixelsAllocation.setSurfaceTexture(surface); 215 filter(); 216 } 217 218 @Override onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)219 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 220 mOutPixelsAllocation.setSurfaceTexture(surface); 221 } 222 223 @Override onSurfaceTextureDestroyed(SurfaceTexture surface)224 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 225 mOutPixelsAllocation.setSurfaceTexture(null); 226 return true; 227 } 228 229 @Override onSurfaceTextureUpdated(SurfaceTexture surface)230 public void onSurfaceTextureUpdated(SurfaceTexture surface) { 231 } 232 } 233