Java Code Examples for android.renderscript.Allocation#destroy()

The following examples show how to use android.renderscript.Allocation#destroy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Blur.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
    final Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    final RenderScript rs = RenderScript.create(context);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);

    sentBitmap.recycle();
    rs.destroy();
    input.destroy();
    output.destroy();
    script.destroy();

    return bitmap;
}
 
Example 2
Source File: Blur.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) {
    final Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    final RenderScript rs = RenderScript.create(context);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);

    sentBitmap.recycle();
    rs.destroy();
    input.destroy();
    output.destroy();
    script.destroy();

    return bitmap;
}
 
Example 3
Source File: BlurTransformation.java    From AcgClub with MIT License 5 votes vote down vote up
private Bitmap doBlur(Context context, Bitmap bitmap, int radius)
    throws RSRuntimeException {
  RenderScript rs = null;
  Allocation input = null;
  Allocation output = null;
  ScriptIntrinsicBlur blur = null;
  try {
    rs = RenderScript.create(context);
    rs.setMessageHandler(new RenderScript.RSMessageHandler());
    input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT);
    output = Allocation.createTyped(rs, input.getType());
    blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(radius);
    blur.forEach(output);
    output.copyTo(bitmap);
  } finally {
    if (rs != null) {
      rs.destroy();
    }
    if (input != null) {
      input.destroy();
    }
    if (output != null) {
      output.destroy();
    }
    if (blur != null) {
      blur.destroy();
    }
  }

  return bitmap;
}
 
Example 4
Source File: ZigzagView.java    From ZigzagView with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void drawShadow() {
    shadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
    shadow.eraseColor(TRANSPARENT);
    Canvas c = new Canvas(shadow);
    c.drawPath(pathZigzag, paintShadow);

    RenderScript rs = RenderScript.create(getContext());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
    Allocation input = Allocation.createFromBitmap(rs, shadow);
    Allocation output = Allocation.createTyped(rs, input.getType());
    blur.setRadius(zigzagElevation);
    blur.setInput(input);
    blur.forEach(output);
    output.copyTo(shadow);
    input.destroy();
    output.destroy();

}
 
Example 5
Source File: Layer.java    From rscnn with MIT License 5 votes vote down vote up
protected void allocFeatureMapNoBlock()
{
    int outNum = outputShape[0];
    int outHeight = outputShape[1];
    int outWidth = outputShape[2];
    int outChannel = outputShape[3];

    if(featureMapOutput!=null){
        FeatureMap old = (FeatureMap)featureMapOutput;
        if(old.getFeatureMap()!=null){
            Allocation out = old.getFeatureMap();
            if(out.getBytesSize()==outNum * outHeight * outWidth * outChannel * 4){
                old.setN(outNum);
                old.setH(outHeight);
                old.setW(outWidth);
                old.setC(outChannel);
                return;
            }
            else{
                out.destroy();
            }
        }
    }

    Type outType = Type.createX(renderScript, Element.F32(renderScript), outNum * outHeight * outWidth * outChannel);
    //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outNum);
    output.setH(outHeight);
    output.setW(outWidth);
    output.setC(outChannel);
    output.setMatrix2D(false);
    featureMapOutput = output;
}
 
Example 6
Source File: TicketView.java    From TicketView with Apache License 2.0 5 votes vote down vote up
private void generateShadow() {
    if (isJellyBeanAndAbove() && !isInEditMode()) {
        if (mShadowBlurRadius == 0f) return;

        if (mShadow == null) {
            mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
        } else {
            mShadow.eraseColor(TRANSPARENT);
        }
        Canvas c = new Canvas(mShadow);
        c.drawPath(mPath, mShadowPaint);
        if (mShowBorder) {
            c.drawPath(mPath, mShadowPaint);
        }
        RenderScript rs = RenderScript.create(getContext());
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
        Allocation input = Allocation.createFromBitmap(rs, mShadow);
        Allocation output = Allocation.createTyped(rs, input.getType());
        blur.setRadius(mShadowBlurRadius);
        blur.setInput(input);
        blur.forEach(output);
        output.copyTo(mShadow);
        input.destroy();
        output.destroy();
        blur.destroy();
    }
}
 
