Home | History | Annotate | Download | only in image
      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.image;
     18 
     19 import java.lang.Math;
     20 
     21 import android.renderscript.Allocation;
     22 import android.renderscript.Element;
     23 import android.renderscript.RenderScript;
     24 import android.renderscript.Script;
     25 import android.renderscript.ScriptC;
     26 import android.renderscript.Type;
     27 import android.util.Log;
     28 import android.widget.SeekBar;
     29 import android.widget.TextView;
     30 
     31 public class Grain extends TestBase {
     32     private ScriptC_grain mScript;
     33     private Allocation mNoise;
     34     private Allocation mNoise2;
     35 
     36 
     37     public boolean onBar1Setup(SeekBar b, TextView t) {
     38         t.setText("Strength");
     39         b.setProgress(50);
     40         return true;
     41     }
     42 
     43     public void onBar1Changed(int progress) {
     44         float s = progress / 100.0f;
     45         mScript.set_gNoiseStrength(s);
     46     }
     47 
     48     private int findHighBit(int v) {
     49         int bit = 0;
     50         while (v > 1) {
     51             bit++;
     52             v >>= 1;
     53         }
     54         return bit;
     55     }
     56 
     57 
     58     public void createTest(android.content.res.Resources res) {
     59         int width = mInPixelsAllocation.getType().getX();
     60         int height = mInPixelsAllocation.getType().getY();
     61 
     62         int noiseW = findHighBit(width);
     63         int noiseH = findHighBit(height);
     64         if (noiseW > 9) {
     65             noiseW = 9;
     66         }
     67         if (noiseH > 9) {
     68             noiseH = 9;
     69         }
     70         noiseW = 1 << noiseW;
     71         noiseH = 1 << noiseH;
     72 
     73         Type.Builder tb = new Type.Builder(mRS, Element.U8(mRS));
     74         tb.setX(noiseW);
     75         tb.setY(noiseH);
     76         mNoise = Allocation.createTyped(mRS, tb.create());
     77         mNoise2 = Allocation.createTyped(mRS, tb.create());
     78 
     79         mScript = new ScriptC_grain(mRS, res, R.raw.grain);
     80         mScript.set_gWMask(noiseW - 1);
     81         mScript.set_gHMask(noiseH - 1);
     82         mScript.set_gNoiseStrength(0.5f);
     83         mScript.set_gBlendSource(mNoise);
     84         mScript.set_gNoise(mNoise2);
     85     }
     86 
     87     public void runTest() {
     88         mScript.forEach_genRand(mNoise);
     89         mScript.forEach_blend9(mNoise2);
     90         mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
     91     }
     92 
     93 }
     94 
     95