android.renderscript.Allocation Java Examples

The following examples show how to use android.renderscript.Allocation. 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: BlurUtils.java    From Decor with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context) {
    RenderScript rs = RenderScript.create(context);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);

    final Allocation input = Allocation.createFromBitmap(rs, src, 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(blurRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
 
Example #2
Source File: STUtils.java    From Fatigue-Detection with MIT License 6 votes vote down vote up
@SuppressLint("NewApi")
public static Bitmap NV21ToRGBABitmap(byte []nv21, int width, int height, Context context) {
	
	TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap");
	
	Rect rect = new Rect(0, 0, width, height);
	
	try {
		Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV");
		Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB");
    	byte[] imageData = nv21;
    	if (mRS == null) {
    		mRS = RenderScript.create(context);
    		mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS));
    		Type.Builder tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
    		tb.setX(width);
    		tb.setY(height);
    		tb.setMipmaps(false);
    		tb.setYuvFormat(ImageFormat.NV21);
    		ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    		timings.addSplit("Prepare for ain");
    		Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS));
    		tb2.setX(width);
    		tb2.setY(height);
    		tb2.setMipmaps(false);
    		aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
    		timings.addSplit("Prepare for aOut");
    		bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    		timings.addSplit("Create Bitmap");
		}
    	ain.copyFrom(imageData);
		timings.addSplit("ain copyFrom");
		mYuvToRgb.setInput(ain);
		timings.addSplit("setInput ain");
		mYuvToRgb.forEach(aOut);
		timings.addSplit("NV21 to ARGB forEach");
		aOut.copyTo(bitmap);
		timings.addSplit("Allocation to Bitmap");
	} catch (Exception e) {
		YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
		timings.addSplit("NV21 bytes to YuvImage");
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(rect, 90, baos);
        byte[] cur = baos.toByteArray();
        timings.addSplit("YuvImage crop and compress to Jpeg Bytes");
        
        bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length);
        timings.addSplit("Jpeg Bytes to Bitmap");
	}
	
   	timings.dumpToLog();
   	return bitmap;
}
 
Example #3
Source File: FullyConnected.java    From CNNdroid with MIT License 6 votes vote down vote up
void initKernelF4F1(float[] myWeight, float[] myBias)
{
    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    Type kernelType,biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4);
    biasType = Type.createX(myRS, Element.F32(myRS), h_w);


    kernelAllocation = Allocation.createTyped(myRS, kernelType);
    kernelAllocation.copyFrom(myWeight);

    biasAllocation = Allocation.createTyped(myRS, biasType);
    biasAllocation.copyFrom(myBias);

    myScriptF4 = new ScriptC_innerProductInF4OutF1(myRS);


    myScriptF4.set_Bias_Blob(biasAllocation);
    myScriptF4.set_Kernel_Blob(kernelAllocation);
    myScriptF4.set_w_w(w_w);
    myScriptF4.set_c_o(h_w);
}
 
Example #4
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(20);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #5
Source File: MonoScript.java    From retroboy with MIT License 6 votes vote down vote up
@Override
public Bitmap apply(Bitmap input) {
       Allocation imagebuf = Allocation.createFromBitmap(
       	_renderContext, input, 
       	Allocation.MipmapControl.MIPMAP_NONE, 
       	Allocation.USAGE_SCRIPT);

       _filter.set_gIn(imagebuf);
       _filter.set_gOut(imagebuf);
       _filter.set_gScript(_filter);
       _filter.invoke_filter();
       
       // Reuse the input bitmap
       imagebuf.copyTo(input);
       return input;
}
 
Example #6
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap fifteen(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix13 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix13.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {

                    2.10279132254252f,  -0.298212630531356f,       0.42128146417712f,   0f,
                    0.222897572029231f,     1.68701190285368f,     -0.883421304780577f,   0f,
                    -0.765688894571747f,   0.171200727677677f,    2.02213984060346f,     0f,
                    0                          ,  0                 ,0                ,1f


            }));
    colorMatrix13.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #7
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap three(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix3 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix3.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    0f, 0f, 0f, 0f,
                    0f, 0.78f, 0f, 0f,
                    0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 1f,
            }));
    colorMatrix3.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #8
Source File: FastBlurActivity.java    From PracticeDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 有些手机不支持....so  不能用
 *
 * @param sentBitmap
 * @param radius
 */
private void RenderScriptblur(Bitmap sentBitmap, int radius) {

    long start = System.currentTimeMillis();

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    final RenderScript rs = RenderScript.create(FastBlurActivity.this);
    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 /* e.g. 3.f */);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);

    mIvBlur.setImageBitmap(bitmap);
    long end = System.currentTimeMillis();

    Log.v(TAG, "fastblur time:" + (end - start));
}
 
