public static void main(String[] args) { if (args.length < 1) { System.out.printf("Usage: ImageProcessor205BW <input image>\n"); return; } String input_filename = args[0]; if (!input_filename.toLowerCase().endsWith(".png")) ErrorExit("Input file must be a PNG image.\n"); String output_filename = null; if (args.length > 1) output_filename = args[1]; else output_filename = input_filename.substring(0, input_filename.length() - 4) + "_output.png"; Color[][] inputPixels = load_image(input_filename); int[][] inputIntensities = ColoursToIntensities(inputPixels); int[][] resultIntensities = ProcessImage(inputIntensities); Color[][] resultPixels = IntensitiesToColours(resultIntensities); save_image(resultPixels, output_filename); }
private static void save_image(Color[][] imagePixels, String image_filename) { int width = imagePixels.length; int height = imagePixels[0].length; BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) outputImage.setRGB(x, y, imagePixels[x][y].getRGB()); try { ImageIO.write(outputImage, "png", new File(image_filename)); } catch (java.io.IOException e) { ErrorExit("Unable to write %s: %s\n", image_filename, e.getMessage()); } System.err.printf("Wrote a %d by %d image\n", width, height); }
private static Color[][] load_image(String image_filename) { BufferedImage inputImage = null; try { System.err.printf("Reading image from %s\n", image_filename); inputImage = ImageIO.read(new File(image_filename)); } catch (java.io.IOException e) { ErrorExit("Unable to open %s: %s\n", image_filename, e.getMessage()); } int width = inputImage.getWidth(); int height = inputImage.getHeight(); Color[][] imagePixels = new Color[width][height]; for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) imagePixels[x][y] = new Color(inputImage.getRGB(x, y)); System.err.printf("Read a %d by %d image\n", width, height); return imagePixels; }