Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

PGM viewer - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

PGM viewer - Tutorial C# Sharp


Lesson 8:

File management


Exercise 8.40:

PGM viewer


Objetive:

El formato PGM es una de las versiones de formatos de imagen NetPBM. En concreto, es la variante capaz de manejar imágenes en tonos de gris.

Su cabecera comienza con una línea que contiene P2 (si los datos de la imagen están en ASCII) o P5 (si están en binario).

La segunda línea contiene el ancho y el alto, separados por un espacio.

Una tercera línea contiene el valor de intensidad que corresponde al blanco (típicamente 255, aunque también podría ser 15 u otro valor).

A partir de ahí comienzan los colores (tonos de gris) de los puntos que forman la imagen. En el formato ASCII (P2) son números de 0 a 255 separados por espacios y quizá por saltos de línea. En el formato binario (P5), son bytes contiguos, del 0 (negro) al 255 (blanco).

Debes crear un programa capaz de leer un fichero en formato PGM binario (cabecera P5), sin comentarios, con 255 tonos de gris (pero con un ancho y un alto que pueden variar). Además, deberás representar los colores (tonos de gris) en consola de la siguiente forma:
•Si la intensidad de gris es mayor de 200, dibujarás un espacio en blanco.
•Si está entre 150 y 199, dibujarás un punto.
•Si está entre 100 y 149, dibujarás un guión (-).
•Si está entre 50 y 99, dibujarás un símbolo de “igual” (=).
•Si está entre 0 y 49, dibujarás una almohadilla (#).

El nombre del fichero a analizar se debe leer desde la línea de comandos, no preguntar al usuario ni estar prefijado.

Nota: los saltos de línea (\n) se representan con el carácter 10 del código ASCII (0x0A)


Source Code:


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();
        }
    }
}
Exercisey 8.40






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.