1 /* 2 * Copyright (C) 2015 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.refocus; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.net.Uri; 24 import android.renderscript.RenderScript; 25 import android.renderscript.cts.RSBaseCompute; 26 import android.util.Log; 27 28 import android.renderscript.cts.R; 29 30 import java.io.IOException; 31 32 /** 33 * This is a test case for large real world renderscript code 34 * Many subtle issues with renderscript may not be caught by small unit test 35 */ 36 public class RefocusTest extends RSBaseCompute { 37 /** 38 * Test the orignal refocus code 39 */ testOriginalRefocus()40 public void testOriginalRefocus() { 41 refocus(RenderScriptTask.script.f32, 95); 42 } 43 44 /** 45 * Test the new refocus code 46 */ testNewRefocus()47 public void testNewRefocus() { 48 // The new implementation may run on a GPU using relaxed floating point 49 // mathematics. Hence more relaxed precision requirement. 50 refocus(RenderScriptTask.script.d1new, 45); 51 } 52 53 /** 54 * Test a refocus operator against the refocus_reference image 55 * @param impl version of refocus to run 56 */ refocus(RenderScriptTask.script impl, double minimumPSNR)57 private void refocus(RenderScriptTask.script impl, double minimumPSNR) { 58 Context ctx = getContext(); 59 60 RenderScript rs = RenderScript.create(ctx); 61 RGBZ current_rgbz = null; 62 try { 63 current_rgbz = new RGBZ(getResourceRef(R.drawable.test_image), 64 getResourceRef(R.drawable.test_depthmap), 65 ctx.getContentResolver(), ctx); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 assertNull(e); 69 } 70 DepthOfFieldOptions current_depth_options = new DepthOfFieldOptions(current_rgbz); 71 RsTaskParams rsTaskParam = new RsTaskParams(rs, current_depth_options); 72 73 RenderScriptTask renderScriptTask = new RenderScriptTask(rs, impl); 74 Bitmap outputImage = renderScriptTask.applyRefocusFilter(rsTaskParam.mOptions); 75 76 Bitmap expectedImage = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.expected_output); 77 78 double psnr = ImageCompare.psnr(outputImage, expectedImage); 79 android.util.Log.i("RefocusTest", "psnr = " + String.format("%.02f", psnr)); 80 if (psnr < minimumPSNR) { 81 MediaStoreSaver.savePNG(outputImage, "refocus", "refocus_output" , ctx); 82 assertTrue("Required minimum psnr = " + String.format("%.02f; ", minimumPSNR) + 83 "Actual psnr = " + String.format("%.02f", psnr), 84 false); 85 } 86 rs.destroy(); 87 } 88 89 90 private static class RsTaskParams { 91 RenderScript mRenderScript; 92 DepthOfFieldOptions mOptions; 93 RsTaskParams(RenderScript renderScript, DepthOfFieldOptions options)94 RsTaskParams(RenderScript renderScript, 95 DepthOfFieldOptions options) { 96 mRenderScript = renderScript; 97 mOptions = options; 98 } 99 } 100 getResourceRef(int resID)101 Uri getResourceRef(int resID) { 102 Context context = getContext().getApplicationContext(); 103 Uri path = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 104 context.getResources().getResourcePackageName(resID) + '/' + 105 context.getResources().getResourceTypeName(resID) + '/' + 106 context.getResources().getResourceEntryName(resID)); 107 return path; 108 } 109 110 } 111