parser-sync.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. var hasSyncZlib = true;
  3. var zlib = require('zlib');
  4. var inflateSync = require('./sync-inflate');
  5. if (!zlib.deflateSync) {
  6. hasSyncZlib = false;
  7. }
  8. var SyncReader = require('./sync-reader');
  9. var FilterSync = require('./filter-parse-sync');
  10. var Parser = require('./parser');
  11. var bitmapper = require('./bitmapper');
  12. var formatNormaliser = require('./format-normaliser');
  13. module.exports = function(buffer, options) {
  14. if (!hasSyncZlib) {
  15. throw new Error('To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0');
  16. }
  17. var err;
  18. function handleError(_err_) {
  19. err = _err_;
  20. }
  21. var metaData;
  22. function handleMetaData(_metaData_) {
  23. metaData = _metaData_;
  24. }
  25. function handleTransColor(transColor) {
  26. metaData.transColor = transColor;
  27. }
  28. function handlePalette(palette) {
  29. metaData.palette = palette;
  30. }
  31. function handleSimpleTransparency() {
  32. metaData.alpha = true;
  33. }
  34. var gamma;
  35. function handleGamma(_gamma_) {
  36. gamma = _gamma_;
  37. }
  38. var inflateDataList = [];
  39. function handleInflateData(inflatedData) {
  40. inflateDataList.push(inflatedData);
  41. }
  42. var reader = new SyncReader(buffer);
  43. var parser = new Parser(options, {
  44. read: reader.read.bind(reader),
  45. error: handleError,
  46. metadata: handleMetaData,
  47. gamma: handleGamma,
  48. palette: handlePalette,
  49. transColor: handleTransColor,
  50. inflateData: handleInflateData,
  51. simpleTransparency: handleSimpleTransparency
  52. });
  53. parser.start();
  54. reader.process();
  55. if (err) {
  56. throw err;
  57. }
  58. //join together the inflate datas
  59. var inflateData = Buffer.concat(inflateDataList);
  60. inflateDataList.length = 0;
  61. var inflatedData;
  62. if (metaData.interlace) {
  63. inflatedData = zlib.inflateSync(inflateData);
  64. }
  65. else {
  66. var rowSize = ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
  67. var imageSize = rowSize * metaData.height;
  68. inflatedData = inflateSync(inflateData, { chunkSize: imageSize, maxLength: imageSize });
  69. }
  70. inflateData = null;
  71. if (!inflatedData || !inflatedData.length) {
  72. throw new Error('bad png - invalid inflate data response');
  73. }
  74. var unfilteredData = FilterSync.process(inflatedData, metaData);
  75. inflateData = null;
  76. var bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
  77. unfilteredData = null;
  78. var normalisedBitmapData = formatNormaliser(bitmapData, metaData);
  79. metaData.data = normalisedBitmapData;
  80. metaData.gamma = gamma || 0;
  81. return metaData;
  82. };