Resize.java revision 0d6043caef208ee6c661eb17bcb376abfe90cd48
1/*
2 * Copyright (C) 2014 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.image;
18
19import java.lang.Math;
20
21import android.renderscript.Allocation;
22import android.renderscript.Element;
23import android.renderscript.Matrix4f;
24import android.renderscript.RenderScript;
25import android.renderscript.Script;
26import android.renderscript.ScriptC;
27import android.renderscript.ScriptIntrinsicResize;
28import android.renderscript.Type;
29import android.util.Log;
30
31public class Resize extends TestBase {
32    private ScriptC_resize mScript;
33    private ScriptIntrinsicResize mIntrinsic;
34
35    private Allocation mScratchAllocation;
36    private int mWidth;
37    private int mHeight;
38    private boolean mUseIntrinsic;
39
40    public Resize(boolean useIntrinsic) {
41        mUseIntrinsic = useIntrinsic;
42    }
43
44    public void createTest(android.content.res.Resources res) {
45        mWidth = mInPixelsAllocation.getType().getX();
46        mHeight = mInPixelsAllocation.getType().getY();
47        float scale = 1.f / 32.f;
48
49        Type.Builder tb = new Type.Builder(mRS, mInPixelsAllocation.getElement());
50        tb.setX((int)(mWidth * scale));
51        tb.setY((int)(mHeight * scale));
52        Type t = tb.create();
53        mScratchAllocation = Allocation.createTyped(mRS, t);
54
55        // make small buffer
56        mScript = new ScriptC_resize(mRS);
57        mScript.set_gIn(mInPixelsAllocation);
58        mScript.set_gWidthIn(mWidth);
59        mScript.set_gHeightIn(mHeight);
60        mScript.set_scale(1.f / scale);
61        mScript.forEach_nearest(mScratchAllocation);
62
63        // setup normal ops
64        mScript.set_gIn(mScratchAllocation);
65        mScript.set_gWidthIn(t.getX());
66        mScript.set_gHeightIn(t.getY());
67        mScript.set_scale(scale);
68        //mScript.forEach_nearest(mScratchAllocation);
69
70        mIntrinsic = ScriptIntrinsicResize.create(mRS);
71        mIntrinsic.setInput(mScratchAllocation);
72    }
73
74    public void runTest() {
75        if (mUseIntrinsic) {
76            mIntrinsic.forEach_bicubic(mOutPixelsAllocation);
77        } else {
78            mScript.forEach_bicubic(mOutPixelsAllocation);
79            //mScript.forEach_nearest(mOutPixelsAllocation);
80        }
81    }
82
83}
84