Java 类android.renderscript.Script 实例源码

项目:Camore    文件:RsFusion.java   
public void config(){
    curBlock = data.blocks.getCurBlock();
    superImRegion = curBlock.getCorrespBlockSuper();
    opt = new Script.LaunchOptions();
    opt.setX(superImRegion.left,superImRegion.right );
    opt.setY(superImRegion.top, superImRegion.bottom );

    rsFusion.set_g_xl_ind_max(data.getOriWidth());
    rsFusion.set_g_yl_ind_max(data.getOriHeight());

    rsFusion.set_low_res(allocImCur);
    rsFusion.set_super_res(data.getAllocSuper());
    rsFusion.set_g_xl_pix_offset(curBlock.xTranslation);
    rsFusion.set_g_yl_pix_offset(curBlock.yTranslation);
    rsFusion.set_g_xl_subpix_offset(curBlock.subPixMotionX);
    rsFusion.set_g_yl_subpix_offset(curBlock.subPixMotionY);

}
项目:live-mandelbox    文件:Render.java   
public Bitmap getImage(float angle) {
    long time1 = System.currentTimeMillis();
    render.invoke_adjust_rot(angle);
       Script.LaunchOptions lc = new Script.LaunchOptions();
       /* Using 256k pixel budget per invocation -- experimentally determined from Nexus 5 */
       int pixelBudget = Math.min(bm2.getHeight(), 256 * 1024 / bm2.getWidth());
       for (int y = 0; y < bm2.getHeight(); y += pixelBudget) {
           lc.setY(y, y + pixelBudget);
           Log.i(TAG, String.format("Rendering chunk from %d to %d", lc.getYStart(), lc.getYEnd()));
           render.forEach_root(abm1, lc);
           rs.finish();
       }
    render.invoke_adjust_rot(-angle);

    long time2 = System.currentTimeMillis();
    fxaa.set_in(abm1);
    fxaa.forEach_root(abm2);
       abm2.copyTo(bm2);
       rs.finish();
    long time3 = System.currentTimeMillis();

    Log.i(TAG, String.format("Completed: %d ms for render, %d ms for fxaa", time2 - time1, time3 - time2));
    return bm2;
}
项目:Camore    文件:RsRegisErrEstimator.java   
private void config(Rect rect){
    //x coordinate refers to index in row , index in column is not used
    opt = new Script.LaunchOptions();

    substRs.set_g_offsetX(step.offsetX);
    substRs.set_g_offsetY(step.offsetY);
    substRs.set_g_x_ind_max(data.getOriWidth() - 1);
    substRs.set_g_y_ind_max(data.getOriHeight() - 1);

    substRs.set_g_imBase(allocGrayBase);
    substRs.set_g_imCur(allocGrayCur);
    this.rect = rect;
    setIndexes();
    createOutAlloc();
}
项目:style-transfer    文件:Healing.java   
/**
 * 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");
}
项目:renderscript_examples    文件:MainActivity.java   
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);

    }
项目:renderscript_examples    文件:MainActivity.java   
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;
}
项目:Camore    文件:RsPartialDerivative.java   
private Script.LaunchOptions createOpt(){
    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(0,data.getOriWidth() );
    options.setY(0,data.getOriHeight() );
    return options;
}