How to write a convolution multiplication in Android Renderscript?

陌路散爱 提交于 2019-12-17 18:44:06

问题


I am new to Android Renderscript. I need to write a convolution multiplication in RenderScript since the final application is going to run on Android. Data stream is going to be an image. More specifically, I am not able to write the core logic using forEach functionality, though I can do it in Java, but speed it too slow! Please help! Steve


回答1:


During the rsForEach call (or other Renderscript function), you can access the neighbouring pixels of the original image (or whatever type of data you are using) by binding the original image allocation to a pointer within the Renderscript where it can then be accessed as an array. Here is an example based upon the HelloCompute example:

#pragma version(1)
#pragma rs java_package_name(com.android.example.hellocompute)

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;

static int mImageWidth;
const uchar4 *gPixels;

const float4 kWhite = {
    1.0f, 1.0f, 1.0f, 1.0f
};
const float4 kBlack = {
    0.0f, 0.0f, 0.0f, 1.0f
};

void init() {
}

static const int kBlurWidth = 20;
static const float kMultiplier = 1.0f / (float)(kBlurWidth * 2 + 1);

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
    float4 original = rsUnpackColor8888(*v_in);

    float4 colour = original * kMultiplier;

    int y_component = mImageWidth * y;

    for ( int i = -kBlurWidth; i < 0; i++) {
        float4 temp_colour;

        if ( (int)x + i >= 0) {
            temp_colour = rsUnpackColor8888(gPixels[x+i + y_component]);
        }
        else {
            temp_colour = kWhite;
        }

        colour += temp_colour * kMultiplier;
    }
    for ( int i = 1; i <= kBlurWidth; i++) {
        float4 temp_colour;

        if ( x + i < mImageWidth) {
            temp_colour = rsUnpackColor8888(gPixels[x+i + y_component]);
        }
        else {
            temp_colour = kWhite;
        }

        colour += temp_colour * kMultiplier;
    }

    colour.a = 1.0f;
    *v_out = rsPackColorTo8888(colour);
}


void filter() {
    mImageWidth = rsAllocationGetDimX(gIn);
    rsDebug("Image size is ", rsAllocationGetDimX(gIn), rsAllocationGetDimY(gOut));
    rsForEach(gScript, gIn, gOut, NULL);
}

Called from the following Java. Note the call to mScript.bind_gPixels(mInAllocation) which binds the original image data to the gPixel pointer in the Renderscript and, therefore, makes the image data available as an array.

    mRS = RenderScript.create(this);

    mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
                                                Allocation.MipmapControl.MIPMAP_NONE,
                                                Allocation.USAGE_SCRIPT);
    mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());

    mScript = new ScriptC_blur(mRS, getResources(), R.raw.blur);

    mScript.bind_gPixels(mInAllocation);

    mScript.set_gIn(mInAllocation);
    mScript.set_gOut(mOutAllocation);
    mScript.set_gScript(mScript);
    mScript.invoke_filter();
    mOutAllocation.copyTo(mBitmapOut);


来源:https://stackoverflow.com/questions/10090583/how-to-write-a-convolution-multiplication-in-android-renderscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!