summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/ansi/examples/imgcat/index.js
blob: 5ff2c13943c8711e6286411e8232508b5d262178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env node

process.title = 'imgcat'

var ansi = require('../../')
  , cursor = ansi(process.stdout, { enabled: true })
  , tty = require('tty')
  , Canvas = require('canvas')
  , imageFile = process.argv[2] || __dirname + '/yoshi.png'
  , screenWidth = process.stdout.isTTY ? process.stdout.getWindowSize()[0] : Infinity
  , maxWidth = parseInt(process.argv[3], 10) || screenWidth
  , image = require('fs').readFileSync(imageFile)
  , pixel = '  '
  , alphaThreshold = 0

var img = new Canvas.Image();
img.src = image;

function draw () {
  var width = maxWidth / pixel.length
    , scaleW = img.width > width ? width / img.width : 1
    , w = Math.floor(img.width * scaleW)
    , h = Math.floor(img.height * scaleW);

  var canvas = new Canvas(w, h)
    , ctx = canvas.getContext('2d');

  ctx.drawImage(img, 0, 0, w, h);

  var data = ctx.getImageData(0, 0, w, h).data;

  for (var i=0, l=data.length; i<l; i+=4) {
    var r = data[i]
      , g = data[i+1]
      , b = data[i+2]
      , alpha = data[i+3];
    if (alpha > alphaThreshold) {
      cursor.bg.rgb(r, g, b);
    } else {
      cursor.bg.reset();
    }
    process.stdout.write(pixel);
    if ((i/4|0) % w === (w-1)) {
      cursor.bg.reset();
      process.stdout.write('\n');
    }
  }
}

draw();