dimanche 15 mars 2015

How to create 1-bits bitmaps using BufferedImage object?


Vote count:

0




I'm trying to obtain a 1-bits bitmap's RGB, put it in a multidimensional array to perform some tasks and recreate the bitmap again to its original form. Although almost the same code worked just fine with 4-bits and 8-bit bitmaps my end result shows a black image for the 1-bits bitmaps, not really sure of what I've done wrong here!! Thanks in advance.



BufferedImage in = ImageIO.read(new File("D:\\SourceImage.bmp"));
int w = in.getWidth(), h = in.getHeight();
int[][] array = new int[w][h];
for (int j = 0; j < w; j++) {
for (int k = 0; k < h; k++) {
array[j][k] = in.getRGB(j, k);
}
}


// perform operations on array[ ][ ]



byte[] v = new byte[2];
for (int i = 0; i < v.length; ++i) {
v[i] = (byte) (i );
}
ColorModel cm = new IndexColorModel(1, v.length, v, v, v);
WritableRaster wr = cm.createCompatibleWritableRaster(w, h);
BufferedImage out = new BufferedImage(cm, wr, false, null);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int Pixel = array[x][y] << 16 | array[x][y] << 8 | array[x][y];
out.setRGB(x, y, array[x][y]);
}
}
Graphics2D g = out.createGraphics();
g.drawImage(out, 0, 0, null);
g.dispose();
ImageIO.write(out, "bmp", new File("D:\\ResultImage.bmp"));


asked 32 secs ago







How to create 1-bits bitmaps using BufferedImage object?

Aucun commentaire:

Enregistrer un commentaire