import java.util.*;
public class Main
{
public static void main(String[] args)
{
BinaryReader inputFile;
String fileName = "";
if (args.length != 1)
{
System.out.println("Please enter filename...");
fileName = new Scanner(System.in).nextLine();
}
else
{
fileName = args[0];
}
if (!(new java.io.File(fileName)).isFile())
{
System.out.println("the file not exists");
return;
}
try
{
inputFile = new BinaryReader(File.Open(fileName, FileMode.Open));
// Leo cabecera 1: deber ser P5
char tag1 = (char)inputFile.ReadByte();
char tag2 = (char)inputFile.ReadByte();
byte endOfLine = inputFile.ReadByte();
if ((tag1 != 'P') || (tag2 != '5') || (endOfLine != 10))
{
System.out.println("The file is not a PGM");
inputFile.Close();
return;
}
// Leo cabecera 2: ancho y alto
byte data = 0;
String sizeOfImage = "";
while (data != 10)
{
data = inputFile.ReadByte();
sizeOfImage += (char)data;
}
String[] widthAndHeight = sizeOfImage.split("[ ]", -1);
int width = Integer.parseInt(widthAndHeight[0]);
int height = Integer.parseInt(widthAndHeight[1]);
// Leo cabecera 3: deber ser 255
char maxIntensity1 = (char)inputFile.ReadByte();
char maxIntensity2 = (char)inputFile.ReadByte();
char maxIntensity3 = (char)inputFile.ReadByte();
byte endOfLine1 = inputFile.ReadByte();
if ((maxIntensity1 != '2') || (maxIntensity2 != '5') || (maxIntensity3 != '5') || (endOfLine1 != 10))
{
System.out.println("Must be 256 grey levels");
inputFile.Close();
return;
}
}
catch (RuntimeException e)
{
System.out.printf("Error: %1$s" + "\r\n", e);
}
}
}