Search code examples
androidmatrixrotationimageviewrenderscript

Rotate image in RenderScript


I need to rotate a image in renderscript and I have the following code:

private ScriptC_flip mScript;
Button flip = (Button)view.findViewById(R.id.flipVertical);
flip.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        mScript.set_direction(1);
        flip();
    }
});
mBitmapIn = loadBitmap(R.drawable.face2);

in = (ImageView) view.findViewById(R.id.displayin);
in.setImageBitmap(mBitmapIn);

createScript();

The following functions are needed:

protected void flip() {
    mScript.invoke_filter();
    mOutAllocation.copyTo(mBitmapIn);

    mRS.destroy();
    mInAllocation.destroy();
    mOutAllocation.destroy();
    mScript.destroy();

    createScript();
    in.invalidate();
}

private void createScript() {
    mRS = RenderScript.create(getActivity());

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

    mScript = new ScriptC_flip(mRS, getResources(), R.raw.flip);
    mScript.set_width(mBitmapIn.getWidth());
    mScript.set_height(mBitmapIn.getHeight());
    mScript.set_gIn(mInAllocation);
    mScript.set_gOut(mOutAllocation);
    mScript.set_gScript(mScript);

}

private Bitmap loadBitmap(int resource) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    return BitmapFactory.decodeResource(getResources(), resource, options);
}

This is my RenderSCript code:

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

rs_allocation gIn;
rs_allocation gOut;
rs_script gScript;
int width;
int height;
int direction = 0; // 0 - flip horizontally, 1 - flip vertically
float rotation;

void init(){
    rotation = 0.0f;
}

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {
    if(direction == 4){ // rotate right
        const uchar4 *element = rsGetElementAt(gIn, x, y);
        float4 color = rsUnpackColor8888(*element);
        float4 output = {color.r, color.g, color.b};
        *v_out = rsPackColorTo8888(output);

        rs_matrix4x4 matrix;
        rsMatrixLoadIdentity(&matrix);
        rsMatrixTranslate(&matrix, 100.0f, 100.0f, 0.0f);
        rsMatrixRotate(&matrix, rotation++, 0.0f, 0.0f, 1.0f);
        //       rsgProgramVertexLoadModelMatrix(&matrix);
    }else if(direction == 5){ // rotate right
        const uchar4 *element = rsGetElementAt(gIn, y, height - x);
        float4 color = rsUnpackColor8888(*element);
        float4 output = {color.r, color.g, color.b};
        *v_out = rsPackColorTo8888(output);
    }
}

void filter() {
    rsForEach(gScript, gIn, gOut, 0);
}

If I try to decomment this line:

//       rsgProgramVertexLoadModelMatrix(&matrix);

I get an error that this method does not exist. Why is this happening? I used it in other renderscript examples. the only difference is that over there i had an RSSurfaceView, here, i set the result on a image view. Now how can i make it rotate? If i set the "direction" to 5, then it rotates right with 90degrees. If i try with "direction" = 4, it doesn't do anything. I took this from an example where it would rotate a mesh over and over again


Solution

  • I figured out how to do it with an RSSurfaceView, but sadly, this class is deprecated, so i cannot use renderscript for graphics anymore.