Example 7
Source File: RenderScriptBlurFilter.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Not-in-place intrinsic Gaussian blur filter using {@link ScriptIntrinsicBlur} and {@link
 * RenderScript}. This require an Android versions >= 4.2.
 *
 * @param dest The {@link Bitmap} where the blurred image is written to.
 * @param src The {@link Bitmap} containing the original image.
 * @param context The {@link Context} necessary to use {@link RenderScript}
 * @param radius The radius of the blur with a supported range 0 < radius <= {@link
 *     #BLUR_MAX_RADIUS}
 */
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void blurBitmap(
    final Bitmap dest, final Bitmap src, final Context context, final int radius) {
  Preconditions.checkNotNull(dest);
  Preconditions.checkNotNull(src);
  Preconditions.checkNotNull(context);
  Preconditions.checkArgument(radius > 0 && radius <= BLUR_MAX_RADIUS);
  RenderScript rs = null;
  try {
    rs = RenderScript.create(context);

    // Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // Create the input/output allocations with Renderscript and the src/dest bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, src);
    Allocation allOut = Allocation.createFromBitmap(rs, dest);

    // Set the radius of the blur
    blurScript.setRadius(radius);
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(dest);

    blurScript.destroy();
    allIn.destroy();
    allOut.destroy();
  } finally {
    if (rs != null) {
      rs.destroy();
    }
  }
}
 
Example 8
Source File: RSRender.java    From LiveBlurListView with Apache License 2.0 5 votes vote down vote up
@Override
public void blur(float radius, Bitmap in, Bitmap out) {
	Allocation tmpIn = Allocation.createFromBitmap(rs, in);
	Allocation tmpOut = Allocation.createFromBitmap(rs, out);
	blur(radius, tmpIn, tmpOut);
	tmpOut.copyTo(out);
	tmpIn.destroy();
	tmpOut.destroy();
}
 
Example 9
Source File: Layer.java    From rscnn with MIT License 4 votes vote down vote up
protected void allocFeatureMapBlock4()
{
    int outNum = outputShape[0];
    int outHeight = outputShape[1];
    int outWidth = outputShape[2];
    int outChannel = outputShape[3];

    int outChannelAlign = outChannel;
    if(outChannelAlign % 4 !=0) {
        outChannelAlign = outChannel + 4 - (outChannel % 4);
    }

    if(featureMapOutput!=null){
        FeatureMap old = (FeatureMap)featureMapOutput;
        if(old.getFeatureMap()!=null){
            Allocation out = old.getFeatureMap();
            if(out.getBytesSize()==outNum * outHeight * outWidth * outChannelAlign * 4){
                old.setN(outNum);
                old.setH(outHeight);
                old.setW(outWidth);
                old.setC(outChannel);
                return;
            }
            else{
                out.destroy();
            }
        }
    }

    Type outType = Type.createX(renderScript, Element.F32_4(renderScript), outNum * outHeight * outWidth * outChannelAlign / 4);
    //Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    Allocation outAllocation = Allocation.createTyped(renderScript, outType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outNum);
    output.setH(outHeight);
    output.setW(outWidth);
    output.setC(outChannel);
    output.setPad4(true);
    output.setMatrix2D(false);
    featureMapOutput = output;

}
 
Example 10
Source File: Convolution.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void computeFeatureMap() {
    int outputHeight = outputShape[1];
    int outputWidth = outputShape[2];
    int inputChannel = inputShape[0][3];
    int outputChannel = outputShape[3];

    int outputChannelAligned = getOutputChannelAligned();
    int inputChannelAligned = getInputChannelAligned();

    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;

    Allocation inputFeatureMap = input.getFeatureMap();
    Allocation outputFeatureMap = output.getFeatureMap();

    scriptConvolution.set_InputData(inputFeatureMap);
    scriptConvolution.set_OutputData(outputFeatureMap);
    ScriptC.LaunchOptions option = new Script.LaunchOptions();
    boolean useIntrinsicBlas = false;

    if(conv1x1UserIntrinsic && kernelH==1 && kernelW==1){
        useIntrinsicBlas = true;
    }
    if(convnxnUseIntrinsic && kernelH!=1 && kernelW!=1){
        conv1x1UserIntrinsic = true;
    }

    if(useIntrinsicBlas){
        if(kernelH==1 && kernelW==1){
            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, inputFeatureMap, kernelAllocation, 0.f, outputFeatureMap);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
            return;
        }
        else {
            Type.Builder colType = new Type.Builder(renderScript, Element.F32(renderScript));
            colType.setX(kernelH * kernelW * inputChannelAligned).setY(outputHeight * outputWidth);
            Allocation colAllocation = Allocation.createTyped(renderScript, colType.create());
            scriptConvolution.set_ColData(colAllocation);

            option.setX(0, kernelH * kernelW).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_im2col2(option);

            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, colAllocation, kernelAllocation, 0.f, outputFeatureMap);

            colAllocation.destroy();
        }

        if(nextRelu && biasTerm){
            scriptConvolution.forEach_conv_bias_relu(outputFeatureMap, outputFeatureMap);
        }
        else if(biasTerm){
            scriptConvolution.forEach_conv_bias(outputFeatureMap, outputFeatureMap);
        }
        else if(nextRelu){
            scriptConvolution.forEach_conv_relu(outputFeatureMap, outputFeatureMap);
        }
    }
    else {
        if(kernelH==1 && kernelW==1){
            option.setX(0, getOutputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv1x1(option);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
        }
        else {
            int blockSize = 4;
            int[] blockSizeList = {256, 128, 96, 64, 48, 32, 16, 8};
            for (int blk : blockSizeList) {
                if (outputChannelAligned % blk == 0) {
                    blockSize = blk;
                    break;
                }
            }
            scriptConvolution.set_nblock(blockSize);
            option.setX(0, outputChannelAligned / blockSize).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv4n(option);
        }
    }
}
 
