Java Code Examples for android.support.v8.renderscript.Allocation#copyFrom()

The following examples show how to use android.support.v8.renderscript.Allocation#copyFrom() . 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: Convolution2D.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public void loadModel(String path) throws IOException {
    mInputStream = mContext.getAssets().open(path + "/W", AssetManager.ACCESS_BUFFER);
    ByteBuffer bb = readInput(mInputStream);
    FloatBuffer.wrap(W).put(bb.asFloatBuffer());

    // padding for GPU BLAS when necessary.
    int W_height_input = in_channels * ksize * ksize;
    if (padded_Y_blas == W_height_input) {
        // If the input width already satisfies the requirement, just copy to the Allocation.
        W_alloc.copyFrom(W);
    } else {
        // If not, a temp allocation needs to be created.
        Allocation input = Allocation.createTyped(mRS,
                Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
        input.copyFrom(W);
        W_alloc.copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
    }

    mInputStream = mContext.getAssets().open(path + "/b", AssetManager.ACCESS_BUFFER);
    bb = readInput(mInputStream);
    FloatBuffer.wrap(b).put(bb.asFloatBuffer());
    b_alloc.copyFrom(b);

    mInputStream.close();
    Log.v(TAG, "Convolution2D loaded: " + b[0]);
}
 
Example 2
Source File: Convolution2DTiled.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public void loadModel(String path) throws IOException {
    mInputStream = mContext.getAssets().open(path + "/W", AssetManager.ACCESS_BUFFER);
    ByteBuffer bb = readInput(mInputStream);
    FloatBuffer.wrap(W).put(bb.asFloatBuffer());

    // padding for GPU BLAS when necessary.
    int W_height_input = in_channels * ksize * ksize;
    if (padded_Y_blas == W_height_input) {
        // If the input width already satisfies the requirement, just copy to the Allocation.
        W_alloc.copyFrom(W);
    } else {
        // If not, a temp allocation needs to be created.
        Allocation input = Allocation.createTyped(mRS,
                Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
        input.copyFrom(W);
        W_alloc.copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
    }

    mInputStream = mContext.getAssets().open(path + "/b", AssetManager.ACCESS_BUFFER);
    bb = readInput(mInputStream);
    FloatBuffer.wrap(b).put(bb.asFloatBuffer());
    b_alloc.copyFrom(b);

    mInputStream.close();
    Log.v(TAG, "Convolution2D loaded: " + b[0]);
}
 
Example 3
Source File: YuvToRgb.java    From easyrs with MIT License 6 votes vote down vote up
/**
 * Converts a NV21 image to a Bitmap.
 * @param nv21Image the NV21 image to convert.
 */
public static Bitmap yuvToRgb(RenderScript rs, Nv21Image nv21Image) {
    long startTime = System.currentTimeMillis();

    Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.U8(rs))
            .setX(nv21Image.nv21ByteArray.length);
    Type yuvType = yuvTypeBuilder.create();
    Allocation yuvAllocation = Allocation.createTyped(rs, yuvType, Allocation.USAGE_SCRIPT);
    yuvAllocation.copyFrom(nv21Image.nv21ByteArray);

    Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
    rgbTypeBuilder.setX(nv21Image.width);
    rgbTypeBuilder.setY(nv21Image.height);
    Allocation rgbAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create());

    ScriptIntrinsicYuvToRGB yuvToRgbScript = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
    yuvToRgbScript.setInput(yuvAllocation);
    yuvToRgbScript.forEach(rgbAllocation);

    Bitmap bitmap = Bitmap.createBitmap(nv21Image.width, nv21Image.height, Bitmap.Config.ARGB_8888);
    rgbAllocation.copyTo(bitmap);

    Log.d("NV21", "Conversion to Bitmap: " + (System.currentTimeMillis() - startTime) + "ms");
    return bitmap;
}
 