Example #9
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap five(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final  ScriptIntrinsicColorMatrix colorMatrix5 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix5.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    -0.359705309629158f, 0.377252728606377f, 0.663841667303255f, 0f,
                    1.56680818833214f, 0.456668209492391f, 1.12613917506705f, 0f,
                    -0.147102878702981f, 0.226079061901232f, -0.729980842370303f, 0f,
                    0f, 0f, 0f, 1f
            }));
    colorMatrix5.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #10
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap six(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix6 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix6.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.2f, 0.1f, 0.2f, 0.7f,

                    0.7f, 1f, 0f, -0.5f,
                    -0.7f, 0.2f, 0.5f, 1.3f,
                    0, -0.1f, 0f, 0.9f
            }));
    colorMatrix6.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example #11
Source File: BitmapUtil.java    From a with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 高斯模糊
 */
public static Bitmap stackBlur(Bitmap srcBitmap) {
    if (srcBitmap == null) return null;
    RenderScript rs = RenderScript.create(MApplication.getInstance());
    Bitmap blurredBitmap = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);

    //分配用于渲染脚本的内存
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    //加载我们想要使用的特定脚本的实例。
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    //设置模糊半径
    script.setRadius(8);

    //启动 ScriptIntrinsicBlur
    script.forEach(output);

    //将输出复制到模糊的位图
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example #12
Source File: UriGlideRenderer.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static @NonNull Bitmap blur(Bitmap bitmap, Context context) {
  Point  previewSize = scaleKeepingAspectRatio(new Point(bitmap.getWidth(), bitmap.getHeight()), PREVIEW_DIMENSION_LIMIT);
  Point  blurSize    = scaleKeepingAspectRatio(new Point(previewSize.x / 2, previewSize.y / 2 ), MAX_BLUR_DIMENSION);
  Bitmap small       = BitmapUtil.createScaledBitmap(bitmap, blurSize.x, blurSize.y);

  Log.d(TAG, "Bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight() + ", Blur: " + blurSize.x + "x" + blurSize.y);

  RenderScript        rs     = RenderScript.create(context);
  Allocation          input  = Allocation.createFromBitmap(rs, small);
  Allocation          output = Allocation.createTyped(rs, input.getType());
  ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

  script.setRadius(25f);
  script.setInput(input);
  script.forEach(output);

  Bitmap blurred = Bitmap.createBitmap(small.getWidth(), small.getHeight(), small.getConfig());
  output.copyTo(blurred);
  return blurred;
}
 
Example #13
Source File: ImageUtils.java    From Pasta-for-Spotify with Apache License 2.0 6 votes vote down vote up
public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
    if (context == null || bitmap == null) return null;

    Bitmap blurredBitmap;
    try {
        blurredBitmap = Bitmap.createBitmap(bitmap);
    } catch (OutOfMemoryError e) {
        return null;
    }

    RenderScript renderScript = RenderScript.create(context);
    Allocation input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(renderScript, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));

    script.setInput(input);
    script.setRadius(20);

    script.forEach(output);
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example #14
Source File: Layer.java    From rscnn with MIT License 6 votes vote down vote up
protected void allocFeatureMap()
{
    Type.Builder outputType = new Type.Builder(renderScript, Element.F32(renderScript));
    outputType.setZ(outputShape[0]);
    outputType.setY(outputShape[1] * outputShape[2]);
    outputType.setX(getOutputChannelAligned());
    Allocation outAllocation = Allocation.createTyped(renderScript, outputType.create());
    FeatureMap output = new FeatureMap();
    output.setFeatureMap(outAllocation);
    output.setN(outputShape[0]);
    output.setH(outputShape[1]);
    output.setW(outputShape[2]);
    output.setC(outputShape[3]);
    output.setPad4(true);
    if(this.featureMapOutput!=null){
        ((FeatureMap)featureMapOutput).getFeatureMap().destroy();
    }
    this.featureMapOutput = output;
}
 
Example #15
Source File: Utils.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
public static Bitmap blurBitmap(Context context, Bitmap bmp, float radius) {
    Bitmap out = Bitmap.createBitmap(bmp);
    RenderScript rs = RenderScript.create(context);
    radius = Math.min(Math.max(radius, 0), 25);

    Allocation input = Allocation.createFromBitmap(
            rs, bmp, MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());

    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);
    script.setRadius(radius);
    script.forEach(output);

    output.copyTo(out);

    rs.destroy();
    return out;
}
 
Example #16
Source File: BlurTransform.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(20);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    bitmap.recycle();

    return blurredBitmap;
}
 
Example #17
Source File: ImageUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * renderScript模糊图片
 * <p>API大于17</p>
 *
 * @param src     源图片
 * @param radius  模糊半径(0...25)
 * @return 模糊后的图片
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(Utils.getContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation
                .USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
Example #18
Source File: ImageUtils.java    From PrivacyStreams with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(UQI uqi, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(uqi.getContext());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example #19
Source File: FeatureMap.java    From rscnn with MIT License 6 votes vote down vote up
private static void copyToAllocation(float[][][][] input, Allocation allocation)
{
    int n = input.length;
    int h = input[0].length;
    int w = input[0][0].length;
    int c = input[0][0][0].length;

    float[] output = new float[n * h * w * c];


    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<h;j++){
            for(int k=0;k<w;k++){
                for(int l=0;l<c;l++){
                    output[count++] = input[i][j][k][l];
                }
            }
        }
    }
    allocation.copyFrom(output);
}
 
