PGM viewer Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

PGM viewer


Objetive:

The PGM format is one of the versions of NetPBM image formats. Specifically, it is the variant capable of handling images in shades of gray.

Its header begins with a line containing P2 (if the image data is in ASCII) or P5 (if it is in binary).

The second line contains the width and height, separated by a space.

A third line contains the intensity value that corresponds to the target (typically 255, although it could also be 15 or another value).

From there, the colors (shades of gray) of the points that make up the image begin. In ASCII format (P2), they are numbers from 0 to 255 separated by spaces and perhaps newlines. In binary (P5) format, they are contiguous bytes, from 0 (black) to 255 (white).

You must create a program capable of reading a file in binary PGM format (header P5), without comments, with 255 shades of gray (but with a width and height that can vary). In addition, you must represent the colors (shades of gray) in the console as follows:

If the intensity of gray is greater than 200, you will draw a blank space.
If it is between 150 and 199, you will draw a point.
If it is between 100 and 149, you will draw a dash (-).
If it is between 50 and 99, you will draw an "equals" symbol (=).
If it is between 0 and 49, you will draw a pound sign (#).
The name of the file to be analyzed must be read from the command line, not prompted by the user or pre-set.

Note: line breaks (\n) are represented by character 10 of the ASCII code (0x0A).


Code:

Imports System
Imports System.IO
Public Class readerImagePGM
    Public Shared Sub Main()
        Console.WriteLine("Enter name of file PGM")
        Dim name As String = Console.ReadLine()
        Dim filePGM As FileStream = File.OpenRead(name)
        Dim data As Byte() = New Byte(filePGM.Length - 1) {}
        filePGM.Read(data, 0, filePGM.Length)
        filePGM.Close()
        Dim mesures As String = ""
        Dim i As Integer = 3

        Do
            mesures += Convert.ToChar(data(i))
            i += 1
        Loop While data(i) <> 10

        i += 1
        Dim size As String()
        Dim width, height As Integer
        size = mesures.Split(" "c)
        width = Convert.ToInt32(size(0))
        height = Convert.ToInt32(size(1))
        Dim colorTone As String = ""

        Do
            colorTone += Convert.ToChar(data(i))
            i += 1
        Loop While data(i) <> 10

        i += 1
        Dim amount As Integer = 0

        For j As Integer = i To filePGM.Length - 1

            If data(j) >= 200 Then
                Console.Write(" ")
            ElseIf data(j) >= 150 OrElse data(j) <= 199 Then
                Console.Write(".")
            ElseIf data(j) >= 100 OrElse data(j) <= 149 Then
                Console.Write("-")
            ElseIf data(j) >= 50 OrElse data(j) <= 99 Then
                Console.Write("=")
            ElseIf data(j) >= 0 OrElse data(j) <= 49 Then
                Console.Write("#")
            End If

            amount += 1
            If amount Mod width = 0 Then Console.WriteLine()
        Next
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.