zopflipng 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var program = require('commander');
  4. var fs = require('fs');
  5. var binary = require('node-pre-gyp');
  6. var path = require('path');
  7. var binding_path = binary.find(path.join(__dirname, '../package.json'));
  8. var zopfli = require(binding_path);
  9. program
  10. .version(require('../package.json').version)
  11. .usage('[options] file destfile')
  12. .option('--lossy_transparent', 'Allow altering hidden colors of fully transparent pixels')
  13. .option('--lossy', 'Convert 16-bit per channel images to 8-bit per channel')
  14. .option('--filter_strategies', 'Filter strategies to try : "zero", "one", "two", "three", "four", "minimum", "entropy", "predefined", "brute", if none, it will be guessed automatically')
  15. .option('--keepchunks', 'Chunks to keep')
  16. .option('--iterations=<n>', 'Number of iterations for small images < 200 KiB', parseInt)
  17. .option('--iterations_large=<n>', 'Number of iterations for large images > 200 KiB', parseInt)
  18. .parse(process.argv);
  19. var options = {
  20. lossy_transparent: false,
  21. lossy_8bit: false,
  22. filter_strategies: [],
  23. auto_filter_strategy: true,
  24. keepchunks: [],
  25. use_zopfli: true,
  26. num_iterations: 15,
  27. num_iterations_large: 5,
  28. block_split_strategy: 'both' // Split chunk strategy none, first, last, both
  29. };
  30. options.lossy_transparent = !!program.lossy_transparent;
  31. options.lossy_8bit = !!program.lossy;
  32. // if (program.filter_strategies) options.filter_strategies = []; //TODO
  33. // if (program.keepchunks) options.keepchunks = []; //TODO
  34. options.num_iterations = program.iterations || options.num_iterations;
  35. options.num_iterations_large = program.iterations_large || options.num_iterations_large;
  36. if (typeof program.args[0] !== 'string') {
  37. console.log('You must provide a file to compress');
  38. process.exit(1);
  39. }
  40. if (typeof program.args[1] !== 'string') {
  41. console.log('You must provide a destination file');
  42. process.exit(1);
  43. }
  44. if (fs.existsSync(program.args[0])) {
  45. zopfli.pngcompress(program.args[0], program.args[1], options);
  46. } else {
  47. console.log('File ' + program.args[0] + ' doesn\'t exist');
  48. process.exit(1);
  49. }