8.41 - Display BMP on console

Objetive

Create a program to display a 72x24 BMP file on console.
You must use the information on the header (see the exercise of Feb. 7th). Pay attention to the field named "start of image data". After that position, you will find the pixels of the image (you can ignore the information about the color palette and draw a "X" when color is 255, and a blank space if the color is a different one).

Note: you can create a test image, with the following steps (on Paint for Windows): Open Paint, create a new image, change its properties in File menu so that it is a color image, width 72, height 24, save as "256 color bitmap (BMP)".


Solution

using System; 
using System.IO; 

public class DisplayPGM
{
    public static void Main(string[] args)
    {
        BinaryReader inputFile;
 
        string fileName = "";
 
        if(args.Length != 1)
        {
            Console.WriteLine("Please enter filename...");
            fileName = Console.ReadLine();
        }
        else
            fileName = args[0];
 
        
        if(!File.Exists(fileName))
        {
            Console.WriteLine("the file not exists");
            return;
        }
        
        
        try
        {
            inputFile = new BinaryReader(
                File.Open(fileName,FileMode.Open));
     
            // Leo cabecera 1: deber ser P5
            char tag1 = Convert.ToChar(inputFile.ReadByte());
            char tag2 = Convert.ToChar(inputFile.ReadByte());
            byte endOfLine = inputFile.ReadByte();
            if ((tag1 != 'P') || (tag2 != '5')
               || (endOfLine != 10))
            {
                Console.WriteLine("The file is not a PGM");
                inputFile.Close();
                return;
            }
            
            // Leo cabecera 2: ancho y alto
            byte data = 0;
            string sizeOfImage="";
            while (data != 10)
            {
                data = inputFile.ReadByte();
                sizeOfImage += Convert.ToChar(data);
            }
            string[] widthAndHeight = sizeOfImage.Split(' ');
            int width = Convert.ToInt32(widthAndHeight[0]);
            int height = Convert.ToInt32(widthAndHeight[1]);
            
            // Leo cabecera 3: deber ser 255
            char maxIntensity1 = Convert.ToChar(inputFile.ReadByte());
            char maxIntensity2 = Convert.ToChar(inputFile.ReadByte());
            char maxIntensity3 = Convert.ToChar(inputFile.ReadByte());
            byte endOfLine1 = inputFile.ReadByte();
            if ((maxIntensity1 != '2') || (maxIntensity2 != '5')
               || (maxIntensity3 != '5') || (endOfLine1 != 10))
            {
                Console.WriteLine("Must be 256 grey levels");
                inputFile.Close();
                return;
            }
            
            // Leo datos
            for (int i=0; i= 200)
                        Console.Write(' ');
                    else if( (data >= 150) && (data <= 199) )
                            Console.Write('.');
                    else if( (data >= 100) && (data <= 149) )
                            Console.Write('-');
                    else if( (data >= 50) && (data <= 99) )
                            Console.Write('=');
                    else 
                            Console.Write('#');
                            
                if (i % width == width -1)
                    Console.WriteLine( );
            }
            inputFile.Close();
            

        }
        catch(Exception e)
        {
            Console.WriteLine("Error: {0}",e);
        }
    }
}


Popular Posts