Java Code Examples for android.renderscript.Allocation#createTyped()

The following examples show how to use android.renderscript.Allocation#createTyped() . 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: FullyConnected.java    From CNNdroid with MIT License 6 votes vote down vote up
void initKernelF4F1(float[] myWeight, float[] myBias)
{
    int h_w = myBias.length;
    int w_w = myWeight.length / h_w;

    Type kernelType,biasType;
    Allocation kernelAllocation;
    Allocation biasAllocation;
    kernelType = Type.createX(myRS, Element.F32_4(myRS), h_w * w_w / 4);
    biasType = Type.createX(myRS, Element.F32(myRS), h_w);


    kernelAllocation = Allocation.createTyped(myRS, kernelType);
    kernelAllocation.copyFrom(myWeight);

    biasAllocation = Allocation.createTyped(myRS, biasType);
    biasAllocation.copyFrom(myBias);

    myScriptF4 = new ScriptC_innerProductInF4OutF1(myRS);


    myScriptF4.set_Bias_Blob(biasAllocation);
    myScriptF4.set_Kernel_Blob(kernelAllocation);
    myScriptF4.set_w_w(w_w);
    myScriptF4.set_c_o(h_w);
}
 
Example 2
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap three(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 ScriptIntrinsicColorMatrix colorMatrix3 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix3.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    0f, 0f, 0f, 0f,
                    0f, 0.78f, 0f, 0f,
                    0f, 0f, 1f, 0f,
                    0f, 0f, 0f, 1f,
            }));
    colorMatrix3.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 3
Source File: BlurTransformation.java    From TuentiTV with Apache License 2.0 6 votes vote down vote up
@Override public Bitmap transform(Bitmap source) {
  Bitmap original = source;
  Bitmap blurred;
  blurred = Bitmap.createBitmap(original);

  RenderScript rs = RenderScript.create(context);

  Allocation input =
      Allocation.createFromBitmap(rs, original, Allocation.MipmapControl.MIPMAP_FULL,
          Allocation.USAGE_SCRIPT);
  Allocation output = Allocation.createTyped(rs, input.getType());

  ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
  script.setInput(input);
  script.setRadius(RADIUS);
  script.forEach(output);

  output.copyTo(blurred);
  source.recycle();
  return blurred;
}
 
Example 4
Source File: ImageUtils.java    From Pasta-for-Spotify with Apache License 2.0 6 votes vote down vote up
public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
    if (context == null || bitmap == null) return null;

    Bitmap blurredBitmap;
    try {
        blurredBitmap = Bitmap.createBitmap(bitmap);
    } catch (OutOfMemoryError e) {
        return null;
    }

    RenderScript renderScript = RenderScript.create(context);
    Allocation input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(renderScript, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));

    script.setInput(input);
    script.setRadius(20);

    script.forEach(output);
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example 5
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap four(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 ScriptIntrinsicColorMatrix colorMatrix4 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix4.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    0.3f, 0f, 0f, 0f,
                    0f, 0.65f, 0f, 0f,
                    0f, 0f, 0.49f, 0f,
                    0f, 0f, 0f, 1f


            }));
    colorMatrix4.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 6
Source File: STUtils.java    From Fatigue-Detection with MIT License 6 votes vote down vote up
@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;
}
 
Example 7
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap nine(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  ScriptIntrinsicColorMatrix colorMatrix9 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix9.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    -2f, -1f, 1f, -2f,
                    0f, -2f, 0f, 1f,
                    0f, 0f, -1f, 1f,
                    0f, 0f, 0f, 1f
            }));
    colorMatrix9.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 8
Source File: BitmapUtil.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 高斯模糊
 */
public static Bitmap stackBlur(Bitmap srcBitmap) {
    if (srcBitmap == null) return null;
    RenderScript rs = RenderScript.create(MApplication.getInstance());
    Bitmap blurredBitmap = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);

    //分配用于渲染脚本的内存
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    //加载我们想要使用的特定脚本的实例。
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    //设置模糊半径
    script.setRadius(8);

    //启动 ScriptIntrinsicBlur
    script.forEach(output);

    //将输出复制到模糊的位图
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example 9
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap seven(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  ScriptIntrinsicColorMatrix colorMatrix7 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix7.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.22994596833595f, 0.0209523774645382f, 0.383244054685119f, 0f,
                    0.450138899443543f, 1.18737418804171f, -0.106933249401007f, 0f
                    - 0.340084867779496f, 0.131673434493755f, 1.06368919471589f, 0f,
                    0f, 0f, 0f,
                    11.91f, 11.91f, 11.91f, 0f}));
    colorMatrix7.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 10
