update tng

This commit is contained in:
Christopher Jeffrey 2015-06-29 22:16:18 -07:00
parent dfedd81d46
commit c473cc527f
3 changed files with 89 additions and 45 deletions

View File

@ -76,10 +76,10 @@ PNG.curl = function(url) {
PNG.prototype.setImage = function(file) { PNG.prototype.setImage = function(file) {
var self = this; var self = this;
this.file = typeof file === 'string' ? file : null;
if (/^https?:/.test(file)) { if (/^https?:/.test(file)) {
file = PNG.curl(file); file = PNG.curl(file);
} }
this.file = file;
var width = this.position.width; var width = this.position.width;
var height = this.position.height; var height = this.position.height;
if (width != null) { if (width != null) {
@ -90,12 +90,14 @@ PNG.prototype.setImage = function(file) {
} }
try { try {
this.setContent(''); this.setContent('');
this.img = tng(this.file, { this.img = tng(file, {
colors: colors, colors: colors,
cellmapWidth: width, width: width,
cellmapHeight: height, height: height,
cellmapScale: this.scale, scale: this.scale,
ascii: this.options.ascii ascii: this.options.ascii,
speed: this.options.speed,
filename: this.file
}); });
if (width == null || height == null) { if (width == null || height == null) {
this.width = this.img.cellmap[0].length; this.width = this.img.cellmap[0].length;

View File

@ -1,13 +1,30 @@
var blessed = require('../'); var blessed = require('../');
var fs = require('fs'); var fs = require('fs');
var argv = {};
process.argv = process.argv.map(function(arg, i) {
if (~arg.indexOf('=')) {
arg = arg.split('=');
if (/^[0-9.]+$/.test(arg[1])) arg[1] = +arg[1];
argv[arg[0].replace(/^-+/, '')] = arg[1];
return;
}
if (arg.indexOf('--') === 0) {
arg = arg.slice(2);
argv[arg] = true;
return;
}
return arg;
}).filter(Boolean);
var screen = blessed.screen({ var screen = blessed.screen({
tput: true, tput: true,
smartCSR: true, smartCSR: true,
dump: __dirname + '/logs/png.log' dump: __dirname + '/logs/png.log'
}); });
var box = blessed.box({ var box1 = blessed.box({
parent: screen, parent: screen,
left: 4, left: 4,
top: 3, top: 3,
@ -17,8 +34,20 @@ var box = blessed.box({
style: { style: {
bg: 'green' bg: 'green'
}, },
content: 'Lorem ipsum doler', content: fs.readFileSync(__dirname + '/lorem.txt', 'utf8')
align: 'center' });
var box2 = blessed.box({
parent: screen,
left: 20,
top: 8,
width: 40,
height: 15,
border: 'line',
style: {
bg: 'green'
},
content: fs.readFileSync(__dirname + '/lorem.txt', 'utf8')
}); });
var file = process.argv[2]; var file = process.argv[2];
@ -41,16 +70,23 @@ if (!file) {
} }
} }
if (!argv.width && !argv.height && !argv.scale) {
argv.width = 20;
}
var png = blessed.png({ var png = blessed.png({
parent: screen, parent: screen,
// border: 'line', // border: 'line',
width: 20, width: argv.width,
height: 19, height: argv.height,
top: 2, top: 2,
left: 0, left: 0,
file: file, file: file,
ascii: false, draggable: true,
draggable: true scale: argv.scale,
ascii: argv.ascii,
optimization: argv.optimization,
speed: argv.speed
}); });
screen.render(); screen.render();

70
vendor/tng.js vendored
View File

@ -30,22 +30,22 @@ function PNG(file, options) {
this.options = options || {}; this.options = options || {};
this.colors = options.colors || require('blessed/lib/colors'); this.colors = options.colors || require('blessed/lib/colors');
this.options.optimization = this.options.optimization || 'mem'; this.optimization = this.options.optimization || 'mem';
this.speed = this.options.speed || 1;
if (Buffer.isBuffer(file)) { if (Buffer.isBuffer(file)) {
this.file = null; this.file = this.options.filename || null;
this.ext = 'png';
buf = file; buf = file;
} else { } else {
this.options.filename = file;
this.file = path.resolve(process.cwd(), file); this.file = path.resolve(process.cwd(), file);
this.ext = path.extname(this.file).slice(1).toLowerCase();
buf = fs.readFileSync(this.file); buf = fs.readFileSync(this.file);
} }
this.format = buf.readUInt32BE(0) === 0x89504e47 ? 'png' this.format = buf.readUInt32BE(0) === 0x89504e47 ? 'png'
: buf.slice(0, 3).toString('ascii') === 'GIF' ? 'gif' : buf.slice(0, 3).toString('ascii') === 'GIF' ? 'gif'
: buf.readUInt16BE(0) === 0xffd8 ? 'jpg' : buf.readUInt16BE(0) === 0xffd8 ? 'jpg'
: this.ext; : path.extname(this.file).slice(1).toLowerCase() || 'png';
if (this.format !== 'png') { if (this.format !== 'png') {
try { try {
@ -65,8 +65,6 @@ function PNG(file, options) {
this.frames = this.compileFrames(this.frames); this.frames = this.compileFrames(this.frames);
} }
PNG.prototype.defaultScale = 0.20;
PNG.prototype.parseRaw = function(buf) { PNG.prototype.parseRaw = function(buf) {
var chunks = [] var chunks = []
, index = 0 , index = 0
@ -595,14 +593,16 @@ PNG.prototype.createCellmap = function(bmp, options) {
var bmp = bmp || this.bmp var bmp = bmp || this.bmp
, options = options || this.options , options = options || this.options
, cellmap = [] , cellmap = []
, scale = options.cellmapScale || this.defaultScale , scale = options.scale || 0.20
, height = bmp.length , height = bmp.length
, width = bmp[0].length , width = bmp[0].length
, cmwidth = options.cellmapWidth , cmwidth = options.width
, cmheight = options.cellmapHeight , cmheight = options.height
, line , line
, x , x
, y , y
, xx
, yy
, scale , scale
, xs , xs
, ys; , ys;
@ -613,21 +613,27 @@ PNG.prototype.createCellmap = function(bmp, options) {
scale = cmheight / height; scale = cmheight / height;
} }
ys = Math.ceil(height / (height * scale)); if (!cmheight) {
xs = Math.ceil(width / (width * scale)); cmheight = Math.round(height * scale);
}
// ys++; if (!cmwidth) {
// xs++; cmwidth = Math.round(width * scale);
}
ys = height / cmheight;
xs = width / cmwidth;
// add a reducePixels() method here
for (y = 0; y < bmp.length; y += ys) { for (y = 0; y < bmp.length; y += ys) {
line = []; line = [];
if (cmheight && cellmap.length === cmheight) break; yy = Math.round(y);
cellmap.push(line); if (!bmp[yy]) break;
for (x = 0; x < bmp[y].length; x += xs) { for (x = 0; x < bmp[yy].length; x += xs) {
if (cmwidth && line.length === cmwidth) break; xx = Math.round(x);
line.push(bmp[y][x]); if (!bmp[yy][xx]) break;
line.push(bmp[yy][xx]);
} }
cellmap.push(line);
} }
return cellmap; return cellmap;
@ -830,7 +836,7 @@ PNG.prototype.getOutch = (function() {
})(); })();
PNG.prototype.compileFrames = function(frames) { PNG.prototype.compileFrames = function(frames) {
return this.options.optimization === 'mem' return this.optimization === 'mem'
? this.compileFrames_lomem(frames) ? this.compileFrames_lomem(frames)
: this.compileFrames_locpu(frames); : this.compileFrames_locpu(frames);
}; };
@ -978,7 +984,7 @@ PNG.prototype._animate = function(callback) {
, cellmap = self.createCellmap(renderBmp); , cellmap = self.createCellmap(renderBmp);
callback(renderBmp, cellmap); callback(renderBmp, cellmap);
return setTimeout(next, frame.delay); return setTimeout(next, frame.delay / self.speed | 0);
}; };
var next_locpu = function() { var next_locpu = function() {
@ -990,10 +996,10 @@ PNG.prototype._animate = function(callback) {
return setImmediate(next); return setImmediate(next);
} }
callback(frame.bmp, frame.cellmap); callback(frame.bmp, frame.cellmap);
return setTimeout(next, frame.delay); return setTimeout(next, frame.delay / self.speed | 0);
}; };
var next = this.options.optimization === 'mem' var next = this.optimization === 'mem'
? next_lomem ? next_lomem
: next_locpu; : next_locpu;
@ -1044,15 +1050,14 @@ PNG.prototype.toPNG = function(input) {
, disposeOp; , disposeOp;
if (format !== 'gif') { if (format !== 'gif') {
buf = exec('convert', buf = exec('convert', [format + ':-', 'png:-'],
[format + ':' + file, 'png:-'], { stdio: ['pipe', 'pipe', 'ignore'], input: input });
{ stdio: ['ignore', 'pipe', 'ignore']});
img = PNG(buf, options); img = PNG(buf, options);
img.file = file; img.file = file;
return img; return img;
} }
gif = GIF(input || file, options); gif = GIF(input, options);
this.width = gif.width; this.width = gif.width;
this.height = gif.height; this.height = gif.height;
@ -1098,7 +1103,7 @@ PNG.prototype.toPNG = function(input) {
// Convert a gif to an apng using imagemagick. Unfortunately imagemagick // Convert a gif to an apng using imagemagick. Unfortunately imagemagick
// doesn't support apngs, so we coalesce the gif frames into one image and then // doesn't support apngs, so we coalesce the gif frames into one image and then
// slice them into frames. // slice them into frames.
PNG.prototype.gifMagick = function() { PNG.prototype.gifMagick = function(input) {
var options = this.options var options = this.options
, file = this.file , file = this.file
, format = this.format , format = this.format
@ -1118,11 +1123,12 @@ PNG.prototype.gifMagick = function() {
, y; , y;
buf = exec('convert', buf = exec('convert',
[format + ':' + file, '-coalesce', '+append', 'png:-']); [format + ':-', '-coalesce', '+append', 'png:-'],
{ stdio: ['pipe', 'pipe', 'ignore'], input: input });
fmt = '{"W":%W,"H":%H,"w":%w,"h":%h,"d":%T,"x":"%X","y":"%Y"},' fmt = '{"W":%W,"H":%H,"w":%w,"h":%h,"d":%T,"x":"%X","y":"%Y"},'
frames = exec('identify', ['-format', fmt, format + ':' + file], frames = exec('identify', ['-format', fmt, format + ':-'],
{ encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }); { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'], input: input });
frames = JSON.parse('[' + frames.trim().slice(0, -1) + ']'); frames = JSON.parse('[' + frames.trim().slice(0, -1) + ']');
img = PNG(buf, options); img = PNG(buf, options);