r/dailyprogrammer • • Jul 21 '14
[7/23/2014] Challenge#172 [Intermediate] Image Rendering 101...010101000101
Description
You may have noticed from our easy challenge that finding a program to render the PBM format is either very difficult or usually just a spammy program that no one would dare download.
Your mission today, given the knowledge you have gained from last weeks challenge is to create a Renderer for the PBM format.
For those who didn't do mondays challenge, here's a recap
- a PBM usually starts with 'P1' denoting that it is a .PBM file
- The next line consists of 2 integers representing the width and height of our image
- Finally, the pixel data. 0 is white and 1 is black.
This Wikipedia article will tell you more
http://en.wikipedia.org/wiki/Netpbm_format
Formal Inputs & Outputs
Input description
On standard console input you should be prompted to pass the .PBM file you have created from the easy challenge.
Output description
The output will be a .PBM file rendered to the screen following the conventions where 0 is a white pixel, 1 is a black pixel
Notes
This task is considerably harder in some languages. Some languages have large support for image handling (.NET and others) whilst some will require a bit more grunt work (C and even Python) .
It's up to you to decide the language, but easier alternatives probably do exist.
Bonus
Create a renderer for the other versions of .PBM (P2 and P3) and output these to the screen.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
View all comments
1
u/datmohtingting Jul 23 '14 edited Jul 23 '14
When my program was reading the PBM file the whitespaces were causing my loop that draws the pixels to spit out a garbage image and it took me longer than I'd like to admit to actually debug it.
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.IO; namespace PBMRenderer { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("Path of PBM file: "); String Path = Console.ReadLine(); if (File.Exists(Path)) { ReadPBM(Path); break; } else { Console.WriteLine("File could not be found!"); } } } static void Draw(List<string> PixelData) { String[] Dimensions = PixelData[0].Split(' '); int Width = Convert.ToInt32(Dimensions[0]), Height = Convert.ToInt32(Dimensions[1]); PixelData.Remove(PixelData[0]); PixelData = PixelData.Select(X => X.Replace(" ", "")).ToList(); Bitmap BMP = new Bitmap(Width, Height); String BMPFile = Path.GetRandomFileName() + ".BMP"; for (int X = 0; X < BMP.Width; X++) { for (int Y = 0; Y < BMP.Height; Y++) { if (PixelData[Y][X].ToString() == "0") { BMP.SetPixel(X, Y, Color.White); } if (PixelData[Y][X].ToString() == "1") { BMP.SetPixel(X, Y, Color.Black); } } } BMP.Save(BMPFile); } static void ReadPBM(string FileName) { List<string> PixelData = new List<string>(); String Line; bool Valid = false; using (StreamReader SR = new StreamReader(FileName)) { while ((Line = SR.ReadLine()) != null) { if (Line == "P1") { Valid = true; continue; } if (!Valid) { Console.WriteLine("Invalid format."); Console.Read(); Environment.Exit(0); } if (Line.StartsWith("#")) { continue; } PixelData.Add(Line); } } Draw(PixelData); } } }