Home | History | Annotate | Download | only in rs
      1 /*
      2  * Copyright 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 
     17 package android.hardware.camera2.cts.rs;
     18 
     19 import static android.hardware.camera2.cts.helpers.Preconditions.*;
     20 
     21 import android.util.Size;
     22 import android.hardware.camera2.cts.ScriptC_means_yuvx_444_1d_to_single;
     23 import android.hardware.camera2.cts.rs.AllocationInfo.ElementInfo;
     24 import android.renderscript.Element;
     25 
     26 /**
     27  * Average a {@code Hx1} {@link ElementInfo#U8_3 U8_3} {@link Allocation allocation} into a 1x1
     28  * {@code U8x3} {@link Allocation allocation}.
     29  *
     30  * <p>Users of this script should chain {@link ScriptYuvMeans1d} immediately before this
     31  * to average the input down to a 1x1 element.</p>
     32  */
     33 public class ScriptYuvMeans1d extends Script<ScriptC_means_yuvx_444_1d_to_single>{
     34     private static final String TAG = "ScriptYuvMeans1d";
     35 
     36     private static final Size UNIT_SQUARE = new Size(/*width*/1, /*height*/1);
     37 
     38     private static AllocationInfo createOutputInfo(AllocationInfo inputInfo) {
     39         checkNotNull("inputInfo", inputInfo);
     40         // (input) Hx1 -> 1x1
     41         return AllocationInfo.newInstance(Element.U8_3(getRS()), UNIT_SQUARE);
     42     }
     43 
     44     public ScriptYuvMeans1d(AllocationInfo inputInfo) {
     45         super(inputInfo,
     46               createOutputInfo(inputInfo),
     47               new ScriptC_means_yuvx_444_1d_to_single(getRS()));
     48 
     49         // U8x3 is the only supported element here
     50         if (!inputInfo.isElementEqualTo(ElementInfo.U8_3)) {
     51             throw new UnsupportedOperationException("Unsupported element "
     52                     + inputInfo.getElement());
     53         }
     54     }
     55 
     56     @Override
     57     protected void executeUnchecked() {
     58         mScript.forEach_means_yuvx_444(mOutputAllocation);
     59     }
     60 
     61     @Override
     62     protected void updateScriptInput() {
     63         mScript.set_mInput(mInputAllocation);
     64 
     65         int width = mInputAllocation.getType().getX();
     66         mScript.set_width(width);
     67         mScript.set_inv_width(1.0f / width);
     68     }
     69 }
     70