My problem is to get the amount of transparent parts/pixels in an image which is changed because of an OnTouchEvent by the user.
So I would like to transform the following java code into renderscript code:
public int transparentPixels(){
int amount = 0;
for(int x = 0; x < sourceBitmap.getWidth(); x++){
for(int y = 0; y < sourceBitmap.getHeight(); y++){
if(sourceBitmap.getPixel(x,y) == Color.TRANSPARENT){
amount += 1;
}
}
}
return amount;
}
Please add code snippets from the rs and java file.
Thanks in advance!
Reference: RenderScript Docs and rsAtomicInc .
Here is my sample code. Refer to winklerrr 's solution here .
RSexample.rs
use in->a to retrive alpha and check its value.
#pragma version(1)
#pragma rs java_package_name(RS)
#pragma rs_fp_relaxed
int32_t count = 0;
rs_allocation rsAllocationCount;
void countPixels(uchar4* in, uint x, uint y) {
if(in->a==0)rsAtomicInc(&count);
rsSetElementAt_int(rsAllocationCount, count, 0);
}
RSContext.java
acts as context between MainActivity.java and RSexample.rs.
public void init(Context context) {
rs = RenderScript.create(context);
scriptC_RS = new ScriptC_RSexample(rs);
}
public void setup(int w, int h){
rgb888Type = Type.createXY(rs, Element.RGBA_8888(rs), w, h);
allocIn = Allocation.createTyped(rs, rgb888Type, Allocation.USAGE_SCRIPT);
allocCount = Allocation.createTyped(rs, Type.createX(rs, Element.I32(rs), 1));
}
public void addPic(Bitmap bitmap) {
allocIn = Allocation.createFromBitmap(rs, bitmap);
}
public int getCount(){
scriptC_RS.set_rsAllocationCount(allocCount);
scriptC_RS.forEach_countPixels(allocIn);
int[] count = new int[1];
allocIn.syncAll(Allocation.USAGE_SCRIPT);
allocCount.copyTo(count);
return count[0];
}
MainActivity.java
RSContext rsContext = new RSContext();
rsContext.init(this);
rsContext.setup(bitmap.getWidth(), bitmap.getHeight());
rsContext.addPic(bitmap);
int transparentPixel = rsContext.getCount();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.