Trying to paint to screen a bit array fast
Im trying to create a smaller memory footprint of black and white tiff images. When I load them with the imageio extension they come out huge as a buffered image. Anyhow, I store them as 2D bit arrays for black and white. Im trying to print these 2D bit arrays, but they paint slow if I go pixel by pixel. Is there a way to paint something to screen at once without having to paint it into an image first?
Heres the code I use:
private int bitpos[] = {1,2,4,8,16,32,64,128};
private VolatileImage renderBitImage(bitimage mybit){
VolatileImage dbImage = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleVolatileImage(mybit.getWidth(), mybit.getHeight());
Graphics2D dbg;
if (dbImage == null) {
System.out.println("dbImage is null");
return null;
}
else{
dbg = (Graphics2D)dbImage.getGraphics();
dbg.setColor(Color.BLUE);
dbg.fillRect(0, 0, mybit.getWidth(), mybit.getHeight());
//draw a line along the x
int linestart = 0;
int lineend = 0;
boolean inblack = false;
dbg.setColor(Color.WHITE);
//dbg.drawLine(0,10,200,10);
for(int y = 0; y < mybit.getHeight(); ++y){
for(int x = 0; x < mybit.getWidth(); ++x){
if(is_black(x,y,mybit.getImage())){
if(!inblack){
inblack = true;
linestart = x;
}
} else{
if(inblack){
inblack = false;
lineend = x - 1;
dbg.drawLine(linestart,y,lineend,y);
//System.out.println("Drawing a line " + linestart + "," + y + "," + lineend + "," + y);
}
}
}
if(inblack){
inblack = false;
lineend = mybit.getWidth() - 1;
dbg.drawLine(linestart,y,lineend,y);
//System.out.println("Drawing a line " + linestart + "," + y + "," + lineend + "," + y);
}
}
return dbImage;
}
}
private boolean is_black(int x, int y, int buf[][]){
try{
int mybyte = buf[(int)((double)x/8d)][y];
if((mybyte & bitpos[x%8]) == bitpos[x%8]) return true;
else return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
bitimage is a class that hold the width and height in int for the image and a 2D array of the on/off bits(int[][]).