@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) public Bitmap thirteen(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 ScriptIntrinsicConvolve3x3 convolve1 = ScriptIntrinsicConvolve3x3.create(renderScript, Element.U8_4(renderScript)); convolve1.setInput(inputAllocation); convolve1.setCoefficients(new float[] { -2, -1, 0, -1, 1, 1, 0, 1, 2 }); convolve1.forEach(outputAllocation); outputAllocation.copyTo(outBitmap); return outBitmap; }
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) public Bitmap fourteen(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 ScriptIntrinsicConvolve3x3 convolve2 = ScriptIntrinsicConvolve3x3.create(renderScript, Element.U8_4(renderScript)); convolve2.setInput(inputAllocation); convolve2.setCoefficients(new float[] { .2f, .3f, .2f, .1f, .1f, .1f, .2f, .3f, .2f, }); convolve2.forEach(outputAllocation); outputAllocation.copyTo(outBitmap); return outBitmap; }
private Bitmap convolve(Bitmap original, float[] coefficients) { Bitmap bitmap = Bitmap.createBitmap( original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888); RenderScript rs = RenderScript.create(this); Allocation allocIn = Allocation.createFromBitmap(rs, original); Allocation allocOut = Allocation.createFromBitmap(rs, bitmap); ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs)); convolution.setInput(allocIn); convolution.setCoefficients(coefficients); convolution.forEach(allocOut); allocOut.copyTo(bitmap); rs.destroy(); return bitmap; }
/** * Sharpens the Bitmap * * @param context * context * @param bitmap * input bitmap * @param weight * Should >= 0. 0 is default * @return new bitmap */ public static Bitmap sharpenBitmap(Context context, Bitmap bitmap, float weight) { RenderScript mRS = RenderScript.create(context); Allocation mInAllocation = Allocation.createFromBitmap(mRS, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); Allocation mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType()); ScriptIntrinsicConvolve3x3 d = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); d.setCoefficients(new float[] { -weight, -weight, -weight, -weight, 8 * weight + 1, -weight, -weight, -weight, -weight }); d.setInput(mInAllocation); d.forEach(mOutAllocation); mOutAllocation.copyTo(bitmap); mRS.destroy(); return bitmap; }
private void example() { RenderScript mRS = RenderScript.create(this); // Loads input image Bitmap inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.houseimage); // Defines all allocations Allocation inputAllocation = Allocation.createFromBitmap(mRS, inputImage); Allocation convolvedAllocation = Allocation.createTyped(mRS, inputAllocation.getType()); // This are the bounds of execution for the allocation (specifically for this example, // the area is where the house is in the image). // This is the start position of the convolution int outIndexX = 253; int outIndexY = 81; // This is the extension of the convolution [x, x+width) -> [outIndexX, outIndexX + outSizeX) int outSizeX = 59; int outSizeY = 56; Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS)); tb.setX(outSizeX); tb.setY(outSizeY); Allocation outputAllocation = Allocation.createTyped(mRS, tb.create()); // --- Pre fill output allocation with black color for debug purposes ScriptC_main scriptC_main = new ScriptC_main(mRS); scriptC_main.forEach_fillWithBlack(convolvedAllocation); scriptC_main.forEach_fillWithBlack(outputAllocation); // --- Convolution section // Defines the execution limits Script.LaunchOptions launchOptions = new Script.LaunchOptions(); launchOptions.setX(outIndexX, outIndexX + outSizeX); launchOptions.setY(outIndexY, outIndexY + outSizeY); // Define the convolution ScriptIntrinsicConvolve3x3 convolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, Element.RGBA_8888(mRS)); // Some coefficients float[] coefficients = { 0.7f, 0, 0.5f, 0, 1.0f, 0, 0.5f, 0, 1.0f }; convolve3x3.setCoefficients(coefficients); // Execute the allocation with limits convolve3x3.setInput(inputAllocation); convolve3x3.forEach(convolvedAllocation, launchOptions); // --- Output section, copies the convolution result to outputAllocation scriptC_main.set_inputAllocation(convolvedAllocation); scriptC_main.set_inputOffsetX(outIndexX); scriptC_main.set_inputOffsetY(outIndexY); scriptC_main.forEach_copyAllocation(outputAllocation); // --- Display all stages Bitmap intermediateImage = Bitmap.createBitmap(inputImage.getWidth(), inputImage.getHeight(), Bitmap.Config.ARGB_8888); Bitmap outputImage = Bitmap.createBitmap(outSizeX, outSizeY, Bitmap.Config.ARGB_8888); convolvedAllocation.copyTo(intermediateImage); outputAllocation.copyTo(outputImage); ((ImageView) findViewById(R.id.imageView)).setImageBitmap(inputImage); ((ImageView) findViewById(R.id.imageView2)).setImageBitmap(intermediateImage); ((ImageView) findViewById(R.id.imageView3)).setImageBitmap(outputImage); }