import java.io.*;
import java.util.regex.*;
import java.util.Scanner;

class Grayscale {

   private static int width, height;
   
   public static byte[] scanFile (String fileName) throws IOException {
      scanHeader(fileName);
      byte[] data = scanData  (fileName);

      return data;
   }

   public static void scanHeader (String fileName) throws IOException {


      // 1. Create a Scanner object to read the file.
      FileInputStream in = new FileInputStream(fileName);
      Scanner scanner = new Scanner(in);

      // 2. Read the file ID token. ("P6")
      String s = scanner.next();
      
      // 3. Skip comments using a Pattern. e.g. Make a pattern 
      //    (Pattern.compile ("#.*"));, check if the next input matches it,
      //    and if it matches, read the line. Continue until there are no
      //    matches.
      Pattern p = Pattern.compile("#.*");
      while(scanner.hasNext(p))
      {
          scanner.nextLine();
      }


      // 4. Read the integer width.
      width = scanner.nextInt();

      // 5. Skip comments again.
      while(scanner.hasNext(p))
      {
          scanner.nextLine();
      }

      // 6. Read the height. The max value (255) will be skipped later.
      height = scanner.nextInt();
      in.close();
   }
   
   public static byte[] scanData (String fileName) throws IOException {
   
      // 7. Create a DataInputStream object to read the file
      DataInputStream reader = new DataInputStream(new FileInputStream(fileName));

      // 8. Using the difference between the number of bytes available 
      //    (available()) and the number of data bytes (width * height * 3), 
      //    skip to the beginning of the data portion of the file (skipBytes()).
      reader.skipBytes(reader.available() - (width * height *3));

      // 9. Create a byte array to hold the RGB values of all the pixels.
      byte array[] = new byte[width * height *3];
      //10. Read in the data using readFully.
      reader.readFully(array);

      //11. Return the byte array.
      return array;
   }
   
   public static byte[] grayscale (byte []data) {
      byte [] gray = new byte [data.length/3];
      // NOTE: in Java, bytes are always signed and in the
      //    range [-128, 127].  However the math needs to be done
      //    with unsigned data. Thus, adding 256 and modding by
      //    256 converts the data to positive.
      
      for (int i=0; i < data.length/3; ++i) {
         int red   = (256+data[3*i+0]) % 256;
         int green = (256+data[3*i+1]) % 256;
         int blue  = (256+data[3*i+2]) % 256;
         
         gray[i] = (byte)(red*.3 + green*.59 + blue*.11);
      }
      return gray;
   }
  
   public static void main (String args[]) throws IOException, FileNotFoundException {

      if (args.length < 2) {
         System.err.println ("Usage: java lab8 file_name1 file_name2");
         return;
      }
      byte data[] = scanFile (args[0]);
      
      // 12. Create a new PrintStream object named "out"
      
      PrintStream out = new PrintStream(args[1]);

      // Print out the header. We use print with the '\n' to avoid the "\r\n" 
      //    that will print if this code is moved to Windows.
      out.print("P5\n" + width + " " + height + "\n255\n");
      
      // 13. Write out the image data using the "write" function.
      out.write(grayscale(data));
      // 14. Print an ending hard return.
      out.println();
      // 15. Close the PrintStream.
      out.close();
   }
}
