Attempt to Crop bitmap using Renderscript failed with invalid slot index error

50 Views Asked by At

I'm trying to apply the solution presented in this Question's answer to crop a bitmap using RenderSript. My approach is a little bit different since I'm using bitmap directly as an input to crop. Having very limited knowledge in RenderScript I developed the below code to crop my bitmap by modifying that answer.

crop.rs

#pragma version(1)
#pragma rs java_package_name(com.xxx.yyy)
#pragma rs_fp_relaxed

int32_t width;
int32_t height;

rs_allocation croppedImg;
uint xStart, yStart;

void __attribute__((kernel)) doCrop(uchar4 in,uint32_t x, uint32_t y) {
    rsSetElementAt_uchar4(croppedImg,in, x-xStart, y-yStart);
}

ImageCropper

import android.content.Context
import android.graphics.Bitmap
import androidx.renderscript.*
import com.xxx.yyy.ScriptC_crop
import kotlin.math.abs

class ImageCropper(context: Context?) {

    val rs = RenderScript.create(context)

    var dx = 0      // (-width < dx < width);
    var dy = 250    // (- height < dy < height);

    var xStart = 50
    var xEnd = 100
    var yStart = 50
    var yEnd = 100

    fun crop(sourceBitmap: Bitmap): Bitmap{

        val width = sourceBitmap.width
        val height = sourceBitmap.height

        if (dx<0) {
            xStart = abs(dx)
            xEnd= width
        } else {
            xStart = 0
            xEnd = width - abs(dx)
        }

        if (dy<0) {
            yStart= abs(dy)
            yEnd=height
        } else {
            yStart = 0;
            yEnd = height - abs(dy)
        }

        val cropperScript = ScriptC_crop(rs)

        val inputType = Type.createXY(rs, Element.RGBA_8888(rs), width, height)
        val inputAllocation = Allocation.createTyped(rs, inputType, Allocation.USAGE_SCRIPT)
        inputAllocation.copyFrom(sourceBitmap)

        val outputType = Type.createXY(rs, Element.RGBA_8888(rs), xEnd - xStart, yEnd - yStart)
        val outputAllocation = Allocation.createTyped(rs, outputType, Allocation.USAGE_SCRIPT)

        cropperScript._croppedImg = outputAllocation
        cropperScript._width = width
        cropperScript._height = height
        cropperScript._xStart = xStart.toLong()
        cropperScript._yStart = yStart.toLong()

        val launchOptions: Script.LaunchOptions = Script.LaunchOptions()
        launchOptions.setX(xStart, xEnd)
        launchOptions.setY(yStart, yEnd)

        cropperScript.forEach_doCrop(inputAllocation, launchOptions)
        val resultBitmap = Bitmap.createBitmap(xEnd - xStart, yEnd - yStart, sourceBitmap.config)
        outputAllocation.copyTo(resultBitmap)

        rs.destroy()

        return resultBitmap
    }

}

When I try to execute the code I get a bellow error.

2021-04-11 20:55:41.639 18145-18202/com.chathuranga.shan.renderscriptexample E/RenderScript: Script::setVar unable to set allocation, invalid slot index
2021-04-11 20:55:41.645 18145-18202/com.chathuranga.shan.renderscriptexample A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x519 in tid 18202 (erscriptexample), pid 18145 (erscriptexample)
2021-04-11 20:55:41.639 18145-18202/com.chathuranga.shan.renderscriptexample E/RenderScript: Script::setVar unable to set allocation, invalid slot index

This error occurs in the line where I set _xStart and _yStart values. What am I doing wrong? is it some sort of data type issue?

0

There are 0 best solutions below