Example 11
Source File: FullyConnected.java    From CNNdroid with MIT License 4 votes vote down vote up
private float[][] fullyConnectedLayerInF4OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) {
    // fully connected layer

    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    // Calculate sizes.
    int n_i, c_i;
    n_i = inputBlob4.length;
    c_i = inputBlob4[0].length;

    int c_i_4 = c_i;
    if (c_i % 4 != 0)
        c_i_4 = c_i + 4 - c_i % 4;

    int n_o = n_i;
    int c_o = h_w;

    // Initialize the result.
    float[][] outputBlob = new float[n_o][c_o];

    //initialize Renderscript
    Type inputType, outType;
    Allocation frameAllocation;
    Allocation outAllocation;
    inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_4 / 4);
    outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o);

    frameAllocation = Allocation.createTyped(myRS, inputType);
    outAllocation = Allocation.createTyped(myRS, outType);

    myScriptF4.set_c_i(c_i_4);
    // calculate the result

    float[] frameMatrix = new float[n_i * c_i_4];
    for (int n = 0 ; n < n_i ; n++)
        for (int i = 0 ; i < c_i_4 ; i++)
            if (i < c_i)
                frameMatrix[n * w_w + i] = inputBlob4[n][i];
            else
                frameMatrix[n * w_w + i] = 0;

    frameAllocation.copyFrom(frameMatrix);
    myScriptF4.set_In_Blob(frameAllocation);


    myScriptF4.forEach_root(outAllocation);

    float[] outMatrix = new float[n_o * c_o];
    outAllocation.copyTo(outMatrix);

    for (int n = 0 ; n < n_i ; n++)
        for (int c = 0 ; c < c_o ; c++) {
            outputBlob[n][c] = outMatrix[n * c_o + c];
            if (nonLinear) {
                switch (nonLinearType) {
                    case RectifiedLinearUnit:
                        if (outputBlob[n][c] < 0)
                            outputBlob[n][c] = 0;
                        break;
                }
            }
        }

    frameAllocation.destroy();
    outAllocation.destroy();

    inputType.destroy();
    outType.destroy();

    if (destroy) {
        myScriptF4.destroy();
        myScriptF4 = null;
    }

    // return the result
    return outputBlob;
}
 
