Permalink
Browse files

working thing

  • Loading branch information...
0 parents commit ef372dc06d0a6ab2f8a6887894df53a4ff111188 @substack committed May 6, 2012
Showing with 92 additions and 0 deletions.
  1. +50 −0 index.js
  2. +42 −0 package.json
@@ -0,0 +1,50 @@
+var PNG = require('png-js');
+var charmer = require('charm');
+var x256 = require('x256');
+var buffers = require('buffers');
+
+var Stream = require('stream').Stream;
+
+module.exports = function (opts) {
+ if (!opts) opts = {};
+ if (!opts.cols) opts.cols = 80;
+
+ var s = new Stream;
+ s.readable = true;
+ s.writable = true;
+
+ var out = new Stream;
+ out.writable = true;
+ out.write = function (buf) { s.emit('data', buf) };
+ out.end = function () { s.emit('end') };
+
+ var charm = charmer(out);
+
+ var bufs = buffers();
+ s.write = function (buf) {
+ if (typeof buf === 'string') buf = new Buffer(buf);
+ bufs.push(buf);
+ };
+
+ s.destroy = s.end = function () {
+ var png = new PNG(bufs.slice());
+ png.decode(function (pixels) {
+ var dx = png.width / opts.cols;
+ var dy = dx * png.height / png.width;
+
+ for (var y = 0; y < png.height; y += dy) {
+ for (var x = 0; x < png.width; x += dx) {
+ var i = (Math.floor(y) * png.height + Math.floor(x)) * 4;
+
+ var ix = x256([ pixels[i], pixels[i+1], pixels[i+2] ]);
+ charm.background(ix).write(' ');
+ }
+ charm.write('\n');
+ }
+
+ out.emit('end');
+ });
+ };
+
+ return s;
+};
@@ -0,0 +1,42 @@
+{
+ "name" : "picture-tube",
+ "description" : "render 256 color images on the terminal",
+ "version" : "0.0.0",
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/picture-tube.git"
+ },
+ "main" : "index.js",
+ "keywords" : [
+ "png",
+ "viewer",
+ "terminal",
+ "ansi"
+ ],
+ "directories" : {
+ "test" : "test"
+ },
+ "scripts" : {
+ "test" : "tap test/*.js"
+ },
+ "dependencies" : {
+ "buffers" : "~0.1.1",
+ "charm" : "~0.0.8",
+ "png-js" : "~0.1.0",
+ "x256" : "~0.0.0",
+ "request" : "~2.9.202",
+ "optimist" : "~0.3.4"
+ },
+ "devDependencies" : {
+ "tap" : "~0.2.4"
+ },
+ "engines" : {
+ "node" : ">=0.4.0"
+ },
+ "license" : "MIT",
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ }
+}

0 comments on commit ef372dc

Please sign in to comment.