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

25 Upvotes

26 comments sorted by

View all comments

1

u/ENoether Jul 21 '14

Python 3 with bonus; graphics via tkinter. As always, feedback and criticism welcome:

from tkinter import *
import sys

def color_to_hex(rgb):
	return "#" + "".join(["{0:0=2x}".format(i) for i in rgb])
				
def create_image(i_width, i_height, pixels, px_size = 5):
	root = Tk()
	root.columnconfigure(0, weight=1)
	root.rowconfigure(0, weight=1)
	c = Canvas(root, width=i_width * px_size, height = i_height * px_size)
	c.grid(column=0, row=0)
	c.xview_moveto(0)
	c.yview_moveto(0)
	for i in range(len(pixels)):
		x = (i % i_width)*px_size
		y = int(i / i_width)*px_size
		c.create_rectangle(x, y, x+px_size, y+px_size, fill=pixels[i], outline='')
	root.mainloop()
	
def strip_comments(lines):
	return [line for line in lines if not line.strip().startswith("#")]
		
def read_image(filename):
	f = open(filename, 'r')
	lines = f.readlines()
	f.close()
	lines = strip_comments(lines)
	entries = " ".join(lines).split()
	filetype = entries[0]
	width = int(entries[1])
	height = int(entries[2])
	if filetype == "P1":
		return (width, height, parse_p1(entries[3:]))
	elif filetype == "P2":
		return (width, height, parse_p2(entries[3:]))
	elif filetype == "P3":
		return (width, height, parse_p3(entries[3:]))
	else:
		raise Exception("Invalid type")
		
		
def remove_whitespace(s):
	return "".join(s.split())

def parse_p1(entries):
	pixels = [ color_to_hex([(1 - int(x)) * 255]*3) for x in remove_whitespace("".join(entries)) ]
	return pixels
	
def parse_p2(entries):
	shade_width = 256.0 / (int(entries[0]) + 1)
	pixels = [ color_to_hex( [int(int(x) * shade_width)] * 3) for x in entries[1:] ]
	return pixels
	
def chunkify(lst, chunk_length):
	if len(lst) <= chunk_length:
		return [lst]
	else:
		return [ lst[0:chunk_length] ] + chunkify(lst[chunk_length:], chunk_length)
	
def parse_p3(entries):
	shade_width = 256.0 / (int(entries[0]) + 1)
	colors = chunkify(entries[1:], 3)
	pixels = [ color_to_hex( (int(int(col[0]) * shade_width), int(int(col[1])*shade_width), int(int(col[2])*shade_width)) ) for col in colors ]
	return pixels
	
image_data = read_image(sys.argv[1])
if(len(sys.argv) > 2):
	create_image(image_data[0], image_data[1], image_data[2], px_size=int(sys.argv[2]))
else:
	create_image(image_data[0], image_data[1], image_data[2])

Output from the previous challenge: http://puu.sh/algtM/0a45c9b564.png

Test file for P2:

P2
# Shows the word "FEEP" (example from Netpbm man page on PGM)
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Output for P2: http://puu.sh/algvz/3c5018c3c2.png

Test input for P3:

P3
# The P3 means colors are in ASCII, then 3 columns and 2 rows,
# then 255 for max color, then RGB triplets
3 2
255
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0

Output for P3: http://puu.sh/algwB/a0e5260f8d.png

(Test data for P2 and P3 from Wikipedia)