I have been working on a gaussian blur on java and libgdx. It mostly works...
This is the image that I am trying to blur.
The problem is the red value equals -1 in most cases rather than something like 255. I have searched lots of forums and they don't address this problem. I have also noticed that most people use ARGB8888 but badlogics library's doesn't have the ARGB8888 format it has RGBA8888 format. I also searched websites for how the RGBA8888 stores its information in bits and I some what understand, but not enough to fix the problem.
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.utils.BufferUtils;
import com.badlogic.gdx.utils.ScreenUtils;
import java.awt.image.ConvolveOp;
import java.nio.ByteBuffer;
public class ScreenCapture {
public void capture(){
float output = 0;
int[] all = new int[11];
Pixmap originalPixmap = new Pixmap(Gdx.files.internal("graphics/menu/test.png"));
Pixmap pixmapVertical = new Pixmap(originalPixmap.getWidth(), originalPixmap.getHeight(), Pixmap.Format.RGBA8888);
Pixmap pixmap = new Pixmap(pixmapVertical.getWidth(), pixmapVertical.getHeight(), Pixmap.Format.RGBA8888);
for (int y = 0;y < originalPixmap.getHeight();y++){
for (int x = 0;x < originalPixmap.getWidth();x++){
for (int i = -5; i < 5; i++) {
all[i+5] = originalPixmap.getPixel(x+i, y);
}
pixmapVertical.setColor(blur(all));
pixmapVertical.drawPixel(x, y);
}
}
for (int x = 0;x < pixmapVertical.getWidth();x++){
for (int y = 0;y < pixmapVertical.getHeight();y++){
for (int i = -5; i < 5; i++) {
all[i+5] = pixmapVertical.getPixel(x, y+i);
}
pixmap.setColor(blur(all));
pixmap.drawPixel(x, y);
}
}
PixmapIO.writePNG(Gdx.files.local("screen.png"), pixmap);
originalPixmap.dispose();
pixmapVertical.dispose();
pixmap.dispose();
int rgb = 0xffffffff;
int test = ((rgb & 0xff000000) >> 24);
System.out.println(test);
}
private int blur(int[] all){
float[] weight = {0.0093f, 0.028002f, 0.065984f, 0.121703f, 0.175713f, 0.198596f, 0.175713f, 0.121703f, 0.065984f, 0.028002f, 0.0093f};
float r = 0;
float g = 0;
float b = 0;
float a = 0;
for (int i = 0; i < 11; i++){
b += ( ((all[i] & 0x0000ff00)>>8)*weight[i]);
r += ( ( (all[i] & 0xff000000)>>24 )*weight[i]);
g += ( ((all[i] & 0x00ff0000)>>16)*weight[i]);
a += ( ((all[i] & 0x000000ff))*weight[i]);
}
return (((int) r << 24 | (int)g << 16 | (int)b << 8 | (int)a ));
}
}
The far left value will cause you number to be negative. eg. When red is 255 your number is negative. You can avoid this by &'ing with 255.
int r += ( ( (all[i]>>24) & 0xff) )*weight[i]);
Essentially -1&0xff = 255
There is also the unsigned right shift operator >>>
which would give you the same results.
int r += ( (all[i]>>>24) )*weight[i]);
User contributions licensed under CC BY-SA 3.0