Source File: YuvToRgb.java    From unity-android-native-camera with MIT License 6 votes vote down vote up
private void createAllocations(RenderScript rs) {

        final int width = mInputSize.getWidth();
        final int height = mInputSize.getHeight();

        mOutBufferInt = new int[width * height];

        Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
        yuvTypeBuilder.setX(width);
        yuvTypeBuilder.setY(height);
        yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
        mInputAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
                Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);

        Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), width, height);
        Type intType = Type.createXY(rs, Element.U32(rs), width, height);

        mOutputAllocation = Allocation.createTyped(rs, rgbType,
                Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
        mOutputAllocationInt = Allocation.createTyped(rs, intType,
                Allocation.USAGE_SCRIPT);
    }
 
Example 11
Source File: RSGaussianBlurTransformation.java    From picasso-transformations with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    if (Build.VERSION.SDK_INT < 17) {
        return source;
    }
    
    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, source, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(mRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(source);
    return source;
}
 
Example 12
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap two(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 ScriptIntrinsicColorMatrix colorMatrix2=ScriptIntrinsicColorMatrix.create(renderScript,Element.U8_4(renderScript));
    colorMatrix2.setGreyscale();
    colorMatrix2.forEach(inputAllocation,outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 13
Source File: BlurTransformation.java    From AcgClub with MIT License 5 votes vote down vote up
private Bitmap doBlur(Context context, Bitmap bitmap, int radius)
    throws RSRuntimeException {
  RenderScript rs = null;
  Allocation input = null;
  Allocation output = null;
  ScriptIntrinsicBlur blur = null;
  try {
    rs = RenderScript.create(context);
    rs.setMessageHandler(new RenderScript.RSMessageHandler());
    input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT);
    output = Allocation.createTyped(rs, input.getType());
    blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(radius);
    blur.forEach(output);
    output.copyTo(bitmap);
  } finally {
    if (rs != null) {
      rs.destroy();
    }
    if (input != null) {
      input.destroy();
    }
    if (output != null) {
      output.destroy();
    }
    if (blur != null) {
      blur.destroy();
    }
  }

  return bitmap;
}
 
Example 14
Source File: BlurTransformation.java    From XKnife-Android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(
            rs,
            blurredBitmap,
            Allocation.MipmapControl.MIPMAP_FULL,
            Allocation.USAGE_SHARED
    );
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example 15
Source File: ViewfinderProcessor.java    From android-HdrViewfinder with Apache License 2.0 5 votes vote down vote up
public ViewfinderProcessor(RenderScript rs, Size dimensions) {
    Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
    yuvTypeBuilder.setX(dimensions.getWidth());
    yuvTypeBuilder.setY(dimensions.getHeight());
    yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
    mInputHdrAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
            Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
    mInputNormalAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
            Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);

    Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
    rgbTypeBuilder.setX(dimensions.getWidth());
    rgbTypeBuilder.setY(dimensions.getHeight());
    mPrevAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
            Allocation.USAGE_SCRIPT);
    mOutputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
            Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);

    HandlerThread processingThread = new HandlerThread("ViewfinderProcessor");
    processingThread.start();
    mProcessingHandler = new Handler(processingThread.getLooper());

    mHdrMergeScript = new ScriptC_hdr_merge(rs);

    mHdrMergeScript.set_gPrevFrame(mPrevAllocation);

    mHdrTask = new ProcessingTask(mInputHdrAllocation, dimensions.getWidth()/2, true);
    mNormalTask = new ProcessingTask(mInputNormalAllocation, 0, false);

    setRenderMode(MODE_NORMAL);
}
 
