Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2013 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 android.renderscript.cts;
     18 
     19 import android.renderscript.*;
     20 import android.util.Log;
     21 
     22 public class IntrinsicHistogram extends IntrinsicBase {
     23     private Allocation mAin;
     24     private Allocation mAout;
     25 
     26     @Override
     27     protected void tearDown() throws Exception {
     28         if (mAin != null) {
     29             mAin.destroy();
     30         }
     31         if (mAout != null) {
     32             mAout.destroy();
     33         }
     34         super.tearDown();
     35     }
     36 
     37     private void createAllocations(int inVSize, int outVSize, int w, int h) {
     38         Element e1;
     39         Element e2;
     40 
     41         switch (inVSize) {
     42         case 4: e1 = Element.U8_4(mRS); break;
     43         case 3: e1 = Element.U8_3(mRS); break;
     44         case 2: e1 = Element.U8_2(mRS); break;
     45         default: e1 = Element.U8(mRS); break;
     46         }
     47 
     48         switch (outVSize) {
     49         case 4: e2 = Element.I32_4(mRS); break;
     50         case 3: e2 = Element.I32_3(mRS); break;
     51         case 2: e2 = Element.I32_2(mRS); break;
     52         default: e2 = Element.I32(mRS); break;
     53         }
     54 
     55         Type.Builder tb = new Type.Builder(mRS, e1);
     56         tb.setX(w).setY(h);
     57 
     58         mAin = Allocation.createTyped(mRS, tb.create());
     59         mAout = Allocation.createSized(mRS, e2, 256);
     60     }
     61 
     62     private void testNorm(int inVSize, int outVSize, int w, int h, boolean clip) {
     63         createAllocations(inVSize, outVSize, w, h);
     64 
     65         int invs2 = (inVSize != 3) ? inVSize : 4;
     66         int outvs2 = (outVSize != 3) ? outVSize : 4;
     67 
     68         byte i[] = new byte[w * h * invs2];
     69         int res[] = new int[256 * outvs2];
     70         int ref[] = new int[256 * outvs2];
     71 
     72         java.util.Random r = new java.util.Random();
     73         r.nextBytes(i);
     74 
     75         int x1 = 0, y1 = 0, x2 = w, y2 = h;
     76         if (clip) {
     77             x1 = 11;
     78             y1 = 11;
     79             x2 = w - 11;
     80             y2 = h - 11;
     81         }
     82 
     83         for (int y = y1; y < y2; y++) {
     84             for (int x = x1; x < x2; x++) {
     85                 int ct = (y * w + x) * invs2;
     86 
     87                 int v = 0;
     88                 for (int c = 0; c < inVSize; c++) {
     89                     int t = i[ct + c];
     90                     if (t < 0) t = 256 + t;
     91 
     92                     if (c < outVSize) {
     93                         ref[(t * outvs2) + c] ++;
     94                     }
     95                 }
     96             }
     97         }
     98 
     99         mAin.copyFrom(i);
    100         ScriptIntrinsicHistogram hist =
    101                 ScriptIntrinsicHistogram.create(mRS, mAin.getType().getElement());
    102         hist.setOutput(mAout);
    103         hist.forEach(mAin, makeClipper(x1, y1, x2, y2));
    104 
    105         mAin.copyFrom(i);
    106         mAout.copyTo(res);
    107         for (int ct=0; ct < res.length; ct++) {
    108             assertTrue(res[ct] == ref[ct]);
    109         }
    110 
    111         hist.destroy();
    112     }
    113 
    114     public void test_norm_4_4() {
    115         testNorm(4, 4, 101, 101, false);
    116     }
    117     public void test_norm_4_3() {
    118         testNorm(4, 3, 101, 101, false);
    119     }
    120     public void test_norm_4_2() {
    121         testNorm(4, 2, 101, 101, false);
    122     }
    123     public void test_norm_4_1() {
    124         testNorm(4, 1, 101, 101, false);
    125     }
    126 
    127     public void test_norm_3_3() {
    128         testNorm(3, 3, 101, 101, false);
    129     }
    130     public void test_norm_3_2() {
    131         testNorm(3, 2, 101, 101, false);
    132     }
    133     public void test_norm_3_1() {
    134         testNorm(3, 1, 101, 101, false);
    135     }
    136 
    137     public void test_norm_2_2() {
    138         testNorm(2, 2, 101, 101, false);
    139     }
    140     public void test_norm_2_1() {
    141         testNorm(2, 1, 101, 101, false);
    142     }
    143 
    144     public void test_norm_1_1() {
    145         testNorm(1, 1, 101, 101, false);
    146     }
    147 
    148 
    149     public void test_norm_4_4C() {
    150         testNorm(4, 4, 101, 101, true);
    151     }
    152     public void test_norm_4_3C() {
    153         testNorm(4, 3, 101, 101, true);
    154     }
    155     public void test_norm_4_2C() {
    156         testNorm(4, 2, 101, 101, true);
    157     }
    158     public void test_norm_4_1C() {
    159         testNorm(4, 1, 101, 101, true);
    160     }
    161 
    162     public void test_norm_3_3C() {
    163         testNorm(3, 3, 101, 101, true);
    164     }
    165     public void test_norm_3_2C() {
    166         testNorm(3, 2, 101, 101, true);
    167     }
    168     public void test_norm_3_1C() {
    169         testNorm(3, 1, 101, 101, true);
    170     }
    171 
    172     public void test_norm_2_2C() {
    173         testNorm(2, 2, 101, 101, true);
    174     }
    175     public void test_norm_2_1C() {
    176         testNorm(2, 1, 101, 101, true);
    177     }
    178 
    179     public void test_norm_1_1C() {
    180         testNorm(1, 1, 101, 101, true);
    181     }
    182 
    183 
    184 
    185     private void testDot(int inVSize, int w, int h, boolean clip) {
    186         createAllocations(inVSize, 1, w, h);
    187 
    188         int invs2 = (inVSize != 3) ? inVSize : 4;
    189 
    190         byte i[] = new byte[w * h * invs2];
    191         int res[] = new int[256];
    192         int ref[] = new int[256];
    193 
    194         java.util.Random r = new java.util.Random();
    195         r.nextBytes(i);
    196 
    197         float dotVals[] = {0.2501f, 0.1251f, 0.06251f, 0.1251f};
    198         int doti[] = new int[4];
    199         for (int ct=0; ct < 4; ct++) {
    200             doti[ct] = (int)((dotVals[ct] * 256.f) + 0.5f);
    201         }
    202 
    203         int x1 = 0, y1 = 0, x2 = w, y2 = h;
    204         if (clip) {
    205             x1 = 11;
    206             y1 = 11;
    207             x2 = w - 11;
    208             y2 = h - 11;
    209         }
    210 
    211         for (int y = y1; y < y2; y++) {
    212             for (int x = x1; x < x2; x++) {
    213                 int ct = (y * w + x) * invs2;
    214 
    215                 int v = 0;
    216                 for (int c = 0; c < inVSize; c++) {
    217                     int t = i[ct + c];
    218                     if (t < 0) t = 256 + t;
    219                     v += doti[c] * t;
    220                 }
    221                 ref[(v + 0x7f) >> 8] ++;
    222             }
    223         }
    224 
    225         mAin.copyFrom(i);
    226         ScriptIntrinsicHistogram hist =
    227                 ScriptIntrinsicHistogram.create(mRS, mAin.getType().getElement());
    228         hist.setOutput(mAout);
    229         hist.setDotCoefficients(dotVals[0], dotVals[1], dotVals[2], dotVals[3]);
    230         hist.forEach_Dot(mAin, makeClipper(x1, y1, x2, y2));
    231 
    232         mAin.copyFrom(i);
    233         mAout.copyTo(res);
    234         for (int ct=0; ct < res.length; ct++) {
    235             assertTrue(res[ct] == ref[ct]);
    236         }
    237 
    238         hist.destroy();
    239     }
    240 
    241     public void test_dot_1() {
    242         testDot(1, 101, 101, false);
    243     }
    244     public void test_dot_2() {
    245         testDot(2, 101, 101, false);
    246     }
    247     public void test_dot_3() {
    248         testDot(3, 101, 101, false);
    249     }
    250     public void test_dot_4() {
    251         testDot(4, 101, 101, false);
    252     }
    253 
    254 
    255     public void test_dot_1C() {
    256         testDot(1, 101, 101, true);
    257     }
    258     public void test_dot_2C() {
    259         testDot(2, 101, 101, true);
    260     }
    261     public void test_dot_3C() {
    262         testDot(3, 101, 101, true);
    263     }
    264     public void test_dot_4C() {
    265         testDot(4, 101, 101, true);
    266     }
    267 
    268 }
    269