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.graphics.ImageFormat;
     22 import android.graphics.RectF;
     23 import android.util.Size;
     24 import android.hardware.camera2.cts.ScriptC_means_yuvx_444_2d_to_1d;
     25 import android.hardware.camera2.cts.rs.AllocationInfo.ElementInfo;
     26 import android.util.Log;
     27 
     28 /**
     29  * Average pixels from a {@link ImageFormat#YUV_420_888 flexible-YUV} or
     30  * {@link ElementInfo#U8_3 U8_3} {@link Allocation allocations} into a 1D Hx1
     31  * {@link ElementInfo#U8_3 U8_3} {@link Allocation allocation}.
     32  *
     33  * <p>Users of this script should chain {@link ScriptYuvMeans1d} immediately afterwards
     34  * to average the output down to a 1x1 element.</p>
     35  */
     36 public class ScriptYuvMeans2dTo1d extends Script<ScriptC_means_yuvx_444_2d_to_1d> {
     37 
     38     private static final String TAG = "ScriptYuvMeans2dTo1d";
     39     private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
     40 
     41     private static AllocationInfo createOutputInfo(AllocationInfo inputInfo) {
     42         checkNotNull("inputInfo", inputInfo);
     43         // (input) WxH -> (output) Hx1
     44         return AllocationInfo.newInstance(inputInfo.getElement(),
     45                 new Size(inputInfo.getSize().getHeight(), /*height*/1));
     46     }
     47 
     48     public ScriptYuvMeans2dTo1d(AllocationInfo inputInfo) {
     49         super(inputInfo,
     50               createOutputInfo(inputInfo),
     51               new ScriptC_means_yuvx_444_2d_to_1d(getRS()));
     52 
     53         // YUV_420_888 and U8_3 is the only supported format here
     54         if (!inputInfo.isElementEqualTo(ElementInfo.YUV) &&
     55                 !inputInfo.isElementEqualTo(ElementInfo.U8_3)) {
     56             throw new UnsupportedOperationException("Unsupported element "
     57                     + inputInfo.getElement());
     58         }
     59     }
     60 
     61     @Override
     62     protected void executeUnchecked() {
     63         // TODO: replace with switch statement
     64         if (mInputInfo.isElementEqualTo(ElementInfo.YUV)) {
     65             mScript.forEach_means_yuvf_420(mOutputAllocation);
     66 
     67             if (VERBOSE) Log.v(TAG, "executeUnchecked - forEach_means_yuvf_420");
     68         } else if (mInputInfo.isElementEqualTo(ElementInfo.U8_3)) {
     69             mScript.forEach_means_yuvx_444(mOutputAllocation);
     70 
     71             if (VERBOSE) Log.v(TAG, "executeUnchecked - forEach_means_yuvx_444");
     72         } else {
     73             throw new UnsupportedOperationException("Unsupported element "
     74                     + mInputInfo.getElement());
     75         }
     76     }
     77 
     78     @Override
     79     protected void updateScriptInput() {
     80         mScript.set_mInput(mInputAllocation);
     81 
     82         int width = mInputAllocation.getType().getX();
     83         mScript.set_width(width);
     84         mScript.set_inv_width(1.0f / width);
     85 
     86         // Do not crop. Those who want to crop should use ScriptYuvCrop.class
     87         mScript.set_src_x(0);
     88         mScript.set_src_y(0);
     89     }
     90 }
     91