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() throws IOException { 41 refocus(RenderScriptTask.script.f32, 95); 42 } 43 44 /** 45 * Test the new refocus code 46 */ testNewRefocus()47 public void testNewRefocus() throws IOException { 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) throws IOException { 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 DepthOfFieldOptions current_depth_options = new DepthOfFieldOptions(current_rgbz); 67 RsTaskParams rsTaskParam = new RsTaskParams(rs, current_depth_options); 68 69 RenderScriptTask renderScriptTask = new RenderScriptTask(rs, impl); 70 Bitmap outputImage = renderScriptTask.applyRefocusFilter(rsTaskParam.mOptions); 71 72 Bitmap expectedImage = BitmapFactory.decodeResource(ctx.getResources(), 73 R.drawable.expected_output); 74 75 double psnr = ImageCompare.psnr(outputImage, expectedImage); 76 android.util.Log.i("RefocusTest", "psnr = " + String.format("%.02f", psnr)); 77 if (psnr < minimumPSNR) { 78 MediaStoreSaver.savePNG(outputImage, "refocus", "refocus_output" , ctx); 79 assertTrue("Required minimum psnr = " + String.format("%.02f; ", minimumPSNR) + 80 "Actual psnr = " + String.format("%.02f", psnr), 81 false); 82 } 83 } finally { 84 rs.destroy(); 85 } 86 } 87 88 89 private static class RsTaskParams { 90 RenderScript mRenderScript; 91 DepthOfFieldOptions mOptions; 92 RsTaskParams(RenderScript renderScript, DepthOfFieldOptions options)93 RsTaskParams(RenderScript renderScript, 94 DepthOfFieldOptions options) { 95 mRenderScript = renderScript; 96 mOptions = options; 97 } 98 } 99 getResourceRef(int resID)100 Uri getResourceRef(int resID) { 101 Context context = getContext().getApplicationContext(); 102 Uri path = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 103 context.getResources().getResourcePackageName(resID) + '/' + 104 context.getResources().getResourceTypeName(resID) + '/' + 105 context.getResources().getResourceEntryName(resID)); 106 return path; 107 } 108 109 } 110