Example 4
Source File: RSStackBlur.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@Override
  public Bitmap blur(int radius, Bitmap blurred) {
radius = Math.min(radius,25);

int width = blurred.getWidth();
int height = blurred.getHeight();

ScriptC_stackblur blurScript = new ScriptC_stackblur(_rs, ctx.getResources(), R.raw.stackblur);

Allocation inAllocation = Allocation.createFromBitmap(_rs, blurred);

blurScript.set_gIn(inAllocation);
blurScript.set_width(width);
blurScript.set_height(height);
blurScript.set_radius(radius);

int[] row_indices = new int[height];
for (int i = 0; i < height; i++) {
	row_indices[i] = i;
}

Allocation rows = Allocation.createSized(_rs, Element.U32(_rs), height, Allocation.USAGE_SCRIPT);
rows.copyFrom(row_indices);

row_indices = new int[width];
for (int i = 0; i < width; i++) {
	row_indices[i] = i;
}

Allocation columns = Allocation.createSized(_rs, Element.U32(_rs), width, Allocation.USAGE_SCRIPT);
columns.copyFrom(row_indices);

blurScript.forEach_blur_h(rows);
blurScript.forEach_blur_v(columns);
inAllocation.copyTo(blurred);

return blurred;

  }
 
Example 5
Source File: RSBlurProcess.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap blur(Bitmap original, float radius) {
	int width = original.getWidth();
	int height = original.getHeight();
	Bitmap blurred = original.copy(Bitmap.Config.ARGB_8888, true);

	ScriptC_blur blurScript = new ScriptC_blur(_rs, context.getResources(), R.raw.blur);

	Allocation inAllocation = Allocation.createFromBitmap(_rs, blurred, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

	blurScript.set_gIn(inAllocation);
	blurScript.set_width(width);
	blurScript.set_height(height);
	blurScript.set_radius((int) radius);

	int[] row_indices = new int[height];
	for (int i = 0; i < height; i++) {
		row_indices[i] = i;
	}

	Allocation rows = Allocation.createSized(_rs, Element.U32(_rs), height, Allocation.USAGE_SCRIPT);
	rows.copyFrom(row_indices);

	row_indices = new int[width];
	for (int i = 0; i < width; i++) {
		row_indices[i] = i;
	}

	Allocation columns = Allocation.createSized(_rs, Element.U32(_rs), width, Allocation.USAGE_SCRIPT);
	columns.copyFrom(row_indices);

	blurScript.forEach_blur_h(rows);
	blurScript.forEach_blur_v(columns);
	inAllocation.copyTo(blurred);

	return blurred;
}
 
Example 6
Source File: ResidualBlockChained.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public void loadModel(String path) throws IOException {
    for (int i = 0; i < mNumBlocks; i++) {
        for (int j = 0; j < 2; j++) {
            // Read all convolution blocks.
            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/c" + (j + 1) + "/W", AssetManager.ACCESS_BUFFER);
            ByteBuffer bb = readInput(mInputStream);
            FloatBuffer.wrap(W).put(bb.asFloatBuffer());

            // padding for GPU BLAS
            int W_height_input = in_channels * ksize * ksize;
            if (padded_Y_blas == W_height_input) {
                // If the input width already satisfies the requirement, just copy to the Allocation.
                W_alloc[i * 2 + j].copyFrom(W);
            } else {
                // If not, a temp allocation needs to be created.
                Allocation input = Allocation.createTyped(mRS,
                        Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
                input.copyFrom(W);
                W_alloc[i * 2 + j].copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
            }

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/c" + (j + 1) + "/b", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(b).put(bb.asFloatBuffer());
            b_alloc[i * 2 + j].copyFrom(b);

            // Read all batch normalization blocks;
            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/gamma", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(gamma).put(bb.asFloatBuffer());
            gamma_alloc[i * 2 + j].copyFrom(gamma);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/beta", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(beta).put(bb.asFloatBuffer());
            beta_alloc[i * 2 + j].copyFrom(beta);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/avg_mean", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(avg_mean).put(bb.asFloatBuffer());
            avg_mean_alloc[i * 2 + j].copyFrom(avg_mean);

            mInputStream = mContext.getAssets().open(path + "/r" + (i + 1) + "/b" + (j + 1) + "/avg_var", AssetManager.ACCESS_BUFFER);
            bb = readInput(mInputStream);
            FloatBuffer.wrap(avg_var).put(bb.asFloatBuffer());
            avg_var_alloc[i * 2 + j].copyFrom(avg_var);

        }

    }
    mInputStream.close();
    Log.v(TAG, "ResidualBlockChained loaded: " + b[0]);
}