Objective
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).
Write Your C# Exercise
C# Exercise Example
// Importing the System namespace to use its classes
using System;
using System.IO; // Importing the IO namespace to handle file operations
class PgmViewer
{
// The main method, where the program execution begins
static void Main(string[] args)
{
// Check if a file name has been provided as a command-line argument
if (args.Length < 1)
{
// Display an error message if no file name is provided
Console.WriteLine("You must provide a PGM file as an argument.");
return; // Exit the program if no file is provided
}
// Retrieve the file name from the command-line arguments
string fileName = args[0];
try
{
// Read the entire PGM file into a byte array
byte[] pgmData = File.ReadAllBytes(fileName);
// Check if the file begins with "P5" to ensure it's a binary PGM file
if (pgmData[0] != 'P' || pgmData[1] != '5')
{
// Display an error message if the file is not in PGM binary format
Console.WriteLine("The file is not a binary PGM (P5) file.");
return; // Exit the program if the format is incorrect
}
// Set the offset to start reading the header after "P5"
int offset = 2; // Skip the first two characters "P5"
// Read the image width and height, skipping spaces or newlines
while (pgmData[offset] == 10 || pgmData[offset] == 32) offset++; // Skip spaces and newlines
int width = 0, height = 0; // Initialize width and height of the image
while (pgmData[offset] >= 48 && pgmData[offset] <= 57) width = width * 10 + (pgmData[offset++] - 48); // Read width (digits)
while (pgmData[offset] == 32) offset++; // Skip the space between width and height
while (pgmData[offset] >= 48 && pgmData[offset] <= 57) height = height * 10 + (pgmData[offset++] - 48); // Read height (digits)
// Skip spaces or newlines before reading the max pixel value
while (pgmData[offset] == 10 || pgmData[offset] == 32) offset++; // Skip spaces and newlines
int maxValue = 0; // Initialize the max intensity value (typically 255)
while (pgmData[offset] >= 48 && pgmData[offset] <= 57) maxValue = maxValue * 10 + (pgmData[offset++] - 48); // Read the max value (digits)
// The image data starts after the header, so we copy it to a separate array
byte[] pixelData = new byte[width * height]; // Allocate space for the pixel data
Array.Copy(pgmData, offset, pixelData, 0, pixelData.Length); // Copy the pixel data into the array
// Now we process and display the pixel data in the console
for (int i = 0; i < pixelData.Length; i++)
{
int intensity = pixelData[i]; // Get the intensity value for the current pixel
// Determine the character to print based on the intensity value
char displayChar = intensity > 200 ? ' ' : // If intensity is greater than 200, print a space
(intensity >= 150) ? '.' : // If intensity is between 150 and 199, print a dot
(intensity >= 100) ? '-' : // If intensity is between 100 and 149, print a dash
(intensity >= 50) ? '=' : // If intensity is between 50 and 99, print an equals sign
'#'; // If intensity is between 0 and 49, print a hash sign
// Print the character for the current pixel
Console.Write(displayChar);
// If we've reached the end of a row, print a new line
if ((i + 1) % width == 0)
{
Console.WriteLine(); // Move to the next line after each row of pixels
}
}
}
catch (Exception ex)
{
// Display an error message if an exception occurs during file processing
Console.WriteLine($"Error processing the file: {ex.Message}");
}
}
}