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

26 Upvotes

26 comments sorted by

View all comments

0

u/[deleted] Jul 22 '14

[deleted] — view deleted comment

2

u/[deleted] Jul 30 '14 edited Jul 30 '14

Also Javascript, but with dom mutation observer and mime type checker. You could include this file on any page and it would convert all <img> tags with Netpbm files into canvas elements of the same height and size. (Mutation observer does not work in all browsers)

Running example can be found here.

(function(){
	'use strict';

	var config = {
		useIMGClass: false, //Boolean imgclass
		class: '', // String class
		id: '', // String id | Boolean imgid
		autorun: true, //Boolean Autorun
		observe: true //Boolean Observe
	};

	var checkIfPBM = function(mutations){
		if(!mutations){return;}

		//check if a mutation is an image
		var imgs = []
		mutations.forEach(function(mutation, index, array){
			var l = mutation.addedNodes.length;
			for(var i=0; i<l; i++){
				var node = mutation.addedNodes[i];
				if(node.tagName==="IMG"){
					imgs.push(node);
				}
			}
		});

		//for each image, check if the source is a PBM
		imgs.forEach(function(img, index, array){
			var x = new XMLHttpRequest();
			x.open("GET", img.src, false);
			x.onreadystatechange = function(){
				if (x.readyState == 4 && /^0|2\d\d$/.test(x.status)){
					//If either the mime type or the opening characters of the image are correct
					if(['P1','P2','P3'].indexOf(x.responseText.substring(0,2)) > -1){
						render(x.responseText, img, x.responseText.substring(0,2));
					}
					else{
						switch(x.getResponseHeader("Content-Type")){
							case 'image/x-portable-pixmap':
								var type="P1";
							break;
							case 'image/x-portable-graymap':
								var type="P2";
							break;
							case 'image/x-portable-bitmap':
								var type="P3";
							break;
						}
						if(type!==undefined){
								render(x.responseText, img, type);
						}
					}
				}
			};
			x.send(null);
		});
	}

	var render = function(pbm, target, type)	{
		//remove comments
		pbm = pbm.replace(/^#.*$/gmi,"");

		//slice all none-pixel data off
		if(type!=='P1'){
			//convert all whitespace to a single space
			pbm = pbm.replace(/\s+/g, " ");

			//convert the file into an array
			pbm = pbm.split(" ");

			var w = pbm[1];
			var h = pbm[2];
			var maxVal = pbm[3];
			pbm = pbm.slice(4);
		}
		else
		{
			//convert all whitespace to a single space
			var hwdata = pbm.replace(/\s+/g, " ");

			//convert the file into an array
			hwdata = hwdata.split(" ");

			var w = hwdata[1];
			var h = hwdata[2];

			//fallback for P1 matrixes without whitespace
			pbm = hwdata.slice(3).join("").split("");
		}

		//create the canvas element
		var c = document.createElement('canvas');

		//set ID
		if(typeof(config.imgid)==='string'){
			c.setAttribute('id', config.imgid);
		}
		else if(config.imgid===true){
			c.setAttribute('id', target.getAttribute("id"));
		}

		//set Class
		if(typeof(config.class)==='string'){
			c.setAttribute('class', config.imgClass);
		}
		if(config.takeIMGClass===true){
			c.setAttribute('class', target.getAttribute("class")+' '+c.getAttribute('class'));
		}

		var title = typeof(target.getAttribute("title"))==="string" ? target.getAttribute("title") : "";
		c.setAttribute('title', title)

		var cw = target.clientWidth;
		var ch = target.clientHeight;
		c.setAttribute('width', cw);
		c.setAttribute('height', ch);

		var pixelw = cw/w;
		var pixelh = ch/h;

		//render to the dom
		var parent = target.parentNode;
		parent.insertBefore(c, target);
		parent.removeChild(target);

		var ctx = c.getContext('2d');

		//declare the method to render the pixel depending on the version
		switch(type){
			case 'P1':
				var getNextPixel = function(c){
					if(pbm[c]==="0"){
						var colour = "rgb(255, 255, 255)";
					}
					else if(pbm[c]==="1"){
						var colour = "rgb(0, 0, 0)";
					}
					else{
						throw "Exception: Image corrupted: pixel index: "+c+", value: "+pbm[c];
					}
					c++;
					return {index: c, colour: colour, pixelnr: c-1};
				}
			break;
			case 'P2':
				var getNextPixel = function(c){
					var val = pbm[c];
					c++;
					val = val/maxVal*255;
					var colour = "rgb("+val+", "+val+", "+val+")";
					return {index: c, colour: colour, pixelnr: c-1};
				}
			break;
			case 'P3':
				var getNextPixel = function(c){
					var val1 = pbm[c];
					val1 = val1/maxVal*255;
					c++;

					var val2 = pbm[c];
					val2 = val2/maxVal*255;
					c++;

					var val3 = pbm[c];
					val3 = val3/maxVal*255;
					c++;

					var colour = "rgb("+val1+", "+val2+", "+val3+")";
					return {index: c, colour: colour, pixelnr: (c-3)/3};
				}
			break;
		}

		//render the pixels to the canvas
		var c = 0;
		do{
			var data = getNextPixel(c);
			c = data.index;

			ctx.fillStyle = data.colour;
			var y = pixelh*Math.floor(data.pixelnr/w);
			var x = pixelw*(data.pixelnr%w);
			ctx.fillRect(x, y, x+pixelw, y+pixelh);

		} while(c<pbm.length);
	}

	if(config.autorun){
		checkIfPBM([{addedNodes:document.getElementsByTagName('img')}]);
	}

	if(config.observe){
		//start observing the dom for mutations
		MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
		var observer = new MutationObserver(function(mutations, observer) {
		    checkIfPBM(mutations);
		});
		observer.observe(document, {
			childList: true,
			subtree: true,
			attributes: true,
			attributeFilter: ['src']
		});
	}
})()

Edit: The comment was so long it annoyed me