Example 16
Source File: Softmax.java    From rscnn with MIT License 5 votes vote down vote up
@Override
public void computeOutputShape() {
    outputShape = inputShape[0];
    if(softmaxScript!=null){
        int n = inputShape[0][0];
        int h = inputShape[0][1];
        int w = inputShape[0][2];
        int c = inputShape[0][3];
        Type expSumType = Type.createX(renderScript, Element.F32(renderScript), n * h * w);
        expSumAlloc = Allocation.createTyped(renderScript, expSumType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
        softmaxScript.set_channel(c);
        softmaxScript.set_expSum(expSumAlloc);
    }
}
 
Example 17
Source File: Convolution.java    From CNNdroid with MIT License 4 votes vote down vote up
private void initKernelF8F2(float[][][][] myWeight, float[] myBias) {
    int n_k = myWeight.length;
    int c_k = myWeight[0].length;
    int h_k = myWeight[0][0].length;
    int w_k = myWeight[0][0][0].length;

    int c_k_8 = c_k;
    if (c_k % 8 != 0)
        c_k_8 = c_k + 8 - c_k % 8;

    int n_k_2 = n_k;
    if (n_k % 2 != 0)
        n_k_2 = n_k + 2 - n_k % 2;


    Allocation kernelAllocation;
    Allocation biasAllocation;
    Type kernelType = Type.createX(myRS, Element.F32_4(myRS), n_k_2 * c_k_8 * h_k * w_k / 4);
    Type biasType = Type.createX(myRS, Element.F32_2(myRS), n_k_2 / 2);

    float[] kernelMatrix = new float[n_k_2 * h_k * w_k * c_k_8];
    float[] biasArray = new float[n_k_2];
    int delta_n = (n_k_2 - n_k) / group;
    for (int i = 0; i < n_k_2; i++)
        for (int j = 0; j < c_k_8; j++)
            for (int k = 0; k < h_k; k++)
                for (int l = 0; l < w_k; l++) {
                    if (j >= c_k || ((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = 0;
                    else if (i >= n_k_2 / group)
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i - delta_n][j][k][l];
                    else
                        kernelMatrix[i * h_k * w_k * c_k_8 + k * w_k * c_k_8 + l * c_k_8 + j] = myWeight[i][j][k][l];
                }

    for (int i = 0; i < n_k_2; i++) {
        if (((i >= n_k_2 / group - delta_n) && (i < n_k_2 / group)) || (i >= n_k_2 - delta_n))
            biasArray[i] = 0;
        else if (i >= n_k_2 / group)
            biasArray[i] = myBias[i - delta_n];
        else
            biasArray[i] = myBias[i];
    }
    kernelAllocation = Allocation.createTyped(myRS, kernelType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    kernelAllocation.copyFrom(kernelMatrix);

    biasAllocation = Allocation.createTyped(myRS, biasType, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_SCRIPT);
    biasAllocation.copyFrom(biasArray);

    myScript82 = new ScriptC_convRolledInF8OutF2(myRS);
    myScript82.set_Bias_Blob(biasAllocation);
    myScript82.set_Kernel_Blob(kernelAllocation);
    myScript82.set_n_k(n_k_2);
    myScript82.set_c_k(c_k_8);
    myScript82.set_h_k(h_k);
    myScript82.set_w_k(w_k);
    myScript82.set_pad_x(pad[0]);
    myScript82.set_pad_y(pad[1]);
    myScript82.set_stride_x(stride[0]);
    myScript82.set_stride_y(stride[1]);
    myScript82.set_group(group);
}
 
Example 18
Source File: Convolution.java    From rscnn with MIT License 4 votes vote down vote up
@Override
public void computeFeatureMap() {
    int outputHeight = outputShape[1];
    int outputWidth = outputShape[2];
    int inputChannel = inputShape[0][3];
    int outputChannel = outputShape[3];

    int outputChannelAligned = getOutputChannelAligned();
    int inputChannelAligned = getInputChannelAligned();

    FeatureMap input = (FeatureMap) featureMapInput[0];
    FeatureMap output = (FeatureMap) featureMapOutput;

    Allocation inputFeatureMap = input.getFeatureMap();
    Allocation outputFeatureMap = output.getFeatureMap();

    scriptConvolution.set_InputData(inputFeatureMap);
    scriptConvolution.set_OutputData(outputFeatureMap);
    ScriptC.LaunchOptions option = new Script.LaunchOptions();
    boolean useIntrinsicBlas = false;

    if(conv1x1UserIntrinsic && kernelH==1 && kernelW==1){
        useIntrinsicBlas = true;
    }
    if(convnxnUseIntrinsic && kernelH!=1 && kernelW!=1){
        conv1x1UserIntrinsic = true;
    }

    if(useIntrinsicBlas){
        if(kernelH==1 && kernelW==1){
            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, inputFeatureMap, kernelAllocation, 0.f, outputFeatureMap);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
            return;
        }
        else {
            Type.Builder colType = new Type.Builder(renderScript, Element.F32(renderScript));
            colType.setX(kernelH * kernelW * inputChannelAligned).setY(outputHeight * outputWidth);
            Allocation colAllocation = Allocation.createTyped(renderScript, colType.create());
            scriptConvolution.set_ColData(colAllocation);

            option.setX(0, kernelH * kernelW).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_im2col2(option);

            scriptIntrinsicBLAS.SGEMM(ScriptIntrinsicBLAS.NO_TRANSPOSE, ScriptIntrinsicBLAS.TRANSPOSE,
                    1.f, colAllocation, kernelAllocation, 0.f, outputFeatureMap);

            colAllocation.destroy();
        }

        if(nextRelu && biasTerm){
            scriptConvolution.forEach_conv_bias_relu(outputFeatureMap, outputFeatureMap);
        }
        else if(biasTerm){
            scriptConvolution.forEach_conv_bias(outputFeatureMap, outputFeatureMap);
        }
        else if(nextRelu){
            scriptConvolution.forEach_conv_relu(outputFeatureMap, outputFeatureMap);
        }
    }
    else {
        if(kernelH==1 && kernelW==1){
            option.setX(0, getOutputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv1x1(option);
        }
        else if (inputChannel == group) {
            option.setX(0, getInputChannelAligned() / 4).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv_dw4(option);
        }
        else {
            int blockSize = 4;
            int[] blockSizeList = {256, 128, 96, 64, 48, 32, 16, 8};
            for (int blk : blockSizeList) {
                if (outputChannelAligned % blk == 0) {
                    blockSize = blk;
                    break;
                }
            }
            scriptConvolution.set_nblock(blockSize);
            option.setX(0, outputChannelAligned / blockSize).setY(0, outputHeight * outputWidth);
            scriptConvolution.forEach_conv4n(option);
        }
    }
}
 
Example 19
Source File: BlurTransformation.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

    Bitmap out = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (out == null) {
        out = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}
 
Example 20
Source File: HeaderFragment.java    From bubble with MIT License 4 votes vote down vote up
@Override
protected Bitmap doInBackground(Void... params) {
    double bw = mBitmap.getWidth();
    double bh = mBitmap.getHeight();
    double vw = mCoverImageView.getWidth();
    double vh = mCoverImageView.getHeight();

    int nbw, nbh, bx, by;
    if (bh/bw > vh/vw) {
        nbw = (int)vw;
        nbh = (int)(bh * (vw / bw));
        bx = 0;
        by = (int)((double)nbh / 2 - vh / 2);
    }
    else {
        nbw = (int)(bw * (vh / bh));
        nbh = (int)vh;
        bx = (int)((double)nbw / 2 - vw / 2);
        by = 0;
    }

    Bitmap scaled = Bitmap.createScaledBitmap(mBitmap, nbw, nbh, false);
    Bitmap mutable = scaled.copy(Bitmap.Config.ARGB_8888, true);
    Bitmap bitmap = Bitmap.createBitmap(mutable, bx, by, (int)vw, (int)vh);

    double s = Math.PI/6;
    int a, r, g, b, l, t, f, p;
    int primary = getResources().getColor(R.color.primary);
    for (int y = 0; y < bitmap.getHeight(); y++) {
        for (int x = 0; x < bitmap.getWidth(); x++) {
            p = bitmap.getPixel(x, y);
            a = Color.alpha(p);
            r = Color.red(p);
            g = Color.green(p);
            b = Color.blue(p);
            l = (int)(0.299 * r + 0.587 * g + 0.114 * b);
            t = (int)((Math.cos(s*(x+0.5))*Math.cos(s*(y+0.5))+1)*127);
            f = (l > t) ? primary : Color.argb(a, 0, 0, 0);
            bitmap.setPixel(x, y, f);
        }
    }

    RenderScript rs = RenderScript.create(getActivity());
    Allocation input = Allocation.createFromBitmap(rs, bitmap);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(1);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);

    return bitmap;
}