var inkjet = require('inkjet');
var decode = require('im-decode');
var async = require('async');

var fs = require('fs');

function end(err) {
  if (!err) process.exit(0);
  console.error(err);
  process.exit(1);
}

fs.readdir('./images', function(err, files) {
  if (err) end(err);
  files = files.map(function(file) {
    return './images/' + file;
  });
  async.map(files, fs.readFile, function(err, files) {
    start(files);
  });
});

function start(files) {
  console.log('files.length:', files.length);
  async.series([
    function(done) {
      console.time('im-decode');
      async.eachSeries(files, decode, function() {
        console.timeEnd('im-decode');
        done();
      });
    },
    function(done) {
      console.time('inkjet');
      async.eachSeries(files, inkjet.decode, function() {
        console.timeEnd('inkjet');
        done();
      });
    }
  ], end);
}