Example #20
Source File: FeatureMap.java    From rscnn with MIT License 6 votes vote down vote up
private static void copyToAllocation(float[][] input, Allocation allocation)
{
    int n = input.length;
    int c = input[0].length;

    float[] output = new float[n * c];


    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<c;j++){
            output[count++] = input[i][j];
        }
    }
    allocation.copyFrom(output);
}
 
Example #21
Source File: OtsuScript.java    From retroboy with MIT License 6 votes vote down vote up
@Override
public Bitmap apply(Bitmap input) {
       Allocation imagebuf = Allocation.createFromBitmap(
       	_renderContext, input, 
       	Allocation.MipmapControl.MIPMAP_NONE, 
       	Allocation.USAGE_SCRIPT);

       _filter.set_gIn(imagebuf);
       _filter.set_gOut(imagebuf);
       _filter.set_gScript(_filter);
       _filter.invoke_filter();
       
       // Reuse the input bitmap
       imagebuf.copyTo(input);
       return input;
}
 
Example #22
Source File: Utils.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * Applies a blur to a {@link Bitmap}.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap fastBlur(Context mContext, Bitmap sentBitmap, final int radius) {
    if (null == rs) {
        rs = RenderScript.create(mContext);
    }

    final Allocation in = Allocation.createFromBitmap(rs, sentBitmap,
            Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation out = Allocation.createTyped(rs, in.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(in);
    script.forEach(out);
    out.copyTo(sentBitmap);
    return (sentBitmap);
}
 
Example #23
Source File: RSGaussianBlurTransformation.java    From picasso-transformations with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    if (Build.VERSION.SDK_INT < 17) {
        return source;
    }
    
    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, source, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(mRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(source);
    return source;
}
 
Example #24
Source File: Pooling.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap() {
    FeatureMap input = (FeatureMap) featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();
    scriptPooling.set_in_blob(frameAllocation);
    scriptPooling.set_out_blob(outAllocation);
    int outputChannelAligned = getOutputChannelAligned();
    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(0, outputChannelAligned / 4).setY(0, outputShape[1] * outputShape[2]);

    if(globalPooling){
        options.setX(0, outputChannelAligned / 4).setY(0, 1);
        scriptPooling.forEach_global_pooling_2d(options);
        float[][][][] data = input.getData();
        float[][][][] data2 = output.getData();
        int len = data.length;
    }
    else if(kernelType.equalsIgnoreCase("MAX")) {
        scriptPooling.forEach_max_pooling_2d(options);
    }
    else if(kernelType.equalsIgnoreCase("AVE")){
        scriptPooling.forEach_mean_pooling_2d(options);
    }
}
 
Example #25
Source File: CameraSource.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    in.copyFrom(nv21);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.setInput(in);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        yuvToRgbIntrinsic.forEach(out);
    }
    return out;
}
 
Example #26
Source File: InnerProduct.java    From rscnn with MIT License 6 votes vote down vote up
@Override
public void computeFeatureMap(){
    FeatureMap input = (FeatureMap) featureMapInput[0];
    Allocation frameAllocation = input.getFeatureMap();
    FeatureMap output = (FeatureMap) featureMapOutput;
    Allocation outAllocation = output.getFeatureMap();
    innerProductScript.set_In_Blob(frameAllocation);
    innerProductScript.set_Out_Blob(outAllocation);
    Script.LaunchOptions option = new Script.LaunchOptions();
    if(numOutput % 128==0){
        int thread_group = 1;
        option.setX(0, outputShape[0] * numOutput / 4 / thread_group);
        //option.setY(0, numOutput / 4 / thread_group);
        innerProductScript.set_thread_group(thread_group);
        innerProductScript.forEach_compute_f8fn_1(option);
    }
    else{
        option.setX(0, outputShape[0]);
        option.setY(0, numOutput);
        innerProductScript.forEach_compute_f8f1(option);
    }
}
 
Example #27
Source File: BlurBuilder.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap blur_real(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example #28
Source File: FeatureMap.java    From rscnn with MIT License 6 votes vote down vote up
private static float[][] copyFromAllocation2DVector4(Allocation allocation, int n, int c)
{
    int ca = c;
    if(c % 4 !=0){
        ca = c + 4 - c % 4;
    }
    float[] alloc = new float[n * ca];
    float[][]output = new float[n][c];

    allocation.copyTo(alloc);
    int count = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<ca;j++){
            if(j >= c){
                count++;
                continue;
            }
            output[i][j] = alloc[count++];
        }

    }
    return output;
}
 
Example #29
Source File: BlurTransformation.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    //分配用于渲染脚本的内存
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    //加载我们想要使用的特定脚本的实例。
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    //设置模糊半径
    script.setRadius(radius);

    // Start the ScriptIntrinsicBlur
    //启动 ScriptIntrinsicBlur,
    script.forEach(output);

    // Copy the output to the blurred bitmap
    //将输出复制到模糊的位图
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example #30
Source File: BlurBuilder.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap blur(Context ctx, @NonNull Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);
    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
    RenderScript rs = RenderScript.create(ctx);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}