Make a counter for transparent pixels from a Bitmap in Renderscript
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!
java
add a comment |
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!
java
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
1
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11
add a comment |
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!
java
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!
java
java
asked Jan 1 at 13:30
FynnFynn
313
313
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
1
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11
add a comment |
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
1
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
1
1
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11
add a comment |
1 Answer
1
active
oldest
votes
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();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995838%2fmake-a-counter-for-transparent-pixels-from-a-bitmap-in-renderscript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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();
add a comment |
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();
add a comment |
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();
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();
edited Jan 11 at 3:18
answered Jan 9 at 6:12
Kuanlin ChenKuanlin Chen
3861715
3861715
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995838%2fmake-a-counter-for-transparent-pixels-from-a-bitmap-in-renderscript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi Fynn, I found some problems in my old answer, I have updated it, please have a look.
– Kuanlin Chen
Jan 11 at 3:26
@KuanlinChen I tried your solution as well as the solution of winklerrr, even though it's just for counting the amount of pixel in an image. Your solution returned 0 and the the other one 1. Is there maybe a different way of live counting transparent pixels from a bitmap without using RenderScript?
– Fynn
Jan 11 at 19:05
What do you mean by "live counting" ?
– Kuanlin Chen
Jan 24 at 2:11
1
The bitmap changes all the time, because the user can draw transparent pixel on it. So my goal is to count the transparent pixel after every time the user draws on the bitmap
– Fynn
Jan 25 at 5:11