Base64-Encode-Decpde.html


Quell Code


	<script>
 
if (typeof(PNGStorage) === "undefined") var PNGStorage = {};

(function() {

PNGStorage.decode = function (data, callback) { // nihilogic canvas decompressor
	if (!data.length) return;
	var canvas = document.createElement("canvas");
	var ctx = canvas.getContext("2d");
	var image = new Image();
	image.style.position = "absolute";
	image.style.left = "-10000px";
	document.body.appendChild(image);
	image.onload = function () {
		var width = this.offsetWidth;
		var height = this.offsetHeight;
		canvas.width = width;
		canvas.height = height;
		canvas.style.width = width + "px";
		canvas.style.height = height + "px";
		ctx.drawImage(this, 0, 0);
		var data = ctx.getImageData(0, 0, width, height).data;
		var a = "";
		var len = data.length;
		var p = -1;
		for (var i = 0; i < len; i += 4) {
			if (data[i + 0]) a += String.fromCharCode(data[i + 0]);
			if (data[i + 1]) a += String.fromCharCode(data[i + 1]);
			if (data[i + 2]) a += String.fromCharCode(data[i + 2]);
		}
		if (callback) callback(a);
		document.body.removeChild(image);
	}
	image.src = data;
	return true;
};

PNGStorage.encode = function (data) {
	if (!data.length) return;
	var filesize = data.length;
	var width = Math.ceil(Math.sqrt(filesize / 3));
	var height = width;
	var canvas = document.createElement("canvas");
	var ctx = canvas.getContext("2d");
	canvas.width = width;
	canvas.height = height;
	var imageData = ctx.getImageData(0, 0, width, height);
	var im = imageData.data;
	var i = 0;
	for (var y = 0; y < height; y++) {
		for (var x = 0; x < width; x++) {
			var pos = y * width * 4 + x * 4;
			var d0 = data[i++];
			var d1 = data[i++];
			var d2 = data[i++];
			if (d0 || d1 || d2) {
				if (d0) im[pos] = ord(d0);
				if (d1) im[pos + 1] = ord(d1);
				if (d2) im[pos + 2] = ord(d2);
				im[pos + 3] = 255;
			}
		}
	}
	ctx.putImageData(imageData, 0, 0);
	return ctx.canvas.toDataURL();
};

var ord = function (string) { // via php.js (MIT License)
	var str = string + "";
	var code = str.charCodeAt(0);
	if (0xD800 <= code && code <= 0xDBFF) {
		var hi = code;
		if (str.length === 1) return code;
		var low = str.charCodeAt(1);
		return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
	}
	if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
		return code;
	}
	return code;
};

})();
     
     
	</script>

<script>

var demoEncode = function() {
	var image = document.querySelector('#image');
	var data0 = document.querySelector('#data0');
	var data1 = document.querySelector('#data1');
	data1.value = PNGStorage.encode(data0.value);
};

var demoDecode = function() {
	var image = document.querySelector('#image');
	var data0 = document.querySelector('#data0');
	var data1 = document.querySelector('#data1');
	PNGStorage.decode(data1.value, function(data) {
		data0.value = data;	
	});
};

</script>
<textarea id="data0" style="width: 400px; height: 300px; float: left;">
A "Hello world" program is a computer program that outputs "Hello, world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to verify that a language or system is operating correctly.

In a device that does not display text, a simple program to produce a signal, such as turning on an LED, is often substituted for "Hello world" as the introductory program.
</textarea>
<textarea id="data1" style="width: 400px; height: 300px; float: left;">
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAACQklEQVQoU3WSS09TURSFv9P77LtQQ0gc2Bj+g2MsDWp42dsYpxiIOBAM4MCfYZw71vTSADapUqVOiANnJurIxKFRaCnt7eO2t9dDwYEYz+zss8/aa6+1xGTqqm9kbjERKhImi+NsU4wsMN+GE72AqUNnK01sNopuOHh2mNNHsb740C8ke0zympjIIQAlX+DzEOw9RTNN1nHYLoaZ4YTdKRMroiAWn6R85fAO2qDGwbtvXEtfIZFQybfiEsKHsM6C85O6EmS8qOBkdhASXWyuLPt2WGXWU+goJmVvcE5TUFH6WIpLrdtn3wuCKrCMHbzBPOL++qKv90yEW2LgztG71IDjUQ7KLW4MZ3bRGaU7eUw7qaHLPjpypeXVlJ/QMtS24lIcufxMBMP0qNKgQgiqLaaNIEZfRSsHJFUbLWRJqql7/pFlENgKMTYn+BGyiVWlonsLjJxJS88UuKUB7lyPIaGPbxBr6ylf8yDQytIQBqVYbagw9gz8aayYRFExLMGha6NKi8Tao2VfqcdpRxuUHJNsvEU9YDC295ynl5cYG99lSj1TvTSiDEGVXgaxkVr1+7ldabxFRKkSLCY5vAsfXhzRtMwztbdVoue0C8kA9KWPa0uP/abIMyhbmNM9Oto+FeMmaUfWZEiSgSxus0+3JDBkAOAXXye+I1IPNnz0BrmWiR0zuC4NMKTD4XoNW8gQ7r3iy8VIerflx80VP2MLgrkg4lihmXCJazsMuvO05QT97UuefWqdJvGv80/hYsP/7r8BQ6vuCLo8wMYAAAAASUVORK5CYII=
</textarea>
<br style="clear:both">
<input value="encode" type="button" onclick="demoEncode()" style="font-size: 1em"><br>
<input value="decode" type="button" onclick="demoDecode()" style="font-size: 1em"><br><br>