Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb0dbf73 authored by Rajeev Sharma's avatar Rajeev Sharma
Browse files

Add vignette filter to Image Processing test

Add a vignette filter, mostly based on the GLSL version in
android.filterpacks.imageproc.VignetteFilter but with more input options.

Change-Id: I6f38a0ca09d35566104ec8e8523548399b8704a0
parent bb2c9476
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -149,6 +149,12 @@ public class ImageProcessingActivity extends Activity
        case 8:
            mTest = new Fisheye(true);
            break;
        case 9:
            mTest = new Vignette(false);
            break;
        case 10:
            mTest = new Vignette(true);
            break;
        }

        mTest.createBaseTest(this, mBitmapIn);
@@ -161,7 +167,7 @@ public class ImageProcessingActivity extends Activity
    }

    void setupTests() {
        mTestNames = new String[9];
        mTestNames = new String[11];
        mTestNames[0] = "Levels Vec3 Relaxed";
        mTestNames[1] = "Levels Vec4 Relaxed";
        mTestNames[2] = "Levels Vec3 Full";
@@ -171,6 +177,8 @@ public class ImageProcessingActivity extends Activity
        mTestNames[6] = "Grain";
        mTestNames[7] = "Fisheye Full";
        mTestNames[8] = "Fisheye Relaxed";
        mTestNames[9] = "Vignette Full";
        mTestNames[10] = "Vignette Relaxed";
        mTestSpinner.setAdapter(new ArrayAdapter<String>(
            this, R.layout.spinner_layout, mTestNames));
    }
+124 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.rs.image;

import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.Sampler;
import android.renderscript.Type;
import android.widget.SeekBar;
import android.widget.TextView;

public class Vignette extends TestBase {
    private ScriptC_vignette_full mScript_full = null;
    private ScriptC_vignette_relaxed mScript_relaxed = null;
    private final boolean relaxed;
    private float center_x = 0.5f;
    private float center_y = 0.5f;
    private float scale = 0.5f;
    private float shade = 0.5f;
    private float slope = 20.0f;

    public Vignette(boolean relaxed) {
        this.relaxed = relaxed;
    }

    public boolean onBar1Setup(SeekBar b, TextView t) {
        t.setText("Scale");
        b.setMax(100);
        b.setProgress(25);
        return true;
    }
    public boolean onBar2Setup(SeekBar b, TextView t) {
        t.setText("Shade");
        b.setMax(100);
        b.setProgress(50);
        return true;
    }
    public boolean onBar3Setup(SeekBar b, TextView t) {
        t.setText("Slope");
        b.setMax(100);
        b.setProgress(20);
        return true;
    }
    public boolean onBar4Setup(SeekBar b, TextView t) {
        t.setText("Shift center X");
        b.setMax(100);
        b.setProgress(50);
        return true;
    }
    public boolean onBar5Setup(SeekBar b, TextView t) {
        t.setText("Shift center Y");
        b.setMax(100);
        b.setProgress(50);
        return true;
    }

    public void onBar1Changed(int progress) {
        scale = progress / 50.0f;
        do_init();
    }
    public void onBar2Changed(int progress) {
        shade = progress / 100.0f;
        do_init();
    }
    public void onBar3Changed(int progress) {
        slope = (float)progress;
        do_init();
    }
    public void onBar4Changed(int progress) {
        center_x = progress / 100.0f;
        do_init();
    }
    public void onBar5Changed(int progress) {
        center_y = progress / 100.0f;
        do_init();
    }

    private void do_init() {
        if (relaxed)
            mScript_relaxed.invoke_init_vignette(
                    mInPixelsAllocation.getType().getX(),
                    mInPixelsAllocation.getType().getY(), center_x, center_y,
                    scale, shade, slope);
        else
            mScript_full.invoke_init_vignette(
                    mInPixelsAllocation.getType().getX(),
                    mInPixelsAllocation.getType().getY(), center_x, center_y,
                    scale, shade, slope);
    }

    public void createTest(android.content.res.Resources res) {
        if (relaxed) {
            mScript_relaxed = new ScriptC_vignette_relaxed(mRS, res,
                    R.raw.vignette_relaxed);
        } else {
            mScript_full = new ScriptC_vignette_full(mRS, res,
                    R.raw.vignette_full);
        }
        do_init();
    }

    public void runTest() {
        if (relaxed)
            mScript_relaxed.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
        else
            mScript_full.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
    }

}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

static float2 scale;
static float2 center, dimensions;
static float range, inv_max_dist, shade, slope;

void init_vignette(uint32_t dim_x, uint32_t dim_y, float center_x, float center_y,
        float desired_scale, float desired_shade, float desired_slope) {

    center.x = center_x;
    center.y = center_y;
    dimensions.x = (float)dim_x;
    dimensions.y = (float)dim_y;

    float max_dist = 0.5;
    if (dim_x > dim_y) {
        scale.x = 1.0;
        scale.y = dimensions.y / dimensions.x;
        max_dist *= sqrt(scale.y*scale.y + 1);
    } else {
        scale.x = dimensions.x / dimensions.y;
        scale.y = 1.0;
        max_dist *= sqrt(scale.x*scale.x + 1);
    }
    inv_max_dist = 1.0/max_dist;
    // Range needs to be between 1.3 to 0.6. When scale is zero then range is
    // 1.3 which means no vignette at all because the luminousity difference is
    // less than 1/256.  Expect input scale to be between 0.0 and 1.0.
    range = 1.3 - 0.7*sqrt(desired_scale);
    shade = desired_shade;
    slope = desired_slope;
}

void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    // Convert x and y to floating point coordinates with center as origin
    const float4 fin = rsUnpackColor8888(*in);
    float2 coord;
    coord.x = (float)x / dimensions.x;
    coord.y = (float)y / dimensions.y;
    coord -= center;
    const float dist = length(scale * coord);
    const float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade);
    float4 fout;
    fout.rgb = fin.rgb * lumen;
    fout.w = fin.w;
    *out = rsPackColorTo8888(fout);
}
+21 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma version(1)
#pragma rs java_package_name(com.android.rs.image)

#include "vignette.rsh"
+22 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma version(1)
#pragma rs java_package_name(com.android.rs.image)
#pragma rs_fp_relaxed

#include "vignette.rsh"