Java Code Examples for android.support.v8.renderscript.Allocation#createFromBitmap()
The following examples show how to use
android.support.v8.renderscript.Allocation#createFromBitmap() .
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: MainActivity.java From renderscript-samples with Apache License 2.0 | 6 votes |
private void createScript() { mRS = RenderScript.create(this); mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn); mOutAllocations = new Allocation[NUM_BITMAPS]; for (int i = 0; i < NUM_BITMAPS; ++i) { mOutAllocations[i] = Allocation.createFromBitmap(mRS, mBitmapsOut[i]); } // Create intrinsics. // RenderScript has built-in features such as blur, convolve filter etc. // These intrinsics are handy for specific operations without writing RenderScript kernel. // In the sample, it's creating blur, convolve and matrix intrinsics. mScriptBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS)); mScriptConvolve = ScriptIntrinsicConvolve5x5.create(mRS, Element.U8_4(mRS)); mScriptMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); }
Example 2
Source File: BlurBuilder.java From react-native-simple-shadow-view with MIT License | 6 votes |
public static Bitmap blur(Context context, Bitmap image, float blurRadius, float scale) { int width = Math.round(image.getWidth() * scale); int height = Math.round(image.getHeight() * scale); Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); RenderScript rs = RenderScript.create(context); ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); intrinsicBlur.setRadius(blurRadius); intrinsicBlur.setInput(tmpIn); intrinsicBlur.forEach(tmpOut); tmpOut.copyTo(outputBitmap); return outputBitmap; }
Example 3
Source File: BlurBuilder.java From FlyWoo with Apache License 2.0 | 6 votes |
public static Bitmap blur(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 4
Source File: GBlurPic.java From AndroidGaussianBlur with MIT License | 6 votes |
public Bitmap gBlurBitmap(Bitmap bitmap, float radius) { if (mBitmap != null) { mBitmap.recycle(); mBitmap = null; } mBitmap = bitmap.copy(bitmap.getConfig(), true); mInAllocation = null; mOutAllocation = null; mInAllocation = Allocation.createFromBitmap(mRS, mBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType()); mBlur.setRadius(radius); mBlur.setInput(mInAllocation); mBlur.forEach(mOutAllocation); mOutAllocation.copyTo(mBitmap); return mBitmap; }
Example 5
Source File: RSGaussian5x5Blur.java From BlurView with Apache License 2.0 | 6 votes |
@Override public Bitmap blur(int radius, Bitmap bitmapOriginal) { radius = Math.min(radius,25); Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicConvolve5x5 script = ScriptIntrinsicConvolve5x5.create(rs, Element.U8_4(rs)); script.setCoefficients(BlurKernels.GAUSSIAN_5x5); for (int i = 0; i < radius; i++) { script.setInput(input); script.forEach(output); input = output; } output.copyTo(bitmapOriginal); return bitmapOriginal; }
Example 6
Source File: RSBox5x5Blur.java From BlurView with Apache License 2.0 | 6 votes |
@Override public Bitmap blur(int radius, Bitmap bitmapOriginal) { radius = Math.min(radius,25); Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicConvolve5x5 script = ScriptIntrinsicConvolve5x5.create(rs, Element.U8_4(rs)); script.setCoefficients(BlurKernels.BOX_5x5); for (int i = 0; i < radius; i++) { script.setInput(input); script.forEach(output); input = output; } output.copyTo(bitmapOriginal); return bitmapOriginal; }
Example 7
Source File: ColorMatrixTest.java From easyrs with MIT License | 5 votes |
@NonNull private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21, Op op) { Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21); Allocation aout = Allocation.createTyped(rs, ain.getType()); ScriptIntrinsicColorMatrix colorMatrixScript = ScriptIntrinsicColorMatrix.create( rs, ain.getElement()); if (op == Op.GRAYSCALE) colorMatrixScript.setGreyscale(); if (op == Op.RGB_TO_YUV) { colorMatrixScript.setRGBtoYUV(); colorMatrixScript.setAdd(0.0f, 0.5f, 0.5f, 0.0f); } if (op == Op.MATRIX3F) { colorMatrixScript.setColorMatrix(MATRIX3F); colorMatrixScript.setAdd(ADD_TERMS); } if (op == Op.MATRIX4F) { colorMatrixScript.setColorMatrix(MATRIX4F); colorMatrixScript.setAdd(ADD_TERMS); } colorMatrixScript.forEach(ain, aout); Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig()); aout.copyTo(expectedBitmap); return expectedBitmap; }
Example 8
Source File: Lut3DTest.java From easyrs with MIT License | 5 votes |
@NonNull private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21) { Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21); Allocation aout = Allocation.createTyped(rs, ain.getType()); ScriptIntrinsic3DLUT script3dLut = ScriptIntrinsic3DLUT.create(rs, ain.getElement()); script3dLut.setLUT(SampleParams.Lut3D.swapRedAndBlueCube().createAllocation(rs)); script3dLut.forEach(ain, aout); Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig()); aout.copyTo(expectedBitmap); return expectedBitmap; }
Example 9
Source File: DefaultBlur.java From ngAndroid with Apache License 2.0 | 5 votes |
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 10
Source File: BlurBuilder.java From talk-android with MIT License | 5 votes |
public static void RsBlur(RenderScript rs, Bitmap original, float radius) { // use this constructor for best performance, because it uses USAGE_SHARED mode which // reuses memory final Allocation input = Allocation.createFromBitmap(rs, original); 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(original); rs.destroy(); }
Example 11
Source File: RenderScriptBlurHelper.java From BlurDialogFragment with Apache License 2.0 | 5 votes |
/** * blur a given bitmap * * @param sentBitmap bitmap to blur * @param radius blur radius * @param canReuseInBitmap true if bitmap must be reused without blur * @param context used by RenderScript, can be null if RenderScript disabled * @return blurred bitmap */ public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) { Bitmap bitmap; if (canReuseInBitmap) { bitmap = sentBitmap; } else { bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); } if (bitmap.getConfig() == Bitmap.Config.RGB_565) { // RenderScript hates RGB_565 so we convert it to ARGB_8888 // (see http://stackoverflow.com/questions/21563299/ // defect-of-image-with-scriptintrinsicblur-from-support-library) bitmap = convertRGB565toARGB888(bitmap); } try { final RenderScript rs = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap(rs, bitmap, 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); return bitmap; } catch (RSRuntimeException e) { Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 " + "continue with the FastBlur approach."); } return null; }
Example 12
Source File: Blend.java From easyrs with MIT License | 5 votes |
public static BaseSetup createFromBitmap(RenderScript rs, Bitmap srcBitmap, Bitmap dstBitmap) { RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, srcBitmap); Allocation aout = Allocation.createFromBitmap(bitmapRSContext.rs, dstBitmap); ScriptIntrinsicBlend blendScript = ScriptIntrinsicBlend.create( bitmapRSContext.rs, bitmapRSContext.ain.getElement()); return new BaseSetup(bitmapRSContext, aout, blendScript); }
Example 13
Source File: MainActivity.java From ImageCropRotateFilter with MIT License | 5 votes |
private void updateDisplay() { if (mFilterListView.isShown()) { mFilter.update(mBitmapOut); mOriginalDisplayView.invalidate(); return; } if (mRotateListView.isShown()) { mOriginalDisplayView.setImageBitmap(mBitmapOut); mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn); mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut); mOriginalDisplayView.invalidate(); return; } }
Example 14
Source File: BlurTransformation.java From Sky31Radio with Apache License 2.0 | 5 votes |
@Override public Bitmap transform(Bitmap source) { Timber.i("start bitmap transform"); try { float radius = 10f; Bitmap outputBitmap; // if(Build.VERSION.SDK_INT >= 17){ outputBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); RenderScript rs = RenderScript.create(ctx); ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation tmpIn = Allocation.createFromBitmap(rs, source); Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); sib.setRadius(radius); sib.setInput(tmpIn); sib.forEach(tmpOut); tmpOut.copyTo(outputBitmap); source.recycle(); // }else{ // outputBitmap = FastBlur.doBlur(source, (int) radius, true); // } Timber.d("blur bitmap success"); return outputBitmap; } catch (Exception e) { Timber.e(e, "occur an error during blurring bitmap"); return source; } }
Example 15
Source File: MainActivity.java From renderscript-samples with Apache License 2.0 | 5 votes |
/** * Initialize RenderScript. * * <p>In the sample, it creates RenderScript kernel that performs saturation manipulation.</p> */ private void createScript() { // Initialize RS RenderScript rs = RenderScript.create(this); // Allocate buffers mInAllocation = Allocation.createFromBitmap(rs, mBitmapIn); mOutAllocations = new Allocation[NUM_BITMAPS]; for (int i = 0; i < NUM_BITMAPS; ++i) { mOutAllocations[i] = Allocation.createFromBitmap(rs, mBitmapsOut[i]); } // Load script mScript = new ScriptC_saturation(rs); }
Example 16
Source File: RSStackBlur.java From BlurView with Apache License 2.0 | 5 votes |
@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 17
Source File: Nv21Image.java From easyrs with MIT License | 4 votes |
/** * Converts an android Bitmap image to NV21 format. If the image has odd dimensions the * conversion process will round down each dimension to its closest even integer. * @param dstArray is an optional byte array to receive the converted NV21 data. It * must be (1.5 * number_of_pixels) bytes long. If null is passed, * a new byte array will be created and returned. */ public static Nv21Image bitmapToNV21(RenderScript rs, Bitmap bitmap, byte[] dstArray) { long startTime = System.currentTimeMillis(); Bitmap croppedBitmap = bitmap; if (bitmap.getWidth() % 2 > 0 || bitmap.getHeight() % 2 > 0) { croppedBitmap = Bitmap.createBitmap(bitmap, 0, 0, (bitmap.getWidth() / 2) * 2, (bitmap.getHeight() / 2) * 2); } Bitmap yuvImage = ColorMatrix.applyMatrix(rs, croppedBitmap, ColorMatrixParams.rgbToNv21Matrix(), new Float4(0.0f, 0.5f, 0.5f, 0.0f)); RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, yuvImage); ScriptC_channel channelScript = new ScriptC_channel(bitmapRSContext.rs); Type outType = Type.createXY(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs), yuvImage.getWidth(), yuvImage.getHeight()); Allocation aout = Allocation.createTyped(bitmapRSContext.rs, outType); channelScript.forEach_channelR(bitmapRSContext.ain, aout); int size = croppedBitmap.getWidth() * croppedBitmap.getHeight(); byte[] yByteArray; if (dstArray == null) yByteArray = new byte[size + size / 2]; else yByteArray = dstArray; aout.copyTo(yByteArray); Bitmap.Config config = yuvImage.getConfig(); Bitmap resizedBmp = Bitmap.createBitmap(yuvImage.getWidth()/2, yuvImage.getHeight()/2, config); Type resizeoutType = Type.createXY(bitmapRSContext.rs, bitmapRSContext.ain.getElement(), yuvImage.getWidth()/2, yuvImage.getHeight()/2); Allocation resizeaout = Allocation.createTyped(bitmapRSContext.rs, resizeoutType); ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(bitmapRSContext.rs); resizeScript.setInput(bitmapRSContext.ain); resizeScript.forEach_bicubic(resizeaout); resizeaout.copyTo(resizedBmp); Allocation resizedIn = Allocation.createFromBitmap(bitmapRSContext.rs, resizedBmp); ScriptC_uvencode encodeScript = new ScriptC_uvencode(bitmapRSContext.rs); Type uvtype = Type.createX(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs), size / 2); Allocation uvAllocation = Allocation.createTyped(bitmapRSContext.rs, uvtype); encodeScript.set_width(yuvImage.getWidth()); encodeScript.set_height(yuvImage.getHeight()); encodeScript.set_gOut(uvAllocation); encodeScript.forEach_root(resizedIn); byte[] uvByteArray = new byte[size / 2]; uvAllocation.copyTo(uvByteArray); System.arraycopy(uvByteArray, 0, yByteArray, size, uvByteArray.length); Log.d("NV21", "Conversion to NV21: " + (System.currentTimeMillis() - startTime) + "ms"); return new Nv21Image(yByteArray, yuvImage.getWidth(), yuvImage.getHeight()); }
Example 18
Source File: RSToolboxContext.java From easyrs with MIT License | 4 votes |
public static RSToolboxContext createFromBitmap(RenderScript rs, Bitmap bitmap) { Allocation ain = Allocation.createFromBitmap(rs, bitmap); Element bitmapElement = ain.getElement(); return new RSToolboxContext(rs, ain, bitmapElement); }
Example 19
Source File: FastStyleModelTiled.java From style-transfer with Apache License 2.0 | 4 votes |
public Bitmap processImage(Bitmap bitmap) { ScriptC_network script = new ScriptC_network(mRS); int numElements = 100; Element floatElement = Element.I32(mRS); Type arrayType = Type.createX(mRS, floatElement, numElements); Allocation inputAlloc = Allocation.createTyped(mRS, arrayType); Allocation outputAlloc = Allocation.createTyped(mRS, arrayType); script.forEach_mapper(inputAlloc, outputAlloc); int[] output = new int[numElements]; outputAlloc.copyTo(output); Log.i(TAG, output[0] + " " + output[99]); if (!mLoaded) { try { loadModel(); } catch (IOException e) { } } int height = bitmap.getHeight(); int width = bitmap.getWidth(); // Crop the image. Bitmap outImgBig = Bitmap.createBitmap(bitmap, (width - MAX_IMG_SIZE) / 2, (height - MAX_IMG_SIZE) / 2, MAX_IMG_SIZE, MAX_IMG_SIZE); // Process the cropped image through the neural net. Allocation outImgBigAlloc = processImgChunk(outImgBig); // Blur the output image a bit. Allocation blurredAlloc = Allocation.createFromBitmap(mRS, outImgBig); ScriptIntrinsicBlur mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS)); mBlur.setInput(outImgBigAlloc); mBlur.setRadius(1.5f); mBlur.forEach(blurredAlloc); blurredAlloc.copyTo(outImgBig); // outImgBigAlloc.copyTo(outImgBig); ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); float[] matrix_sharpen = { 0, -1, 0, -1, 5, -1, 0, -1, 0}; convolution.setInput(blurredAlloc); convolution.setCoefficients(matrix_sharpen); convolution.forEach(outImgBigAlloc); outImgBigAlloc.copyTo(outImgBig); logBenchmarkResult(); return outImgBig; }
Example 20
Source File: FastStyleModel.java From style-transfer with Apache License 2.0 | 4 votes |
private Allocation processImgChunk(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getWidth(); mImg2Alloc.set_height(height); mImg2Alloc.set_weight(width); Bitmap outImg = Bitmap.createBitmap(bitmap); // RGB bitmap Allocation. Allocation imgAlloc = Allocation.createFromBitmap(mRS, bitmap); mImg2Alloc.set_img_alloc(imgAlloc); // Float input Allocation. Allocation result = Allocation.createTyped(mRS, Type.createXY(mRS, Element.F32(mRS), height * width, 3)); // convert the bitmap to 3 * (h * w) float Allocation; mImg2Alloc.forEach_img2alloc(result); // Actual computation; // 1st Convolution layer. result = mConvLayer[0].process(result, height, width); // Use ELU for activation. mActivation.forEach_elu(result, result); // 1st Batch Normalization. mBatchNormLayer[0].process(result); // 2nd Convolution layer. result = mConvLayer[1].process(result, mConvLayer[0].outH, mConvLayer[0].outW); mActivation.forEach_elu(result, result); // 2nd Batch Normalization. mBatchNormLayer[1].process(result); // 3rd Convolution layer. result = mConvLayer[2].process(result, mConvLayer[1].outH, mConvLayer[1].outW); mActivation.forEach_elu(result, result); // 3rd Batch Normalization. mBatchNormLayer[2].process(result); // Process through 5 consecutive residual blocks. result = mResidualLayer[0].process(result, mConvLayer[2].outH, mConvLayer[2].outW); result = mResidualLayer[1].process(result, mResidualLayer[0].outH, mResidualLayer[0].outW); result = mResidualLayer[2].process(result, mResidualLayer[1].outH, mResidualLayer[1].outW); result = mResidualLayer[3].process(result, mResidualLayer[2].outH, mResidualLayer[2].outW); result = mResidualLayer[4].process(result, mResidualLayer[3].outH, mResidualLayer[3].outW); // 1st Deconvolution layer. result = mDeconvLayer[0].process(result, mResidualLayer[4].outH, mResidualLayer[4].outW); mActivation.forEach_elu(result, result); // 4th Batch Normalization. mBatchNormLayer[3].process(result); // 2nd Deconvolution layer. result = mDeconvLayer[1].process(result, mDeconvLayer[0].outH, mDeconvLayer[0].outW); mActivation.forEach_elu(result, result); // 5th Batch Normalization. mBatchNormLayer[4].process(result); // 3rd Deconvolution layer. result = mDeconvLayer[2].process(result, mDeconvLayer[1].outH, mDeconvLayer[1].outW); // Convert floating point result to RGB image. mImg2Alloc.set_nn_alloc(result); Allocation outAlloc = Allocation.createFromBitmap(mRS, outImg); mImg2Alloc.forEach_alloc2img(outAlloc); return outAlloc; }