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;
20
21import android.support.v8.renderscript.*;
22import android.util.Log;
23
24public class Convolve3x3 extends TestBase {
25    private ScriptC_convolve3x3 mScript;
26    private ScriptIntrinsicConvolve3x3 mIntrinsic;
27
28    private int mWidth;
29    private int mHeight;
30    private boolean mUseIntrinsic;
31
32    public Convolve3x3(boolean useIntrinsic) {
33        mUseIntrinsic = useIntrinsic;
34    }
35
36    private float blend(float v1, float v2, float p) {
37        return (v2 * p) + (v1 * (1.f-p));
38    }
39
40    private float[] updateMatrix(float str) {
41        float f[] = new float[9];
42        float cf1 = blend(1.f / 9.f, 0.f, str);
43        float cf2 = blend(1.f / 9.f, -1.f, str);
44        float cf3 = blend(1.f / 9.f, 5.f, str);
45        f[0] =  cf1;  f[1] = cf2;   f[2] = cf1;
46        f[3] =  cf2;  f[4] = cf3;   f[5] = cf2;
47        f[6] =  cf1;  f[7] = cf2;   f[8] = cf1;
48        return f;
49    }
50
51    public void createTest(android.content.res.Resources res) {
52        mWidth = mInPixelsAllocation.getType().getX();
53        mHeight = mInPixelsAllocation.getType().getY();
54
55        float f[] = updateMatrix(1.f);
56        if (mUseIntrinsic) {
57            mIntrinsic = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
58            mIntrinsic.setCoefficients(f);
59            mIntrinsic.setInput(mInPixelsAllocation);
60        } else {
61            mScript = new ScriptC_convolve3x3(mRS);
62            mScript.set_gCoeffs(f);
63            mScript.set_gIn(mInPixelsAllocation);
64            mScript.set_gWidth(mWidth);
65            mScript.set_gHeight(mHeight);
66        }
67    }
68
69    public void animateBars(float time) {
70        float f[] = updateMatrix(time % 1.f);
71        if (mUseIntrinsic) {
72            mIntrinsic.setCoefficients(f);
73        } else {
74            mScript.set_gCoeffs(f);
75        }
76    }
77
78    public void runTest() {
79        if (mUseIntrinsic) {
80            mIntrinsic.forEach(mOutPixelsAllocation);
81        } else {
82            mScript.forEach_root(mOutPixelsAllocation);
83        }
84    }
85
86}
87