I have a task to read grayscale image in png format to 2d array, and make some arithmetic operations on each pixel. I wrote some code which read my image and transform it to 2d array, but in the result my image is more dark even if I don't change anything yet. My code:
BufferedImage image = ImageIO.read(new File(IMAGE_PATH + "gray/squirrel.png"));
int width = image.getWidth();
int height = image.getHeight();
int[][] imageData = new int[width][height];
for(int i = 0; i < width ; ++i){
for(int j = 0 ; j < height ; ++j){
Color color = new Color(image.getRGB(i,j));
imageData[i][j] = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
}
}
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for (int x = 0; x < width ; x++) {
for (int y = 0; y < height; y++) {
newImage.setRGB(x, y, imageData[x][y]);
}
}
File outputfile = new File("image.png");
ImageIO.write(newImage, "png", outputfile);