Example 12
Source File: FullyConnected.java    From CNNdroid with MIT License 4 votes vote down vote up
private float[][] fullyConnectedLayerInF8OutF1(float[][] inputBlob4, float[] myWeight, float[] myBias, boolean destroy) {
    // fully connected layer

    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    // Calculate sizes.
    int n_i, c_i;
    n_i = inputBlob4.length;
    c_i = inputBlob4[0].length;

    int c_i_8 = c_i;
    if (c_i % 8 != 0)
        c_i_8 = c_i + 8 - c_i % 8;

    int n_o = n_i;
    int c_o = h_w;

    // Initialize the result.
    float[][] outputBlob = new float[n_o][c_o];

    //initialize Renderscript
    Type inputType, outType;
    Allocation frameAllocation;
    Allocation outAllocation;
    inputType = Type.createX(myRS, Element.F32_4(myRS), n_i * c_i_8 / 4);
    outType = Type.createX(myRS, Element.F32(myRS), n_o * c_o);

    frameAllocation = Allocation.createTyped(myRS, inputType);
    outAllocation = Allocation.createTyped(myRS, outType);

    myScriptF8.set_c_i(c_i_8);
    // calculate the result

    float[] frameMatrix = new float[n_i * c_i_8];
    for (int n = 0 ; n < n_i ; n++)
        for (int i = 0 ; i < c_i_8 ; i++)
            if (i < c_i)
                frameMatrix[n * w_w + i] = inputBlob4[n][i];
            else
                frameMatrix[n * w_w + i] = 0;

    frameAllocation.copyFrom(frameMatrix);
    myScriptF8.set_In_Blob(frameAllocation);


    myScriptF8.forEach_root(outAllocation);

    float[] outMatrix = new float[n_o * c_o];
    outAllocation.copyTo(outMatrix);

    for (int n = 0 ; n < n_i ; n++)
        for (int c = 0 ; c < c_o ; c++) {
            outputBlob[n][c] = outMatrix[n * c_o + c];
            if (nonLinear) {
                switch (nonLinearType) {
                    case RectifiedLinearUnit:
                        if (outputBlob[n][c] < 0)
                            outputBlob[n][c] = 0;
                        break;
                }
            }
        }

    frameAllocation.destroy();
    outAllocation.destroy();

    inputType.destroy();
    outType.destroy();

    if (destroy) {
        myScriptF8.destroy();
        myScriptF8 = null;
    }

    // return the result
    return outputBlob;
}
 
Example 13
Source File: ResizeBitmapHelper.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
static Bitmap resizeNice(@NonNull final RenderScript rs, final Bitmap src, float xScale, float yScale) {
        // Calculate gaussian's radius
        float sigma = (1 / xScale) / (float) Math.PI;
        // https://android.googlesource.com/platform/frameworks/rs/+/master/cpu_ref/rsCpuIntrinsicBlur.cpp
        float radius = 2.5f * sigma/* - 1.5f*/; // Works better that way
        radius = Math.min(25, Math.max(0.0001f, radius));
        Timber.d(">> using sigma=%s for xScale=%s => radius=%s", sigma, xScale, radius);

        // Defensive programming in case the threading/view recycling recycles a bitmap just before that methods is reached
        if (null == src || src.isRecycled()) return src;

        Bitmap.Config bitmapConfig = src.getConfig();
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int dstWidth = Math.round(srcWidth * xScale);
        int dstHeight = Math.round(srcHeight * yScale);
        src.setHasAlpha(false);

        // Gaussian filter
        Allocation tmpIn = Allocation.createFromBitmap(rs, src);
        Allocation tmpFiltered = Allocation.createTyped(rs, tmpIn.getType());
        ScriptIntrinsicBlur blurInstrinsic = ScriptIntrinsicBlur.create(rs, tmpIn.getElement());

        blurInstrinsic.setRadius(radius);
        blurInstrinsic.setInput(tmpIn);
        blurInstrinsic.forEach(tmpFiltered);

        src.recycle();
        tmpIn.destroy();
        blurInstrinsic.destroy();


        // Resize
        Bitmap dst = Bitmap.createBitmap(dstWidth, dstHeight, bitmapConfig);
        Type t = Type.createXY(rs, tmpFiltered.getElement(), dstWidth, dstHeight);
        Allocation tmpOut = Allocation.createTyped(rs, t);
        ScriptIntrinsicResize resizeIntrinsic = ScriptIntrinsicResize.create(rs);

        resizeIntrinsic.setInput(tmpFiltered);
        resizeIntrinsic.forEach_bicubic(tmpOut);
        tmpOut.copyTo(dst);

        tmpFiltered.destroy();
        resizeIntrinsic.destroy();
        tmpOut.destroy();
/*
        // Additional sharpen script just in case (WIP)
        Allocation tmpSharpOut = Allocation.createTyped(rs, t);
        //ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, tmpOut.getElement());
        ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
        sharpen.setCoefficients(getSharpenCoefficients());
        sharpen.setInput(tmpOut);
        sharpen.forEach(tmpSharpOut);

        tmpSharpOut.copyTo(dst);

        tmpOut.destroy();
        tmpSharpOut.destroy();
        sharpen.destroy();
*/

        return dst;
    }