Display BPM on console Learn programming C# Sharp

Lesson:

File Management


Exercise:

Display BPM on console 14


Objetive:

The Netpbm format is a family of image file formats designed with simplicity in mind, rather than small size. They can represent color, grayscale or BW images using plain text (even though a binary variant exists).

For example, a black and white image coded in ASCII is represented using the header "P1".

The following line (optional) might be a comment, preceded with #.

The next line contains the width and height of the image.

The remaining line(s) contain the data: 1 for black points 0 for white points, as in this example:

P1
# This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

(that would be the content of a file named "j.pbm").

Create a program to decode an image file like this and display it on screen, using just the console. Remember that the comment is optional.


Code:

using System;
using System.IO;
class leerPBM
{
    public static void Main(string[] args)
    {
        string fileName;
        string data;
        string line;
        int width;
        int height;

        Console.WriteLine("Enter the file name: ");
        fileName = Console.ReadLine();
        if (!fileName.Contains(".pbm"))
            fileName += ".pbm";

        if (!File.Exists(fileName))
        {
            Console.WriteLine("File not found!");
            return;
        }

        // File reading
        try
        {
            StreamReader myFile = File.OpenText(fileName);

            line = myFile.ReadLine();
            if (line != "P1")
            {
                Console.WriteLine("Does not seem a B&W ASCII PBM");
                return;
            }
            Console.WriteLine("Found B&W ASCII PBM");

            // Width and height
            line = myFile.ReadLine();
            if ((line.Length > 1) && (line[0] == '#'))
            {
                Console.WriteLine("Comment: "
                    + line.Substring(1));
                line = myFile.ReadLine();
            }

            string[] widthheight = line.Split(' ');
            width = Convert.ToInt32(widthheight[0]);
            height = Convert.ToInt32(widthheight[1]);
            Console.WriteLine("width: {0}", width);
            Console.WriteLine("height: {0}", height);


            // Data reading (gathering all the lines in one)
            data = "";
            line = myFile.ReadLine();
            while (line != null)
            {
                data += line;
                line = myFile.ReadLine();
            }
            myFile.Close();
        }
        catch (IOException problem)
        {
            Console.WriteLine("File was not read properly");
            Console.WriteLine("Details: {0}", problem.Message);
            return;
        }

        // Delete spaces, so that ony 0 and 1 remain
        data = data.Replace(" ", "");

        // And traversing the data, displaying them
        int pos = 0;
        foreach (char symbol in data)
        {
            // After the width, we must advance to the next line
            if (pos % width == 0)
                Console.WriteLine();

            if (symbol == '1')
                Console.Write("X");
            else if (symbol == '0')
                Console.Write(".");

            pos++;
        }

        Console.WriteLine();

    }
}