Imports System
Imports System.IO
Public Class DisplayPGM
Public Shared Sub Main(ByVal args As String())
Dim inputFile As BinaryReader
Dim fileName As String = ""
If args.Length <> 1 Then
Console.WriteLine("Please enter filename...")
fileName = Console.ReadLine()
Else
fileName = args(0)
End If
If Not File.Exists(fileName) Then
Console.WriteLine("the file not exists")
Return
End If
Try
inputFile = New BinaryReader(File.Open(fileName, FileMode.Open))
Dim tag1 As Char = Convert.ToChar(inputFile.ReadByte())
Dim tag2 As Char = Convert.ToChar(inputFile.ReadByte())
Dim endOfLine As Byte = inputFile.ReadByte()
If (tag1 <> "P"c) OrElse (tag2 <> "5"c) OrElse (endOfLine <> 10) Then
Console.WriteLine("The file is not a PGM")
inputFile.Close()
Return
End If
Dim data As Byte = 0
Dim sizeOfImage As String = ""
While data <> 10
data = inputFile.ReadByte()
sizeOfImage += Convert.ToChar(data)
End While
Dim widthAndHeight As String() = sizeOfImage.Split(" "c)
Dim width As Integer = Convert.ToInt32(widthAndHeight(0))
Dim height As Integer = Convert.ToInt32(widthAndHeight(1))
Dim maxIntensity1 As Char = Convert.ToChar(inputFile.ReadByte())
Dim maxIntensity2 As Char = Convert.ToChar(inputFile.ReadByte())
Dim maxIntensity3 As Char = Convert.ToChar(inputFile.ReadByte())
Dim endOfLine1 As Byte = inputFile.ReadByte()
If (maxIntensity1 <> "2"c) OrElse (maxIntensity2 <> "5"c) OrElse (maxIntensity3 <> "5"c) OrElse (endOfLine1 <> 10) Then
Console.WriteLine("Must be 256 grey levels")
inputFile.Close()
Return
End If
Catch e As Exception
Console.WriteLine("Error: {0}", e)
End Try
End Sub
End Class