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 17package com.android.rs.image2; 18 19import java.lang.Math; 20import java.lang.Short; 21 22import android.support.v8.renderscript.*; 23import android.util.Log; 24import android.widget.SeekBar; 25import android.widget.TextView; 26import android.widget.AdapterView; 27import android.widget.ArrayAdapter; 28import android.view.View; 29import android.widget.Spinner; 30 31public class Blend extends TestBase { 32 private ScriptIntrinsicBlend mBlend; 33 private ScriptC_blend mBlendHelper; 34 private short image1Alpha = 128; 35 private short image2Alpha = 128; 36 37 String mIntrinsicNames[]; 38 39 private Allocation image1; 40 private Allocation image2; 41 private int currentIntrinsic = 0; 42 43 private AdapterView.OnItemSelectedListener mIntrinsicSpinnerListener = 44 new AdapterView.OnItemSelectedListener() { 45 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 46 currentIntrinsic = pos; 47 if (mRS != null) { 48 runTest(); 49 act.mProcessor.update(); 50 } 51 } 52 53 public void onNothingSelected(AdapterView parent) { 54 55 } 56 }; 57 58 public void createTest(android.content.res.Resources res) { 59 mBlend = ScriptIntrinsicBlend.create(mRS, Element.U8_4(mRS)); 60 mBlendHelper = new ScriptC_blend(mRS); 61 mBlendHelper.set_alpha((short)128); 62 63 image1 = Allocation.createTyped(mRS, mInPixelsAllocation.getType()); 64 image2 = Allocation.createTyped(mRS, mInPixelsAllocation2.getType()); 65 66 mIntrinsicNames = new String[14]; 67 mIntrinsicNames[0] = "Source"; 68 mIntrinsicNames[1] = "Destination"; 69 mIntrinsicNames[2] = "Source Over"; 70 mIntrinsicNames[3] = "Destination Over"; 71 mIntrinsicNames[4] = "Source In"; 72 mIntrinsicNames[5] = "Destination In"; 73 mIntrinsicNames[6] = "Source Out"; 74 mIntrinsicNames[7] = "Destination Out"; 75 mIntrinsicNames[8] = "Source Atop"; 76 mIntrinsicNames[9] = "Destination Atop"; 77 mIntrinsicNames[10] = "XOR"; 78 mIntrinsicNames[11] = "Add"; 79 mIntrinsicNames[12] = "Subtract"; 80 mIntrinsicNames[13] = "Multiply"; 81 } 82 83 public boolean onSpinner1Setup(Spinner s) { 84 s.setAdapter(new ArrayAdapter<String>( 85 act, R.layout.spinner_layout, mIntrinsicNames)); 86 s.setOnItemSelectedListener(mIntrinsicSpinnerListener); 87 return true; 88 } 89 90 public boolean onBar1Setup(SeekBar b, TextView t) { 91 t.setText("Image 1 Alpha"); 92 b.setMax(255); 93 b.setProgress(image1Alpha); 94 return true; 95 } 96 97 public void onBar1Changed(int progress) { 98 image1Alpha = (short)progress; 99 } 100 101 public boolean onBar2Setup(SeekBar b, TextView t) { 102 t.setText("Image 2 Alpha"); 103 b.setMax(255); 104 b.setProgress(image2Alpha); 105 return true; 106 } 107 108 public void onBar2Changed(int progress) { 109 image2Alpha = (short)progress; 110 } 111 112 public void runTest() { 113 image1.copy2DRangeFrom(0, 0, mInPixelsAllocation.getType().getX(), mInPixelsAllocation.getType().getY(), mInPixelsAllocation, 0, 0); 114 image2.copy2DRangeFrom(0, 0, mInPixelsAllocation2.getType().getX(), mInPixelsAllocation2.getType().getY(), mInPixelsAllocation2, 0, 0); 115 116 mBlendHelper.set_alpha(image1Alpha); 117 mBlendHelper.forEach_setImageAlpha(image1, image1); 118 119 mBlendHelper.set_alpha(image2Alpha); 120 mBlendHelper.forEach_setImageAlpha(image2, image2); 121 122 switch (currentIntrinsic) { 123 case 0: 124 mBlend.forEachSrc(image1, image2); 125 break; 126 case 1: 127 mBlend.forEachDst(image1, image2); 128 break; 129 case 2: 130 mBlend.forEachSrcOver(image1, image2); 131 break; 132 case 3: 133 mBlend.forEachDstOver(image1, image2); 134 break; 135 case 4: 136 mBlend.forEachSrcIn(image1, image2); 137 break; 138 case 5: 139 mBlend.forEachDstIn(image1, image2); 140 break; 141 case 6: 142 mBlend.forEachSrcOut(image1, image2); 143 break; 144 case 7: 145 mBlend.forEachDstOut(image1, image2); 146 break; 147 case 8: 148 mBlend.forEachSrcAtop(image1, image2); 149 break; 150 case 9: 151 mBlend.forEachDstAtop(image1, image2); 152 break; 153 case 10: 154 mBlend.forEachXor(image1, image2); 155 break; 156 case 11: 157 mBlend.forEachAdd(image1, image2); 158 break; 159 case 12: 160 mBlend.forEachSubtract(image1, image2); 161 break; 162 case 13: 163 mBlend.forEachMultiply(image1, image2); 164 break; 165 } 166 167 mOutPixelsAllocation.copy2DRangeFrom(0, 0, image2.getType().getX(), image2.getType().getY(), image2, 0, 0); 168 } 169 170} 171