//Kevin Liu //Project Code //Filter.java //Comparison of Digital Image Filtering Techniques import java.io.*; import java.util.*; class Filter { public static void main(String args[]) { // args.length is equivalent to argc in C if (args.length == 1) { try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(args[0]); // Convert our input stream to a // DataInputStream DataInputStream in = new DataInputStream(fstream); //Reads the pgm file and convert to a two-dimensional array String type = in.readLine(); String size = in.readLine(); size = in.readLine(); //skip the second line int numCol = Integer.valueOf( size.substring(0, size.indexOf(" ")) ).intValue(); int numRow = Integer.valueOf( size.substring(size.indexOf(" ")+1) ).intValue(); System.out.println(numRow); System.out.println(numCol); String maxValue = in.readLine(); if( type.equals("P2") ) //if file is pgm image { int[][] data = new int[numRow][numCol]; int row; for(int column = 0; column < numCol; column++) { for(row = 0; row < numRow; row++) { data[row][column] = Integer.valueOf(in.readLine()).intValue(); } } in.close(); Median image = new Median(data, numRow, numCol); //create Median instance int[][] filtered = image.process(); //attaches "filtered" to the end of the original file name and calls fileOut fileOutPGM(args[0].substring(0, args[0].indexOf(".")) + "filtered", filtered, numRow, numCol); } //else if ( type.equals("P3") ) // if file is ppm image, header for future development //{ // int[][] data = new int[in.available()][3]; //} } catch (Exception e) { System.err.println("File input error"); } } else System.out.println("Invalid parameters"); // Read in File } public static void fileOutPGM(String title, int[][] content, int numRow, int numCol) //writes the output file { FileOutputStream out; // declare a file output object PrintStream p; // declare a print stream object try { // Create a new file output stream // connected to "myfile.txt" out = new FileOutputStream(title + ".pgm"); // Connect print stream to the output stream p = new PrintStream( out ); // Writes a new pgm file of the filtered image. p.println("P2"); p.println("# produced by image filter implemented by Kevin"); p.println(numCol + " " + numRow); p.println("255"); int row; for(int column = 0; column < numCol; column++) { for(row = 0; row < numRow; row++) { p.println (content[row][column]); } } p.close(); } catch (Exception e) { System.err.println ("Error writing to file"); } } }