parser-async.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. var util = require('util');
  3. var zlib = require('zlib');
  4. var ChunkStream = require('./chunkstream');
  5. var FilterAsync = require('./filter-parse-async');
  6. var Parser = require('./parser');
  7. var bitmapper = require('./bitmapper');
  8. var formatNormaliser = require('./format-normaliser');
  9. var ParserAsync = module.exports = function(options) {
  10. ChunkStream.call(this);
  11. this._parser = new Parser(options, {
  12. read: this.read.bind(this),
  13. error: this._handleError.bind(this),
  14. metadata: this._handleMetaData.bind(this),
  15. gamma: this.emit.bind(this, 'gamma'),
  16. palette: this._handlePalette.bind(this),
  17. transColor: this._handleTransColor.bind(this),
  18. finished: this._finished.bind(this),
  19. inflateData: this._inflateData.bind(this),
  20. simpleTransparency: this._simpleTransparency.bind(this),
  21. headersFinished: this._headersFinished.bind(this)
  22. });
  23. this._options = options;
  24. this.writable = true;
  25. this._parser.start();
  26. };
  27. util.inherits(ParserAsync, ChunkStream);
  28. ParserAsync.prototype._handleError = function(err) {
  29. this.emit('error', err);
  30. this.writable = false;
  31. this.destroy();
  32. if (this._inflate && this._inflate.destroy) {
  33. this._inflate.destroy();
  34. }
  35. if (this._filter) {
  36. this._filter.destroy();
  37. // For backward compatibility with Node 7 and below.
  38. // Suppress errors due to _inflate calling write() even after
  39. // it's destroy()'ed.
  40. this._filter.on('error', function() {});
  41. }
  42. this.errord = true;
  43. };
  44. ParserAsync.prototype._inflateData = function(data) {
  45. if (!this._inflate) {
  46. if (this._bitmapInfo.interlace) {
  47. this._inflate = zlib.createInflate();
  48. this._inflate.on('error', this.emit.bind(this, 'error'));
  49. this._filter.on('complete', this._complete.bind(this));
  50. this._inflate.pipe(this._filter);
  51. }
  52. else {
  53. var rowSize = ((this._bitmapInfo.width * this._bitmapInfo.bpp * this._bitmapInfo.depth + 7) >> 3) + 1;
  54. var imageSize = rowSize * this._bitmapInfo.height;
  55. var chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK);
  56. this._inflate = zlib.createInflate({ chunkSize: chunkSize });
  57. var leftToInflate = imageSize;
  58. var emitError = this.emit.bind(this, 'error');
  59. this._inflate.on('error', function(err) {
  60. if (!leftToInflate) {
  61. return;
  62. }
  63. emitError(err);
  64. });
  65. this._filter.on('complete', this._complete.bind(this));
  66. var filterWrite = this._filter.write.bind(this._filter);
  67. this._inflate.on('data', function(chunk) {
  68. if (!leftToInflate) {
  69. return;
  70. }
  71. if (chunk.length > leftToInflate) {
  72. chunk = chunk.slice(0, leftToInflate);
  73. }
  74. leftToInflate -= chunk.length;
  75. filterWrite(chunk);
  76. });
  77. this._inflate.on('end', this._filter.end.bind(this._filter));
  78. }
  79. }
  80. this._inflate.write(data);
  81. };
  82. ParserAsync.prototype._handleMetaData = function(metaData) {
  83. this._metaData = metaData;
  84. this._bitmapInfo = Object.create(metaData);
  85. this._filter = new FilterAsync(this._bitmapInfo);
  86. };
  87. ParserAsync.prototype._handleTransColor = function(transColor) {
  88. this._bitmapInfo.transColor = transColor;
  89. };
  90. ParserAsync.prototype._handlePalette = function(palette) {
  91. this._bitmapInfo.palette = palette;
  92. };
  93. ParserAsync.prototype._simpleTransparency = function() {
  94. this._metaData.alpha = true;
  95. };
  96. ParserAsync.prototype._headersFinished = function() {
  97. // Up until this point, we don't know if we have a tRNS chunk (alpha)
  98. // so we can't emit metadata any earlier
  99. this.emit('metadata', this._metaData);
  100. };
  101. ParserAsync.prototype._finished = function() {
  102. if (this.errord) {
  103. return;
  104. }
  105. if (!this._inflate) {
  106. this.emit('error', 'No Inflate block');
  107. }
  108. else {
  109. // no more data to inflate
  110. this._inflate.end();
  111. }
  112. this.destroySoon();
  113. };
  114. ParserAsync.prototype._complete = function(filteredData) {
  115. if (this.errord) {
  116. return;
  117. }
  118. try {
  119. var bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);
  120. var normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo);
  121. bitmapData = null;
  122. }
  123. catch (ex) {
  124. this._handleError(ex);
  125. return;
  126. }
  127. this.emit('parsed', normalisedBitmapData);
  128. };