using System;
using System.IO;
public class readerImagePGM
{
public static void Main()
{
// Read name file PGM
Console.WriteLine("Enter name of file PGM");
string name = Console.ReadLine();
// Read all data
FileStream filePGM = File.OpenRead(name);
byte[] data = new byte[filePGM.Length];
filePGM.Read(data, 0, filePGM.Length);
filePGM.Close();
// Read mesures file PGM
string mesures = "";
int i = 3;
do
{
mesures += Convert.ToChar(data[i]);
i++;
}
while (data[i] != 10);
i++;
string[] size;
int width, height;
size = mesures.Split(' ');
width = Convert.ToInt32(size[0]);
height = Convert.ToInt32(size[1]);
// Read color tone PGM
string colorTone = "";
do
{
colorTone += Convert.ToChar(data[i]);
i++;
}
while (data[i] != 10);
i++;
// Read pixels of PGM
int amount = 0;
for (int j = i; j < filePGM.Length; j++)
{
if (data[j] >= 200)
Console.Write(" ");
else if (data[j] >= 150 || data[j] <= 199)
Console.Write(".");
else if (data[j] >= 100 || data[j] <= 149)
Console.Write("-");
else if (data[j] >= 50 || data[j] <= 99)
Console.Write("=");
else if (data[j] >= 0 || data[j] <= 49)
Console.Write("#");
amount++;
if (amount % width == 0)
Console.WriteLine();
}
}
}