public static Type createType(RenderScript rs, Element e, int x, int y) { if (Build.VERSION.SDK_INT >= 21) { return Type.createXY(rs, e, x, y); } else { return new Type.Builder(rs, e).setX(x).setY(y).create(); } }
@RequiresApi(18) public static Type createYuvType(RenderScript rs, int x, int y, int yuvFormat) { boolean supported = yuvFormat == ImageFormat.NV21 || yuvFormat == ImageFormat.YV12; if (Build.VERSION.SDK_INT >= 19) { supported |= yuvFormat == ImageFormat.YUV_420_888; } if (!supported) { throw new IllegalArgumentException("invalid yuv format: " + yuvFormat); } return new Type.Builder(rs, createYuvElement(rs)).setX(x).setY(y).setYuvFormat(yuvFormat) .create(); }
/** * This function only assumes mPointsXY, mPasteOffX, mPasteOffY * * @param healing * @param rs * @param image */ public void heal(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) { long time = System.nanoTime(); Type.Builder floatImage = new Type.Builder(rs, Element.F32_3(rs)); floatImage.setX(mRoiBounds.width()); floatImage.setY(mRoiBounds.height()); Bitmap mask_bitmap = buildMask(mRoiBounds, mPointsXY); Bitmap dest_bitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top, mRoiBounds.width(), mRoiBounds.height()); Allocation dest_alloc = Allocation.createFromBitmap(rs, dest_bitmap); Bitmap src_bitmap = createMutableBitmap(image, mCutOffsetX, mCutOffsetY, mRoiBounds.width(), mRoiBounds.height()); Allocation src_alloc = Allocation.createFromBitmap(rs, src_bitmap); Allocation mask_alloc = Allocation.createFromBitmap(rs, mask_bitmap); healing.invoke_heal(mask_alloc, src_alloc, dest_alloc); dest_alloc.copyTo(dest_bitmap); dest_bitmap.setHasAlpha(true); // build the undo mUndoBitmap = Bitmap.createBitmap(mRoiBounds.width(), mRoiBounds.height(), Bitmap.Config.ARGB_8888); Canvas undoCanvas = new Canvas(mUndoBitmap); Rect undoRect = new Rect(0, 0, mRoiBounds.width(), mRoiBounds.height()); undoCanvas.drawBitmap(output, mRoiBounds, undoRect, null); Canvas c = new Canvas(output); c.drawBitmap(image, 0, 0, null); c.drawBitmap(dest_bitmap, mRoiBounds.left, mRoiBounds.top, null); Log.v(TAG, " time ss to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms"); heal_orig(healing, rs, image, output); }
Allocation allocFloat2(float[] p, RenderScript rs) { Type.Builder builderF32_2 = new Type.Builder(rs, Element.F32_2(rs)); builderF32_2.setX(p.length / 2); Allocation ret = Allocation.createTyped(rs, builderF32_2.create()); ret.copyFrom(p); return ret; }
private void initializeParamsAllocation() { Type wInType = Type.createXY(mRs, Element.F32(mRs), hidden_units, inDim); Allocation wInAlloc = Allocation.createTyped(mRs, wInType); wInAlloc.copyFrom(convertedWIn); scriptC_main.set_w_in(wInAlloc); Allocation bInAlloc = Allocation.createSized(mRs, Element.F32(mRs), hidden_units); bInAlloc.copyFrom(b_in); scriptC_main.set_b_in(bInAlloc); Type wOutType = Type.createXY(mRs, Element.F32(mRs), outDim, hidden_units); Allocation wOutAlloc = Allocation.createTyped(mRs, wOutType); wOutAlloc.copyFrom(convertedWOut); scriptC_main.set_w_out(wOutAlloc); Allocation bOutAlloc = Allocation.createSized(mRs, Element.F32(mRs), outDim); bOutAlloc.copyFrom(b_out); scriptC_main.set_b_out(bOutAlloc); Type weightType = Type.createXYZ(mRs, Element.F32(mRs), hidden_units * 4, hidden_units * 2, layerSize); Allocation weightAlloc = Allocation.createTyped(mRs, weightType); weightAlloc.copyFrom(convertedWeights); scriptC_main.set_weights(weightAlloc); Type biasType = Type.createXY(mRs, Element.F32(mRs), hidden_units * 4, layerSize); Allocation biasAlloc = Allocation.createTyped(mRs, biasType); biasAlloc.copyFrom(convertedBiases); scriptC_main.set_biases(biasAlloc); }
protected void config(){ opt.setX(0,height); Type.Builder builder = new Type.Builder(rs, Element.F32(rs)); builder.setX(height); out = Allocation.createTyped(rs,builder.create()); rsSum.set_g_x_start(0); rsSum.set_g_x_end(width); rsSum.set_g_src(parameter.allocIn); }
private void createAllocOutEstimateErr(){ Type.Builder builder = new Type.Builder(rs, Element.F32(rs)); builder.setX(curRect.width() + 1); builder.setY(curRect.height()+1); Type type = builder.create(); outErr = Allocation.createTyped(rs,type); }
static private void allocGray(){ Type.Builder builder = new Type.Builder(rs,Element.I32(rs)); builder.setX(data.getOriWidth()); builder.setY(data.getOriHeight()); allocGrayBase = Allocation.createTyped(rs,builder.create()); allocGrayCur = Allocation.createTyped(rs,builder.create()); }
static private void allocPartial(){ Type.Builder builder = new Type.Builder(rs,Element.I32(rs)); builder.setX(data.getOriWidth()); builder.setY(data.getOriHeight()); allocBasePartialX = Allocation.createTyped(rs,builder.create()); allocBasePartialY = Allocation.createTyped(rs,builder.create()); }
static private void allocSecPartial(){ Type.Builder builder = new Type.Builder(rs,Element.F32(rs)); builder.setX(data.getOriWidth()); builder.setY(data.getOriHeight()); allocBaseSecPartialX = Allocation.createTyped(rs,builder.create()); allocBaseSecPartialY = Allocation.createTyped(rs,builder.create()); }
@Override protected void createFilter(android.content.res.Resources res, float scaleFactor, int quality, Allocation in) { RenderScript rsCtx = getRenderScriptContext(); Type.Builder tb_float = new Type.Builder(rsCtx, Element.F32_4(rsCtx)); tb_float.setX(in.getType().getX()); tb_float.setY(in.getType().getY()); mScript = new ScriptC_saturation(rsCtx, res, R.raw.saturation); }
@Override protected void createFilter(android.content.res.Resources res, float scaleFactor, int quality, Allocation in) { RenderScript rsCtx = getRenderScriptContext(); Type.Builder tb_float = new Type.Builder(rsCtx, Element.F32_4(rsCtx)); tb_float.setX(in.getType().getX()); tb_float.setY(in.getType().getY()); mScript = new ScriptC_grad(rsCtx, res, R.raw.grad); }
public Camera2Callback() //---------------------- { rs = RenderScript.create(activity); switch (fileFormat) { case YUV_420: YUVToRGBA = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs)); Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs)); yuvTypeBuilder.setX(previewWidth).setY(previewHeight).setYuvFormat(ImageFormat.YUV_420_888); ain = Allocation.createTyped(rs, yuvTypeBuilder.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs)); rgbTypeBuilder.setX(previewWidth).setY(previewHeight); aOut = Allocation.createTyped(rs, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT); break; case NV21: YUVToRGBA = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(previewWidth). setY(previewHeight).setMipmaps(false).setYuvFormat(ImageFormat.NV21); Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(previewWidth). setY(previewHeight).setMipmaps(false); ain = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); aOut = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); break; } }
public byte[] toRGBA(Context context, byte[] frame, int previewWidth, int previewHeight, int rgbaSize, byte[] grey) //---------------------------------------------------------------------------------------------------------------- { try { final int inputFormat = ImageFormat.YUV_420_888; Type.Builder yuvTypeBuilder = new Type.Builder(renderscript, Element.YUV(renderscript)); yuvTypeBuilder.setX(previewWidth).setY(previewHeight).setYuvFormat(inputFormat); Allocation allocYUVIn = Allocation.createTyped(renderscript, yuvTypeBuilder.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbTypeBuilder = new Type.Builder(renderscript, Element.RGBA_8888(renderscript)); rgbTypeBuilder.setX(previewWidth).setY(previewHeight); Allocation allocRGBAOut = Allocation.createTyped(renderscript, rgbTypeBuilder.create(), Allocation.USAGE_SCRIPT); ScriptIntrinsicYuvToRGB YUVToRGB = ScriptIntrinsicYuvToRGB.create(renderscript, Element.RGBA_8888(renderscript)); allocYUVIn.copyFrom(frame); YUVToRGB.setInput(allocYUVIn); YUVToRGB.forEach(allocRGBAOut); byte[] rgbaBuffer = new byte[rgbaSize]; allocRGBAOut.copyTo(rgbaBuffer); if (grey != null) { Type.Builder greyTypeBuilder = new Type.Builder(renderscript, Element.U8(renderscript)); greyTypeBuilder.setX(cameraWidth).setY(cameraHeight); Allocation allocGrayOut = Allocation.createTyped(renderscript, greyTypeBuilder.create(), Allocation.USAGE_SCRIPT); ScriptC_yuv2grey rsYUVtoGrey = new ScriptC_yuv2grey(renderscript); allocYUVIn.copyFrom(frame); rsYUVtoGrey.set_in(allocYUVIn); rsYUVtoGrey.forEach_yuv2grey(allocGrayOut); allocGrayOut.copyTo(grey); } return rgbaBuffer; } catch (Exception e) { Log.e(LOGTAG, "", e); return null; } }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public Allocation createPreviewAllocation(RenderScript rs, int usage) throws RSIllegalArgumentException //------------------------------------------------------------------------------------------------------ { Type.Builder yuvBuilder = new Type.Builder(rs, Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV) ); yuvBuilder.setYuvFormat(ImageFormat.YV12); yuvBuilder.setX(previewWidth); yuvBuilder.setY(previewHeight); Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), usage | Allocation.USAGE_IO_INPUT); return a; }
@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; }
/** * This function only assumes mPointsXY, mPasteOffX, mPasteOffY * * @param healing * @param rs * @param image */ public void heal_orig(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) { long time = System.nanoTime(); Type.Builder floatImage = new Type.Builder(rs, Element.F32_3(rs)); floatImage.setX(mRoiBounds.width()); floatImage.setY(mRoiBounds.height()); Bitmap maskBitmap = buildMask(mRoiBounds, mPointsXY); Allocation dest1 = Allocation.createTyped(rs, floatImage.create()); Allocation dest2 = Allocation.createTyped(rs, floatImage.create()); healing.set_dest1(dest1); healing.set_dest2(dest2); Bitmap destBitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top, mRoiBounds.width(), mRoiBounds.height()); Allocation dest_uc4 = Allocation.createFromBitmap(rs, destBitmap); healing.forEach_convert_to_f(dest_uc4, dest1); Bitmap src = createMutableBitmap(image, mCutOffsetX, mCutOffsetY, mRoiBounds.width(), mRoiBounds.height()); Allocation src_f3 = Allocation.createTyped(rs, floatImage.create()); Allocation src_uc4 = Allocation.createFromBitmap(rs, src); healing.forEach_convert_to_f(src_uc4, src_f3); healing.set_src(src_f3); Allocation mask = Allocation.createFromBitmap(rs, maskBitmap); healing.set_mask(mask); Allocation laplace_f3 = Allocation.createTyped(rs, floatImage.create()); healing.set_laplace(laplace_f3); Script.LaunchOptions options = new Script.LaunchOptions(); options.setX(1, mRoiBounds.width() - 1); options.setY(1, mRoiBounds.height() - 1); healing.forEach_laplacian(laplace_f3, options); healing.forEach_copyMasked(mask, dest1); int steps = (int) Math.hypot(mRoiBounds.width(), mRoiBounds.height()); // match RS Single source Log.v(TAG, "Healing_orig :steps = " + steps); for (int i = 0; i < steps; i++) { healing.forEach_solve1(mask, dest2); healing.forEach_solve2(mask, dest1); } healing.forEach_convert_to_uc(dest1, dest_uc4); rs.finish(); healing.forEach_alphaMask(dest_uc4, dest_uc4); rs.finish(); dest_uc4.copyTo(destBitmap); rs.finish(); destBitmap.setHasAlpha(true); rs.finish(); // build the undo mUndoBitmap = Bitmap.createBitmap(mRoiBounds.width(), mRoiBounds.height(), Bitmap.Config.ARGB_8888); Canvas undoCanvas = new Canvas(mUndoBitmap); Rect undoRect = new Rect(0, 0, mRoiBounds.width(), mRoiBounds.height()); undoCanvas.drawBitmap(output, mRoiBounds, undoRect, null); Canvas c = new Canvas(output); c.drawBitmap(image, 0, 0, null); c.drawBitmap(destBitmap, mRoiBounds.left, mRoiBounds.top, null); Log.v(TAG, " time to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms"); }
private int predictOnGpu(float[][] x) { if (mRs == null) { return -1; } int timeSteps = x.length; float[] convertedX = alter2Dto1D(x); scriptC_main.set_time_steps(timeSteps); scriptC_main.set_in_dim(inDim); scriptC_main.set_hidden_unites(hidden_units); scriptC_main.set_layer_size(layerSize); // initialize input raw data allocation Type inRawType = Type.createXY(mRs, Element.F32(mRs), inDim, timeSteps); Allocation inputRawAlloc = Allocation.createTyped(mRs, inRawType); inputRawAlloc.copyFrom(convertedX); scriptC_main.set_input_raw(inputRawAlloc); // initialize activated input data allocation Type cellDataType = Type.createXY(mRs, Element.F32(mRs), hidden_units, timeSteps); Allocation inputsAlloc = Allocation.createTyped(mRs, cellDataType); scriptC_main.set_inputs(inputsAlloc); // initialize model parameters(weights and biases) allocation initializeParamsAllocation(); allocIntermediateVariables(); // initialize label probability output allocation Allocation labelProbAlloc = Allocation.createSized(mRs, Element.F32(mRs), outDim); scriptC_main.bind_label_prob(labelProbAlloc); scriptC_main.set_out_dim(outDim); // begin model forward pass computation long start = System.currentTimeMillis(); scriptC_main.invoke_all_in_one(); //// scriptC_main.forEach_input_transform(inputsAlloc); // scriptC_main.invoke_input_transform_func(); // for (int i = 0; i < layerSize; i++) { //// scriptC_main.forEach_set_zeros(cAlloc); //// scriptC_main.forEach_set_zeros(hAlloc); // scriptC_main.invoke_set_ch_zeros(); // scriptC_main.set_current_layer(i); // for (int j = 0; j < timeSteps; j++) { // scriptC_main.set_current_step(j); // scriptC_main.invoke_calc_cell_one_step(); //// scriptC_main.invoke_concat_in_h(); // //// scriptC_main.forEach_linear_map(linearResultAlloc); //// scriptC_main.invoke_linear_map_func(); // //// scriptC_main.forEach_pointwise_ch(cAlloc);// or pass hAlloc //// scriptC_main.invoke_pointwise_ch_func(); // //// scriptC_main.forEach_update_input(hAlloc); //// scriptC_main.invoke_update_input_func(); // } // } //// scriptC_main.forEach_output_transform(labelProbAlloc); // scriptC_main.invoke_output_transform_func(); mRs.finish(); long end = System.currentTimeMillis(); // copy result back float[] labelProb = new float[outDim]; labelProbAlloc.copyTo(labelProb); Logger.i("invoke time: %s", (end - start)); return DataUtil.argmax(labelProb) + 1; }
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); }
private void instantiateRS() { mRS = RenderScript.create(this); //, RenderScript.ContextType.DEBUG); timings.setTimingCallback(new Timings.TimingCallback() { @Override public void run() { mRS.finish(); } }); inputImageSize = cameraHandler.getCameraSize(); // Initialize holder for NDK FAST extraction implementation setImageSize(inputImageSize.width, inputImageSize.height); // Initialize RenderScript scripts scriptIntrinsicYuvToRGB = ScriptIntrinsicYuvToRGB.create(mRS, Element.RGBA_8888(mRS)); customYUVToGrayscaleConverter = new ScriptC_customYUVToGrayscaleConverter(mRS); scriptCFastNoOptimization = new ScriptC_fast_no_optimization(mRS); scriptCFast = new ScriptC_fast(mRS); scriptCFastOpenCV = new ScriptC_fast_opencv(mRS); scriptCUtil = new ScriptC_util(mRS); // Build type for YUV input image Type.Builder tb; tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); tb.setX(inputImageSize.width); tb.setY(inputImageSize.height); tb.setYuvFormat(android.graphics.ImageFormat.NV21); // Create input allocation, that will receive the camera frame inputAllocation = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT); scriptIntrinsicYuvToRGB.setInput(inputAllocation); customYUVToGrayscaleConverter.invoke_setInputImageSize(inputImageSize.width, inputImageSize.height); customYUVToGrayscaleConverter.set_inputAllocation(inputAllocation); // Build type for converted image (YUV to RGBA) tb = new Type.Builder(mRS, Element.RGBA_8888(mRS)).setX(inputImageSize.width).setY(inputImageSize.height); Type rgbaType = tb.create(); // Build type for converted image (RGBA to GRAY) tb = new Type.Builder(mRS, Element.U8(mRS)).setX(inputImageSize.width).setY(inputImageSize.height); Type grayType = tb.create(); // Define all allocations rgbAllocation = Allocation.createTyped(mRS, rgbaType, Allocation.USAGE_SCRIPT); grayAllocation = Allocation.createTyped(mRS, grayType, Allocation.USAGE_SCRIPT); fastKpAllocation = Allocation.createTyped(mRS, grayType, Allocation.USAGE_SCRIPT); outputAllocation = Allocation.createTyped(mRS, rgbaType, Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT); // Tells the output allocation which surface is its outputAllocation.setSurface(rsResultSurface); // Prepare RS scripts scriptCFastNoOptimization.set_grayAllocation(grayAllocation); //scriptCFast.set_grayAllocation(grayAllocation); scriptCFast.set_grayAllocation(grayAllocation); scriptCFastOpenCV.set_grayAllocation(grayAllocation); // Defines limits for RS kernels execution, as FAST extraction requires // a border of 3 pixels to operate and harris score requires 4 of them, so // the maximum is chosen. fastLaunchOptions = new Script.LaunchOptions(); fastLaunchOptions.setX(3, inputImageSize.width - 3); fastLaunchOptions.setY(3, inputImageSize.height - 3); // Settings this to true tells the camera preview callback that the RS code // can be executed rsInstantiated = true; }
public CameraView(int idealPreviewWidth, int idealPreviewHeight, ImageView CameraPreview, boolean processingEnabled, TextView textFps, Context context) { Camera.Size suitableSize = FindSuitableCameraSize(idealPreviewWidth, idealPreviewHeight); if (suitableSize != null) { previewWidth = suitableSize.width; previewHeight = suitableSize.height; } else { outWidth = idealPreviewWidth; outHeight = idealPreviewHeight; } previewWidth = 2 * outWidth; previewHeight = 2 * outHeight; this.MyCameraPreview = CameraPreview; this.processingEnabled = processingEnabled; this.textFps = textFps; previousTime = 0; textFps.setText("x"); outputBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); Bitmap tmpBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888); kernelContext = RenderScript.create(context); scriptIP = new ScriptC_imageprocessing(kernelContext); allocationOut = Allocation.createFromBitmap(kernelContext, outputBitmap); allocationIn = Allocation.createFromBitmap(kernelContext, tmpBitmap); Type.Builder typeYUV = new Type.Builder(kernelContext, Element.createPixel(kernelContext, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV)); typeYUV.setYuvFormat(ImageFormat.NV21); // allocation for the YUV input from the camera allocationYUV = Allocation.createTyped(kernelContext, typeYUV.setX(previewWidth).setY(previewHeight).create(), Allocation.USAGE_SCRIPT); // allocations for image processing allocationTmp1 = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32_3(kernelContext), previewWidth, previewHeight), Allocation.USAGE_SCRIPT); allocationTmp2 = Allocation.createTyped(kernelContext, allocationTmp1.getType(), Allocation.USAGE_SCRIPT); allocationTmp5 = Allocation.createTyped(kernelContext, allocationTmp1.getType(), Allocation.USAGE_SCRIPT); allocationEdges = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32(kernelContext), previewWidth, previewHeight), Allocation.USAGE_SCRIPT); allocationTmp3 = Allocation.createTyped(kernelContext, Type.createXY(kernelContext, Element.F32_3(kernelContext), outWidth, outHeight), Allocation.USAGE_SCRIPT); allocationTmp4 = Allocation.createTyped(kernelContext, allocationTmp3.getType(), Allocation.USAGE_SCRIPT); //create the instance of the YUV2RGB (built-in) RS intrinsic scriptYuvToRGB = ScriptIntrinsicYuvToRGB.create(kernelContext, Element.U8_4(kernelContext)); // set initial RenderScript global values scriptIP.set_edgeBuffer(allocationEdges); scriptIP.set_imageWidth(previewWidth); scriptIP.set_imageHeight(previewHeight); scriptIP.set_sImageWidth(outWidth); scriptIP.set_sImageHeight(outHeight); }
private Type getTypeOutAlloc(){ Type.Builder allocConfig = new Type.Builder(rs, Element.F32(rs)); allocConfig.setX(rect.height()+1); return allocConfig.create(); }
@Override public byte[] toRGBA(Context context, byte[] frame, int previewWidth, int previewHeight, int rgbaSize, byte[] grey) //----------------------------------------------------------------------------------------------------------------- { ScriptIntrinsicYuvToRGB YUVToRGB = null; byte[] rgbaBuffer = null; try { Type.Builder yuvType = new Type.Builder(renderscript, Element.U8(renderscript)).setX(previewWidth). setY(previewHeight).setMipmaps(false); yuvType.setYuvFormat(ImageFormat.NV21); Allocation ain = Allocation.createTyped(renderscript, yuvType.create(), Allocation.USAGE_SCRIPT); Type.Builder rgbType = null; YUVToRGB = ScriptIntrinsicYuvToRGB.create(renderscript, Element.U8_4(renderscript)); rgbType = new Type.Builder(renderscript, Element.RGBA_8888(renderscript)); rgbaBuffer = new byte[rgbaSize]; rgbType.setX(previewWidth).setY(previewHeight).setMipmaps(false); Allocation aOut = Allocation.createTyped(renderscript, rgbType.create(), Allocation.USAGE_SCRIPT); ain.copyFrom(frame); YUVToRGB.setInput(ain); YUVToRGB.forEach(aOut); aOut.copyTo(rgbaBuffer); if (grey != null) { Allocation allocGrayOut = null; ScriptC_yuv2grey rsYUVtoGrey = null; Type.Builder greyTypeBuilder = new Type.Builder(renderscript, Element.U8(renderscript)); greyTypeBuilder.setX(previewWidth).setY(previewHeight); allocGrayOut = Allocation.createTyped(renderscript, greyTypeBuilder.create(), Allocation.USAGE_SCRIPT); rsYUVtoGrey = new ScriptC_yuv2grey(renderscript); Allocation ainbw = Allocation.createTyped(renderscript, yuvType.create(), Allocation.USAGE_SCRIPT); ainbw.copyFrom(frame); rsYUVtoGrey.set_in(ainbw); rsYUVtoGrey.forEach_yuv2grey(allocGrayOut); allocGrayOut.copyTo(grey); } return rgbaBuffer; } catch (Exception e) { Log.e(LOGTAG, "", e); return null; } }
/** * <p>Create a {@link android.renderscript RenderScript} * {@link android.renderscript.Allocation Allocation} to use as a * destination of preview callback frames. Use * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use * the created Allocation as a destination for camera preview frames.</p> * @see android.hardware.Camera createPreviewAllocation * This version allows setting NV21 or YV12 image format. * * @param rs The RenderScript context for this Allocation. * @param usage Allocation usage * @param imageFormat ImageFormat.NV21 or ImageFormat.YV12. * @return a new YUV-type Allocation with dimensions equal to the current * preview size. * @throws RSIllegalArgumentException */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public Allocation createPreviewAllocation(RenderScript rs, int usage, int imageFormat) throws RSIllegalArgumentException //------------------------------------------------------------------------------------------------------ { Type.Builder yuvBuilder = new Type.Builder(rs, Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV) ); yuvBuilder.setYuvFormat(imageFormat); yuvBuilder.setX(previewWidth); yuvBuilder.setY(previewHeight); Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), usage | Allocation.USAGE_IO_INPUT); return a; }
public void drawToSurface(final byte[] buffer) //-------------------------------------------- { int[] ARGB = new int[buffer.length/4]; try { Type.Builder rgbaType = new Type.Builder(renderscript, Element.RGBA_8888(renderscript)).setX(width). setY( height).setMipmaps(false); Allocation aIn = Allocation.createTyped(renderscript, rgbaType.create(), Allocation.USAGE_SCRIPT); Type.Builder argbType = new Type.Builder(renderscript, Element.U32(renderscript)).setX(width). setY( height).setMipmaps(false); Allocation aOut = Allocation.createTyped(renderscript, argbType.create(), Allocation.USAGE_SCRIPT); ScriptC_rgba2argb rs = new ScriptC_rgba2argb(renderscript); aIn.copyFrom(buffer); rs.set_in(aIn); rs.forEach_rgba2argb(aOut); aOut.copyTo(ARGB); } catch (Exception e) { Log.e("PlaybackThreadFree", "drawToSurface: Renderscript RGBA to ARGB error", e); int i=0, j = 0; while (i<buffer.length) { int r = (int) buffer[i++]; if (r < 0) r = 256 + r; // Brain-dead Java has no unsigned char int g = (int) buffer[i++]; if (g < 0) g = 256 + g; int b = (int) buffer[i++]; if (b < 0) b = 256 + b; int a = buffer[i++]; if (a < 0) a = 256 + a; ARGB[j++] = Color.argb(a, r, g, b); } } Bitmap bmp = Bitmap.createBitmap(ARGB, width, height, Bitmap.Config.ARGB_8888); Canvas canvas = surface.lockCanvas(null); canvas.drawBitmap(bmp, 0, 0, null); surface.unlockCanvasAndPost(canvas); }