Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Display PBM on console - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,55302940

Display PBM on console - Practice Exercises C# Sharp


Lesson 8:

File management


Exercise 8.32:

Display PBM on console


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.


Source 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();

    }
}
Exercisey 8.32

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.