简体   繁体   English

使用renderScript放大位图的一部分

[英]Using renderScript to enlarge part of a bitmap

Recently I am learning about the usage of RenderScript while I have implemented blur effet .Now I want to enlarge part of a bitmap. 最近,我在实现模糊效果的同时学习了RenderScript的用法。现在,我想放大位图的一部分。

like this 像这样

I use flowing .rs code to process a bitmap: 我使用流动的.rs代码处理位图:

#pragma version(1)
#pragma rs_fp_relaxed
#pragma rs java_package_name(com.uniquestudio.renderscript)

#include "rs_debug.rsh"

int center_x,center_y;
int radius;
int scale;
int i;
int width;

uchar4 tempArray[4194304];

void init() {
   i = 0;
}
uchar4 __attribute__((kernel)) traversal(uchar4 in, uint32_t x, uint32_t y)     {
  tempArray[i] = in;
  i++;
  return in;
}

uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) {
      int distance = (int) ((center_x -x) * (center_x - x) + (center_y - y) * (center_y - y));

      if(distance<radius*radius){
         int src_x = (int)((float)(x - center_x) / scale + center_x);
         int src_y = (int)((float)(x - center_y) / scale + center_y);

         return tempArray[src_y * width + src_x];
      }
      return in;
}

As you see,first I put pixels data into a array tempArray . 如您所见,首先我将像素数据放入数组tempArray

The Java code : Java代码:

public static Bitmap magnifierBitmap(Bitmap bitmap, int x, int y, int radius,int scale, Context context){
    RenderScript rs = RenderScript.create(context);

    Allocation in = Allocation.createFromBitmap(rs, bitmap);
    Allocation out = Allocation.createTyped(rs,in.getType());

    int width = bitmap.getWidth();
    ScriptC_magnifier magnifier = new ScriptC_magnifier(rs);

    magnifier.set_center_x(x);
    magnifier.set_center_y(y);
    magnifier.set_radius(radius);
    magnifier.set_scale(scale);
    magnifier.set_width(width);

    // first call kernel method
    magnifier.forEach_traversal(in,in);
    // second call kernel method
    magnifier.forEach_invert(in,out);

    out.copyTo(bitmap);

    rs.destroy();
    magnifier.destroy();
    in.destroy();
    out.destroy();

    return bitmap;


}

In java code , I first call traversal kernel method. 在Java代码中,我首先调用traversal内核方法。 Then I call invert . 然后我叫invert

However it does not work for me. 但是,它对我不起作用。

I just created an example to show you the process. 我只是创建了一个示例来向您展示该过程。 You can find it here: https://bitbucket.org/cmaster11/rsbookexamples/src/tip/ImageZoomExample/ . 您可以在这里找到它: https : //bitbucket.org/cmaster11/rsbookexamples/src/tip/ImageZoomExample/

The main code (I tried to make it auto-explicative) is the following: 主要代码(我试图使其具有自动复制性)如下:

RenderScript side RenderScript方面

// Store the input allocation
rs_allocation inputAllocation;

// Magnifying
// TODO: here, some checks should be performed to prevent atX and atY to be < 0, as well
//   as them to not be greater than width and height
int atX;
int atY;
float radius;
float scale; // The scale is >= 1

// Magnifier border size, to distinguish between magnified image and original one
static float borderSize = 8.0f;
// Border color, defaults to red
static uchar4 border = {255.0f, 0.0f, 0.0f, 255.0f};

uchar4 __attribute__((kernel)) magnify(uchar4 in, int x, int y) {

    // Calculates the distance between the touched point and the current kernel
    // iteration pixel coordinated
    // Reference: http://math.stackexchange.com/a/198769
    float pointDistanceFromCircleCenter = sqrt(pow((float)(x - atX),2) + pow((float)(y - atY),2));

    // Notice: NOT OPTIMIZED, is good to understand the process
    float distancePow = pow(pointDistanceFromCircleCenter,2);
    float radiusPow = pow(radius, 2);

    // These distances show two different radiuses, which belong to the border
    // of the magnifier
    float borderInnerRadius = radiusPow - pow(borderSize,2);
    float borderOuterRadius = radiusPow + pow(borderSize,2);

    // Is this pixel outside the magnify radius?
    if(distancePow > borderOuterRadius)
    {
        // In this case, just copy the original image
        return in;
    }
    // Is the current pixel inside the magnifier border
    else if (distancePow >= borderInnerRadius && distancePow <= borderOuterRadius)
    {
        // Draw border
        return border;
    }

    // If the point is inside the magnifying inner radius, draw the magnified image

    // Calculates the current distance from the chosen magnifying center
    float diffX = x - atX;
    float diffY = y - atY;

    // Scales down the distance accordingly to scale and returns the original coordinates
    int originalX = atX + round(diffX / scale);
    int originalY = atY + round(diffY / scale);

    // Return the original image pixel at the calculated coordinates
    return rsGetElementAt_uchar4(inputAllocation, originalX, originalY);
}

Java side Java方面

// Example code
RenderScript mRS;
Allocation inputAllocation;
Allocation outputAllocation;
ScriptC_magnifier magnifier;

ImageView originalImageView;
ImageView zoomedImageView;

Bitmap inputImage, outputImage;

private void example() {
    // ImageViews that will handle input and output
    originalImageView = (ImageView) findViewById(R.id.imageView);
    zoomedImageView = (ImageView) findViewById(R.id.imageView2);

    // Initialize RenderScript context
    initRS();

    // Perform first magnification
    magnify();

    // Set up a click listener on the magnified image.
    // When touched, the magnifier will be moved to the touch position.
    zoomedImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int touchX = (int) event.getX();
            int touchY = (int) event.getY();

            Log.d("Touch", String.format("Touch: %d, %d", touchX, touchY));

            magnifier.set_atX(touchX);
            magnifier.set_atY(touchY);

            magnify();

            return false;
        }
    });
}

private void initRS() {
    mRS = RenderScript.create(this);

    // Our magnifier script
    magnifier = new ScriptC_magnifier(mRS);

    // Input image
    inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.houseimage);
    originalImageView.setImageBitmap(inputImage);
    inputAllocation = Allocation.createFromBitmap(mRS, inputImage);
    outputAllocation = Allocation.createTyped(mRS, inputAllocation.getType());

    // Initializes magnifier
    magnifier.set_inputAllocation(inputAllocation);
    magnifier.set_atX(300);
    magnifier.set_atY(230);
    magnifier.set_radius(100);
    magnifier.set_scale(3);
}

private void magnify() {
    // Run the kernel
    magnifier.forEach_magnify(inputAllocation, outputAllocation);

    // Displays the magnification output
    outputImage = Bitmap.createBitmap(inputImage.getWidth(), inputImage.getHeight(), Bitmap.Config.ARGB_8888);
    outputAllocation.copyTo(outputImage);

    zoomedImageView.setImageBitmap(outputImage);
}

Reference: RenderScript: parallel computing on Android, the easy way 参考: RenderScript:Android上的并行计算的简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM