0
I’m making an algorithm that converts one bitmap leaving it in shades of gray.
I could do it using for but I’d like to do it recursively.
Using for (This working well)
//BUTTON - ON CLICK . . .
public void go(View v){
//BM_3 É UM BITMAP, VARIAVEL GLOBAL QUE JÁ POSSUI UMA IMAGEM EM SEU CONTEUDO . . .
bm_3 = bm_3.copy(Bitmap.Config.ARGB_8888, true);
for (int x = 0; x < bm_3.getWidth(); x++){
for (int y = 0; y < bm_3.getHeight(); y++) {
String hex = Integer.toHexString(bm_3.getPixel(x, y));
if(hex.length() == 8) {
String hexX = "#" + convert_Hex_to_grey("" + hex.charAt(0) + hex.charAt(1),
"" + hex.charAt(2) + hex.charAt(3),
"" + hex.charAt(4) + hex.charAt(5),
"" + hex.charAt(6) + hex.charAt(7));
bm_3.setPixel(x, y, Color.parseColor(hexX));
}
}
}
//IMG É UMA IMAGEVIEW . . .
img.setImageBitmap(bm_3);
}
Based on the above algorithm, I have tried to implement it recursively. However, it shows error of java.lang.StackOverflowError: stack size 1038KB
Using recursion (java.lang.Stackoverflowerror)
RECURSION
private Bitmap grey_scale(int x, int y, Bitmap bm_3) {
//IF PARA PERCORRER TODO Y . . .
if (y < bm_3.getHeight()) {
String hex = Integer.toHexString(bm_3.getPixel(x, y));
if (hex.length() == 8) {
String hexX = "#" + convert_Hex_to_grey("" + hex.charAt(0) + hex.charAt(1),
"" + hex.charAt(2) + hex.charAt(3),
"" + hex.charAt(4) + hex.charAt(5),
"" + hex.charAt(6) + hex.charAt(7));
bm_3.setPixel(x, y, Color.parseColor(hexX));
}
//O ERRO OCORRE NESTE RETURN ! ! !
return grey_scale(x, y+1, bm_3);
}
//IF PARA PERCORRER TODO X, ZERANDO Y A CADA TROCA DE X.
if (x < bm_3.getWidth()) {
return grey_scale(x+1, 0, bm_3);
}
//RETORNA BITMAP
return bm_3;
}
BUTTON ON CLICK CALLING RECURSION
public void go(View v){
bm_3 = bm_3.copy(Bitmap.Config.ARGB_8888, true);
//FAZ RECURSAO DENTRO DE UMA THREAD . . .
new Thread(new Runnable() {
@Override
public void run() {
//CHAMA A RECURSAO
bm_3 = grey_scale(0, 0, bm_3);
//VOLTA PRA THREAD PRINCIPAL PARA ATUALIZAR O IMAGEVIEW . . .
runOnUiThread(new Runnable() {
@Override
public void run() {
img.setImageBitmap(bm_3);
}
});
}
}).start();
}
What size of the image?
– Jefferson Quesado
The image size is 5,577 bytes, giving 788 by 788 pixels
– CristianCotrena
The approximate depth is 621,000 recursive calls. You must not have set the stack size for this. I believe that only fix the size of the stack not adjusted for larger cases.
– Jefferson Quesado
How do you set her size?
– CristianCotrena
Each call adds by 12 bytes on the argument-only stack, not counting the extra function call structure information. Most calls also add at least 4 bytes of reference to the local variable
hex
. Maybe the compiler is smart enough to drop the reference tohex
before making the recursive call.– Jefferson Quesado
-Xms
i think. But you need to get an idea of how much you’re going to consume, or test until you don’t overflow stack– Jefferson Quesado
-Xss
is the size of the stack,-Xmx
is the initial heap size; more details here: https://stackoverflow.com/q/3700459/4438007– Jefferson Quesado
tried this = Thread(Threadgroup group, Runnable target, String name, long stackSize) . But even putting 80m, it ta error.
– CristianCotrena
tried with a smaller image, 252x252 and gave the same error
– CristianCotrena
It is running through the eclipse, command line, another ide or as?
– Jefferson Quesado
By Android Studio
– CristianCotrena
From what I’ve seen, the JRE is free to define the size of the stack, taking as reference a suggestion of what is passed as argument. I also don’t know what the relationship between the secondary thread stack and the main one is. I also don’t know if it’s platform dependent
– Jefferson Quesado
in this case, I think that trying to use the arguments of the Java virtual machine will not help.
– Jefferson Quesado
Ever tried with a 1 pixel high image? Just to make sure it’s not an algorithm error?
– Jefferson Quesado
I did something similar this when I was testing with the big printava image at each line change, it hangs on line 2. But it gets to start her. In the smaller image, it hangs on line 55 (with Thread) with limit of 80m
– CristianCotrena
tries a smaller and more controlled image. That’s why the 1 pixel
– Jefferson Quesado
Can it be indirect recursion? Put it on a stack and process the result? The advantage of this is that the pressure stays on the dynamic part of the memory
– Jefferson Quesado